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

Documentation is available at TMultiSearch.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Core\AdiantiApplicationConfig;
  6. use Adianti\Control\TPage;
  7. use Adianti\Control\TAction;
  8. use Adianti\Core\AdiantiCoreTranslator;
  9. use Adianti\Widget\Base\TElement;
  10. use Adianti\Widget\Base\TScript;
  11. use Adianti\Widget\Form\TField;
  12.  
  13. use Exception;
  14.  
  15. /**
  16.  * Multi Search Widget
  17.  *
  18.  * @version    7.4
  19.  * @package    widget
  20.  * @subpackage form
  21.  * @author     Matheus Agnes Dias
  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 TMultiSearch extends TSelect implements AdiantiWidgetInterface
  27. {
  28.     protected $id;
  29.     protected $items;
  30.     protected $size;
  31.     protected $height;
  32.     protected $minLength;
  33.     protected $maxSize;
  34.     protected $editable;
  35.     protected $changeAction;
  36.     protected $changeFunction;
  37.     protected $allowClear;
  38.     protected $allowSearch;
  39.     protected $separator;
  40.     protected $value;
  41.     
  42.     /**
  43.      * Class Constructor
  44.      * @param  $name Widget's name
  45.      */
  46.     public function __construct($name)
  47.     {
  48.         // executes the parent class constructor
  49.         parent::__construct($name);
  50.         $this->id   = 'tmultisearch_'.mt_rand(10000000001999999999);
  51.  
  52.         $this->height = 100;
  53.         $this->minLength = 3;
  54.         $this->maxSize = 0;
  55.         $this->allowClear = TRUE;
  56.         $this->allowSearch = TRUE;
  57.         
  58.         parent::setDefaultOption(FALSE);
  59.         $this->tag->{'component''multisearch';
  60.         $this->tag->{'widget''tmultisearch';
  61.     }
  62.     
  63.     /**
  64.      * Disable multiple selection
  65.      */
  66.     public function disableMultiple()
  67.     {
  68.         unset($this->tag->{'multiple'});
  69.     }
  70.     
  71.     /**
  72.      * Disable clear
  73.      */
  74.     public function disableClear()
  75.     {
  76.         $this->allowClear FALSE;
  77.     }
  78.     
  79.     /**
  80.      * Disable search
  81.      */
  82.     public function disableSearch()
  83.     {
  84.         $this->allowSearch FALSE;
  85.     }
  86.     
  87.     /**
  88.      * Define the widget's size
  89.      * @param  $width   Widget's width
  90.      * @param  $height  Widget's height
  91.      */
  92.     public function setSize($width$height NULL)
  93.     {
  94.         $this->size   $width;
  95.         if ($height)
  96.         {
  97.             $this->height $height;
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Returns the size
  103.      * @return array(width, height)
  104.      */
  105.     public function getSize()
  106.     {
  107.         return array$this->size$this->height );
  108.     }
  109.     
  110.     /**
  111.      * Define the minimum length for search
  112.      */
  113.     public function setMinLength($length)
  114.     {
  115.         $this->minLength $length;
  116.     }
  117.  
  118.     /**
  119.      * Define the maximum number of items that can be selected
  120.      */
  121.     public function setMaxSize($maxsize)
  122.     {
  123.         $this->maxSize $maxsize;
  124.         
  125.         if ($maxsize == 1)
  126.         {
  127.             unset($this->height);
  128.             parent::setDefaultOption(TRUE);
  129.         }
  130.     }
  131.     
  132.     /**
  133.      * Define the field's separator
  134.      * @param $sep A string containing the field's separator
  135.      */
  136.     public function setValueSeparator($sep)
  137.     {
  138.         $this->separator $sep;
  139.     }
  140.     
  141.     /**
  142.      * Define the field's value
  143.      * @param $value A string containing the field's value
  144.      */
  145.     public function setValue($value)
  146.     {
  147.         $ini AdiantiApplicationConfig::get();
  148.         
  149.         if (isset($ini['general']['compat']AND $ini['general']['compat'==  '4')
  150.         {
  151.             if ($value)
  152.             {
  153.                 parent::setValue(array_keys((array)$value));
  154.             }
  155.         }
  156.         else
  157.         {
  158.             parent::setValue($value);
  159.         }
  160.     }
  161.     
  162.     /**
  163.      * Return the post data
  164.      */
  165.     public function getPostData()
  166.     {
  167.         $ini AdiantiApplicationConfig::get();
  168.         
  169.         if (isset($_POST[$this->name]))
  170.         {
  171.             $values $_POST[$this->name];
  172.             
  173.             if (isset($ini['general']['compat']AND $ini['general']['compat'==  '4')
  174.             {
  175.                 $return [];
  176.                 if (is_array($values))
  177.                 {
  178.                     foreach ($values as $item)
  179.                     {
  180.                         $return[$item$this->items[$item];
  181.                     }
  182.                 }
  183.                 return $return;
  184.             }
  185.             else
  186.             {
  187.                 if (empty($this->separator))
  188.                 {
  189.                     return $values;
  190.                 }
  191.                 else
  192.                 {
  193.                     return implode($this->separator$values);
  194.                 }
  195.             }
  196.         }
  197.         else
  198.         {
  199.             return '';
  200.         }
  201.     }
  202.     
  203.     /**
  204.      * Enable the field
  205.      * @param $form_name Form name
  206.      * @param $field Field name
  207.      */
  208.     public static function enableField($form_name$field)
  209.     {
  210.         TScript::create" tmultisearch_enable_field('{$form_name}', '{$field}'); );
  211.     }
  212.     
  213.     /**
  214.      * Disable the field
  215.      * @param $form_name Form name
  216.      * @param $field Field name
  217.      */
  218.     public static function disableField($form_name$field)
  219.     {
  220.         TScript::create" tmultisearch_disable_field('{$form_name}', '{$field}'); );
  221.     }
  222.  
  223.     /**
  224.      * Clear the field
  225.      * @param $form_name Form name
  226.      * @param $field Field name
  227.      */
  228.     public static function clearField($form_name$field)
  229.     {
  230.         TScript::create" tmultisearch_clear_field('{$form_name}', '{$field}'); );
  231.     }
  232.     
  233.     /**
  234.      * Shows the widget
  235.      */
  236.     public function show()
  237.     {
  238.         // define the tag properties
  239.         $this->tag->{'id'}    $this->id// tag id
  240.         
  241.         if (empty($this->tag->{'name'})) // may be defined by child classes
  242.         {
  243.             $this->tag->{'name'}  $this->name.'[]'// tag name
  244.         }
  245.         
  246.         if (strstr(string) $this->size'%'!== FALSE)
  247.         {
  248.             $this->setProperty('style'"width:{$this->size};"false)//aggregate style info
  249.             $size  "{$this->size}";
  250.         }
  251.         else
  252.         {
  253.             $this->setProperty('style'"width:{$this->size}px;"false)//aggregate style info
  254.             $size  "{$this->size}px";
  255.         }
  256.         
  257.         $multiple $this->maxSize == 'false' 'true';
  258.         $search_word !empty($this->getProperty('placeholder'))$this->getProperty('placeholder'AdiantiCoreTranslator::translate('Search');
  259.         $change_action 'function() {}';
  260.         $allowclear  $this->allowClear  'true' 'false';
  261.         $allowsearch $this->allowSearch '1' 'Infinity';
  262.         $with_titles $this->withTitles 'true' 'false';
  263.  
  264.         if (isset($this->changeAction))
  265.         {
  266.             if (!TForm::getFormByName($this->formNameinstanceof TForm)
  267.             {
  268.                 throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  269.             }
  270.             
  271.             $string_action $this->changeAction->serialize(FALSE);
  272.             $change_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback'); }";
  273.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  274.         }
  275.         else if (isset($this->changeFunction))
  276.         {
  277.             $change_action "function() { $this->changeFunction }";
  278.             $this->setProperty('changeaction'$this->changeFunctionFALSE);
  279.         }
  280.         
  281.         // shows the component
  282.         parent::renderItemsfalse );
  283.         $this->tag->show();
  284.         
  285.         TScript::create(" tmultisearch_start( '{$this->id}', '{$this->minLength}', '{$this->maxSize}', '{$search_word}', $multiple, '{$size}', '{$this->height}px', {$allowclear}, {$allowsearch}$change_action, {$with_titles} ); ");
  286.         
  287.         if (!$this->editable)
  288.         {
  289.             TScript::create(" tmultisearch_disable_field( '{$this->formName}', '{$this->name}'); ");
  290.         }
  291.     }
  292. }