Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_erros', 1);
- error_reporting(E_ALL);
- /**
- * @version 1.0
- * @package connection
- * @subpackage tutor
- * @author Jackson M
- * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
- * @license http://www.adianti.com.br/framework-license
- */
- class TesteFormList extends TPage {
- private $form; // registration form
- private $datagrid; // listing
- private $loaded;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- public function __construct() {
- parent::__construct();
- TPage::include_js('app/lib/include/sisyphus.min.js');//inclui JS
- $breadcrumb = new TBreadCrumb;
- $breadcrumb->setHomeController('TesteFormList');
- $breadcrumb->addHome();
- $breadcrumb->addItem('Testes');
- $breadcrumb->addItem('Teste Modulo');
- $breadcrumb->addItem('Teste Aulas');
- $breadcrumb->addItem('Aula');
- $breadcrumb->select('Testes');
- // create the form
- $this->form = new Adianti\Widget\Wrapper\TQuickForm('form_object');
- $this->form->class = 'tform'; // CSS class
- $this->form->setProperty('data-persist', 'garlic');
- $this->form->setFormTitle('Formulario e Listagem Teste');
- // funcao sisyphus para utilizar o JS LocalStore salvando os dados do form
- $script = new TElement('script');
- $script->type = 'text/javascript';
- $script->add('
- $( function() {
- $( "form" ).sisyphus();
- } );
- ');
- // create the form fields
- $id = new THidden('id');
- $nome = new Adianti\Widget\Form\TEntry('nome');
- $ano = new Adianti\Widget\Form\TEntry('ano');
- $semestre = new Adianti\Widget\Form\TEntry('semestre');
- $nome->addValidation('Nome', new TRequiredValidator);
- $ano->addValidation('Ano', new TRequiredValidator);
- $semestre->addValidation('Semestre', new TRequiredValidator);
- // add the fields in the form
- $this->form->addQuickField(null, $id, "40%");
- $this->form->addQuickField('Nome', $nome, "80%");
- $this->form->addQuickField('Ano', $ano, "40%");
- $this->form->addQuickField('Semestre', $semestre, "40%");
- // create the form actions
- $this->form->addQuickAction('Salvar', new TAction(array($this, 'onSave')), 'ico_save.png');
- $this->form->addQuickAction('Novo', new TAction(array($this, 'onClear')), 'ico_new.png');
- // id not editable
- $id->setEditable(FALSE);
- // create the datagrid
- $this->datagrid = new TQuickGrid;
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->setHeight(320);
- // add the datagrid columns
- $this->datagrid->addQuickColumn('ID', 'id', 'center', 50, new TAction(array($this, 'onReload')), array('order', 'id'));
- $this->datagrid->addQuickColumn('Nome', 'nome', 'left', 450, new TAction(array($this, 'onReload')), array('order', 'nome'));
- $this->datagrid->addQuickColumn('Ano', 'ano', 'left', 200);
- $this->datagrid->addQuickColumn('Semestre', 'semestre', 'left', 200);
- $action = new TDataGridAction(array('TesteDetailFormList', 'onReload'));
- $action->setParameter('id', filter_input(INPUT_GET, 'object_id'));
- $action->setImage('fa:external-link');
- // add the datagrid actions
- $this->datagrid->addQuickAction('Editar', new TDataGridAction(array($this, 'onEdit')), 'id', 'ico_edit.png');
- $this->datagrid->addQuickAction('Deletar', new TDataGridAction(array($this, 'onDelete')), 'id', 'ico_delete.png');
- $this->datagrid->addQuickAction('Testes', $action, 'id');
- // create the datagrid model
- $this->datagrid->createModel();
- // wrap objects
- $vbox = new TVBox;
- $vbox->add($breadcrumb);
- $vbox->add($this->form);
- $vbox->add($this->datagrid);
- $vbox->add($script); //add stript Sisyphus
- // add the box in the page
- parent::add($vbox);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL) {
- try {
- // open a transaction with database 'connection'
- TTransaction::open('connection');
- // creates a repository for Teste
- $repository = new TRepository('Teste');
- // creates a criteria, ordered by id
- $criteria = new TCriteria;
- // $order = isset($param['order']) ? $param['order'] : 'id';
- $criteria->setProperty('order', 'nome');
- // 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);
- }
- }
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- function onSave() {
- try {
- // open a transaction with database 'connection'
- TTransaction::open('connection');
- // get the form data into an active record Teste
- $object = $this->form->getData('Teste');
- // form validation
- $this->form->validate();
- // stores the object
- $object->store();
- // close the transaction
- TTransaction::close();
- $this->onClear();// limpar dados do formulario e localstore
- // shows the success message
- new TMessage('info', 'Record saved');
- // reload the listing
- $this->onReload();
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * Clear form
- */
- public function onClear() {
- $this->form->clear();
- TScript::create('
- $(function () {
- $("form").sisyphus({
- locationBased: true,
- excludeFields:$( "input, :input" )
- });
- });'
- );
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button
- */
- function onEdit($param) {
- try {
- if (isset($param['key'])) {
- $this->onClear();// limpar dados do formulario e localstore
- // get the parameter e exibe mensagem
- $key = $param['key'];
- // open a transaction with database 'connection'
- TTransaction::open('connection');
- // instantiates object Teste
- $object = new Teste($key);
- // lança os data do modulo no form
- $this->form->setData($object);
- // close the transaction
- TTransaction::close();
- $this->onReload();
- }
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', $e->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) {
- // define the delete action
- $action = new TAction(array($this, 'Delete'));
- $action->setParameters($param); // pass the key parameter ahead
- // shows a dialog to the user
- new TQuestion('Você quer deletar?', $action);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param) {
- try {
- // get the parameter $key
- $key = $param['key'];
- // open a transaction with database 'connection'
- TTransaction::open('connection');
- // instantiates object Teste
- $object = new Teste($key);
- // deletes the object from the database
- $object->delete();
- // close the transaction
- TTransaction::close();
- $this->onClear();// limpar dados do formulario e localstore
- // reload the listing
- $this->onReload($param);
- // shows the success message
- new TMessage('info', "Record Deleted");
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method show()
- * Shows the page e seu conteúdo
- */
- function show() {
- // check if the datagrid is already loaded
- if (!$this->loaded) {
- $this->onReload(func_get_arg(0));
- }
- parent::show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement