HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Savant.php

Documentation is available at Savant.php

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

Documentation generated on Mon, 11 Mar 2019 15:59:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.