Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * InscricaoList Listing
- * @author <your name here>
- */
- class InscricaoList extends TPage {
- private $form; // registration form
- private $datagrid; // listing
- private $pageNavigation;
- private $loaded;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- function __construct() {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_list_inscricao');
- // creates a table
- $table = new TTable;
- // add the table inside the form
- $this->form->add($table);
- // create the form fields
- $nome = new TEntry('nome');
- $opcao = new TCombo('opcao');
- $nome->setValue(TSession::getValue('inscricao_nome'));
- $item = array();
- $item['curso'] = 'Curso';
- $item['nome'] = 'Nome';
- $item['tipo'] = 'Tipo';
- $opcao->addItems($item);
- $opcao->setValue('curso');
- // add a row for the filter field
- $row = $table->addRow();
- $row->addCell(new TLabel('Opção:'));
- $row->addCell($opcao);
- // add a row for the filter field
- // $row = $table->addRow();
- $row->addCell(new TLabel('Nome:'));
- $row->addCell($nome);
- // create two action buttons to the form
- $find_button = new TButton('find');
- $new_button = new TButton('new');
- // define the button actions
- $find_button->setAction(new TAction(array($this, 'onSearch')), _t('Find'));
- $find_button->setImage('ico_find.png');
- $new_button->setAction(new TAction(array('InscricaoForm', 'onEdit')), _t('New'));
- $new_button->setImage('ico_new.png');
- // add a row for the form actions
- $row = $table->addRow();
- $row->addCell($find_button);
- $row->addCell($new_button);
- // define wich are the form fields
- $this->form->setFields(array($nome, $opcao, $find_button, $new_button));
- // creates a DataGrid
- $this->datagrid = new TDataGrid();
- // $this->datagrid->setHeight(320);
- // creates the datagrid columns
- $id = new TDataGridColumn('id', 'id', 'right', 100);
- $nome = new TDataGridColumn('nome', 'nome', 'left', 200);
- $curso = new TDataGridColumn('curso', 'curso', 'left', 200);
- $unidade = new TDataGridColumn('unidade', 'unidade', 'left', 200);
- $turno = new TDataGridColumn('turno', 'turno', 'left', 100);
- $tipo = new TDataGridColumn('tipo', 'tipo', 'left', 200);
- $datainscricao = new TDataGridColumn('datainscricao', 'Data Inscrição', 'left', 200);
- $medio = new TDataGridColumn('medio', 'medio', 'left', 200);
- $cpf = new TDataGridColumn('cpf', 'cpf', 'left', 200);
- $cidade = new TDataGridColumn('cidade', 'cidade', 'left', 200);
- $estado = new TDataGridColumn('estado', 'estado', 'left', 200);
- $fone = new TDataGridColumn('fone', 'fone', 'left', 100);
- $email = new TDataGridColumn('email', 'email', 'left', 150);
- $datainscricao->setTransformer(array($this, 'formatDate'));
- // add the columns to the DataGrid
- $this->datagrid->addColumn($id);
- $this->datagrid->addColumn($nome);
- $this->datagrid->addColumn($curso);
- // $this->datagrid->addColumn($unidade);
- $this->datagrid->addColumn($turno);
- $this->datagrid->addColumn($tipo);
- $this->datagrid->addColumn($datainscricao);
- // $this->datagrid->addColumn($medio);
- $this->datagrid->addColumn($cpf);
- // $this->datagrid->addColumn($cidade);
- // $this->datagrid->addColumn($estado);
- $this->datagrid->addColumn($fone);
- $this->datagrid->addColumn($email);
- // creates two datagrid actions
- $action1 = new TDataGridAction(array('InscricaoForm', 'onEdit'));
- $action1->setLabel(_t('Edit'));
- $action1->setImage('ico_edit.png');
- $action1->setField('id');
- $action2 = new TDataGridAction(array($this, 'onDelete'));
- $action2->setLabel(_t('Delete'));
- $action2->setImage('ico_delete.png');
- $action2->setField('id');
- // add the actions to the datagrid
- $this->datagrid->addAction($action1);
- $this->datagrid->addAction($action2);
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // creates the page structure using a table
- $table = new TTable;
- $table->addRow()->addCell($this->form);
- $table->addRow()->addCell($this->datagrid);
- $table->addRow()->addCell($this->pageNavigation);
- // add the table inside the page
- parent::add($table);
- }
- /**
- * method onInlineEdit()
- * Inline record editing
- * @param $param Array containing:
- * key: object ID value
- * field name: object attribute to be updated
- * value: new attribute content
- */
- function onInlineEdit($param) {
- try {
- // get the parameter $key
- $field = $param['field'];
- $key = $param['key'];
- $value = $param['value'];
- // open a transaction with database 'ctead'
- TTransaction::open('ctead');
- // instantiates object Inscricao
- $object = new Inscricao($key);
- // deletes the object from the database
- $object->{$field} = $value;
- $object->store();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload($param);
- // shows the success message
- new TMessage('info', "Record Updated");
- } catch (Exception $) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onSearch()
- * Register the filter in the session when the user performs a search
- */
- function onSearch() {
- // get the search form data
- $data = $this->form->getData();
- TSession::setValue('inscricao_nome', $data->name);
- // TSession::setValue('Inscricao_filter', NULL);
- //TSession::setValue('inscricao_nome', '');
- // check if the user has filled the form
- if ($data) {
- var_dump($data);
- // creates a filter using what the user has typed
- // $filter = new TFilter('(SELECT nome from inscricao WHERE nome=inscricao.nome)', 'like', "%{$data->nome}%");
- $filter = new TFilter($data->opcao, 'like', "%{$data->nome}%");
- // stores the filter in the session
- TSession::setValue('Inscricao_filter', $filter);
- TSession::setValue('inscricao_nome', $data->nome);
- // fill the form with data again
- // var_dump($this->form);
- // var_dump($filter);
- // exit();
- }
- $this->form->setData($data);
- $param = array();
- $param['offset'] = 0;
- $param['first_page'] = 1;
- $this->onReload($param);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL) {
- try {
- // open a transaction with database 'ctead'
- TTransaction::open('ctead');
- // creates a repository for Inscricao
- $repository = new TRepository('Inscricao');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('order', 'id desc');
- $criteria->setProperty('limit', $limit);
- // if (TSession::getValue('Inscricao_filter'))
- // {
- // // add the filter stored in the session to the criteria
- // $criteria->add(TSession::getValue('Inscricao_filter'));
- // }
- // load the objects according to criteria
- $objects = $repository->load($criteria);
- $this->datagrid->clear();
- if ($objects) {
- // iterate the collection of active records
- foreach ($objects as $object) {
- // add the object inside the datagrid
- $this->datagrid->addItem($object);
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count = $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- } catch (Exception $) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onDelete()
- * executed whenever the user clicks at the delete button
- * Ask if the user really wants to delete the record
- */
- function onDelete($param) {
- // get the parameter $key
- $key = $param['key'];
- // define two actions
- $action = new TAction(array($this, 'Delete'));
- // define the action parameters
- $action->setParameter('key', $key);
- // shows a dialog to the user
- new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param) {
- try {
- // get the parameter $key
- $key = $param['key'];
- // open a transaction with database 'ctead'
- TTransaction::open('ctead');
- // instantiates object Inscricao
- $object = new Inscricao($key);
- // deletes the object from the database
- $object->delete();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload();
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
- } catch (Exception $) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method show()
- * Shows the page
- */
- function show() {
- // check if the datagrid is already loaded
- if (!$this->loaded) {
- $this->onReload();
- }
- parent::show();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement