menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TCheckGroup.php

Documentation is available at TCheckGroup.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Adianti\Control\TAction;
  7. use Adianti\Widget\Base\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TForm;
  10. use Adianti\Widget\Form\TLabel;
  11. use Adianti\Widget\Form\TField;
  12. use Adianti\Widget\Form\TCheckButton;
  13.  
  14. use Exception;
  15.  
  16. /**
  17.  * A group of CheckButton's
  18.  *
  19.  * @version    5.6
  20.  * @package    widget
  21.  * @subpackage form
  22.  * @author     Pablo Dall'Oglio
  23.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  24.  * @license    http://www.adianti.com.br/framework-license
  25.  */
  26. class TCheckGroup extends TField implements AdiantiWidgetInterface
  27. {
  28.     private $layout 'vertical';
  29.     private $changeAction;
  30.     private $items;
  31.     private $breakItems;
  32.     private $buttons;
  33.     private $labels;
  34.     private $allItemsChecked;
  35.     protected $separator;
  36.     protected $changeFunction;
  37.     protected $formName;
  38.     protected $labelClass;
  39.     protected $useButton;
  40.     protected $value;
  41.     
  42.     /**
  43.      * Class Constructor
  44.      * @param  $name name of the field
  45.      */
  46.     public function __construct($name)
  47.     {
  48.         parent::__construct($name);
  49.         parent::setSize(NULL);
  50.         $this->labelClass = 'tcheckgroup_label ';
  51.         $this->useButton  = FALSE;
  52.     }
  53.     
  54.     /**
  55.      * Clone object
  56.      */
  57.     public function __clone()
  58.     {
  59.         if (is_array($this->items))
  60.         {
  61.             $oldbuttons $this->buttons;
  62.             $this->buttons array();
  63.             $this->labels  array();
  64.  
  65.             foreach ($this->items as $key => $value)
  66.             {
  67.                 $button new TCheckButton("{$this->name}[]");
  68.                 $button->setProperty('checkgroup'$this->name);
  69.                 $button->setIndexValue($key);
  70.                 $button->setProperty('onchange'$oldbuttons[$key]->getProperty('onchange'));
  71.                 
  72.                 $obj new TLabel($value);
  73.                 $this->buttons[$key$button;
  74.                 $this->labels[$key$obj;
  75.             }
  76.         }
  77.     }
  78.     
  79.     /**
  80.      * Check all options
  81.      */
  82.     public function checkAll()
  83.     {
  84.         $this->allItemsChecked = TRUE;
  85.     }
  86.     
  87.     /**
  88.      * Define the direction of the options
  89.      * @param $direction String (vertical, horizontal)
  90.      */
  91.     public function setLayout($dir)
  92.     {
  93.         $this->layout = $dir;
  94.     }
  95.     
  96.     /**
  97.      * Get the direction (vertical or horizontal)
  98.      */
  99.     public function getLayout()
  100.     {
  101.         return $this->layout;
  102.     }
  103.     
  104.     /**
  105.      * Define after how much items, it will break
  106.      */
  107.     public function setBreakItems($breakItems)
  108.     {
  109.         $this->breakItems = $breakItems;
  110.     }
  111.     
  112.     /**
  113.      * Show as button
  114.      */
  115.     public function setUseButton()
  116.     {
  117.        $this->labelClass = 'btn btn-default ';
  118.        $this->useButton  = TRUE;
  119.     }
  120.     
  121.     /**
  122.      * Add items to the check group
  123.      * @param $items An indexed array containing the options
  124.      */
  125.     public function addItems($items)
  126.     {
  127.         if (is_array($items))
  128.         {
  129.             $this->items = $items;
  130.             $this->buttons = array();
  131.             $this->labels  = array();
  132.  
  133.             foreach ($items as $key => $value)
  134.             {
  135.                 $button = new <a href="widget/form/TCheckButton.html">TCheckButton</a>("{$this->name}[]");
  136.                 $button->setProperty('checkgroup'$this->name);
  137.                 $button->setIndexValue($key);
  138.  
  139.                 $obj new TLabel($value);
  140.                 $this->buttons[$key$button;
  141.                 $this->labels[$key$obj;
  142.             }
  143.         }
  144.     }
  145.     
  146.     /**
  147.      * Return the items
  148.      */
  149.     public function getItems()
  150.     {
  151.         return $this->items;
  152.     }
  153.     
  154.     /**
  155.      * Return the option buttons
  156.      */
  157.     public function getButtons()
  158.     {
  159.         return $this->buttons;
  160.     }
  161.  
  162.     /**
  163.      * Return the option labels
  164.      */
  165.     public function getLabels()
  166.     {
  167.         return $this->labels;
  168.     }
  169.     
  170.     /**
  171.      * Define the field's separator
  172.      * @param $sep A string containing the field's separator
  173.      */
  174.     public function setValueSeparator($sep)
  175.     {
  176.         $this->separator = $sep;
  177.     }
  178.     
  179.     /**
  180.      * Define the field's value
  181.      * @param $value A string containing the field's value
  182.      */
  183.     public function setValue($value)
  184.     {
  185.         if (empty($this->separator))
  186.         {
  187.             $this->value = $value;
  188.         }
  189.         else
  190.         {
  191.             if ($value)
  192.             {
  193.                 $this->value = explode($this->separator, $value);
  194.             }
  195.         }
  196.     }
  197.     
  198.     /**
  199.      * Return the post data
  200.      */
  201.     public function getPostData()
  202.     {
  203.         if (isset($_POST[$this->name]))
  204.         {
  205.             if (empty($this->separator))
  206.             {
  207.                 return $_POST[$this->name];
  208.             }
  209.             else
  210.             {
  211.                 return implode($this->separator, $_POST[$this->name]);
  212.             }
  213.         }
  214.         else
  215.         {
  216.             return array();
  217.         }
  218.     }
  219.     
  220.     /**
  221.      * Define the action to be executed when the user changes the combo
  222.      * @param $action TAction object
  223.      */
  224.     public function setChangeAction(TAction $action)
  225.     {
  226.         if ($action->isStatic())
  227.         {
  228.             $this->changeAction = $action;
  229.         }
  230.         else
  231.         {
  232.             $string_action = $action->toString();
  233.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2', $string_action, __METHOD__));
  234.         }
  235.     }
  236.     
  237.     /**
  238.      * Set change function
  239.      */
  240.     public function setChangeFunction($function)
  241.     {
  242.         $this->changeFunction = $function;
  243.     }
  244.     
  245.     /**
  246.      * Enable the field
  247.      * @param $form_name Form name
  248.      * @param $field Field name
  249.      */
  250.     public static function enableField($form_name, $field)
  251.     {
  252.         TScript::create( " tcheckgroup_enable_field('{$form_name}', '{$field}'); " );
  253.     }
  254.     
  255.     /**
  256.      * Disable the field
  257.      * @param $form_name Form name
  258.      * @param $field Field name
  259.      */
  260.     public static function disableField($form_name, $field)
  261.     {
  262.         TScript::create( " tcheckgroup_disable_field('{$form_name}', '{$field}'); " );
  263.     }
  264.     
  265.     /**
  266.      * clear the field
  267.      * @param $form_name Form name
  268.      * @param $field Field name
  269.      */
  270.     public static function clearField($form_name, $field)
  271.     {
  272.         TScript::create( " tcheckgroup_clear_field('{$form_name}', '{$field}'); " );
  273.     }
  274.     
  275. /**    
  276.      * Shows the widget at the screen
  277.      */
  278.     public function show()
  279.     {
  280.         if ($this->useButton)
  281.         {
  282.             echo '<div data-toggle="buttons">';
  283.             echo '<div class="btn-group" style="clear:both;float:left">';
  284.         }
  285.         
  286.         if ($this->items)
  287.         {
  288.             // iterate the checkgroup options
  289.             $i = 0;
  290.             foreach ($this->items as $index => $label)
  291.             {
  292.                 $button = $this->buttons[$index];
  293.                 $button->setName($this->name.'[]');
  294.                 $active = FALSE;
  295.                 $id = $button->getId();
  296.                 
  297.                 // verify if the checkbutton is checked
  298.                 if ((@in_array($index, $this->value) AND !(is_null($this->value))) OR $this->allItemsChecked)
  299.                 {
  300.                     $button->setValue($index); // value=indexvalue (checked)
  301.                     $active = TRUE;
  302.                 }
  303.                 
  304.                 // create the label for the button
  305.                 $obj = $this->labels[$index];
  306.                 $obj->{'class'} = $this->labelClass . ($active?'active':'');
  307.                 $obj->setTip($this->tag->title);
  308.                 
  309.                 if ($this->getSize() AND !$obj->getSize())
  310.                 {
  311.                     $obj->setSize($this->getSize());
  312.                 }
  313.                 
  314.                 // check whether the widget is non-editable
  315.                 if (parent::getEditable())
  316.                 {
  317.                     if (isset($this->changeAction))
  318.                     {
  319.                         if (!<a href="widget/form/TForm.html">TForm</a>::getFormByName($this->formName) instanceof <a href="widget/form/TForm.html">TForm</a>)
  320.                         {
  321.                             throw new Exception(<a href="core/AdiantiCoreTranslator.html">AdiantiCoreTranslator</a>::translate('You must pass the ^1 (^2) as a parameter to ^3', __CLASS__, $this->name, 'TForm::setFields()') );
  322.                         }
  323.                         $string_action = $this->changeAction->serialize(FALSE);
  324.                         
  325.                         $button->setProperty('changeaction', "__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$id}', 'callback')");
  326.                         $button->setProperty('onChange', $button->getProperty('changeaction'), FALSE);
  327.                     }
  328.                     
  329.                     if (isset($this->changeFunction))
  330.                     {
  331.                         $button->setProperty('changeaction', $this->changeFunction, FALSE);
  332.                         $button->setProperty('onChange', $this->changeFunction, FALSE);
  333.                     }
  334.                 }
  335.                 else
  336.                 {
  337.                     $button->setEditable(FALSE);
  338.                     $obj->setFontColor('gray');
  339.                 }
  340.                 
  341.                 $obj->add($button);
  342.                 $obj->show();
  343.                 $i ++;
  344.                 
  345.                 if ($this->layout == 'vertical' OR ($this->breakItems == $i))
  346.                 {
  347.                     $i = 0;
  348.                     if ($this->useButton)
  349.                     {
  350.                        echo '</div>';
  351.                        echo '<div class="btn-group" style="clear:both;float:left">';
  352.                     }
  353.                     else
  354.                     {
  355.                         // shows a line break
  356.                         $br = new <a href="widget/base/TElement.html">TElement</a>('br');
  357.                         $br->show();
  358.                     }
  359.                 }
  360.                 echo "\n";
  361.             }
  362.         }
  363.         
  364.         if ($this->useButton)
  365.         {
  366.             echo '</div>';
  367.             echo '</div>';
  368.         }
  369.     }