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

Source for file Element.php

Documentation is available at Element.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category Cnz
  17.  * @package  Cnz_Html
  18.  *
  19.  * @author    Lyle Frost <lfrost@cnz.com>
  20.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  21.  * @filesource
  22.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  23.  * @version   $Id: Element.php 27 2007-07-19 18:47:54Z lfrost $
  24.  */
  25.  
  26. /**
  27.  * Required classes.
  28.  *
  29.  * @ignore
  30.  */
  31. Zend_Loader::loadClass('Zend_Config');
  32. Zend_Loader::loadClass('Zend_Config_Ini');
  33.  
  34. /**
  35.  * HTML Element class
  36.  *
  37.  * This is an abstract base class for all high-level HTML display elements.  Its
  38.  * primary function is to generate the configuration section names and style
  39.  * names in a standardized way.
  40.  *
  41.  * The configuration section name is generated as
  42.  *
  43.  * <pre>
  44.  *   Html<i>Type</i>[-<i>name</i>]
  45.  * </pre>
  46.  *
  47.  * where type is third field of class name and name is the element name passed
  48.  * to the constructor.  For example, Cnz_Html_Form_Checkbox with a name of pay
  49.  * would yield HtmlFormPay.
  50.  *
  51.  * The style name is generated as
  52.  *
  53.  * <pre>
  54.  *   <i>type</i>[<i>Subtype</i>][-<i>name</i>][-<i>suffix</i>]
  55.  * </pre>
  56.  *
  57.  * This design allows for a single default configuration section and/or style,
  58.  * with the ability to override the defaults for special elements.
  59.  *
  60.  * Some elements will have special style suffixes (e.g., hilighted table rows).
  61.  * For example, a highlighted table row with the custom name <kbd>team</kbd>
  62.  * would have the following returned by <kbd>genStyles()</kbd>.
  63.  *
  64.  * <pre>
  65.  *   table table-team table-hilight table-team-hilight
  66.  * </pre>
  67.  *
  68.  * Subtypes are only used for situations where the same HTML tag has different
  69.  * structural roles in the same complex element.  For example, a form will use
  70.  * a div block to format the label for a field as well as the field itself, but
  71.  * different styling will normally be desired for these.  For example, a form
  72.  * label with the custom name pay would have the following returned by
  73.  * <kbd>genStyles()</kbd>.
  74.  *
  75.  * <pre>
  76.  *   formLabel formLabel-pay
  77.  * </pre>
  78.  *
  79.  * This allows the definition of default styles with overrides for named tables
  80.  * and special cases.  Remember that the order of the style names in the class
  81.  * attribute is irrelevant.  The order in the style sheet is what matters.  The
  82.  * more specific styles should occur below the more general styles.
  83.  *
  84.  * HTML id values can also be generated with <kbd>genId()</kbd>.  This is
  85.  * generated as
  86.  *
  87.  * <pre>
  88.  *   <i>type</i>[-<i>name</i>]
  89.  * </pre>
  90.  *
  91.  * @category Cnz
  92.  * @package  Cnz_Html
  93.  */
  94. abstract class Cnz_Html_Element
  95. {
  96.     const CONFIG_PREFIX        'Html';
  97.     const STYLE_SEPARATOR        '-';
  98.  
  99.     /**#@+ @var string */
  100.     private   $baseStyleName    NULL;
  101.     /**#@-*/
  102.  
  103.     /**#@+ @var boolean */
  104.     protected $customConfigFlag    = false;
  105.     protected $customStyleFlag    = false;
  106.     /**#@-*/
  107.  
  108.     /**#@+ @var string */
  109.     protected $configFile        = NULL;
  110.     protected $configSection    = NULL;
  111.     protected $name            = NULL;
  112.     /**#@-*/
  113.  
  114.     /** @var Zend_Config */
  115.     protected $config        = NULL;
  116.  
  117.     /** @var Cnz_Element */
  118.     protected $master        = NULL;
  119.  
  120.     /* Getters/Setters ===================================================*/
  121.  
  122.     /** @return string Name */
  123.     public function getName()
  124.     {
  125.         return $this->name;
  126.     }
  127.  
  128.     /* Abstract Methods ==================================================*/
  129.  
  130.     /**
  131.      * @param  string $style Value for outermost style attribute
  132.      * @return void 
  133.      */
  134.     public abstract function display($style NULL);
  135.  
  136.     /* Methods ===========================================================*/
  137.  
  138.     /**
  139.      * Option fields:
  140.      *   string       name     Element name
  141.      *   Cnz_Element  master   Master element
  142.      *   string       filename Configuration filename
  143.      *   boolean      config   Has custom configuration
  144.      *   boolean      style    Has custom style
  145.      *
  146.      * Configuration fields:
  147.      *   none
  148.      *
  149.      * @param  array $options Associative array of common options.
  150.      * @return void 
  151.      */
  152.     public function __construct(array &$options array())
  153.     {
  154.         // Process options.
  155.         if (isset($options['name'])) $this->name = strtolower($options['name']);
  156.         if (isset($options['master'])) $this->master = $options['master'];
  157.         else $this->master = $options['master'$this;
  158.         if (isset($options['filename'])) $this->configFile = $_SERVER['DOCUMENT_ROOT''/../cfg/' $options['filename'];
  159.         if (!empty($this->name))
  160.         {
  161.             if (isset($options['config'])) $this->customConfigFlag = (boolean)$options['config'];
  162.             if (isset($options['style'])) $this->customStyleFlag = (boolean)$options['style'];
  163.         }
  164.         if (empty($this->configFile)) $this->configFile = Cnz_Html::getHtmlConfigFile();
  165.  
  166.         // Generate the configuration section and style names.
  167.         $type $this->extractBaseName();
  168.         $this->configSection = self::CONFIG_PREFIX ucfirst($type);
  169.         if ($this->customConfigFlag)
  170.         {
  171.             $this->configSection .= '-' $this->name;
  172.         }
  173.         $this->baseStyleName $type;
  174.  
  175.         // Load configuration.
  176.         if (defined(get_class($this'::HAS_CONFIG'))
  177.         {
  178.             $this->config = new Zend_Config_Ini($this->configFile$this->configSection);
  179.         }
  180.  
  181.         return;
  182.     }
  183.  
  184.     /**
  185.      * Extracts the third field of the class name.
  186.      *
  187.      * @return string Third field of the class name.
  188.      */
  189.     private function extractBaseName()
  190.     {
  191.         $type get_class($this);
  192.         $subType strstr($type'Html_');
  193.         if ($subType === false)
  194.         {
  195.             trigger_error("Invalid object name $type."E_USER_ERROR
  196. );
  197.         }
  198.         $subType strtolower(substr($subType5));
  199.         $end strpos($subType'_');
  200.         if ($end 0$subType substr($subType0$end);
  201.         return $subType;
  202.     }
  203.  
  204.     /**
  205.      * Generates the id attribute value.
  206.      *
  207.      * @param  string $number 
  208.      * @return string Value for use with HTML id attribute
  209.      */
  210.     protected function genId($number NULL)
  211.     {
  212.         $baseStyleName $this->baseStyleName;
  213.         $s $baseStyleName;
  214.         if (!empty($this->master&& $this !== $this->master)
  215.         {
  216.             $s .= self::STYLE_SEPARATOR $this->master->getName();
  217.         }
  218.         if (!empty($this->name))
  219.         {
  220.             $s .= self::STYLE_SEPARATOR $this->name;
  221.         }
  222.         if (!empty($number))
  223.         {
  224.             $s .= self::STYLE_SEPARATOR $number;
  225.         }
  226.         return $s;
  227.     }
  228.  
  229.     /**
  230.      * Generates the class attribute value.
  231.      *
  232.      * @param  string $subtype Style name subtype
  233.      * @param  string $suffix  Style name suffix
  234.      * @return string List of styles for use with HTML class attribute
  235.      */
  236.     protected function genStyles($subtype NULL$suffix NULL)
  237.     {
  238.         $baseStyleName $this->baseStyleName;
  239.         if (!empty($subtype))
  240.         {
  241.             $baseStyleName .=  ucfirst(strtolower($subtype));
  242.         }
  243.         if ($this->customStyleFlag)
  244.         {
  245.             if (!empty($subtype))
  246.             {
  247.                 $styleName $this->baseStyleName ucfirst(strtolower($subtype)) self::STYLE_SEPARATOR $this->master->getName();
  248.             }
  249.             else
  250.             {
  251.                 $styleName $this->baseStyleName self::STYLE_SEPARATOR $this->master->getName();
  252.             }
  253.         }
  254.  
  255.         $s $baseStyleName;
  256.         if (!empty($styleName)) $s .= ' ' $styleName;
  257.         if (!empty($suffix))
  258.         {
  259.             $s .= ' ' $baseStyleName self::STYLE_SEPARATOR $suffix;
  260.             if ($this->customStyleFlag$s .= ' ' $styleName self::STYLE_SEPARATOR $suffix;
  261.         }
  262.         return $s;
  263.     }
  264. }

Documentation generated on Thu, 19 Jul 2007 15:02:01 -0400 by phpDocumentor 1.4.0RC2