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

Documentation is available at TStandardSeek.php

  1. <?php
  2. namespace Adianti\Base;
  3.  
  4. use Adianti\Core\AdiantiApplicationConfig;
  5. use Adianti\Core\AdiantiCoreTranslator;
  6. use Adianti\Widget\Dialog\TMessage;
  7. use Adianti\Widget\Container\TTable;
  8. use Adianti\Widget\Container\TVBox;
  9. use Adianti\Widget\Form\TForm;
  10. use Adianti\Widget\Form\TEntry;
  11. use Adianti\Widget\Form\TLabel;
  12. use Adianti\Widget\Form\TButton;
  13. use Adianti\Widget\Datagrid\TDataGrid;
  14. use Adianti\Widget\Datagrid\TDataGridColumn;
  15. use Adianti\Widget\Datagrid\TDataGridAction;
  16. use Adianti\Widget\Datagrid\TPageNavigation;
  17. use Adianti\Control\TWindow;
  18. use Adianti\Control\TAction;
  19. use Adianti\Database\TTransaction;
  20. use Adianti\Database\TRepository;
  21. use Adianti\Database\TRecord;
  22. use Adianti\Database\TFilter;
  23. use Adianti\Database\TCriteria;
  24. use Adianti\Registry\TSession;
  25. use Adianti\Widget\Container\TPanelGroup;
  26. use Adianti\Wrapper\BootstrapDatagridWrapper;
  27. use Exception;
  28. use StdClass;
  29.  
  30. /**
  31.  * Standard Page controller for Seek buttons
  32.  *
  33.  * @version    7.4
  34.  * @package    base
  35.  * @author     Pablo Dall'Oglio
  36.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  37.  * @license    http://www.adianti.com.br/framework-license
  38.  */
  39. class TStandardSeek extends TWindow
  40. {
  41.     private $form;      // search form
  42.     private $datagrid;  // listing
  43.     private $pageNavigation;
  44.     private $parentForm;
  45.     private $loaded;
  46.     private $items;
  47.     
  48.     /**
  49.      * Constructor Method
  50.      * Creates the page, the search form and the listing
  51.      */
  52.     public function __construct()
  53.     {
  54.         parent::__construct();
  55.         parent::setTitleAdiantiCoreTranslator::translate('Search record') );
  56.         parent::setSize(0.7null);
  57.         parent::removePadding();
  58.         
  59.         // creates a new form
  60.         $this->form new TForm('form_standard_seek');
  61.         // creates a new table
  62.         $table new TTable;
  63.         $table->{'width''100%';
  64.         // adds the table into the form
  65.         $this->form->add($table);
  66.         
  67.         // create the form fields
  68.         $display_fieldnew TEntry('display_field');
  69.         $display_field->setSize('90%');
  70.         
  71.         // keeps the field's value
  72.         $display_field->setValueTSession::getValue('tstandardseek_display_value') );
  73.         
  74.         // create the action button
  75.         $find_button new TButton('busca');
  76.         // define the button action
  77.         $find_action new TAction(array($this'onSearch'));
  78.         $find_action->setParameter('register_state''false');
  79.         $find_button->setAction($find_actionAdiantiCoreTranslator::translate('Search'));
  80.         $find_button->setImage('fa:search blue');
  81.         
  82.         // add a row for the filter field
  83.         $table->addRowSetnew TLabel(AdiantiCoreTranslator::translate('Search').': ')$display_field$find_button);
  84.         
  85.         // define wich are the form fields
  86.         $this->form->setFields(array($display_field$find_button));
  87.         
  88.         // creates a new datagrid
  89.         $this->datagrid new BootstrapDatagridWrapper(new TDataGrid);
  90.         $this->datagrid->{'style''width: 100%';
  91.         
  92.         // creates the paginator
  93.         $this->pageNavigation new TPageNavigation;
  94.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  95.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  96.         
  97.         $panel new TPanelGroup($this->form);
  98.         $panel->{'style''width: 100%;margin-bottom:0;border-radius:0';
  99.         $panel->add($this->datagrid);
  100.         $panel->addFooter($this->pageNavigation);
  101.         
  102.         // add the container to the page
  103.         parent::add($panel);
  104.     }
  105.     
  106.     /**
  107.      * Render datagrid
  108.      */
  109.     public function render()
  110.     {
  111.         // create two datagrid columns
  112.         $id      new TDataGridColumn('id',            'Id',    'center''50');
  113.         $display new TDataGridColumn('display_field'TSession::getValue('standard_seek_label')'left');
  114.         
  115.         // add the columns to the datagrid
  116.         $this->datagrid->addColumn($id);
  117.         $this->datagrid->addColumn($display);
  118.         
  119.         // order by PK
  120.         $order_id new TAction[$this'onReload');
  121.         $order_id->setParameter('order''id');
  122.         $id->setAction($order_id);
  123.         
  124.         // order by Display field
  125.         $order_display new TAction[$this'onReload');
  126.         $order_display->setParameter('order''display_field');
  127.         $display->setAction($order_display);
  128.         
  129.         // create a datagrid action
  130.         $action1 new TDataGridAction(array($this'onSelect'));
  131.         $action1->setLabel('');
  132.         $action1->setImage('far:hand-pointer green');
  133.         $action1->setUseButton(TRUE);
  134.         $action1->setButtonClass('nopadding');
  135.         $action1->setField('id');
  136.         
  137.         // add the actions to the datagrid
  138.         $this->datagrid->addAction($action1);
  139.         
  140.         // create the datagrid model
  141.         $this->datagrid->createModel();
  142.     }
  143.     
  144.     /**
  145.      * Fill datagrid
  146.      */
  147.     public function fill()
  148.     {
  149.         $this->datagrid->clear();
  150.         if ($this->items)
  151.         {
  152.             foreach ($this->items as $item)
  153.             {
  154.                 $this->datagrid->addItem($item);
  155.             }
  156.         }
  157.     }
  158.     
  159.     /**
  160.      * Search datagrid
  161.      */
  162.     public function onSearch()
  163.     {
  164.         // get the form data
  165.         $data $this->form->getData();
  166.         
  167.         // check if the user has filled the form
  168.         if (isset($data-> display_fieldAND ($data-> display_field))
  169.         {
  170.             $operator TSession::getValue('standard_seek_operator');
  171.             
  172.             // creates a filter using the form content
  173.             $display_field TSession::getValue('standard_seek_display_field');
  174.             $filter new TFilter($display_field$operator"%{$data-> display_field}%");
  175.             
  176.             // store the filter in section
  177.             TSession::setValue('tstandardseek_filter',        $filter);
  178.             TSession::setValue('tstandardseek_display_value'$data-> display_field);
  179.         }
  180.         else
  181.         {
  182.             TSession::setValue('tstandardseek_filter',        NULL);
  183.             TSession::setValue('tstandardseek_display_value''');
  184.         }
  185.         
  186.         TSession::setValue('tstandardseek_filter_data'$data);
  187.         
  188.         // set the data back to the form
  189.         $this->form->setData($data);
  190.         
  191.         $param=array();
  192.         $param['offset']    =0;
  193.         $param['first_page']=1;
  194.         $this->onReload($param);
  195.     }
  196.     
  197.     /**
  198.      * Load the datagrid with objects
  199.      */
  200.     public function onReload($param NULL)
  201.     {
  202.         try
  203.         {
  204.             $model    TSession::getValue('standard_seek_model');
  205.             $database TSession::getValue('standard_seek_database');
  206.             $display_field TSession::getValue('standard_seek_display_field');
  207.             
  208.             $pk   constant("{$model}::PRIMARYKEY");
  209.             
  210.             // begins the transaction with database
  211.             TTransaction::open($database);
  212.             
  213.             // creates a repository for the model
  214.             $repository new TRepository($model);
  215.             $limit 10;
  216.             
  217.             // creates a criteria
  218.             if (TSession::getValue('standard_seek_criteria'))
  219.             {
  220.                 $criteria clone TSession::getValue('standard_seek_criteria');
  221.             }
  222.             else
  223.             {
  224.                 $criteria new TCriteria;
  225.                 
  226.                 // default order
  227.                 if (empty($param['order']))
  228.                 {
  229.                     $param['order'$pk;
  230.                     $param['direction''asc';
  231.                 }
  232.             }
  233.             
  234.             if (!empty($param['order']AND $param['order'== 'display_field')
  235.             {
  236.                 $param['order'$display_field;
  237.             }
  238.             
  239.             $criteria->setProperties($param)// order, offset
  240.             $criteria->setProperty('limit'$limit);
  241.             
  242.             if (TSession::getValue('tstandardseek_filter'))
  243.             {
  244.                 // add the filter to the criteria
  245.                 $criteria->add(TSession::getValue('tstandardseek_filter'));
  246.             }
  247.             
  248.             // load all objects according with the criteria
  249.             $objects $repository->load($criteriaFALSE);
  250.             if ($objects)
  251.             {
  252.                 foreach ($objects as $object)
  253.                 {
  254.                     $item $object;
  255.                     $item->{'id'$object->$pk;
  256.                     
  257.                     if (!empty(TSession::getValue('standard_seek_mask')))
  258.                     {
  259.                         $item->{'display_field'$object->render(TSession::getValue('standard_seek_mask'));
  260.                     }
  261.                     else
  262.                     {
  263.                         $item->{'display_field'$object->$display_field;
  264.                     }
  265.                     
  266.                     $this->items[$item;
  267.                 }
  268.             }
  269.             
  270.             // clear the crieteria to count the records
  271.             $criteria->resetProperties();
  272.             $count$repository->count($criteria);
  273.             
  274.             $this->pageNavigation->setCount($count)// count of records
  275.             $this->pageNavigation->setProperties($param)// order, page
  276.             $this->pageNavigation->setLimit($limit)// limit
  277.             
  278.             // closes the transaction
  279.             TTransaction::close();
  280.             $this->loaded true;
  281.         }
  282.         catch (Exception $e// in case of exception
  283.         {
  284.             // shows the exception genearated message
  285.             new TMessage('error'$e->getMessage());
  286.             // rollback all the database operations 
  287.             TTransaction::rollback();
  288.         }
  289.     }
  290.     
  291.     /**
  292.      * Setup seek parameters
  293.      */
  294.     public function onSetup($param=NULL)
  295.     {
  296.         $ini  AdiantiApplicationConfig::get();
  297.         $seed APPLICATION_NAME !empty($ini['general']['seed']$ini['general']['seed''s8dkld83kf73kf094' );
  298.         
  299.         if (isset($param['hash']AND $param['hash'== md5($seed.$param['database'].$param['model'].$param['display_field']))
  300.         {
  301.             // store the parameters in the section
  302.             TSession::setValue('tstandardseek_filter'NULL);
  303.             TSession::setValue('tstandardseek_display_value'NULL);
  304.             TSession::setValue('standard_seek_receive_key',   $param['receive_key']);
  305.             TSession::setValue('standard_seek_receive_field'$param['receive_field'?? null);
  306.             TSession::setValue('standard_seek_display_field'$param['display_field']);
  307.             TSession::setValue('standard_seek_model',         $param['model']);
  308.             TSession::setValue('standard_seek_database',      $param['database']);
  309.             TSession::setValue('standard_seek_parent',        $param['parent']);
  310.             TSession::setValue('standard_seek_operator',      ($param['operator'?? null) );
  311.             TSession::setValue('standard_seek_mask',          ($param['mask']  ?? null) );
  312.             TSession::setValue('standard_seek_label',         ($param['label']  ?? null) );
  313.             
  314.             if (isset($param['criteria']AND $param['criteria'])
  315.             {
  316.                 TSession::setValue('standard_seek_criteria',  unserialize(base64_decode($param['criteria'])));
  317.             }
  318.             $this->onReload();
  319.         }
  320.     }
  321.     
  322.     /**
  323.      * Send the selected register to parent form
  324.      */
  325.     public static function onSelect($param)
  326.     {
  327.         $key $param['key'];
  328.         $database      = isset($param['database'])      $param['database']      TSession::getValue('standard_seek_database');
  329.         $receive_key   = isset($param['receive_key'])   $param['receive_key']   TSession::getValue('standard_seek_receive_key');
  330.         $receive_field = isset($param['receive_field']$param['receive_field'TSession::getValue('standard_seek_receive_field');
  331.         $display_field = isset($param['display_field']$param['display_field'TSession::getValue('standard_seek_display_field');
  332.         $parent        = isset($param['parent'])        $param['parent']        TSession::getValue('standard_seek_parent');
  333.         $seek_mask     = isset($param['mask'])          $param['mask']          TSession::getValue('standard_seek_mask');
  334.         
  335.         try
  336.         {
  337.             TTransaction::open($database);
  338.             
  339.             // load the active record
  340.             $model = isset($param['model']$param['model'TSession::getValue('standard_seek_model');
  341.             $pk constant("{$model}::PRIMARYKEY");
  342.             
  343.             // creates a criteria
  344.             if (TSession::getValue('standard_seek_criteria'))
  345.             {
  346.                 $criteria clone TSession::getValue('standard_seek_criteria');
  347.             }
  348.             else
  349.             {
  350.                 $criteria new TCriteria;
  351.             }
  352.             
  353.             $criteria->add(new TFilter$pk'='$key));
  354.             $criteria->setProperty('limit'1);
  355.             $repository new TRepository($model);
  356.             $objects $repository->load($criteria);
  357.             
  358.             if ($objects)
  359.             {
  360.                 $activeRecord $objects[0];
  361.             
  362.                 $object new StdClass;
  363.                 $object->$receive_key   = isset($activeRecord->$pk$activeRecord->$pk '';
  364.                 
  365.                 if (!empty($seek_mask))
  366.                 {
  367.                     $object->$receive_field $activeRecord->render($seek_mask);
  368.                 }
  369.                 else
  370.                 {
  371.                     $object->$receive_field = isset($activeRecord->$display_field$activeRecord->$display_field '';
  372.                 }
  373.                 
  374.                 TTransaction::close();
  375.                 
  376.                 TForm::sendData($parent$object);
  377.                 parent::closeWindow()// closes the window
  378.             }
  379.             else
  380.             {
  381.                 throw new Exception;
  382.             }
  383.         }
  384.         catch (Exception $e// in case of exception
  385.         {
  386.             // clear fields
  387.             $object new StdClass;
  388.             $object->$receive_key   '';
  389.             $object->$receive_field '';
  390.             TForm::sendData($parent$object);
  391.             
  392.             // undo all pending operations
  393.             TTransaction::rollback();
  394.         }
  395.     }
  396.     
  397.     /**
  398.      * Show page
  399.      */
  400.     public function show()
  401.     {
  402.         parent::setIsWrapped(true);
  403.         $this->run();
  404.         $this->render();
  405.         $this->fill();
  406.         parent::show();
  407.     }
  408. }