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

Documentation is available at TSelect.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\TField;
  11.  
  12. use Exception;
  13.  
  14. /**
  15.  * Select Widget
  16.  *
  17.  * @version    7.4
  18.  * @package    widget
  19.  * @subpackage form
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. class TSelect extends TField implements AdiantiWidgetInterface
  25. {
  26.     protected $id;
  27.     protected $height;
  28.     protected $items// array containing the combobox options
  29.     protected $formName;
  30.     protected $changeFunction;
  31.     protected $changeAction;
  32.     protected $defaultOption;
  33.     protected $separator;
  34.     protected $value;
  35.     protected $withTitles;
  36.     
  37.     /**
  38.      * Class Constructor
  39.      * @param  $name widget's name
  40.      */
  41.     public function __construct($name)
  42.     {
  43.         // executes the parent class constructor
  44.         parent::__construct($name);
  45.         $this->id   = 'tselect_' mt_rand(10000000001999999999);
  46.         $this->defaultOption = '';
  47.         $this->withTitles = true;
  48.         
  49.         // creates a <select> tag
  50.         $this->tag = new TElement('select');
  51.         $this->tag->{'class''tselect'// CSS
  52.         $this->tag->{'multiple''1';
  53.         $this->tag->{'widget''tselect';
  54.     }
  55.     
  56.     
  57.     /**
  58.      * Disable multiple selection
  59.      */
  60.     public function disableMultiple()
  61.     {
  62.         unset($this->tag->{'multiple'});
  63.         $this->tag->{'size'3;
  64.     }
  65.  
  66.     /**
  67.      * Disable option titles
  68.      */
  69.     public function disableTitles()
  70.     {
  71.         $this->withTitles false;
  72.     }
  73.     
  74.     public function setDefaultOption($option)
  75.     {
  76.         $this->defaultOption $option;
  77.     }
  78.     
  79.     /**
  80.      * Add items to the select
  81.      * @param $items An indexed array containing the combo options
  82.      */
  83.     public function addItems($items)
  84.     {
  85.         if (is_array($items))
  86.         {
  87.             $this->items $items;
  88.         }
  89.     }
  90.     
  91.     /**
  92.      * Return the items
  93.      */
  94.     public function getItems()
  95.     {
  96.         return $this->items;
  97.     }
  98.     
  99.     /**
  100.      * Define the Field's width
  101.      * @param $width Field's width in pixels
  102.      * @param $height Field's height in pixels
  103.      */
  104.     public function setSize($width$height NULL)
  105.     {
  106.         $this->size $width;
  107.         $this->height $height;
  108.     }
  109.     
  110.     /**
  111.      * Returns the size
  112.      * @return array(width, height)
  113.      */
  114.     public function getSize()
  115.     {
  116.         return array$this->size$this->height );
  117.     }
  118.     
  119.     /**
  120.      * Define the field's separator
  121.      * @param $sep A string containing the field's separator
  122.      */
  123.     public function setValueSeparator($sep)
  124.     {
  125.         $this->separator $sep;
  126.     }
  127.     
  128.     /**
  129.      * Define the field's value
  130.      * @param $value A string containing the field's value
  131.      */
  132.     public function setValue($value)
  133.     {
  134.         if (empty($this->separator))
  135.         {
  136.             $this->value $value;
  137.         }
  138.         else
  139.         {
  140.             if ($value)
  141.             {
  142.                 $this->value explode($this->separator$value);
  143.             }
  144.             else
  145.             {
  146.                 $this->value null;
  147.             }
  148.         }
  149.     }
  150.     
  151.     /**
  152.      * Return the post data
  153.      */
  154.     public function getPostData()
  155.     {
  156.         if (isset($_POST[$this->name]))
  157.         {
  158.             if ($this->tag->{'multiple'})
  159.             {
  160.                 if (empty($this->separator))
  161.                 {
  162.                     return $_POST[$this->name];
  163.                 }
  164.                 else
  165.                 {
  166.                     return implode($this->separator$_POST[$this->name]);
  167.                 }
  168.             }
  169.             else
  170.             {
  171.                 return $_POST[$this->name][0];
  172.             }
  173.         }
  174.         else
  175.         {
  176.             return array();
  177.         }
  178.     }
  179.     
  180.     /**
  181.      * Define the action to be executed when the user changes the combo
  182.      * @param $action TAction object
  183.      */
  184.     public function setChangeAction(TAction $action)
  185.     {
  186.         if ($action->isStatic())
  187.         {
  188.             $this->changeAction $action;
  189.         }
  190.         else
  191.         {
  192.             $string_action $action->toString();
  193.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  194.         }
  195.     }
  196.     
  197.     /**
  198.      * Set change function
  199.      */
  200.     public function setChangeFunction($function)
  201.     {
  202.         $this->changeFunction $function;
  203.     }
  204.     
  205.     /**
  206.      * Reload combobox items after it is already shown
  207.      * @param $formname form name (used in gtk version)
  208.      * @param $name field name
  209.      * @param $items array with items
  210.      * @param $startEmpty ...
  211.      */
  212.     public static function reload($formname$name$items$startEmpty FALSE)
  213.     {
  214.         $code "tselect_clear('{$formname}', '{$name}'); ";
  215.         if ($startEmpty)
  216.         {
  217.             $code .= "tselect_add_option('{$formname}', '{$name}', '', ''); ";
  218.         }
  219.         
  220.         if ($items)
  221.         {
  222.             foreach ($items as $key => $value)
  223.             {
  224.                 $code .= "tselect_add_option('{$formname}', '{$name}', '{$key}', '{$value}'); ";
  225.             }
  226.         }
  227.         TScript::create($code);
  228.     }
  229.     
  230.     /**
  231.      * Enable the field
  232.      * @param $form_name Form name
  233.      * @param $field Field name
  234.      */
  235.     public static function enableField($form_name$field)
  236.     {
  237.         TScript::create" tselect_enable_field('{$form_name}', '{$field}'); );
  238.     }
  239.     
  240.     /**
  241.      * Disable the field
  242.      * @param $form_name Form name
  243.      * @param $field Field name
  244.      */
  245.     public static function disableField($form_name$field)
  246.     {
  247.         TScript::create" tselect_disable_field('{$form_name}', '{$field}'); );
  248.     }
  249.     
  250.     /**
  251.      * Clear the field
  252.      * @param $form_name Form name
  253.      * @param $field Field name
  254.      */
  255.     public static function clearField($form_name$field)
  256.     {
  257.         TScript::create" tselect_clear_field('{$form_name}', '{$field}'); );
  258.     }
  259.     
  260.     /**
  261.      * Render items
  262.      */
  263.     protected function renderItems$with_titles true )
  264.     {
  265.         if ($this->defaultOption !== FALSE)
  266.         {
  267.             // creates an empty <option> tag
  268.             $option new TElement('option');
  269.             
  270.             $option->add$this->defaultOption );
  271.             $option->{'value''';   // tag value
  272.  
  273.             // add the option tag to the combo
  274.             $this->tag->add($option);
  275.         }
  276.         
  277.         if ($this->items)
  278.         {
  279.             // iterate the combobox items
  280.             foreach ($this->items as $chave => $item)
  281.             {
  282.                 if (substr($chave03== '>>>')
  283.                 {
  284.                     $optgroup new TElement('optgroup');
  285.                     $optgroup->{'label'$item;
  286.                     // add the option to the combo
  287.                     $this->tag->add($optgroup);
  288.                 }
  289.                 else
  290.                 {
  291.                     // creates an <option> tag
  292.                     $option new TElement('option');
  293.                     $option->{'value'$chave;  // define the index
  294.                     if ($with_titles)
  295.                     {
  296.                         $option->{'title'$item;  // define the title
  297.                     }
  298.                     $option->{'titside''left';  // define the title side
  299.                     $option->add(htmlspecialchars($item));      // add the item label
  300.                     
  301.                     // verify if this option is selected
  302.                     if ( (is_array($this->value)  AND @in_array($chave$this->value)) OR
  303.                          (is_scalar($this->valueAND strlen(string) $this->value AND @in_array($chave(array) $this->value)))
  304.                     {
  305.                         // mark as selected
  306.                         $option->{'selected'1;
  307.                     }
  308.                     
  309.                     if (isset($optgroup))
  310.                     {
  311.                         $optgroup->add($option);
  312.                     }
  313.                     else
  314.                     {
  315.                         $this->tag->add($option);
  316.                     }                    
  317.                 }
  318.             }
  319.         }
  320.     }
  321.     
  322.     /**
  323.      * Shows the widget
  324.      */
  325.     public function show()
  326.     {
  327.         // define the tag properties
  328.         $this->tag->{'name'}  $this->name.'[]';    // tag name
  329.         $this->tag->{'id'}    $this->id;
  330.         
  331.         $this->setProperty('style'(strstr((string) $this->size'%'!== FALSE)   "width:{$this->size}"    "width:{$this->size}px",   false)//aggregate style info
  332.         
  333.         if (!empty($this->height))
  334.         {
  335.             $this->setProperty('style'(strstr($this->height'%'!== FALSE"height:{$this->height}"height:{$this->height}px"false)//aggregate style info
  336.         }
  337.         
  338.         // verify whether the widget is editable
  339.         if (parent::getEditable())
  340.         {
  341.             if (isset($this->changeAction))
  342.             {
  343.                 if (!TForm::getFormByName($this->formNameinstanceof TForm)
  344.                 {
  345.                     throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  346.                 }
  347.                 
  348.                 $string_action $this->changeAction->serialize(FALSE);
  349.                 $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  350.                 $this->setProperty('onChange'$this->getProperty('changeaction'));
  351.             }
  352.             
  353.             if (isset($this->changeFunction))
  354.             {
  355.                 $this->setProperty('changeaction'$this->changeFunctionFALSE);
  356.                 $this->setProperty('onChange'$this->changeFunctionFALSE);
  357.             }
  358.         }
  359.         else
  360.         {
  361.             // make the widget read-only
  362.             $this->tag->{'onclick'"return false;";
  363.             $this->tag->{'style'}  .= ';pointer-events:none';
  364.             $this->tag->{'class'}   'tselect_disabled'// CSS
  365.         }
  366.         
  367.         // shows the widget
  368.         $this->renderItems$this->withTitles );
  369.         $this->tag->show();
  370.     }
  371. }