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

Source for file Radio.php

Documentation is available at Radio.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.  * Input type radio for formagic formgenerator
  25.  *
  26.  * @category    Formagic
  27.  * @package     Item
  28.  * @author      Florian Sonnenburg
  29.  * @copyright   Copyright (c) 2007 Florian Sonnenburg
  30.  * @version     $Id: Radio.php 11 2007-08-12 20:02:57Z meweasle $
  31.  */
  32.  
  33.     /**
  34.      * Array containing select options
  35.      * @var     array 
  36.      * @access  private
  37.      */
  38.     private $data;
  39.  
  40.     /**
  41.      * String to separate radio elements from each other
  42.      * @var     string 
  43.      * @access  private
  44.      */
  45.     private $separator "\n";
  46.  
  47.     /**
  48.      * Tells if an empty radio element should be added to list of elements
  49.      * @var     array 
  50.      * @access  private
  51.      */
  52.     private $emptyElement null;
  53.  
  54.     /**
  55.      * Argument handler for arguments that are unknown to item parent class
  56.      *
  57.      * @param string $key Argument name
  58.      * @param mixed $arg Argument value
  59.      * @return boolean 
  60.      */
  61.     protected function _handleArg($key$arg)
  62.     {
  63.         switch($key){
  64.             case 'separator':
  65.                 $this->separator $arg;
  66.                 break;
  67.             case 'addEmptyFirst':
  68.                 $this->addEmpty($arg'first');
  69.                 break;
  70.             case 'addEmpty':
  71.             case 'addEmptyLast':
  72.                 $this->addEmpty($arg'last');
  73.                 break;
  74.             case 'data':
  75.                 $this->setData($arg);
  76.                 break;
  77.             default:
  78.                 throw new Exception("Argument type '$keynot supported");
  79.         // switch
  80.         return true;
  81.     }
  82.  
  83.     /**
  84.      * Saves empty element
  85.      *
  86.      * @param mixed $element Label of empty element if string. If true, '---' is
  87.      *           label of empty element.
  88.      * @param mixed $pos Where to add empty element. Allowed values:
  89.      *           'first' or 'last'
  90.      * @return boolean 
  91.      */
  92.     public function addEmpty($element$pos)
  93.     {
  94.         if (!$element{
  95.             return false;
  96.         }
  97.         $element is_bool($element'---' $element;
  98.         $this->emptyElement array($element,$pos);
  99.         return true;
  100.     }
  101.  
  102.     /**
  103.      * Adds empty element to data array after constructor is processed
  104.      *
  105.      * @return boolean 
  106.      */
  107.     protected function _onCreate()
  108.     {
  109.         if ($this->emptyElement{
  110.             $element $this->emptyElement[0];
  111.             $pos $this->emptyElement[1];
  112.             $this->data $pos == 'last'
  113.                     ? $this->data array(''=>$element)
  114.                     : array(''=>$element$this->data;
  115.         }
  116.         return true;
  117.     }
  118.  
  119.     /**
  120.      * Sets data for radio elements
  121.      *
  122.      * @param array $arr Associative array. Array-key will be value of radio
  123.      *           elements, array-value will be label.
  124.      * @return boolean 
  125.      */
  126.     public function setData($arr)
  127.     {
  128.         $this->data $arr;
  129.         return true;
  130.     }
  131.  
  132.     /**
  133.      * Sets separator string
  134.      *
  135.      * @param string $separator 
  136.      * @return string 
  137.      */
  138.     public function setSeparator($separator)
  139.     {
  140.         return $this->separator $separator;
  141.     }
  142.  
  143.     /**
  144.      * Returns string with HTML representation of radio elements
  145.      *
  146.      * @return string 
  147.      */
  148.     public function getHtml()
  149.     {
  150.         // HTML blocked
  151.         $currVal $this->getValue();
  152.         if ($this->_isBlocked{
  153.             $str "";
  154.             if ($this->isPostItem{
  155.                 $str .= '<input type="hidden" name="' $this->name .
  156.                         '" value="' $currVal '" />';
  157.             }
  158.             foreach($this->data as $key => $value{
  159.                 $checkIndicator $key == $currVal "(o)" "(&nbsp;&nbsp;)";
  160.                 $str .= $checkIndicator " " $value $this->separator;
  161.             }
  162.  
  163.         // HTML default
  164.         else {
  165.             $str "";
  166.             $attrStr $this->_getAttributeStr();
  167.  
  168.             foreach($this->data as $key => $value{
  169.                 $c = (string)$key == (string)$currVal ' checked="checked"' '';
  170.                 $rName $this->name . $key;
  171.                 $rId $this->id . $key;
  172.  
  173.                 // id=\"" . $this->name . "\"
  174.                 $str .= "<input type=\"radio\" id=\"$rId\" name=\"{$this->name}\"" .
  175.                         " value=\"$key\"{$c}$attrStr /><label for=\"$rId\">$value" .
  176.                         "</label>{$this->separator}";
  177.             }
  178.         }
  179.         $str = rtrim($str, $this->separator);
  180.         return $str;
  181.     }
  182.  

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