Advertisement
lordjackson

InscricaoList

Oct 16th, 2014
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.37 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * InscricaoList Listing
  5.  * @author  <your name here>
  6.  */
  7. class InscricaoList extends TPage {
  8.  
  9.     private $form;     // registration form
  10.     private $datagrid; // listing
  11.     private $pageNavigation;
  12.     private $loaded;
  13.  
  14.     /**
  15.      * Class constructor
  16.      * Creates the page, the form and the listing
  17.      */
  18.      function __construct() {
  19.         parent::__construct();
  20.  
  21.         // creates the form
  22.         $this->form = new TForm('form_list_inscricao');
  23.  
  24.         // creates a table
  25.         $table = new TTable;
  26.  
  27.         // add the table inside the form
  28.         $this->form->add($table);
  29.  
  30.         // create the form fields
  31.         $nome = new TEntry('nome');
  32.         $opcao = new TCombo('opcao');
  33.         $nome->setValue(TSession::getValue('inscricao_nome'));
  34.  
  35.         $item = array();
  36.         $item['curso'] = 'Curso';
  37.         $item['nome'] = 'Nome';
  38.         $item['tipo'] = 'Tipo';
  39.         $opcao->addItems($item);
  40.         $opcao->setValue('curso');
  41.  
  42.         // add a row for the filter field
  43.         $row = $table->addRow();
  44.         $row->addCell(new TLabel('Opção:'));
  45.         $row->addCell($opcao);
  46.  
  47.         // add a row for the filter field
  48.         //  $row = $table->addRow();
  49.         $row->addCell(new TLabel('Nome:'));
  50.         $row->addCell($nome);
  51.  
  52.         // create two action buttons to the form
  53.         $find_button = new TButton('find');
  54.         $new_button = new TButton('new');
  55.         // define the button actions
  56.         $find_button->setAction(new TAction(array($this, 'onSearch')), _t('Find'));
  57.         $find_button->setImage('ico_find.png');
  58.  
  59.         $new_button->setAction(new TAction(array('InscricaoForm', 'onEdit')), _t('New'));
  60.         $new_button->setImage('ico_new.png');
  61.  
  62.         // add a row for the form actions
  63.         $row = $table->addRow();
  64.         $row->addCell($find_button);
  65.         $row->addCell($new_button);
  66.  
  67.         // define wich are the form fields
  68.         $this->form->setFields(array($nome, $opcao, $find_button, $new_button));
  69.  
  70.         // creates a DataGrid
  71.         $this->datagrid = new TDataGrid();
  72.         // $this->datagrid->setHeight(320);
  73.         // creates the datagrid columns
  74.         $id = new TDataGridColumn('id', 'id', 'right', 100);
  75.         $nome = new TDataGridColumn('nome', 'nome', 'left', 200);
  76.         $curso = new TDataGridColumn('curso', 'curso', 'left', 200);
  77.         $unidade = new TDataGridColumn('unidade', 'unidade', 'left', 200);
  78.         $turno = new TDataGridColumn('turno', 'turno', 'left', 100);
  79.         $tipo = new TDataGridColumn('tipo', 'tipo', 'left', 200);
  80.         $datainscricao = new TDataGridColumn('datainscricao', 'Data Inscrição', 'left', 200);
  81.         $medio = new TDataGridColumn('medio', 'medio', 'left', 200);
  82.         $cpf = new TDataGridColumn('cpf', 'cpf', 'left', 200);
  83.         $cidade = new TDataGridColumn('cidade', 'cidade', 'left', 200);
  84.         $estado = new TDataGridColumn('estado', 'estado', 'left', 200);
  85.         $fone = new TDataGridColumn('fone', 'fone', 'left', 100);
  86.         $email = new TDataGridColumn('email', 'email', 'left', 150);
  87.  
  88.         $datainscricao->setTransformer(array($this, 'formatDate'));
  89.  
  90.         // add the columns to the DataGrid
  91.         $this->datagrid->addColumn($id);
  92.         $this->datagrid->addColumn($nome);
  93.         $this->datagrid->addColumn($curso);
  94. //        $this->datagrid->addColumn($unidade);
  95.         $this->datagrid->addColumn($turno);
  96.         $this->datagrid->addColumn($tipo);
  97.         $this->datagrid->addColumn($datainscricao);
  98. //        $this->datagrid->addColumn($medio);
  99.         $this->datagrid->addColumn($cpf);
  100. //        $this->datagrid->addColumn($cidade);
  101. //        $this->datagrid->addColumn($estado);
  102.         $this->datagrid->addColumn($fone);
  103.         $this->datagrid->addColumn($email);
  104.  
  105.  
  106.         // creates two datagrid actions
  107.         $action1 = new TDataGridAction(array('InscricaoForm', 'onEdit'));
  108.         $action1->setLabel(_t('Edit'));
  109.         $action1->setImage('ico_edit.png');
  110.         $action1->setField('id');
  111.  
  112.         $action2 = new TDataGridAction(array($this, 'onDelete'));
  113.         $action2->setLabel(_t('Delete'));
  114.         $action2->setImage('ico_delete.png');
  115.         $action2->setField('id');
  116.  
  117.         // add the actions to the datagrid
  118.         $this->datagrid->addAction($action1);
  119.         $this->datagrid->addAction($action2);
  120.  
  121.         // create the datagrid model
  122.         $this->datagrid->createModel();
  123.  
  124.         // creates the page navigation
  125.         $this->pageNavigation = new TPageNavigation;
  126.         $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  127.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  128.  
  129.         // creates the page structure using a table
  130.         $table = new TTable;
  131.         $table->addRow()->addCell($this->form);
  132.         $table->addRow()->addCell($this->datagrid);
  133.         $table->addRow()->addCell($this->pageNavigation);
  134.  
  135.         // add the table inside the page
  136.         parent::add($table);
  137.     }
  138.  
  139.     /**
  140.      * method onInlineEdit()
  141.      * Inline record editing
  142.      * @param $param Array containing:
  143.      *              key: object ID value
  144.      *              field name: object attribute to be updated
  145.      *              value: new attribute content
  146.      */
  147.     function onInlineEdit($param) {
  148.         try {
  149.             // get the parameter $key
  150.             $field = $param['field'];
  151.             $key = $param['key'];
  152.             $value = $param['value'];
  153.  
  154.             // open a transaction with database 'ctead'
  155.             TTransaction::open('ctead');
  156.  
  157.             // instantiates object Inscricao
  158.             $object = new Inscricao($key);
  159.             // deletes the object from the database
  160.             $object->{$field} = $value;
  161.             $object->store();
  162.  
  163.             // close the transaction
  164.             TTransaction::close();
  165.  
  166.             // reload the listing
  167.             $this->onReload($param);
  168.             // shows the success message
  169.             new TMessage('info', "Record Updated");
  170.         } catch (Exception $) { // in case of exception
  171.             // shows the exception error message
  172.             new TMessage('error', '<b>Error</b> ' . $->getMessage());
  173.             // undo all pending operations
  174.             TTransaction::rollback();
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * method onSearch()
  180.      * Register the filter in the session when the user performs a search
  181.      */
  182.     function onSearch() {
  183.         // get the search form data
  184.         $data = $this->form->getData();
  185.         TSession::setValue('inscricao_nome', $data->name);
  186. //        TSession::setValue('Inscricao_filter',   NULL);
  187.         //TSession::setValue('inscricao_nome', '');
  188.         // check if the user has filled the form
  189.         if ($data) {
  190.             var_dump($data);
  191.             // creates a filter using what the user has typed
  192. //            $filter = new TFilter('(SELECT nome from inscricao WHERE nome=inscricao.nome)', 'like', "%{$data->nome}%");
  193.             $filter = new TFilter($data->opcao, 'like', "%{$data->nome}%");
  194.  
  195.             // stores the filter in the session
  196.             TSession::setValue('Inscricao_filter', $filter);
  197.             TSession::setValue('inscricao_nome', $data->nome);
  198.             // fill the form with data again
  199. //                    var_dump($this->form);
  200. //                    var_dump($filter);
  201. //        exit();
  202.         }
  203.         $this->form->setData($data);
  204.  
  205.         $param = array();
  206.         $param['offset'] = 0;
  207.         $param['first_page'] = 1;
  208.         $this->onReload($param);
  209.     }
  210.  
  211.     /**
  212.      * method onReload()
  213.      * Load the datagrid with the database objects
  214.      */
  215.     function onReload($param = NULL) {
  216.         try {
  217.             // open a transaction with database 'ctead'
  218.             TTransaction::open('ctead');
  219.  
  220.             // creates a repository for Inscricao
  221.             $repository = new TRepository('Inscricao');
  222.             $limit = 10;
  223.             // creates a criteria
  224.             $criteria = new TCriteria;
  225.             $criteria->setProperties($param); // order, offset
  226.             $criteria->setProperty('order', 'id desc');
  227.             $criteria->setProperty('limit', $limit);
  228.  
  229. //            if (TSession::getValue('Inscricao_filter'))
  230. //            {
  231. //                // add the filter stored in the session to the criteria
  232. //                $criteria->add(TSession::getValue('Inscricao_filter'));
  233. //            }
  234.             // load the objects according to criteria
  235.             $objects = $repository->load($criteria);
  236.  
  237.             $this->datagrid->clear();
  238.             if ($objects) {
  239.                 // iterate the collection of active records
  240.                 foreach ($objects as $object) {
  241.                     // add the object inside the datagrid
  242.                     $this->datagrid->addItem($object);
  243.                 }
  244.             }
  245.  
  246.             // reset the criteria for record count
  247.             $criteria->resetProperties();
  248.             $count = $repository->count($criteria);
  249.  
  250.             $this->pageNavigation->setCount($count); // count of records
  251.             $this->pageNavigation->setProperties($param); // order, page
  252.             $this->pageNavigation->setLimit($limit); // limit
  253.             // close the transaction
  254.             TTransaction::close();
  255.             $this->loaded = true;
  256.         } catch (Exception $) { // in case of exception
  257.             // shows the exception error message
  258.             new TMessage('error', '<b>Error</b> ' . $->getMessage());
  259.  
  260.             // undo all pending operations
  261.             TTransaction::rollback();
  262.         }
  263.     }
  264.  
  265.     /**
  266.      * method onDelete()
  267.      * executed whenever the user clicks at the delete button
  268.      * Ask if the user really wants to delete the record
  269.      */
  270.     function onDelete($param) {
  271.         // get the parameter $key
  272.         $key = $param['key'];
  273.  
  274.         // define two actions
  275.         $action = new TAction(array($this, 'Delete'));
  276.  
  277.         // define the action parameters
  278.         $action->setParameter('key', $key);
  279.  
  280.         // shows a dialog to the user
  281.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  282.     }
  283.  
  284.     /**
  285.      * method Delete()
  286.      * Delete a record
  287.      */
  288.     function Delete($param) {
  289.         try {
  290.             // get the parameter $key
  291.             $key = $param['key'];
  292.             // open a transaction with database 'ctead'
  293.             TTransaction::open('ctead');
  294.  
  295.             // instantiates object Inscricao
  296.             $object = new Inscricao($key);
  297.  
  298.             // deletes the object from the database
  299.             $object->delete();
  300.  
  301.             // close the transaction
  302.             TTransaction::close();
  303.  
  304.             // reload the listing
  305.             $this->onReload();
  306.             // shows the success message
  307.             new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
  308.         } catch (Exception $) { // in case of exception
  309.             // shows the exception error message
  310.             new TMessage('error', '<b>Error</b> ' . $->getMessage());
  311.  
  312.             // undo all pending operations
  313.             TTransaction::rollback();
  314.         }
  315.     }
  316.  
  317.     /**
  318.      * method show()
  319.      * Shows the page
  320.      */
  321.     function show() {
  322.         // check if the datagrid is already loaded
  323.         if (!$this->loaded) {
  324.             $this->onReload();
  325.         }
  326.         parent::show();
  327.     }
  328.  
  329. }
  330.  
  331. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement