Advertisement
Guest User

na1k

a guest
Sep 16th, 2009
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.05 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Renderuje kazdy radio input jako radek tabulky, volitelne s rozsirenymi popisky
  5.  */
  6. class ExtendedRadioList extends FormControl {
  7.  
  8.    /** @var Html  separator element template */
  9.     protected $separator;
  10.  
  11.     /** @var Html  container element template */
  12.     protected $container;
  13.  
  14.     /** @var array */
  15.     protected $items = array();
  16.  
  17.    /** @var rozsirene popisky inputu */
  18.    private $descriptions = NULL;
  19.    
  20.    
  21.    /**
  22.     * Oproti RadioList je konstruktor rozsireny o treti parametr - pole objektu s popisky
  23.     * TODO: zavest pro popisky rozhrani?
  24.     * @param mixed $label [optional]
  25.     * @param array $items [optional]
  26.     * @param array $descriptions [optional] pole poli s popisky
  27.     * @return
  28.     */
  29.    public function __construct($label = NULL, array $items = NULL, array $descriptions = NULL) {
  30.         parent::__construct($label);
  31.         $this->control->type = 'radio';
  32.         $this->container = Html::el('');
  33.         $this->separator = Html::el('br');
  34.         if ($items !== NULL) $this->setItems($items);
  35.       if ($descriptions !== NULL) $this->setDescriptions($descriptions);
  36.     }
  37.    
  38.    /**
  39.     * Setter pro $descriptions
  40.     * @param array $desc
  41.     * @return void
  42.     */
  43.    public function setDescriptions(array $desc) {
  44.       $this->descriptions = $desc;
  45.    }
  46.  
  47.    
  48.     /**
  49.      * Returns selected radio value.
  50.      * @param  bool
  51.      * @return mixed
  52.      */
  53.     public function getValue($raw = FALSE)
  54.     {
  55.         return is_scalar($this->value) && ($raw || isset($this->items[$this->value])) ? $this->value : NULL;
  56.     }
  57.  
  58.  
  59.  
  60.     /**
  61.      * Sets options from which to choose.
  62.      * @param  array
  63.      * @return RadioList  provides a fluent interface
  64.      */
  65.     public function setItems(array $items)
  66.     {
  67.         $this->items = $items;
  68.         return $this;
  69.     }
  70.  
  71.  
  72.  
  73.     /**
  74.      * Returns options from which to choose.
  75.      * @return array
  76.      */
  77.     final public function getItems()
  78.     {
  79.         return $this->items;
  80.     }
  81.  
  82.  
  83.  
  84.     /**
  85.      * Returns separator HTML element template.
  86.      * @return Html
  87.      */
  88.     final public function getSeparatorPrototype()
  89.     {
  90.         return $this->separator;
  91.     }
  92.  
  93.  
  94.  
  95.     /**
  96.      * Returns container HTML element template.
  97.      * @return Html
  98.      */
  99.     final public function getContainerPrototype()
  100.     {
  101.         return $this->container;
  102.     }
  103.  
  104.    /**
  105.     * Vraci strukturu controlu k vykresleni
  106.     * @param object $key [optional] vraci pouze control s danym keyem
  107.     * @return Html
  108.     */
  109.    public function getControl($key = NULL) {
  110.       $container = clone $this->container;
  111.      
  112.       if (!isset($this->items[$key])) {
  113.             return NULL;
  114.         }
  115.  
  116.         $control = parent::getControl($key);
  117.         $id = $control->id;
  118.         $counter = -1;
  119.         $value = $this->value === NULL ? NULL : (string) $this->getValue();
  120.  
  121.         foreach ($this->items as $k => $val) {
  122.             $counter++;
  123.             if ($key !== NULL && $key != $k) continue; // intentionally ==
  124.  
  125.          $control->id = $id . '-' . $counter;
  126.             $control->checked = (string) $k === $value;
  127.             $control->value = $k;
  128.  
  129.  
  130.          /*  /----------------------------------------------\
  131.           *  |     |         desc[0]            |           |
  132.           *  |  o  |----------------------------|  desc[1]  |
  133.           *  |     |         desc[2]            |           |
  134.           *  \----------------------------------------------/
  135.           */
  136.             if ($this->descriptions !== NULL && isset($this->descriptions[$k])) {            
  137.             $desc = $this->descriptions[$k];
  138.             $row = Html::el('tr');
  139.            
  140.             $td1 = Html::el('td')->setHtml( (string)$control );
  141.             if (count($desc)>2) $td1->rowspan(2);
  142.             $row->add($td1);                      
  143.            
  144.             $td2 = Html::el('td')->setHtml($desc[0]);
  145.             $row->add($td2);
  146.            
  147.             if (isset($desc[1])) {
  148.                $td3 = Html::el('td')->setHtml($desc[1]);
  149.                if (count($desc)>2) $td3->rowspan(2);
  150.                $row->add($td3);
  151.             }
  152.            
  153.             $container->add($row);
  154.            
  155.             if (isset($desc[2])) {
  156.                $row = Html::el('tr');
  157.                $td4 = Html::el('td')->setHtml($desc[2]);
  158.                $row->add($td4);
  159.                $container->add($row);
  160.             }
  161.            
  162.             } else {               
  163.             $row = Html::el('tr');
  164.            
  165.             $td1 = Html::el('td')->setHtml( (string)$control );
  166.             $row->add($td1);               
  167.            
  168.             $td2 = Html::el('td');
  169.             if ($val instanceof Html) {
  170.                 $td2->setHtml($val);
  171.             } else {
  172.                 $td2->setText($this->translate($val));
  173.             }
  174.             $row->add($td2);
  175.            
  176.                $container->add($row);  
  177.             }
  178.  
  179.            
  180.         }
  181.  
  182.         return $container;  
  183.    }
  184.  
  185.    
  186.     /**
  187.      * Generates label's HTML element.
  188.      * @return void
  189.      */
  190.     public function getLabel()
  191.     {
  192.         $label = parent::getLabel();
  193.         $label->for = NULL;
  194.         return $label;
  195.     }
  196.  
  197.  
  198.  
  199.     /**
  200.      * Filled validator: has been any radio button selected?
  201.      * @param  IFormControl
  202.      * @return bool
  203.      */
  204.     public static function validateFilled(IFormControl $control)
  205.     {
  206.         return $control->getValue() !== NULL;
  207.     }
  208.  
  209.  
  210. }
  211. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement