PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/HTML/Template/Flexy/Plugin/Savant.php

https://github.com/clayhinson/sgl2
PHP | 756 lines | 232 code | 99 blank | 425 comment | 34 complexity | d628c23b58bf5cc5a5696b3140ff58b9 MD5 | raw file
  1. <?php
  2. /**
  3. * it under the terms of the GNU Lesser General Public License as
  4. * published by the Free Software Foundation; either version 2.1 of the
  5. * License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * Lesser General Public License for more details.
  11. *
  12. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  13. *
  14. */
  15. class HTML_Template_Flexy_Plugin_Savant {
  16. /**
  17. * Output an HTML <a href="">...</a> tag.
  18. *
  19. * @author Paul M. Jones <pmjones@ciaweb.net>
  20. *
  21. * @package Savant
  22. *
  23. *
  24. * @access public
  25. *
  26. * @param string $href The URL for the resulting <a href="">...</a> tag.
  27. *
  28. * @param string $text The text surrounded by the <a>...</a> tag set.
  29. *
  30. * @param string $extra Any "extra" HTML code to place within the <a>
  31. * opening tag.
  32. *
  33. * @return string
  34. */
  35. function ahref($href, $text, $extra = null)
  36. {
  37. $output = '<a href="' . $href . '"';
  38. if (! is_null($extra)) {
  39. $output .= ' ' . $extra;
  40. }
  41. $output .= '>' . $text . '</a>';
  42. return $output;
  43. }
  44. /**
  45. *
  46. * Output a single checkbox <input> element.
  47. * @author Paul M. Jones <pmjones@ciaweb.net>
  48. *
  49. * @package Savant
  50. *
  51. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  52. *
  53. * @access public
  54. *
  55. * @param string $name The HTML "name=" value for the checkbox.
  56. *
  57. * @param mixed $value The value of the checkbox if checked.
  58. *
  59. * @param mixed $selected Check $value against this; if they match,
  60. * mark the checkbox as checked.
  61. *
  62. * @param string $set_unchecked If null, this will add no HTML to the
  63. * output. However, if set to any non-null value, the value will be
  64. * added as a hidden element before the checkbox so that if the
  65. * checkbox is unchecked, the hidden value will be returned instead
  66. * of the checked value.
  67. *
  68. * @param string $extra Any "extra" HTML code to place within the
  69. * checkbox element.
  70. *
  71. * @return string
  72. *
  73. */
  74. function checkbox(
  75. $name,
  76. $value,
  77. $selected = null,
  78. $set_unchecked = null,
  79. $extra = null)
  80. {
  81. $html = '';
  82. if (! is_null($set_unchecked)) {
  83. // this sets the unchecked value of the checkbox.
  84. $html .= "<input type=\"hidden\" ";
  85. $html .= "name=\"$name\" ";
  86. $html .= "value=\"$set_unchecked\" />\n";
  87. }
  88. $html .= "<input type=\"checkbox\" ";
  89. $html .= "name=\"$name\" ";
  90. $html .= "value=\"$value\"";
  91. if ($value == $selected) {
  92. $html .= " checked=\"checked\"";
  93. }
  94. $html .= " $extra />";
  95. return $html;
  96. }
  97. /**
  98. *
  99. * Output a set of checkbox <input>s.
  100. *
  101. *
  102. * @author Paul M. Jones <pmjones@ciaweb.net>
  103. *
  104. * @package Savant
  105. *
  106. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  107. *
  108. * @access public
  109. *
  110. * @param string $name The HTML "name=" value of all the checkbox
  111. * <input>s. The name will get [] appended to it to make it an array
  112. * when returned to the server.
  113. *
  114. * @param array $options An array of key-value pairs where the key is
  115. * the checkbox value and the value is the checkbox label.
  116. *
  117. * @param string $set_unchecked If null, this will add no HTML to the
  118. * output. However, if set to any non-null value, the value will be
  119. * added as a hidden element before every checkbox so that if the
  120. * checkbox is unchecked, the hidden value will be returned instead
  121. * of the checked value.
  122. *
  123. * @param string $sep The HTML text to place between every checkbox
  124. * in the set.
  125. *
  126. * @param string $extra Any "extra" HTML code to place within the
  127. * checkbox element.
  128. *
  129. * @return string
  130. *
  131. */
  132. function checkboxes(
  133. $name,
  134. $options,
  135. $selected = array(),
  136. $set_unchecked = null,
  137. $sep = "<br />\n",
  138. $extra = null)
  139. {
  140. // force $selected to be an array. this allows multi-checks to
  141. // have multiple checked boxes.
  142. settype($selected, 'array');
  143. // the text to be returned
  144. $html = '';
  145. if (is_array($options)) {
  146. // an iteration counter. we use this to track which array
  147. // elements are checked and which are unchecked.
  148. $i = 0;
  149. foreach ($options as $value => $label) {
  150. if (! is_null($set_unchecked)) {
  151. // this sets the unchecked value of the checkbox.
  152. $html .= "<input type=\"hidden\" ";
  153. $html .= "name=\"{$name}[$i]\" ";
  154. $html .= "value=\"$set_unchecked\" />\n";
  155. }
  156. $html .= "<input type=\"checkbox\" ";
  157. $html .= "name=\"{$name}[$i]\" ";
  158. $html .= "value=\"$value\"";
  159. if (in_array($value, $selected)) {
  160. $html .= " checked=\"checked\"";
  161. }
  162. if (! is_null($extra)) {
  163. $html .= " $extra";
  164. }
  165. $html .= " />$label$sep";
  166. $i++;
  167. }
  168. }
  169. return $html;
  170. }
  171. /**
  172. *
  173. * Cycle through a series of values based on an iteration number,
  174. * with optional group repetition.
  175. *
  176. * For example, if you have three values in a cycle (a, b, c) the iteration
  177. * returns look like this:
  178. *
  179. * 0 => a
  180. * 1 => b
  181. * 2 => c
  182. * 3 => a
  183. * 4 => b
  184. * 5 => c
  185. *
  186. * If you repeat each cycle value (a,b,c) 2 times on the iterations,
  187. * the returns look like this:
  188. *
  189. * 0 => a
  190. * 1 => a
  191. * 2 => b
  192. * 3 => b
  193. * 4 => c
  194. * 5 => c
  195. *
  196. *
  197. * @author Paul M. Jones <pmjones@ciaweb.net>
  198. *
  199. * @package Savant
  200. *
  201. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  202. *
  203. * @access public
  204. *
  205. * @param int $iteration The iteration number for the cycle.
  206. *
  207. * @param array $values The values to cycle through.
  208. *
  209. * @param int $repeat The number of times to repeat a cycle value.
  210. *
  211. * @return string
  212. *
  213. */
  214. function cycle($iteration, $values = null, $repeat = 1)
  215. {
  216. settype($values, 'array');
  217. // prevent divide-by-zero errors
  218. if ($repeat == 0) {
  219. $repeat = 1;
  220. }
  221. return $values[($iteration / $repeat) % count($values)];
  222. }
  223. /**
  224. *
  225. * Output a formatted date using strftime() conventions.
  226. *
  227. *
  228. * @author Paul M. Jones <pmjones@ciaweb.net>
  229. *
  230. * @package Savant
  231. *
  232. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  233. *
  234. * @access public
  235. *
  236. * @param string $datestring Any date-time string suitable for
  237. * strtotime().
  238. *
  239. * @param string $format The strftime() formatting string.
  240. *
  241. * @return string
  242. *
  243. */
  244. function dateformat($datestring, $format = false)
  245. {
  246. if ($format === false) {
  247. $format = isset($this->flexy->options['plugin.dateformat']) ?
  248. $this->flexy->options['plugin.dateformat'] : '%d %b %Y';
  249. }
  250. if (trim($datestring) == '') {
  251. return '';
  252. }
  253. $date = strtotime($datestring);
  254. if ($date > 1) {
  255. return strftime($format, $date);
  256. }
  257. require_once 'Date.php';
  258. $date = new Date($date);
  259. return $date->format($format);
  260. }
  261. /**
  262. *
  263. * Output a formatted number using number_format
  264. *
  265. *
  266. *
  267. * @param string $datestring Any date-time string suitable for
  268. * strtotime().
  269. *
  270. * @param string $format The strftime() formatting string.
  271. *
  272. * @return string
  273. *
  274. */
  275. function numberformat($number, $dec=false,$point=false,$thousands=false)
  276. {
  277. if (!strlen(trim($number))) {
  278. return;
  279. }
  280. // numberformat int decimals, string dec_point, string thousands_sep
  281. $dec = ($dec !== false) ? $dec : (
  282. isset($this->flexy->options['plugin.numberformat.decimals']) ?
  283. $this->flexy->options['plugin.numberformat.decimals'] : 2
  284. );
  285. $point = ($point !== false) ? $point : (
  286. isset($this->flexy->options['plugin.numberformat.point']) ?
  287. $this->flexy->options['plugin.numberformat.point'] : '.');
  288. $thousands = ($thousands !== false) ? $thousands : (
  289. isset($this->flexy->options['plugin.numberformat.thousands']) ?
  290. $this->flexy->options['plugin.numberformat.thousands'] : ',');
  291. return number_format($number,$dec,$point,$thousands);
  292. }
  293. /**
  294. *
  295. * Output an <image ... /> tag.
  296. *
  297. *
  298. * @author Paul M. Jones <pmjones@ciaweb.net>
  299. *
  300. * @package Savant
  301. *
  302. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  303. *
  304. * @access public
  305. *
  306. * @param string $src The image source as a relative or absolute HREF.
  307. *
  308. * @param string $link Providing a link will make the image clickable,
  309. * leading to the URL indicated by $link; defaults to null.
  310. *
  311. * @param string $alt Alternative descriptive text for the image;
  312. * defaults to the filename of the image.
  313. *
  314. * @param int $border The border width for the image; defaults to zero.
  315. *
  316. * @param int $width The displayed image width in pixels; defaults to
  317. * the width of the image.
  318. *
  319. * @param int $height The displayed image height in pixels; defaults to
  320. * the height of the image.
  321. *
  322. */
  323. function image(
  324. $src,
  325. $alt = null,
  326. $border = 0,
  327. $width = null,
  328. $height = null)
  329. {
  330. $size = '';
  331. // build the alt tag
  332. if (is_null($alt)) {
  333. $alt = basename($src);
  334. }
  335. $alt = ' alt="' . htmlentities($alt) . '"';
  336. // build the border tag
  337. $border = ' border="' . htmlentities($border) . '"';
  338. // get the width and height of the image
  339. if (is_null($width) && is_null($height)) {
  340. if (substr(strtolower($src), 0, 7) == 'http://' ||
  341. substr(strtolower($src), 0, 8) == 'https://') {
  342. // the image is not on the local filesystem
  343. $root = '';
  344. } else {
  345. // we need to set a base root path so we can find images on the
  346. // local file system
  347. $root = isset($GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'])
  348. ? $GLOBALS['HTTP_SERVER_VARS']['DOCUMENT_ROOT'] . '/'
  349. : '';
  350. }
  351. $info = @getimagesize($root . $src);
  352. $width = (is_null($width)) ? $info[0] : $width;
  353. $height = (is_null($height)) ? $info[1] : $height;
  354. unset($info);
  355. }
  356. // build the width tag
  357. if ($width > 0) {
  358. $size .= ' width="' . htmlentities($width) . '"';
  359. }
  360. // build the height tag
  361. if ($height > 0) {
  362. $size .= ' height="' . htmlentities($height) . '"';
  363. }
  364. // done!
  365. return '<img src="' . $src . '"' .
  366. $alt .
  367. $border .
  368. $size .
  369. ' />';
  370. }
  371. /**
  372. *
  373. * Output a single <input> element.
  374. *
  375. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  376. *
  377. * @author Paul M. Jones <pmjones@ciaweb.net>
  378. *
  379. * @package Savant
  380. *
  381. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  382. *
  383. * @access public
  384. *
  385. * @param string $type The HTML "type=" value (e.g., 'text',
  386. * 'hidden', 'password').
  387. *
  388. * @param string $name The HTML "name=" value.
  389. *
  390. * @param mixed $value The initial value of the input element.
  391. *
  392. * @param string $extra Any "extra" HTML code to place within the
  393. * checkbox element.
  394. *
  395. * @return string
  396. *
  397. */
  398. function input($type, $name, $value = '', $extra = '')
  399. {
  400. $output = "<input type=\"$type\" name=\"$name\" ";
  401. $output .= "value=\"$value\" $extra />";
  402. return $output;
  403. }
  404. /**
  405. *
  406. * Output a <script></script> link to a JavaScript file.
  407. *
  408. *
  409. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  410. *
  411. * @author Paul M. Jones <pmjones@ciaweb.net>
  412. *
  413. * @package Savant
  414. *
  415. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  416. *
  417. * @access public
  418. *
  419. * @param string $href The HREF leading to the JavaScript source
  420. * file.
  421. *
  422. * @return string
  423. *
  424. */
  425. function javascript($href)
  426. {
  427. return '<script language="javascript" type="text/javascript" src="' .
  428. $href . '"></script>';
  429. }
  430. /**
  431. *
  432. * Output a value using echo after processing with optional modifier
  433. * functions.
  434. *
  435. * Allows you to pass a space-separated list of value-manipulation
  436. * functions so that the value is "massaged" before output. For
  437. * example, if you want to strip slashes, force to lower case, and
  438. * convert to HTML entities (as for an input text box), you might do
  439. * this:
  440. *
  441. * $this->modify($value, 'stripslashes strtolower htmlentities');
  442. *
  443. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  444. *
  445. * @author Paul M. Jones <pmjones@ciaweb.net>
  446. *
  447. * @package Savant
  448. *
  449. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  450. *
  451. * @access public
  452. *
  453. * @param string $value The value to be printed.
  454. *
  455. * @param string $functions A space-separated list of
  456. * single-parameter functions to be applied to the $value before
  457. * printing.
  458. *
  459. * @return string
  460. *
  461. */
  462. function modify($value, $functions = null)
  463. {
  464. // is there a space-delimited function list?
  465. if (is_string($functions)) {
  466. // yes. split into an array of the
  467. // functions to be called.
  468. $list = explode(' ', $functions);
  469. // loop through the function list and
  470. // apply to the output in sequence.
  471. foreach ($list as $func) {
  472. if (!function_exists($func)) {
  473. continue;
  474. }
  475. // extend this..
  476. if (!in_array($func, array('htmlspecialchars','nl2br','urlencode'))) {
  477. continue;
  478. }
  479. $value = $func($value);
  480. }
  481. }
  482. return $value;
  483. }
  484. /**
  485. *
  486. * Output a series of HTML <option>s based on an associative array
  487. * where the key is the option value and the value is the option
  488. * label. You can pass a "selected" value as well to tell the
  489. * function which option value(s) should be marked as seleted.
  490. *
  491. *
  492. * @author Paul M. Jones <pmjones@ciaweb.net>
  493. *
  494. * @package Savant
  495. *
  496. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  497. *
  498. * @access public
  499. *
  500. * @param array $options An associative array of key-value pairs; the
  501. * key is the option value, the value is the option lable.
  502. *
  503. * @param mixed $selected A string or array that matches one or more
  504. * option values, to tell the function what options should be marked
  505. * as selected. Defaults to an empty array.
  506. *
  507. * @return string
  508. *
  509. */
  510. function options( $options, $selected = array(), $extra = null)
  511. {
  512. $html = '';
  513. // force $selected to be an array. this allows multi-selects to
  514. // have multiple selected options.
  515. settype($selected, 'array');
  516. // is $options an array?
  517. if (is_array($options)) {
  518. // loop through the options array
  519. foreach ($options as $value => $label) {
  520. $html .= '<option value="' . $value . '"';
  521. $html .= ' label="' . $label . '"';
  522. if (in_array($value, $selected)) {
  523. $html .= ' selected="selected"';
  524. }
  525. if (! is_null($extra)) {
  526. $html .= ' ' . $extra;
  527. }
  528. $html .= ">$label</option>\n";
  529. }
  530. }
  531. return $html;
  532. }
  533. /**
  534. *
  535. * Output a set of radio <input>s with the same name.
  536. *
  537. *
  538. * @author Paul M. Jones <pmjones@ciaweb.net>
  539. *
  540. * @package Savant
  541. *
  542. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  543. *
  544. * @access public
  545. *
  546. * @param string $name The HTML "name=" value of all the radio <input>s.
  547. *
  548. * @param array $options An array of key-value pairs where the key is the
  549. * radio button value and the value is the radio button label.
  550. *
  551. * $options = array (
  552. * 0 => 'zero',
  553. * 1 => 'one',
  554. * 2 => 'two'
  555. * );
  556. *
  557. * @param string $checked A comparison string; if any of the $option
  558. * element values and $checked are the same, that radio button will
  559. * be marked as "checked" (otherwise not).
  560. *
  561. * @param string $extra Any "extra" HTML code to place within the
  562. * <input /> element.
  563. *
  564. * @param string $sep The HTML text to place between every radio
  565. * button in the set.
  566. *
  567. * @return string
  568. *
  569. */
  570. function radios(
  571. $name,
  572. $options,
  573. $checked = null,
  574. $set_unchecked = null,
  575. $sep = "<br />\n",
  576. $extra = null)
  577. {
  578. $html = '';
  579. if (is_array($options)) {
  580. if (! is_null($set_unchecked)) {
  581. // this sets the unchecked value of the
  582. // radio button set.
  583. $html .= "<input type=\"hidden\" ";
  584. $html .= "name=\"$name\" ";
  585. $html .= "value=\"$set_unchecked\" />\n";
  586. }
  587. foreach ($options as $value => $label) {
  588. $html .= "<input type=\"radio\" ";
  589. $html .= "name=\"$name\" ";
  590. $html .= "value=\"$value\"";
  591. if ($value == $checked) {
  592. $html .= " checked=\"checked\"";
  593. }
  594. $html .= " $extra />$label$sep";
  595. }
  596. }
  597. return $html;
  598. }
  599. /**
  600. *
  601. * Output a <link ... /> to a CSS stylesheet.
  602. *
  603. *
  604. * @author Paul M. Jones <pmjones@ciaweb.net>
  605. *
  606. * @package Savant
  607. *
  608. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  609. *
  610. * @access public
  611. *
  612. * @param string $href The HREF leading to the stylesheet file.
  613. *
  614. * @return string
  615. *
  616. */
  617. function stylesheet($href)
  618. {
  619. return '<link rel="stylesheet" type="text/css" href="' .
  620. $href . '" />';
  621. }
  622. /**
  623. *
  624. * Output a single <textarea> element.
  625. *
  626. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  627. *
  628. * @author Paul M. Jones <pmjones@ciaweb.net>
  629. *
  630. * @package Savant
  631. *
  632. * @version $Id: Savant.php,v 1.7 2005/05/14 03:39:11 alan_k Exp $
  633. *
  634. * @access public
  635. *
  636. * @param string $name The HTML "name=" value.
  637. *
  638. * @param string $text The initial value of the textarea element.
  639. *
  640. * @param int $tall How many rows tall should the area be?
  641. *
  642. * @param mixed $wide The many columns wide should the area be?
  643. *
  644. * @param string $extra Any "extra" HTML code to place within the
  645. * checkbox element.
  646. *
  647. * @return string
  648. *
  649. */
  650. function textarea($name, $text, $tall = 24, $wide = 80, $extra = '')
  651. {
  652. $output = "<textarea name=\"$name\" rows=\"$tall\" ";
  653. $output .= "cols=\"$wide\" $extra>$text</textarea>";
  654. return $output;
  655. }
  656. }