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

Source for file Container.php

Documentation is available at Container.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: 11 $
  21.  */
  22.  
  23. /**
  24.  * Formagic Container Item class
  25.  *
  26.  * Changes on following flag will be applied recursively to all items added to
  27.  * the container object:
  28.  *  - _isHidden
  29.  *  - _isBlocked
  30.  *  - _isDisabled
  31.  *
  32.  * Changes on the flag _isPostItem will only be applied to the container item
  33.  * itself (and defaults to false as there is no use posting a container item).
  34.  *
  35.  * @category    Formagic
  36.  * @package     Item
  37.  * @author      Florian Sonnenburg
  38.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  39.  * @version     $Id: Container.php 11 2007-08-12 20:02:57Z meweasle $
  40.  */
  41.  
  42.     /**
  43.      * Pointer to items array section of formagic object
  44.      * @var     string 
  45.      ***/
  46.     protected $_items = array();
  47.  
  48.     /**
  49.      * Container headline
  50.      * @var     string 
  51.      ***/
  52.     public $headline = '';
  53.  
  54.     /**
  55.      * Tells renderer if container has its own surrounding template
  56.      * @var     boolean 
  57.      ***/
  58.     public $hasHtml = true;
  59.  
  60.     /**
  61.      * Generic argument handler
  62.      *
  63.      * @param string $key 
  64.      * @param mixed $arg 
  65.      * @return void 
  66.      */
  67.     protected function _handleArg($key$arg)
  68.     {
  69.         switch($key){
  70.             case 'headline':
  71.                 $this->headline = $arg;
  72.                 break;
  73.             default:
  74.                 throw new Exception("Argument type '$keynot supported");
  75.         // switch
  76.     }
  77.  
  78.     /**
  79.      * OnCreate-Handler implementation
  80.      *
  81.      * @return boolean 
  82.      */
  83.     protected function _onCreate()
  84.     {
  85.         parent::_onCreate();
  86.         $this->isPostItem = false;
  87.         $this->wide true;
  88.         $this->attributes['id'$this->id;
  89.         return true;
  90.     }
  91.  
  92.     /**
  93.      * Adds formagic item object to array of items.
  94.      *
  95.      * Creates new item object and adds this, or adds passed formagicItem object
  96.      *
  97.      * @param mixed $type String with item type or formagicItem object
  98.      * @param string $name String with item name. NULL if $type is formagicItem
  99.      *         object
  100.      * @param array $args Array with additional item information. NULL if $type
  101.      *         is formagicItem object
  102.      * @return object formagicItem object
  103.      */
  104.     public function addItem($type$name$args=null)
  105.     {
  106.         try {
  107.             // hand down status flags to added items
  108.             if ($this->_isHidden{
  109.                 $args['hidden'true;
  110.             }
  111.             if ($this->_isBlocked{
  112.                 $args['blocked'true;
  113.             }
  114.  
  115.             $obj =$this->_caller->createItem($type$name$args);
  116.             $this->_items[$name=$obj;
  117.         catch (Formagic_Exception $e{
  118.             $this->_caller->setMessage($e->getMessage()Formagic::ERROR);
  119.             return false;
  120.         }
  121.         return $obj;
  122.     }
  123.  
  124.     /**
  125.      * Counts items added to self and all sub-containers
  126.      *
  127.      * @return integer 
  128.      */
  129.     public function countItems()
  130.     {
  131.         $count 0;
  132.         foreach($this->_items as $item{
  133.             if ($item instanceOf Formagic_Item_Container{
  134.                 $count += $item->countItems();
  135.             else {
  136.                 $count += 1;
  137.             }
  138.         }
  139.         return $count;
  140.     }
  141.  
  142.     /**
  143.      * Returns pointer to items array
  144.      *
  145.      * @return array 
  146.      */
  147.     public function &getItems()
  148.     {
  149.         return $this->_items;
  150.     }
  151.  
  152.     /**
  153.      * Returns added item $name
  154.      *
  155.      * Throws Formagic_Exception if item does not exist
  156.      *
  157.      * @throws Formagic_Exception
  158.      * @param integer $name 
  159.      * @return Formagic_Item 
  160.      */
  161.     public function getItem($name)
  162.     {
  163.         if (isset($this->_items[$name])) {
  164.             return $this->_items[$name];
  165.         }
  166.         throw new Formagic_Exception("Item '$namedoes not exist in container $this->name);
  167.     }
  168.  
  169.     /**
  170.      * Blocks container item and all its descendants
  171.      *
  172.      * @return boolean 
  173.      */
  174.     public function block()
  175.     {
  176.         $this->_isBlocked = true;
  177.         $items =$this->getItems();
  178.         foreach($items as &$item{
  179.             $item->block();
  180.         }
  181.         return true;
  182.     }
  183.  
  184.     /**
  185.      * Sets hidden flag on container item and all its descendants
  186.      *
  187.      * @return boolean 
  188.      */
  189.     public function setHidden($flag)
  190.     {
  191.         $this->_isHidden = $flag;
  192.  
  193.         $items =$this->getItems();
  194.         foreach($items as &$item{
  195.             $item->setHidden($flag);
  196.         }
  197.         return $flag;
  198.     }
  199.  
  200.     /**
  201.      * Disables container item and all its descendants and thus removes all
  202.      * involved items from form
  203.      *
  204.      * @return boolean 
  205.      */
  206.     public function disable()
  207.     {
  208.         $this->_isDisabled = true;
  209.         $items =$this->getItems();
  210.         foreach($items as &$item{
  211.             $item->disable();
  212.         }
  213.         return true;
  214.     }
  215.  
  216.     /**
  217.      * Returns values of all stored items.
  218.      *
  219.      * @return array 
  220.      */
  221.     public function &getValue()
  222.     {
  223.         $res array();
  224.         foreach($this->getItems(as $item{
  225.             $res[$item->name$item->getValue();
  226.         }
  227.         return $res;
  228.     }
  229.  
  230.     /**
  231.      * Returns HTML attributes as one assembled string
  232.      *
  233.      * @return string 
  234.      */
  235.     public function getAttributeStr()
  236.     {
  237.         return $this->_getAttributeStr();
  238.     }
  239.  
  240.     /**
  241.      * Validates containing items
  242.      *
  243.      * Iterates through all containing items. If any rule is violated, sub-item
  244.      * will return violated Formagic_Rule item and the container will pass it
  245.      * on.
  246.      *
  247.      * If all items pass violation, validate() calls onValidate-handler and
  248.      * returns its result.
  249.      *
  250.      * @return boolean 
  251.      */
  252.     public function validate()
  253.     {
  254.         if (!$this->_onPreValidate()) {
  255.             return false;
  256.         }
  257.  
  258.         foreach($this->getItems(as $item{
  259.             $res $item->validate();
  260.             if ($res instanceOf Formagic_Rule{
  261.                 return $res;
  262.             }
  263.         }
  264.  
  265.         return $this->_onPostValidate();
  266.     }
  267.  
  268.     /**
  269.      * OnFetch-handler implementation
  270.      *
  271.      * @return void 
  272.      */
  273.     protected function _onFetch()
  274.     {
  275.         foreach($this->getItems(as $item{
  276.             $item->_onFetch();
  277.         }
  278.     }
  279.  
  280. }

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