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

Documentation is available at TCombo.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Control\TAction;
  6. use Adianti\Core\AdiantiCoreTranslator;
  7. use Adianti\Widget\Base\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TField;
  10. use Exception;
  11.  
  12. /**
  13.  * ComboBox Widget
  14.  *
  15.  * @version    7.4
  16.  * @package    widget
  17.  * @subpackage form
  18.  * @author     Pablo Dall'Oglio
  19.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  20.  * @license    http://www.adianti.com.br/framework-license
  21.  */
  22. class TCombo extends TField implements AdiantiWidgetInterface
  23. {
  24.     protected $id;
  25.     protected $items// array containing the combobox options
  26.     protected $formName;
  27.     private   $searchable;
  28.     private   $changeAction;
  29.     private   $defaultOption;
  30.     protected $changeFunction;
  31.     protected $is_boolean;
  32.  
  33.     /**
  34.      * Class Constructor
  35.      * @param  $name widget's name
  36.      */
  37.     public function __construct($name)
  38.     {
  39.         // executes the parent class constructor
  40.         parent::__construct($name);
  41.         
  42.         $this->id = 'tcombo_'.mt_rand(10000000001999999999);
  43.         $this->defaultOption '';
  44.  
  45.         // creates a <select> tag
  46.         $this->tag = new TElement('select');
  47.         $this->tag->{'class'}  'tcombo'// CSS
  48.         $this->tag->{'widget''tcombo';
  49.         $this->is_boolean FALSE;
  50.     }
  51.     
  52.     /**
  53.      * Enable/disable boolean mode
  54.      */
  55.     public function setBooleanMode()
  56.     {
  57.         $this->is_boolean true;
  58.         $this->addItems'1' => AdiantiCoreTranslator::translate('Yes'),
  59.                            '2' => AdiantiCoreTranslator::translate('No');
  60.                            
  61.         // if setValue() was called previously
  62.         if ($this->value === true)
  63.         {
  64.             $this->value '1';
  65.         }
  66.         else if ($this->value === false)
  67.         {
  68.             $this->value '2';
  69.         }
  70.     }
  71.     
  72.     /**
  73.      * Define the field's value
  74.      * @param $value A string containing the field's value
  75.      */
  76.     public function setValue($value)
  77.     {
  78.         if ($this->is_boolean)
  79.         {
  80.             $this->value $value '1' '2';
  81.         }
  82.         else
  83.         {
  84.             parent::setValue($value);
  85.         }
  86.     }
  87.     
  88.     /**
  89.      * Returns the field's value
  90.      */
  91.     public function getValue()
  92.     {
  93.         if ($this->is_boolean)
  94.         {
  95.             return $this->value == '1' true false;
  96.         }
  97.         else
  98.         {
  99.             return parent::getValue();
  100.         }
  101.     }
  102.     
  103.     /**
  104.      * Clear combo
  105.      */
  106.     public function clear()
  107.     {
  108.         $this->items array();
  109.     }
  110.     
  111.     /**
  112.      * Add items to the combo box
  113.      * @param $items An indexed array containing the combo options
  114.      */
  115.     public function addItems($items)
  116.     {
  117.         if (is_array($items))
  118.         {
  119.             $this->items $items;
  120.         }
  121.     }
  122.     
  123.     /**
  124.      * Return the combo items
  125.      */
  126.     public function getItems()
  127.     {
  128.         return $this->items;
  129.     }
  130.     
  131.     /**
  132.      * Enable search
  133.      */
  134.     public function enableSearch()
  135.     {
  136.         unset($this->tag->{'class'});
  137.         $this->searchable true;
  138.     }
  139.     
  140.     /**
  141.      * Return the post data
  142.      */
  143.     public function getPostData()
  144.     {
  145.         $name str_replace(['[',']']['','']$this->name);
  146.         
  147.         if (isset($_POST[$name]))
  148.         {
  149.             $val $_POST[$name];
  150.             
  151.             if ($val == ''// empty option
  152.             {
  153.                 return '';
  154.             }
  155.             else
  156.             {
  157.                 if (is_string($valand strpos($val'::'))
  158.                 {
  159.                     $tmp explode('::'$val);
  160.                     return trim($tmp[0]);
  161.                 }
  162.                 else
  163.                 {
  164.                     if ($this->is_boolean)
  165.                     {
  166.                         return $val == '1' true false;
  167.                     }
  168.                     else
  169.                     {
  170.                         return $val;
  171.                     }
  172.                 }
  173.             }
  174.         }
  175.         else
  176.         {
  177.             return '';
  178.         }
  179.     }
  180.     
  181.     /**
  182.      * Define the action to be executed when the user changes the combo
  183.      * @param $action TAction object
  184.      */
  185.     public function setChangeAction(TAction $action)
  186.     {
  187.         if ($action->isStatic())
  188.         {
  189.             $this->changeAction $action;
  190.         }
  191.         else
  192.         {
  193.             $string_action $action->toString();
  194.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  195.         }
  196.     }
  197.     
  198.     /**
  199.      * Set change function
  200.      */
  201.     public function setChangeFunction($function)
  202.     {
  203.         $this->changeFunction $function;
  204.     }
  205.     
  206.     /**
  207.      * Reload combobox items after it is already shown
  208.      * @param $formname form name (used in gtk version)
  209.      * @param $name field name
  210.      * @param $items array with items
  211.      * @param $startEmpty if the combo will have an empty first item
  212.      * @param $fire_events If change action will be fired
  213.      */
  214.     public static function reload($formname$name$items$startEmpty FALSE$fire_events TRUE)
  215.     {
  216.         $fire_param $fire_events 'true' 'false';
  217.         $code "tcombo_clear('{$formname}', '{$name}', $fire_param); ";
  218.         if ($startEmpty)
  219.         {
  220.             $code .= "tcombo_add_option('{$formname}', '{$name}', '', ''); ";
  221.         }
  222.         
  223.         if ($items)
  224.         {
  225.             foreach ($items as $key => $value)
  226.             {
  227.                 if (substr($key03== '>>>')
  228.                 {
  229.                     $code .= "tcombo_create_opt_group('{$formname}', '{$name}', '{$value}'); ";
  230.                 }
  231.                 else
  232.                 {
  233.                     // se exitsir optgroup add nele
  234.                     $value addslashes($value);
  235.                     $code .= "tcombo_add_option('{$formname}', '{$name}', '{$key}', '{$value}'); ";
  236.                 }
  237.             }
  238.         }
  239.         TScript::create($code);
  240.     }
  241.     
  242.     /**
  243.      * Enable the field
  244.      * @param $form_name Form name
  245.      * @param $field Field name
  246.      */
  247.     public static function enableField($form_name$field)
  248.     {
  249.         TScript::create" tcombo_enable_field('{$form_name}', '{$field}'); );
  250.     }
  251.     
  252.     /**
  253.      * Disable the field
  254.      * @param $form_name Form name
  255.      * @param $field Field name
  256.      */
  257.     public static function disableField($form_name$field)
  258.     {
  259.         TScript::create" tcombo_disable_field('{$form_name}', '{$field}'); );
  260.     }
  261.     
  262.     /**
  263.      * Clear the field
  264.      * @param $form_name   Form name
  265.      * @param $field       Field name
  266.      * @param $fire_events If change action will be fired
  267.      */
  268.     public static function clearField($form_name$field$fire_events TRUE)
  269.     {
  270.         $fire_param $fire_events 'true' 'false';
  271.         TScript::create" tcombo_clear('{$form_name}', '{$field}', $fire_param); );
  272.     }
  273.     
  274.     /**
  275.      * Define the combo default option value
  276.      * @param $option option value
  277.      */
  278.     public function setDefaultOption($option)
  279.     {
  280.         $this->defaultOption $option;
  281.     }
  282.     
  283.     /**
  284.      * Render items
  285.      */
  286.     public function renderItems()
  287.     {
  288.         if ($this->defaultOption !== FALSE)
  289.         {
  290.             // creates an empty <option> tag
  291.             $option new TElement('option');
  292.             
  293.             $option->add$this->defaultOption );
  294.             $option->{'value''';   // tag value
  295.  
  296.             // add the option tag to the combo
  297.             $this->tag->add($option);
  298.         }
  299.                     
  300.         if ($this->items)
  301.         {
  302.             // iterate the combobox items
  303.             foreach ($this->items as $chave => $item)
  304.             {
  305.                 if (substr($chave03== '>>>')
  306.                 {
  307.                     $optgroup new TElement('optgroup');
  308.                     $optgroup->{'label'$item;
  309.                     
  310.                     // add the option to the combo
  311.                     $this->tag->add($optgroup);
  312.                 }
  313.                 else
  314.                 {
  315.                     // creates an <option> tag
  316.                     $option new TElement('option');
  317.                     $option->{'value'$chave;  // define the index
  318.                     $option->add(htmlspecialchars($item))// add the item label
  319.                     
  320.                     if (substr($chave03== '###')
  321.                     {
  322.                         $option->{'disabled''1';
  323.                         $option->{'class''disabled';
  324.                     }
  325.                     
  326.                     // verify if this option is selected
  327.                     if (($chave == $this->valueAND !(is_null($this->value)) AND strlen((string) $this->value0)
  328.                     {
  329.                         // mark as selected
  330.                         $option->{'selected'1;
  331.                     }
  332.                     
  333.                     if (isset($optgroup))
  334.                     {
  335.                         $optgroup->add($option);
  336.                     }
  337.                     else
  338.                     {
  339.                         $this->tag->add($option);
  340.                     }                    
  341.                 }
  342.             }
  343.         }
  344.     }
  345.     
  346.     /**
  347.      * Shows the widget
  348.      */
  349.     public function show()
  350.     {
  351.         // define the tag properties
  352.         $this->tag->{'name'}  $this->name;    // tag name
  353.         
  354.         if ($this->id and empty($this->tag->{'id'}))
  355.         {
  356.             $this->tag->{'id'$this->id;
  357.         }
  358.         
  359.         if (!empty($this->size))
  360.         {
  361.             if (strstr((string) $this->size'%'!== FALSE)
  362.             {
  363.                 $this->setProperty('style'"width:{$this->size};"false)//aggregate style info
  364.             }
  365.             else
  366.             {
  367.                 $this->setProperty('style'"width:{$this->size}px;"false)//aggregate style info
  368.             }
  369.         }
  370.         
  371.         if (isset($this->changeAction))
  372.         {
  373.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  374.             {
  375.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  376.             }
  377.             
  378.             $string_action $this->changeAction->serialize(FALSE);
  379.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  380.             $this->setProperty('onChange'$this->getProperty('changeaction'));
  381.         }
  382.         
  383.         if (isset($this->changeFunction))
  384.         {
  385.             $this->setProperty('changeaction'$this->changeFunctionFALSE);
  386.             $this->setProperty('onChange'$this->changeFunctionFALSE);
  387.         }
  388.         
  389.         // verify whether the widget is editable
  390.         if (!parent::getEditable())
  391.         {
  392.             // make the widget read-only
  393.             $this->tag->{'onclick'}  "return false;";
  394.             $this->tag->{'style'}   .= ';pointer-events:none';
  395.             $this->tag->{'tabindex''-1';
  396.             $this->tag->{'class'}    'tcombo tcombo_disabled'// CSS
  397.         }
  398.         
  399.         if ($this->searchable)
  400.         {
  401.             $this->tag->{'role''tcombosearch';
  402.         }
  403.         
  404.         // shows the combobox
  405.         $this->renderItems();
  406.         $this->tag->show();
  407.         
  408.         if ($this->searchable)
  409.         {
  410.             $select AdiantiCoreTranslator::translate('Select');
  411.             TScript::create("tcombo_enable_search('#{$this->id}', '{$select}')");
  412.             
  413.             if (!parent::getEditable())
  414.             {
  415.                 TScript::create(" tmultisearch_disable_field( '{$this->formName}', '{$this->name}', '{$this->tag->{'title'}}'); ");
  416.             }
  417.         }
  418.     }
  419. }