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

Documentation is available at TDBMultiSearch.php

  1. <?php
  2. namespace Adianti\Widget\Wrapper;
  3.  
  4. use Adianti\Core\AdiantiApplicationConfig;
  5. use Adianti\Core\AdiantiCoreApplication;
  6. use Adianti\Core\AdiantiCoreTranslator;
  7. use Adianti\Widget\Base\TElement;
  8. use Adianti\Widget\Base\TScript;
  9. use Adianti\Widget\Form\TMultiSearch;
  10. use Adianti\Database\TTransaction;
  11. use Adianti\Database\TCriteria;
  12. use Adianti\Widget\Form\TForm;
  13.  
  14. use Exception;
  15.  
  16. /**
  17.  * Database Multisearch Widget
  18.  *
  19.  * @version    7.4
  20.  * @package    widget
  21.  * @subpackage wrapper
  22.  * @author     Pablo Dall'Oglio
  23.  * @author     Matheus Agnes Dias
  24.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  25.  * @license    http://www.adianti.com.br/framework-license
  26.  */
  27. class TDBMultiSearch extends TMultiSearch
  28. {
  29.     protected $id;
  30.     protected $initialItems;
  31.     protected $items;
  32.     protected $size;
  33.     protected $height;
  34.     protected $minLength;
  35.     protected $maxSize;
  36.     protected $database;
  37.     protected $model;
  38.     protected $key;
  39.     protected $column;
  40.     protected $operator;
  41.     protected $orderColumn;
  42.     protected $criteria;
  43.     protected $mask;
  44.     protected $service;
  45.     protected $seed;
  46.     protected $editable;
  47.     protected $changeFunction;
  48.     protected $idSearch;
  49.     protected $idTextSearch;
  50.     
  51.     /**
  52.      * Class Constructor
  53.      * @param  $name     widget's name
  54.      * @param  $database database name
  55.      * @param  $model    model class name
  56.      * @param  $key      table field to be used as key in the combo
  57.      * @param  $value    table field to be listed in the combo
  58.      * @param  $ordercolumn column to order the fields (optional)
  59.      * @param  $criteria criteria (TCriteria object) to filter the model (optional)
  60.      */
  61.     public function __construct($name$database$model$key$value$orderColumn NULLTCriteria $criteria NULL)
  62.     {
  63.         // executes the parent class constructor
  64.         parent::__construct($name);
  65.         $this->id   = 'tdbmultisearch_'.mt_rand(10000000001999999999);
  66.         
  67.         $key   trim($key);
  68.         $value trim($value);
  69.         
  70.         if (empty($database))
  71.         {
  72.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''database'__CLASS__));
  73.         }
  74.         
  75.         if (empty($model))
  76.         {
  77.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''model'__CLASS__));
  78.         }
  79.         
  80.         if (empty($key))
  81.         {
  82.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''key'__CLASS__));
  83.         }
  84.         
  85.         if (empty($value))
  86.         {
  87.             throw new Exception(AdiantiCoreTranslator::translate('The parameter (^1) of ^2 is required''value'__CLASS__));
  88.         }
  89.         
  90.         $ini AdiantiApplicationConfig::get();
  91.         
  92.         $this->database = $database;
  93.         $this->model = $model;
  94.         $this->key = $key;
  95.         $this->column = $value;
  96.         $this->operator = null;
  97.         $this->orderColumn = isset($orderColumn$orderColumn NULL;
  98.         $this->criteria = $criteria;
  99.         
  100.         if (strpos($value,','!== false)
  101.         {
  102.             $columns explode(','$value);
  103.             $this->mask = '{'.$columns[0].'}';
  104.         }
  105.         else
  106.         {
  107.             $this->mask = '{'.$value.'}';
  108.         }
  109.         
  110.         $this->service = 'AdiantiMultiSearchService';
  111.         $this->seed = APPLICATION_NAME !empty($ini['general']['seed']$ini['general']['seed''s8dkld83kf73kf094' );
  112.         $this->tag->{'widget''tdbmultisearch';
  113.         $this->idSearch = true;
  114.         $this->idTextSearch = false;
  115.         
  116.         if ((defined("{$model}::IDPOLICY")) AND (constant("{$model}::IDPOLICY") == 'uuid'))
  117.         {
  118.             $this-><a href="widget/wrapper/TDBMultiSearch.html#var$idTextSearch">idTextSearch</a> = true;
  119.         }
  120.     }
  121.     
  122. /**    
  123.      * Define the search service
  124.      * @param $service Search service
  125.      */
  126.     public function setService($service)
  127.     {
  128.         $this-><a href="widget/wrapper/TDBMultiSearch.html#var$service">service</a> = $service;
  129.     }
  130.     
  131. /**    
  132.      * Disable search by id
  133.      */
  134.     public function disableIdSearch()
  135.     {
  136.         $this-><a href="widget/wrapper/TDBMultiSearch.html#var$idSearch">idSearch</a> = false;
  137.     }
  138.     
  139. /**    
  140.      * Enable Id textual search
  141.      */
  142.     public function enableIdTextualSearch()
  143.     {
  144.         $this-><a href="widget/wrapper/TDBMultiSearch.html#var$idTextSearch">idTextSearch</a> = true;
  145.     }
  146.     
  147. /**    
  148.      * Define the search operator
  149.      * @param $operator Search operator
  150.      */
  151.     public function setOperator($operator)
  152.     {
  153.         $this-><a href="widget/wrapper/TDBMultiSearch.html#var$operator">operator</a> = $operator;
  154.     }
  155.     
  156. /**    
  157.      * Define the display mask
  158.      * @param $mask Show mask
  159.      */
  160.     public function setMask($mask)
  161.     {
  162.         $this-><a href="widget/wrapper/TDBMultiSearch.html#var$mask">mask</a> = $mask;
  163.     }
  164.     
  165. /**    
  166.      * Define the field's value
  167.      * @param $values An array the field's values
  168.      */
  169.     public function setValue($values)
  170.     {
  171.         $original_values = $values;
  172.         $ini = <a href="core/AdiantiApplicationConfig.html">AdiantiApplicationConfig</a>::get();
  173.         
  174.         if (isset($ini['general']['compat']) AND $ini['general']['compat'] ==  '4')
  175.         {
  176.             if ($values)
  177.             {
  178.                 parent::setValue( $values );
  179.                 parent::addItems( $values );
  180.             }
  181.         }
  182.         else
  183.         {
  184.             $items = [];
  185.             if ($values)
  186.             {
  187.                 if (!empty($this-><a href="widget/form/TMultiSearch.html#var$separator">separator</a>))
  188.                 {
  189.                     $values = explode($this-><a href="widget/form/TMultiSearch.html#var$separator">separator</a>, $values);
  190.                 }
  191.                 
  192.                 <a href="database/TTransaction.html">TTransaction</a>::openFake($this-><a href="widget/wrapper/TDBMultiSearch.html#var$database">database</a>);
  193.                 foreach ($values as $value)
  194.                 {
  195.                     if ($value)
  196.                     {
  197.                         $model = $this-><a href="widget/wrapper/TDBMultiSearch.html#var$model">model</a>;
  198.                         
  199.                         $pk = constant("{$model}::PRIMARYKEY");
  200.                         
  201.                         if ($pk === $this->key// key is the primary key (default)
  202.                         {
  203.                             // use find because it uses cache
  204.                             $object $model::find$value );
  205.                         }
  206.                         else // key is an alternative key (uses where->first)
  207.                         {
  208.                             $object $model::where$this->key'='$value )->first();
  209.                         }
  210.                         
  211.                         if ($object)
  212.                         {
  213.                             $description $object->render($this->mask);
  214.                             $items[$value$description;
  215.                         }
  216.                     }
  217.                 }
  218.                 TTransaction::close();
  219.                 
  220.                 parent::addItems$items );
  221.             }
  222.             parent::setValue$original_values );
  223.         }
  224.     }
  225.     
  226.     /**
  227.      * Return the post data
  228.      */
  229.     public function getPostData()
  230.     {
  231.         $ini AdiantiApplicationConfig::get();
  232.         
  233.         if (isset($_POST[$this->name]))
  234.         {
  235.             $values $_POST[$this->name];
  236.             
  237.             if (isset($ini['general']['compat']AND $ini['general']['compat'==  '4')
  238.             {
  239.                 $return [];
  240.                 if (is_array($values))
  241.                 {
  242.                     TTransaction::openFake($this->database);
  243.                     foreach ($values as $value)
  244.                     {
  245.                         if ($value)
  246.                         {
  247.                             $model $this->model;
  248.                             $pk constant("{$model}::PRIMARYKEY");
  249.                             
  250.                             if ($pk === $this->key// key is the primary key (default)
  251.                             {
  252.                                 // use find because it uses cache
  253.                                 $object $model::find$value );
  254.                             }
  255.                             else // key is an alternative key (uses where->first)
  256.                             {
  257.                                 $object $model::where$this->key'='$value )->first();
  258.                             }
  259.                             
  260.                             if ($object)
  261.                             {
  262.                                 $description $object->render($this->mask);
  263.                                 $return[$value$description;
  264.                             }
  265.                         }
  266.                     }
  267.                     TTransaction::close();
  268.                 }
  269.                 return $return;
  270.             }
  271.             else
  272.             {
  273.                 if (empty($this->separator))
  274.                 {
  275.                     return $values;
  276.                 }
  277.                 else
  278.                 {
  279.                     return implode($this->separator$values);
  280.                 }
  281.             }
  282.         }
  283.         else
  284.         {
  285.             return '';
  286.         }
  287.     }
  288.     
  289.     /**
  290.      * Shows the widget
  291.      */
  292.     public function show()
  293.     {
  294.         // define the tag properties
  295.         $this->tag->{'id'}    $this->id// tag id
  296.         
  297.         if (empty($this->tag->{'name'})) // may be defined by child classes
  298.         {
  299.             $this->tag->{'name'}  $this->name.'[]';  // tag name
  300.         }
  301.         
  302.         if (strstr(string) $this->size'%'!== FALSE)
  303.         {
  304.             $this->setProperty('style'"width:{$this->size};"false)//aggregate style info
  305.             $size  "{$this->size}";
  306.         }
  307.         else
  308.         {
  309.             $this->setProperty('style'"width:{$this->size}px;"false)//aggregate style info
  310.             $size  "{$this->size}px";
  311.         }
  312.         
  313.         $multiple $this->maxSize == 'false' 'true';
  314.         $orderColumn = isset($this->orderColumn$this->orderColumn $this->column;
  315.         $criteria '';
  316.         if ($this->criteria)
  317.         {
  318.             $criteria str_replace(array('+''/')array('-''_')base64_encode(serialize($this->criteria)));
  319.         }
  320.         
  321.         $hash md5("{$this->seed}{$this->database}{$this->key}{$this->column}{$this->model}");
  322.         $length $this->minLength;
  323.         
  324.         $class $this->service;
  325.         $callback array($class'onSearch');
  326.         $method $callback[1];
  327.         $id_search_string $this->idSearch '1' '0';
  328.         $id_text_search $this->idTextSearch '1' '0';
  329.         $with_titles $this->withTitles 'true' 'false';
  330.  
  331.         $search_word !empty($this->getProperty('placeholder'))$this->getProperty('placeholder'AdiantiCoreTranslator::translate('Search');
  332.         $url "engine.php?class={$class}&method={$method}&static=1&database={$this->database}&key={$this->key}&column={$this->column}&model={$this->model}&orderColumn={$orderColumn}&criteria={$criteria}&operator={$this->operator}&mask={$this->mask}&idsearch={$id_search_string}&idtextsearch={$id_text_search}&minlength={$length}";
  333.         
  334.         if ($router AdiantiCoreApplication::getRouter())
  335.         {
  336.             $url $router($urlfalse);
  337.         }
  338.  
  339.         $change_action 'function() {}';
  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.             $change_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback'); }";
  350.             $this->setProperty('changeaction'"__adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback')");
  351.         }
  352.         else if (isset($this->changeFunction))
  353.         {
  354.             $change_action "function() { $this->changeFunction }";
  355.             $this->setProperty('changeaction'$this->changeFunctionFALSE);
  356.         }
  357.         
  358.         // shows the component
  359.         parent::renderItemsfalse );
  360.         $this->tag->show();
  361.  
  362.         TScript::create(" tdbmultisearch_start( '{$this->id}', '{$length}', '{$this->maxSize}', '{$search_word}', $multiple, '{$url}', '{$size}', '{$this->height}px', '{$hash}', {$change_action}, {$with_titles} ); ");
  363.         
  364.         if (!$this->editable)
  365.         {
  366.             TScript::create(" tmultisearch_disable_field( '{$this->formName}', '{$this->name}', '{$this->tag->{'title'}}'); ");
  367.         }
  368.     }
  369. }