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 TRadioGroup.php

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