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

Source for file Item.php

Documentation is available at Item.php

  1. <?php
  2. /**
  3.  * Formagic
  4.  *
  5.  * LICENCE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at
  10.  * http://formagic.weasle.de/licence.txt
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to licence@weasle.de so we can send you a copy immediately.
  14.  *
  15.  * @category    Formagic
  16.  * @package     Item
  17.  * @author      Florian Sonnenburg
  18.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  19.  * @license     http://formagic.weasle.de/licence.txt     New BSD License
  20.  * @revision    $Revision: 14 $
  21.  */
  22.  
  23. /**
  24.  * Abstract superclass for Formagic items
  25.  *
  26.  * @category    Formagic
  27.  * @package     Item
  28.  * @author      Florian Sonnenburg
  29.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  30.  * @version     $Id: Item.php 14 2007-08-13 20:33:26Z meweasle $
  31.  ***/
  32. abstract class Formagic_Item
  33. {
  34.     /**
  35.      * Type of form item
  36.      * @var string 
  37.      */
  38.     public $type;
  39.  
  40.     /**
  41.      * Form item name
  42.      * @var string 
  43.      */
  44.     public $name;
  45.  
  46.     /**
  47.      * Form item ID
  48.      * @var string 
  49.      */
  50.     public $id;
  51.  
  52.     /**
  53.      * Form item label
  54.      * @var boolean 
  55.      */
  56.     protected $_label = 'undefined';
  57.  
  58.     /**
  59.      * Additional attributes for item
  60.      * @var array 
  61.      */
  62.     public $attributes = array();
  63.  
  64.     /**
  65.      * Formagic object that called and contains item
  66.      * @var object 
  67.      */
  68.     protected $_caller = null;
  69.  
  70.     /**
  71.      * Array of rule object that are applied for this item
  72.      * @var object 
  73.      */
  74.     protected $_rules = array();
  75.  
  76.     /**
  77.      * Flag that defines if item content can be edited.
  78.      * @var boolean 
  79.      */
  80.     protected $_isBlocked = false;
  81.  
  82.     /**
  83.      * Flag that defines if item will be displayed
  84.      * @var boolean 
  85.      */
  86.     protected $_isHidden = false;
  87.  
  88.     /**
  89.      * Flag that defines if item is removed from Formagic
  90.      * @var boolean 
  91.      */
  92.     protected $_isDisabled = false;
  93.  
  94.     /**
  95.      * Flag that defines if item content will be posted on submit
  96.      * @var boolean 
  97.      */
  98.     public $isPostItem = true;
  99.  
  100.     /**
  101.      * Pointer to submitted value for this item, stored in calling Formagic object
  102.      * @see Formagic_Item::setCaller()
  103.      * @var string 
  104.      */
  105.     protected $submittedValue = null;
  106.  
  107.     /**
  108.      * Values that are not changed by submitted values
  109.      * @var array 
  110.      ***/
  111.     protected $_constantValue = null;
  112.  
  113.     /**
  114.      * Pointer to default value for this item, stored in calling Formagic object
  115.      * @see FormagicItem::setCaller()
  116.      * @var string 
  117.      */
  118.     protected $defaultValue = null;
  119.  
  120.     /**
  121.      * Pointer to mulitpage value for this item, stored in calling Formagic object
  122.      * @var string 
  123.      */
  124.     protected $savedValue = null;
  125.  
  126.     /**
  127.      * Flag that tells if this Formagic item has been submitted
  128.      * @var boolean 
  129.      */
  130.     public $isSubmitted = false;
  131.  
  132.     /**
  133.      * Error message string
  134.      * @var string 
  135.      */
  136.     private $_errorMessage null;
  137.  
  138.     /**
  139.      * Defines if template takes full width
  140.      * @var boolean 
  141.      */
  142.     public $isWide = false;
  143.  
  144.     /**
  145.      * Constructor
  146.      *
  147.      * @param string $type Type of item
  148.      * @param string $name Name of item
  149.      * @param array $args Additional attributes
  150.      * @param object $caller containing Formagic object
  151.      * @throws Formagic_Exception
  152.      * @return void 
  153.      ***/
  154.     public function __construct($type$name&$args=null&$caller=null)
  155.     {
  156.         $this->type = $type;
  157.         $this->name = $name;
  158.         $this->id = str_replace(array('[',']')array('_','')$this->name);
  159.         $this->setCaller($caller);
  160.  
  161.         if (is_array($args)) {
  162.             foreach($args as $key => $arg{
  163.                 switch($key{
  164.                     case 'label':
  165.                         $this->_label = $arg;
  166.                         break;
  167.                     case 'constant':
  168.                         if (!$this->_caller{
  169.                             throw new Formagic_Exception("Can't set constant value: " .
  170.                                         "Calling Formagic object not found");
  171.                         }
  172.                         $this->_constantValue = $arg;
  173.                         break;
  174.                     case 'default':
  175.                         if (!$this->_caller{
  176.                             throw new Formagic_Exception("Can't set default value: " .
  177.                                         "Calling Formagic object not found");
  178.                         }
  179.                         $this->defaultValue = $arg;
  180.                         break;
  181.                     case 'mulitpage':
  182.                         if (!$this->_caller{
  183.                             throw new Formagic_Exception("Can't add to multipage items: " .
  184.                                         "Calling Formagic object not found");
  185.                         }
  186.                         $this->_caller->addMultipageItem($this->name);
  187.                         break;
  188.                     case 'attributes':
  189.                         $this->setAttributes($arg);
  190.                         break;
  191.                     case 'hidden':
  192.                         $this->setHidden($arg);
  193.                         break;
  194.                     case 'blocked':
  195.                         $this->block();
  196.                         break;
  197.                     case 'rule':
  198.                         $arg = (array)$arg;
  199.                         foreach($arg as $rule{
  200.                             $this->addRule($rule);
  201.                         }
  202.                         break;
  203.                     default:
  204.                         // Argument handler for unknown arguments. Defined in
  205.                         // item classes
  206.                         $this->_handleArg($key$arg);
  207.                 // switch
  208.             }
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Handler for additional item arguments
  214.      *
  215.      * @param string $key Argument name
  216.      * @param mixed $arg Argument value
  217.      * @return boolean 
  218.      */
  219.     protected function _handleArg($key$arg)
  220.     {
  221.         return true;
  222.     }
  223.  
  224.     /**
  225.      * Can be called to set caller object if not passed to constructor
  226.      *
  227.      * @param object $caller 
  228.      * @return boolean 
  229.      ***/
  230.     public function setCaller(&$caller)
  231.     {
  232.         if (!$caller{
  233.             return false;
  234.         }
  235.  
  236.         $this->_caller =$caller;
  237.  
  238.         if (isset($this->_caller->submitValues[$this->name])) {
  239.             $this->isSubmitted = true;
  240.             $this->submittedValue =$this->_caller->submitValues[$this->name];
  241.         }
  242.  
  243.         $this->_constantValue =$this->_caller->constantValues[$this->name];
  244.         $this->defaultValue =$this->_caller->defaultValues[$this->name];
  245.         $this->savedValue =$this->_caller->savedValues[$this->name];
  246.  
  247.         return true;
  248.     }
  249.  
  250.     /**
  251.      * Returns parent Formagic object
  252.      *
  253.      * @return Formagic 
  254.      */
  255.     public function getCaller()
  256.     {
  257.         return $this->_caller;
  258.     }
  259.  
  260.     /**
  261.      * Returns string representation of formItem object
  262.      *
  263.      * @return string 
  264.      ***/
  265.     public function __toString()
  266.     {
  267.         $callerPresent = isset($this->_caller);
  268.         $str "FormagicItem [type: {$this->type}][name: {$this->name}]" .
  269.                "[submitted value: '{$this->submittedValue}']" .
  270.                "[default value: '{$this->defaultValue}']" .
  271.                "[saved value: '{$this->savedValue}']" .
  272.                "[flagshidden '{$this->_isHidden}', blocked '{$this->_isBlocked}', disabled '{$this->_isDisabled}']" .
  273.                "<br />";
  274.         return $str;
  275.     }
  276.  
  277.     /**
  278.      * Returns current value for this item
  279.      *
  280.      * Calls Formagic::getValue() as all value storages are held there.
  281.      *
  282.      * @return mixed
  283.      */
  284.     public function &getValue()
  285.     {
  286.         $value = &$this->_caller->getValue($this->name);
  287.         return $value;
  288.     }
  289.  
  290.     /**
  291.      * Returns label for this item.
  292.      *
  293.      * @return string
  294.      */
  295.     public function getLabel()
  296.     {
  297.         return $this->_label;
  298.     }
  299.  
  300.     /**
  301.      * HTML template for renderers that use HTML-Code
  302.      *
  303.      * @return string
  304.      **/
  305.     public function getHtml()
  306.     {
  307.         return "";
  308.     }
  309.  
  310.     /**
  311.      * Sets additional attributes for this item
  312.      *
  313.      * Mainly used for additional HTML attributes other than "name", "id" or
  314.      * "value", such as "style", "class", javascript-handlers etc. Attributes
  315.      * are added corresponding to key->value-pairs in $attArray.
  316.      *
  317.      * @param array $attArray
  318.      * @return array
  319.      */
  320.     public function setAttributes($attArray)
  321.     {
  322.         return $this->attributes = $attArray;
  323.     }
  324.  
  325.     /**
  326.      * Returns attribute string for HTML tag
  327.      *
  328.      * @return string
  329.      */
  330.     protected function _getAttributeStr()
  331.     {
  332.         if (!count($this->attributes)) {
  333.             return "";
  334.         }
  335.         $res = "";
  336.         foreach($this->attributes as $key => $att{
  337.             $res .= " $key=\"$att\"";
  338.         }
  339.         return $res;
  340.     }
  341.  
  342.     /**
  343.      * Adds rule object to Formagic item
  344.      *
  345.      * Formagic items can have multiple rules, which are saved in
  346.      * FormagicItem::rules.
  347.      * First parameter $rule can either be a string or a FormagicRule object.
  348.      * String value is assumed to be the type of rule to be added.
  349.      *
  350.      * This method throws an exception if no valid role object can be identified.
  351.      *
  352.      * @param mixed $rule Rule type or FormagicRule object.
  353.      * @param string $errorMessage Message returned if rule is violated
  354.      * @param array $args Optional.
  355.      * @throws Formagic_Exception
  356.      * @return integer
  357.      **/
  358.     public function addRule($rule, $errorMessage=null, $args=null)
  359.     {
  360.         // argument is assumed rule type if string.
  361.         if (is_string($rule)) {
  362.             $class = 'Formagic_Rule_' . ucFirst($rule);
  363.             $this->_caller->loadClass($class);
  364.             $this->_rules[$rulenew $class($rule,$errorMessage,$args);
  365.         } elseif($rule instanceof Formagic_Rule) {
  366.             $type = $rule->getType();
  367.             $this->_rules[$type$rule;
  368.         } else {
  369.             throw new Formagic_Exception('Invalid rule type or rule object');
  370.         }
  371.         return true;
  372.     }
  373.  
  374.     /**
  375.      * Tells if rule exists for this item
  376.      *
  377.      * @param string $rule
  378.      * @return boolean
  379.      */
  380.     public function hasRule($rule)
  381.     {
  382.         return isset($this->_rules[$rule]);
  383.     }
  384.  
  385.     /**
  386.      * Performs rule checks
  387.      *
  388.      * Iterates through all defined rules of Formagic item. Returns true if all
  389.      * rules apply or violated Formagic_Rule object
  390.      *
  391.      * @throws Formagic_Rule_Exception
  392.      * @return mixed Rule object or boolean
  393.      */
  394.     public function validate()
  395.     {
  396.         if (!$this->_onPreValidate()) {
  397.             return false;
  398.         }
  399.  
  400.         foreach($this->_rules as $ruleObj{
  401.             if (!$ruleObj->check($this)) {
  402.                 $this->_errorMessage $ruleObj->getMessage();
  403.                 return $ruleObj;
  404.             }
  405.         }
  406.  
  407.         if (!$this->_onPostValidate()) {
  408.             return false;
  409.         }
  410.  
  411.         return true;
  412.     }
  413.  
  414.     /**
  415.      * Returns item error message
  416.      *
  417.      * Returns error message if item check failed or empty string if not
  418.      *
  419.      * @return string
  420.      */
  421.     public function getErrorMessage()
  422.     {
  423.         return $this->_errorMessage;
  424.     }
  425.  
  426.     /**
  427.      * Sets blocked flag
  428.      *
  429.      * @return boolean
  430.      */
  431.     public function block()
  432.     {
  433.         return $this->_isBlocked = true;
  434.     }
  435.  
  436.     /**
  437.      * Sets hidden flag for item
  438.      *
  439.      * @return boolean
  440.      */
  441.     public function setHidden($flag)
  442.     {
  443.         return $this->_isHidden = $flag;
  444.     }
  445.  
  446.     /**
  447.      * Returns hidden status of item
  448.      *
  449.      * @return boolean
  450.      */
  451.     public function isHidden()
  452.     {
  453.         return $this->_isHidden;
  454.     }
  455.  
  456.     /**
  457.      * Sets disabled flag for item and thus removes it from form
  458.      *
  459.      * @return boolean
  460.      */
  461.     public function disable()
  462.     {
  463.         return $this->_isDisabled = true;
  464.     }
  465.  
  466.     /**
  467.      * Returns disabled status of item
  468.      *
  469.      * @return boolean
  470.      */
  471.     public function isDisabled()
  472.     {
  473.         return $this->_isDisabled;
  474.     }
  475.  
  476.     /**
  477.      * Event dispatcher
  478.      *
  479.      * @param string $event
  480.      * @throws Formagic_Exception
  481.      * @return boolean
  482.      **/
  483.     public function _execEvent($event)
  484.     {
  485.         switch($event) {
  486.             case 'onCreate':
  487.                 return $this->_onCreate();
  488.             case 'onFetch':
  489.                 $this->_onFetch();
  490.                 break;
  491.             default:
  492.                 throw new Formagic_Exception('Event "' $event '" not supported');
  493.         } // switch
  494.         return true;
  495.     }
  496.  
  497.     /**
  498.      * Executed before item validation
  499.      *
  500.      * Can be overwritten. Must return true if successful. If not,
  501.      * can either return false or throw exception
  502.      *
  503.      * @return boolean
  504.      */
  505.     protected function _onPreValidate()
  506.     {
  507.         return true;
  508.     }
  509.  
  510.     /**
  511.      * Executed after item validation is done
  512.      *
  513.      * @return boolean
  514.      */
  515.     protected function _onPostValidate()
  516.     {
  517.         return true;
  518.     }
  519.  
  520.     /**
  521.      * Executed when object is created
  522.      *
  523.      * @return boolean
  524.      */
  525.     protected function _onCreate()
  526.     {
  527.         return true;
  528.     }
  529.  
  530.     /**
  531.      * Executed when Formagic::fetch() is called
  532.      *
  533.      * @return void
  534.      */
  535.     protected function _onFetch()
  536.     {
  537.     }
  538.  

Documentation generated on Thu, 23 Aug 2007 00:29:42 +0200 by phpDocumentor 1.4.0