Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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    7.4
  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 $useSwitch;
  41.     protected $value;
  42.     
  43.     /**
  44.      * Class Constructor
  45.      * @param  $name name of the field
  46.      */
  47.     public function __construct($name)
  48.     {
  49.         parent::__construct($name);
  50.         parent::setSize(NULL);
  51.         $this->labelClass = 'tcheckgroup_label ';
  52.         $this->useButton  = FALSE;
  53.         $this->useSwitch  = FALSE;
  54.     }
  55.     
  56.     /**
  57.      * Clone object
  58.      */
  59.     public function __clone()
  60.     {
  61.         if (is_array($this->items))
  62.         {
  63.             $oldbuttons $this->buttons;
  64.             $this->buttons array();
  65.             $this->labels  array();
  66.  
  67.             foreach ($this->items as $key => $value)
  68.             {
  69.                 $button new TCheckButton("{$this->name}[]");
  70.                 $button->setProperty('checkgroup'$this->name);
  71.                 $button->setIndexValue($key);
  72.                 $button->setProperty('onchange'$oldbuttons[$key]->getProperty('onchange'));
  73.                 
  74.                 $obj new TLabel($value);
  75.                 $this->buttons[$key$button;
  76.                 $this->labels[$key$obj;
  77.             }
  78.         }
  79.     }
  80.     
  81.     /**
  82.      * Check all options
  83.      */
  84.     public function checkAll()
  85.     {
  86.         $this->allItemsChecked = TRUE;
  87.     }
  88.     
  89.     /**
  90.      * Define the direction of the options
  91.      * @param $direction String (vertical, horizontal)
  92.      */
  93.     public function setLayout($dir)
  94.     {
  95.         $this->layout = $dir;
  96.     }
  97.     
  98.     /**
  99.      * Get the direction (vertical or horizontal)
  100.      */
  101.     public function getLayout()
  102.     {
  103.         return $this->layout;
  104.     }
  105.     
  106.     /**
  107.      * Define after how much items, it will break
  108.      */
  109.     public function setBreakItems($breakItems)
  110.     {
  111.         $this->breakItems = $breakItems;
  112.     }
  113.     
  114.     /**
  115.      * Show as button
  116.      */
  117.     public function setUseButton()
  118.     {
  119.        $this->labelClass = 'btn btn-default ';
  120.        $this->useButton  = TRUE;
  121.     }
  122.  
  123.     /**
  124.      * Show as switch
  125.      */
  126.     public function setUseSwitch($useSwitch = TRUE, $labelClass = 'blue')
  127.     {
  128.        $this->labelClass = 'tswitch ' . $labelClass . ' ';
  129.        $this->useSwitch  = $useSwitch;
  130.     }
  131.     
  132.     /**
  133.      * Add items to the check group
  134.      * @param $items An indexed array containing the options
  135.      */
  136.     public function addItems($items)
  137.     {
  138.         if (is_array($items))
  139.         {
  140.             $this->items = $items;
  141.             $this->buttons = array();
  142.             $this->labels  = array();
  143.  
  144.             foreach ($items as $key => $value)
  145.             {
  146.                 $button = new <a href="widget/form/TCheckButton.html">TCheckButton</a>("{$this->name}[]");
  147.                 $button->setProperty('checkgroup'$this->name);
  148.                 $button->setIndexValue($key);
  149.  
  150.                 $obj new TLabel($value);
  151.                 $this->buttons[$key$button;
  152.                 $this->labels[$key$obj;
  153.             }
  154.         }
  155.     }
  156.     
  157.     /**
  158.      * Return the items
  159.      */
  160.     public function getItems()
  161.     {
  162.         return $this->items;
  163.     }
  164.     
  165.     /**
  166.      * Return the option buttons
  167.      */
  168.     public function getButtons()
  169.     {
  170.         return $this->buttons;
  171.     }
  172.  
  173.     /**
  174.      * Return the option labels
  175.      */
  176.     public function getLabels()
  177.     {
  178.         return $this->labels;
  179.     }
  180.     
  181.     /**
  182.      * Define the field's separator
  183.      * @param $sep A string containing the field's separator
  184.      */
  185.     public function setValueSeparator($sep)
  186.     {
  187.         $this->separator = $sep;
  188.     }
  189.     
  190.     /**
  191.      * Define the field's value
  192.      * @param $value A string containing the field's value
  193.      */
  194.     public function setValue($value)
  195.     {
  196.         if (empty($this->separator))
  197.         {
  198.             $this->value = $value;
  199.         }
  200.         else
  201.         {
  202.             if ($value)
  203.             {
  204.                 $this->value = explode($this->separator, $value);
  205.             }
  206.             else
  207.             {
  208.                 $this->value = null;
  209.             }
  210.         }
  211.     }
  212.     
  213.     /**
  214.      * Return the post data
  215.      */
  216.     public function getPostData()
  217.     {
  218.         if (isset($_POST[$this->name]))
  219.         {
  220.             if (empty($this->separator))
  221.             {
  222.                 return $_POST[$this->name];
  223.             }
  224.             else
  225.             {
  226.                 return implode($this->separator, $_POST[$this->name]);
  227.             }
  228.         }
  229.         else
  230.         {
  231.             return array();
  232.         }
  233.     }
  234.     
  235.     /**
  236.      * Define the action to be executed when the user changes the combo
  237.      * @param $action TAction object
  238.      */
  239.     public function setChangeAction(TAction $action)
  240.     {
  241.         if ($action->isStatic())
  242.         {
  243.             $this->changeAction = $action;
  244.         }
  245.         else
  246.         {
  247.             $string_action = $action->toString();
  248.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2', $string_action, __METHOD__));
  249.         }
  250.     }
  251.     
  252.     /**
  253.      * Set change function
  254.      */
  255.     public function setChangeFunction($function)
  256.     {
  257.         $this->changeFunction = $function;
  258.     }
  259.     
  260.     /**
  261.      * Reload checkbox items after it is already shown
  262.      * @param $formname form name (used in gtk version)
  263.      * @param $name field name
  264.      * @param $items array with items
  265.      * @param $options array of options [layout, size, breakItems, useButton, valueSeparator, value, changeAction, changeFunction, checkAll]
  266.      */
  267.     public static function reload($formname, $name, $items, $options)
  268.     {
  269.         $field = new self($name);
  270.         $field->addItems($items);
  271.  
  272.         if (! empty($options['layout']))
  273.         {
  274.             $field->setLayout($options['layout']);
  275.         }
  276.  
  277.         if (! empty($options['size']))
  278.         {
  279.             $field->setSize($options['size']);
  280.         }
  281.  
  282.         if (! empty($options['breakItems']))
  283.         {
  284.             $field->setBreakItems($options['breakItems']);
  285.         }
  286.  
  287.         if (! empty($options['useButton']))
  288.         {
  289.             $field->setUseButton($options['useButton']);
  290.         }
  291.  
  292.         if (! empty($options['valueSeparator']))
  293.         {
  294.             $field->setValueSeparator($options['valueSeparator']);
  295.         }
  296.  
  297.         if (! empty($options['value']))
  298.         {
  299.             $field->setValue($options['value']);
  300.         }
  301.  
  302.         if (! empty($options['changeAction']))
  303.         {
  304.             $field->setChangeAction($options['changeAction']);
  305.         }
  306.  
  307.         if (! empty($options['changeFunction']))
  308.         {
  309.             $field->setChangeFunction($options['changeFunction']);
  310.         }
  311.  
  312.         if (! empty($options['checkAll']))
  313.         {
  314.             $field->checkAll($options['checkAll']);
  315.         }
  316.  
  317.         $content = $field->getContents();
  318.  
  319.         <a href="widget/base/TScript.html">TScript</a>::create( " tcheckgroup_reload('{$formname}', '{$name}', `{$content}`); );
  320.     }
  321.  
  322.     /**
  323.      * Enable the field
  324.      * @param $form_name Form name
  325.      * @param $field Field name
  326.      */
  327.     public static function enableField($form_name, $field)
  328.     {
  329.         TScript::create( " tcheckgroup_enable_field('{$form_name}', '{$field}'); );
  330.     }
  331.     
  332.     /**
  333.      * Disable the field
  334.      * @param $form_name Form name
  335.      * @param $field Field name
  336.      */
  337.     public static function disableField($form_name, $field)
  338.     {
  339.         TScript::create( " tcheckgroup_disable_field('{$form_name}', '{$field}'); );
  340.     }
  341.     
  342.     /**
  343.      * clear the field
  344.      * @param $form_name Form name
  345.      * @param $field Field name
  346.      */
  347.     public static function clearField($form_name, $field)
  348.     {
  349.         TScript::create( " tcheckgroup_clear_field('{$form_name}', '{$field}'); );
  350.     }
  351.     
  352.     /**
  353.      * Shows the widget at the screen
  354.      */
  355.     public function show()
  356.     {
  357.         $editable_class = (!parent::getEditable()) ? 'tfield_block_events' : '';
  358.         
  359.         if ($this->useButton)
  360.         {
  361.             echo "<div class=\"toggle-wrapper {$editable_class}\" ".$this->getPropertiesAsString('aria').' data-toggle="buttons">';
  362.             echo '<div class="btn-group" style="clear:both;float:left;display:table">';
  363.         }
  364.         else
  365.         {
  366.             echo "<div class=\"toggle-wrapper {$editable_class}\" ".$this->getPropertiesAsString('aria').' role="group">';
  367.         }
  368.         
  369.         if ($this->items)
  370.         {
  371.             // iterate the checkgroup options
  372.             $i = 0;
  373.             foreach ($this->items as $index => $label)
  374.             {
  375.                 $button = $this->buttons[$index];
  376.                 $button->setName($this->name.'[]');
  377.                 $active = FALSE;
  378.                 $id = $button->getId();
  379.                 
  380.                 // verify if the checkbutton is checked
  381.                 if (!(is_null($this->value)) && (@in_array($index, $this->value)) OR $this->allItemsChecked)
  382.                 {
  383.                     $button->setValue($index); // value=indexvalue (checked)
  384.                     $active = TRUE;
  385.                 }
  386.                 
  387.                 // create the label for the button
  388.                 $obj = $this->labels[$index];
  389.                 $obj->{'class'} = $this->labelClass . ($active?'active':'');
  390.                 $obj->setTip($this->tag->title);
  391.                 
  392.                 if ($this->getSize() AND !$obj->getSize())
  393.                 {
  394.                     $obj->setSize($this->getSize());
  395.                 }
  396.                 
  397.                 // check whether the widget is non-editable
  398.                 if (parent::getEditable())
  399.                 {
  400.                     if (isset($this->changeAction))
  401.                     {
  402.                         if (!<a href="widget/form/TForm.html">TForm</a>::getFormByName($this->formName) instanceof <a href="widget/form/TForm.html">TForm</a>)
  403.                         {
  404.                             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()') );
  405.                         }
  406.                         $string_action = $this->changeAction->serialize(FALSE);
  407.                         
  408.                         $button->setProperty('changeaction', "__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$id}', 'callback')");
  409.                         $button->setProperty('onChange', $button->getProperty('changeaction'), FALSE);
  410.                     }
  411.                     
  412.                     if (isset($this->changeFunction))
  413.                     {
  414.                         $button->setProperty('changeaction', $this->changeFunction, FALSE);
  415.                         $button->setProperty('onChange', $this->changeFunction, FALSE);
  416.                     }
  417.                 }
  418.                 else
  419.                 {
  420.                     $button->setEditable(FALSE);
  421.                     //$obj->setFontColor('gray');
  422.                 }
  423.                 
  424.                 if ($this->useButton)
  425.                 {
  426.                     $obj->add($button);
  427.                     $obj->show();
  428.                 }
  429.                 else
  430.                 {
  431.                     $classButton = 'filled-in';
  432.  
  433.                     if ($this->useSwitch)
  434.                     {
  435.                         $classButton .= ' btn-tswitch';
  436.                     }
  437.  
  438.                     $button->setProperty('class', $classButton);
  439.                     
  440.                     $obj->{'for'} = $button->getId();
  441.  
  442.                     $wrapper = new <a href="widget/base/TElement.html">TElement</a>('div');
  443.                     $wrapper->{'style'} = 'display:inline-flex;align-items:center;';
  444.                     $wrapper->add($button);
  445.                     $wrapper->add($obj);
  446.                     $wrapper->show();
  447.                 }
  448.                 
  449.                 $i ++;
  450.                 
  451.                 if ($this->layout == 'vertical' OR ($this->breakItems == $i))
  452.                 {
  453.                     $i = 0;
  454.                     if ($this->useButton)
  455.                     {
  456.                        echo '</div>';
  457.                        echo '<div class="btn-group" style="clear:both;float:left;display:table">';
  458.                     }
  459.                     else
  460.                     {
  461.                         // shows a line break
  462.                         $br = new <a href="widget/base/TElement.html">TElement</a>('br');
  463.                         $br->show();
  464.                     }
  465.                 }
  466.                 echo "\n";
  467.             }
  468.         }
  469.         
  470.         if ($this->useButton)
  471.         {
  472.             echo '</div>';
  473.             echo '</div>';
  474.         }
  475.         else
  476.         {
  477.             echo '</div>';
  478.         }
  479.         
  480.         if (!empty($this->getAfterElement()))
  481.         {
  482.             $this->getAfterElement()->show();
  483.         }
  484.     }