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

Source for file helpers.php

Documentation is available at helpers.php

  1. <?php
  2. /**
  3.  *  File containing the Helpers class and associated functions
  4.  *
  5.  *  (PHP 5)
  6.  *
  7.  *  @package PHPonTrax
  8.  *  @version $Id: helpers.php 280 2007-01-30 06:55:13Z john $
  9.  *  @copyright (c) 2005 John Peterson
  10.  *
  11.  *   Permission is hereby granted, free of charge, to any person obtaining
  12.  *   a copy of this software and associated documentation files (the
  13.  *   "Software"), to deal in the Software without restriction, including
  14.  *   without limitation the rights to use, copy, modify, merge, publish,
  15.  *   distribute, sublicense, and/or sell copies of the Software, and to
  16.  *   permit persons to whom the Software is furnished to do so, subject to
  17.  *   the following conditions:
  18.  *
  19.  *   The above copyright notice and this permission notice shall be
  20.  *   included in all copies or substantial portions of the Software.
  21.  *
  22.  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23.  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24.  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25.  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26.  *   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27.  *   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28.  *   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.  */
  30.  
  31. /**
  32.  *  Basic helper functions
  33.  *
  34.  *  A collection of methods used to generate basic HTML/XML.
  35.  */
  36. class Helpers {
  37.  
  38.     /**
  39.      *  @todo Document this variable
  40.      *  @var boolean 
  41.      */
  42.     public $auto_index;
  43.  
  44.     /**
  45.      *  @todo Document this variable
  46.      *   Name of a PHP class(?)
  47.      *  @var string 
  48.      */
  49.     public $object_name;
  50.  
  51.     /**
  52.      *  @todo Document this variable
  53.      */
  54.     public $attribute_name;
  55.  
  56.     /**
  57.      *  Current controller object
  58.      *
  59.      *  Local copy of Trax::$current_controller_object<br />
  60.      *  <b>NB:</b> {@link object()} faults if this does not contain a
  61.      *  valid instance of ActionController.
  62.      *  @var ActionController 
  63.      */
  64.     public $controller_object;
  65.  
  66.     /**
  67.      *  Current controller name
  68.      *
  69.      *  Local copy of Trax::$current_controller_name
  70.      *  @var string 
  71.      */
  72.     public $controller_name;
  73.  
  74.     /**
  75.      *  Current controller path
  76.      *
  77.      *  Local copy of Trax::$current_controller_path
  78.      *  @var string 
  79.      */
  80.     public $controller_path;
  81.  
  82.  
  83.     /**
  84.      *  Construct a Helpers object
  85.      *
  86.      *  @param string Name of ActiveRecord subclass
  87.      *  @param string Attribute of ActiveRecord subclass
  88.      *  @uses auto_index
  89.      *  @uses object_name
  90.      *  @uses attribute_name
  91.      *  @uses controller_name
  92.      *  @uses controller_path
  93.      *  @uses controller_object
  94.      */
  95.     function __construct($object_name null$attribute_name null{
  96.         if(substr($object_name-2== "[]"{
  97.             $auto_index true;
  98.         else {
  99.             $auto_index false;
  100.         }
  101.         $this->auto_index = false;
  102.         $this->object_name = str_replace("[]"""$object_name);     
  103.         $this->attribute_name = $attribute_name;        
  104.  
  105.         //  Copy controller information from $GLOBALS
  106.         $this->controller_name =
  107.             !is_null(Trax::$current_controller_name)           
  108.             ? Trax::$current_controller_name null;
  109.         $this->controller_path =
  110.             !is_null(Trax::$current_controller_path)
  111.             ? Trax::$current_controller_path null;
  112.         $this->controller_object =
  113.             (!is_null(Trax::$current_controller_object
  114.             && is_object(Trax::$current_controller_object))
  115.             ? Trax::$current_controller_object null;
  116.         if($auto_index{
  117.             $object $this->object();
  118.             if(is_object($object)) {
  119.                 $index $object->index_on# should be primary key (usually id field)
  120.                 $this->auto_index = $object->$index;      
  121.                }  
  122.         }         
  123.     }
  124.  
  125.     /**
  126.      *  Get value of current attribute in the current ActiveRecord object
  127.      *
  128.      *  If there is a value in $_REQUEST[][], return it.
  129.      *  Otherwise fetch the value from the database.
  130.      *  @uses attribute_name
  131.      *  @uses object()
  132.      *  @uses object_name
  133.      *  @uses ActiveRecord::send()
  134.      */
  135.     protected function value({
  136.         if (array_key_exists($this->object_name$_REQUEST)
  137.             && array_key_exists($this->attribute_name,
  138.                                  $_REQUEST[$this->object_name])) {
  139.             $value $_REQUEST[$this->object_name][$this->attribute_name];
  140.         else {
  141.  
  142.             //  Attribute value not found in $_REQUEST.  Find the
  143.             //  ActiveRecord subclass instance and query it.
  144.             $object $this->object();
  145.             if(is_object($object&& $this->attribute_name{
  146.                 //$value = $object->send($this->attribute_name);
  147.                 $value $object->{$this->attribute_name};
  148.             }
  149.         }
  150.         return $value;
  151.     }
  152.  
  153.     /**
  154.      *  Given the name of an ActiveRecord subclass, find an instance
  155.      *
  156.      *  Finds the AR instance from the ActionController instance.
  157.      *  Assumes that if a $object_name is defined either as the
  158.      *  argument or an instance variable, then there must be
  159.      *  a controller object instance which points to a single instance
  160.      *  of the ActiveRecord.
  161.      *  <b>FIXME:</b> Handle errors better.
  162.      *  @param string Name of an ActiveRecord subclass or null
  163.      *  @return mixed Instance of the subclass, or null if
  164.      *                 object not available.
  165.      *  @uses controller_object
  166.      *  @uses object_name
  167.      */
  168.     protected function object($object_name null{
  169.         $object_name $object_name $object_name $this->object_name;
  170.         if($object_name
  171.            && isset($this->controller_object)
  172.            && isset($this->controller_object->$object_name)) {
  173.             return $this->controller_object->$object_name;
  174.         }
  175.         return null;
  176.     }   
  177.     
  178.     /**
  179.      *  Convert array of tag attribute names and values to string
  180.      *
  181.      *  @param string[] $options 
  182.      *  @return string 
  183.      */
  184.     protected function tag_options($options{
  185.         if(count($options)) {
  186.             $html array();
  187.             foreach($options as $key => $value{
  188.                 $html["$key=\"".@htmlspecialchars($valueENT_COMPAT)."\"";
  189.             }
  190.             sort($html);
  191.             $html implode(" "$html);
  192.             return $html;
  193.         else {
  194.             return '';
  195.         }
  196.     }
  197.  
  198.     /**
  199.      *  Convert selected attributes to proper XML boolean form
  200.      *
  201.      *  @uses boolean_attribute()
  202.      *  @param string[] $options 
  203.      *  @return string[] Input argument with selected attributes converted
  204.      *                    to proper XML boolean form
  205.      */
  206.     protected function convert_options($options array()) {
  207.         foreach(array('disabled''readonly''multiple'as $a{
  208.             $this->boolean_attribute($options$a);
  209.         }
  210.         return $options;
  211.     }
  212.  
  213.     /**
  214.      *  Convert an attribute to proper XML boolean form
  215.      *
  216.      *  @param string[] $options 
  217.      *  @param string $attribute 
  218.      *  @return void Contents of $options have been converted
  219.      */
  220.     protected function boolean_attribute(&$options$attribute{
  221.         if(array_key_exists($attribute,$options)
  222.            && $options[$attribute]{
  223.             $options[$attribute$attribute;
  224.         else {
  225.             unset($options[$attribute]);
  226.         }
  227.     }
  228.     
  229.     /**
  230.      *  Wrap CDATA begin and end tags around argument
  231.      *
  232.      *  Returns a CDATA section for the given content.  CDATA sections
  233.      *  are used to escape blocks of text containing characters which would
  234.      *  otherwise be recognized as markup. CDATA sections begin with the string
  235.      *  <samp><![CDATA[</samp> and end with (and may not contain) the string
  236.      *  <samp>]]></samp>.
  237.      *  @param string $content  Content to wrap
  238.      *  @return string          Wrapped argument
  239.      */
  240.     function cdata_section($content{
  241.         return "<![CDATA[".$content."]]>";
  242.     }    
  243.  
  244.     /**
  245.      *  Generate an HTML or XML tag with optional attributes and self-ending
  246.      *
  247.      *  <ul>
  248.      *   <li>Example: <samp>tag("br");</samp><br>
  249.      *       Returns: <samp><br  />\n</samp></li>
  250.      *   <li> Example: <samp>tag("div", array("class" => "warning"), true);</samp><br>
  251.      *       Returns: <samp><div class="warning">\n</samp></li>
  252.      *  </ul>
  253.      *  @param string $name      Tag name
  254.      *  @param string[] $options Tag attributes to apply, specified as
  255.      *                   array('attr1' => 'value1'[, 'attr2' => 'value2']...)
  256.      *  @param boolean $open 
  257.      *   <ul>
  258.      *     <li>true =>  make opening tag (end with '>')</li>
  259.      *     <li>false => make self-terminating tag (end with ' \>')</li>
  260.      *   </ul>
  261.      *  @return string The generated tag, followed by "\n"
  262.      *  @uses tag_options()
  263.      */
  264.     function tag($name$options array()$open false{
  265.         $html "<$name ";
  266.         $html .= $this->tag_options($options);
  267.         $html .= $open ">" " />";
  268.         return $html."\n";
  269.     }
  270.  
  271.     /**
  272.      *  Generate an open/close pair of tags with optional attributes and content between
  273.      *
  274.      *  <ul>
  275.      *   <li>Example: <samp>content_tag("p", "Hello world!");</samp><br />
  276.      *       Returns: <samp><p>Hello world!</p>\n</samp><li>
  277.      *   <li>Example:
  278.      *     <samp>content_tag("div",
  279.      *                       content_tag("p", "Hello world!"),
  280.      *                       array("class" => "strong"));</samp><br />
  281.      *     Returns:
  282.      *     <samp><div class="strong"><p>Hello world!</p></div>\n</samp></li>
  283.      *  </ul>
  284.      *  @uses tag_options()
  285.      *  @param string $name    Tag to wrap around $content
  286.      *  @param string $content Text to put between tags
  287.      *  @param string[] $options Tag attributes to apply, specified as
  288.      *                   array('attr1' => 'value1'[, 'attr2' => 'value2']...)
  289.      *  @return string Text wrapped with tag and attributes,
  290.      *                  followed by "\n"
  291.      */
  292.     function content_tag($name$content$options array()) {
  293.         $html "<$name ";
  294.         $html .= $this->tag_options($options);
  295.         if(isset($options['strip_slashes'])) {
  296.             $content stripslashes($content);    
  297.         }
  298.         $html .= ">$content</$name>";
  299.         return $html."\n";
  300.     }
  301.     
  302.     /**
  303.      *
  304.      *  @uses content_tag()
  305.      *  @uses value()
  306.      */    
  307.     function to_content_tag($tag_name$options array()) {
  308.         return $this->content_tag($tag_name$this->value()$options);
  309.     
  310.     
  311.     /**
  312.      *  If this tag has an error, wrap it with a visual indicator
  313.      *
  314.      *  @param string HTML to be wrapped
  315.      *  @param boolean  true=>error, false=>no error
  316.      *  @return string 
  317.      */
  318.     function error_wrapping($html_tag$has_error{
  319.         return ($has_error '<span class="fieldWithErrors">' eregi_replace("[\n\r]"''$html_tag'</span>' $html_tag);
  320.     }         
  321.  
  322. }
  323.  
  324. /**
  325.  *  Create a Helpers object and call its content_tag() method
  326.  *
  327.  *  @see Helpers::content_tag()
  328.  *  @param string $name    Tag to wrap around $content
  329.  *  @param string $content Text to put between tags
  330.  *  @param string[] $options Tag attributes to apply
  331.  *  @return string Text wrapped with tag and attributes,
  332.  *                  followed by "\n"
  333.  */
  334. function content_tag({
  335.     $helper new Helpers();
  336.     $args func_get_args();
  337.     return call_user_func_array(array($helper'content_tag')$args);
  338. }
  339.  
  340. /**
  341.  *  Create a Helpers object and call its tag() method
  342.  *
  343.  *  @see Helpers::tag()
  344.  *  @param string $name    Tag name
  345.  *  @param string[] $options Tag attributes to apply
  346.  *  @param boolean $open 
  347.  *   <ul>
  348.  *     <li>true =>  make opening tag (end with '>')</li>
  349.  *     <li>false => make self-terminating tag (end with ' \>')</li>
  350.  *   </ul>
  351.  *  @return string The tag, followed by "\n"
  352.  */
  353. function tag({
  354.     $helper new Helpers();
  355.     $args func_get_args();
  356.     return call_user_func_array(array($helper'tag')$args);
  357. }
  358.  
  359. /**
  360.  *  Create a Helpers object and call its cdata_section() method
  361.  */
  362. function cdata_section({
  363.     $helper new Helpers();
  364.     $args func_get_args();
  365.     return call_user_func_array(array($helper'cdata_section')$args);
  366. }
  367.  
  368. // -- set Emacs parameters --
  369. // Local variables:
  370. // tab-width: 4
  371. // c-basic-offset: 4
  372. // c-hanging-comment-ender-p: nil
  373. // indent-tabs-mode: nil
  374. // End:
  375. ?>

Documentation generated on Mon, 21 May 2007 22:28:34 -0600 by phpDocumentor 1.3.2