Advertisement
lordjackson

Auto Save Form com PHP e ADIANTI usando o plugin Sisyphus

Mar 1st, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.90 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_erros', 1);
  5. error_reporting(E_ALL);
  6.  
  7. /**
  8.  * @version    1.0
  9.  * @package    connection
  10.  * @subpackage tutor
  11.  * @author     Jackson M
  12.  * @copyright  Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  13.  * @license    http://www.adianti.com.br/framework-license
  14.  */
  15. class TesteFormList extends TPage {
  16.  
  17.     private $form;      // registration form
  18.     private $datagrid;  // listing
  19.     private $loaded;
  20.  
  21.     /**
  22.      * Class constructor
  23.      * Creates the page, the form and the listing
  24.      */
  25.     public function __construct() {
  26.         parent::__construct();
  27.  
  28.         TPage::include_js('app/lib/include/sisyphus.min.js');//inclui JS
  29.  
  30.         $breadcrumb = new TBreadCrumb;
  31.         $breadcrumb->setHomeController('TesteFormList');
  32.         $breadcrumb->addHome();
  33.         $breadcrumb->addItem('Testes');
  34.         $breadcrumb->addItem('Teste Modulo');
  35.         $breadcrumb->addItem('Teste Aulas');
  36.         $breadcrumb->addItem('Aula');
  37.         $breadcrumb->select('Testes');
  38.  
  39.         // create the form
  40.         $this->form = new Adianti\Widget\Wrapper\TQuickForm('form_object');
  41.         $this->form->class = 'tform'; // CSS class
  42.         $this->form->setProperty('data-persist', 'garlic');
  43.         $this->form->setFormTitle('Formulario e Listagem Teste');
  44.  
  45.         // funcao sisyphus para utilizar o JS LocalStore salvando os dados do form
  46.         $script = new TElement('script');
  47.         $script->type = 'text/javascript';
  48.         $script->add('
  49.                        $( function() {
  50.                           $( "form" ).sisyphus();
  51.                        } );
  52.        ');
  53.  
  54.         // create the form fields
  55.         $id = new THidden('id');
  56.         $nome = new Adianti\Widget\Form\TEntry('nome');
  57.         $ano = new Adianti\Widget\Form\TEntry('ano');
  58.         $semestre = new Adianti\Widget\Form\TEntry('semestre');
  59.  
  60.         $nome->addValidation('Nome', new TRequiredValidator);
  61.         $ano->addValidation('Ano', new TRequiredValidator);
  62.         $semestre->addValidation('Semestre', new TRequiredValidator);
  63.  
  64.         // add the fields in the form
  65.         $this->form->addQuickField(null, $id, "40%");
  66.         $this->form->addQuickField('Nome', $nome, "80%");
  67.         $this->form->addQuickField('Ano', $ano, "40%");
  68.         $this->form->addQuickField('Semestre', $semestre, "40%");
  69.        
  70.         // create the form actions
  71.         $this->form->addQuickAction('Salvar', new TAction(array($this, 'onSave')), 'ico_save.png');
  72.         $this->form->addQuickAction('Novo', new TAction(array($this, 'onClear')), 'ico_new.png');
  73.  
  74.         // id not editable
  75.         $id->setEditable(FALSE);
  76.  
  77.         // create the datagrid
  78.         $this->datagrid = new TQuickGrid;
  79.         $this->datagrid->style = 'width: 100%';
  80.         $this->datagrid->setHeight(320);
  81.  
  82.         // add the datagrid columns
  83.         $this->datagrid->addQuickColumn('ID',   'id',  'center', 50, new TAction(array($this, 'onReload')), array('order', 'id'));
  84.         $this->datagrid->addQuickColumn('Nome', 'nome', 'left', 450, new TAction(array($this, 'onReload')), array('order', 'nome'));
  85.         $this->datagrid->addQuickColumn('Ano', 'ano', 'left', 200);
  86.         $this->datagrid->addQuickColumn('Semestre', 'semestre', 'left', 200);
  87.  
  88.         $action = new TDataGridAction(array('TesteDetailFormList', 'onReload'));
  89.         $action->setParameter('id', filter_input(INPUT_GET, 'object_id'));
  90.         $action->setImage('fa:external-link');
  91.  
  92.         // add the datagrid actions
  93.         $this->datagrid->addQuickAction('Editar', new TDataGridAction(array($this, 'onEdit')), 'id', 'ico_edit.png');
  94.         $this->datagrid->addQuickAction('Deletar', new TDataGridAction(array($this, 'onDelete')), 'id', 'ico_delete.png');
  95.         $this->datagrid->addQuickAction('Testes', $action, 'id');
  96.         // create the datagrid model
  97.         $this->datagrid->createModel();
  98.  
  99.         // wrap objects
  100.         $vbox = new TVBox;
  101.         $vbox->add($breadcrumb);
  102.         $vbox->add($this->form);
  103.         $vbox->add($this->datagrid);
  104.         $vbox->add($script); //add stript Sisyphus
  105.        
  106.         // add the box in the page
  107.         parent::add($vbox);
  108.     }
  109.  
  110.     /**
  111.      * method onReload()
  112.      * Load the datagrid with the database objects
  113.      */
  114.     function onReload($param = NULL) {
  115.         try {
  116.             // open a transaction with database 'connection'
  117.             TTransaction::open('connection');
  118.  
  119.             // creates a repository for Teste
  120.             $repository = new TRepository('Teste');
  121.  
  122.             // creates a criteria, ordered by id
  123.             $criteria = new TCriteria;
  124.             //  $order    = isset($param['order']) ? $param['order'] : 'id';
  125.             $criteria->setProperty('order', 'nome');
  126.  
  127.             // load the objects according to criteria
  128.             $objects = $repository->load($criteria);
  129.             $this->datagrid->clear();
  130.             if ($objects) {
  131.                 // iterate the collection of active records
  132.                 foreach ($objects as $object) {
  133.                     // add the object inside the datagrid
  134.                     $this->datagrid->addItem($object);
  135.                 }
  136.             }
  137.             // close the transaction
  138.             TTransaction::close();
  139.             $this->loaded = true;
  140.         } catch (Exception $e) { // in case of exception
  141.             // shows the exception error message
  142.             new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  143.             // undo all pending operations
  144.             TTransaction::rollback();
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * method onSave()
  150.      * Executed whenever the user clicks at the save button
  151.      */
  152.     function onSave() {
  153.         try {
  154.             // open a transaction with database 'connection'
  155.             TTransaction::open('connection');
  156.  
  157.             // get the form data into an active record Teste
  158.             $object = $this->form->getData('Teste');
  159.  
  160.             // form validation
  161.             $this->form->validate();
  162.  
  163.             // stores the object
  164.             $object->store();
  165.  
  166.             // close the transaction
  167.             TTransaction::close();
  168.            
  169.             $this->onClear();// limpar dados do formulario e localstore
  170.            
  171.             // shows the success message
  172.             new TMessage('info', 'Record saved');
  173.  
  174.             // reload the listing
  175.             $this->onReload();
  176.         } catch (Exception $e) { // in case of exception
  177.             // shows the exception error message
  178.             new TMessage('error', $e->getMessage());
  179.  
  180.             // undo all pending operations
  181.             TTransaction::rollback();
  182.         }
  183.     }
  184.  
  185.     /**
  186.      * Clear form
  187.      */
  188.     public function onClear() {
  189.         $this->form->clear();
  190.        
  191.          TScript::create('
  192.                $(function () {
  193.                    $("form").sisyphus({
  194.                        locationBased: true,
  195.                        excludeFields:$( "input, :input" )
  196.                    });
  197.                });'
  198.                         );
  199.      
  200.     }
  201.  
  202.     /**
  203.      * method onEdit()
  204.      * Executed whenever the user clicks at the edit button
  205.      */
  206.     function onEdit($param) {
  207.         try {
  208.             if (isset($param['key'])) {
  209.                 $this->onClear();// limpar dados do formulario e localstore
  210.                
  211.                 // get the parameter e exibe mensagem
  212.                 $key = $param['key'];
  213.  
  214.                 // open a transaction with database 'connection'
  215.                 TTransaction::open('connection');
  216.  
  217.                 // instantiates object Teste
  218.                 $object = new Teste($key);
  219.  
  220.                 // lança os data do modulo no form
  221.                 $this->form->setData($object);
  222.  
  223.                 // close the transaction
  224.                 TTransaction::close();
  225.                 $this->onReload();
  226.             }
  227.         } catch (Exception $e) { // in case of exception
  228.             // shows the exception error message
  229.             new TMessage('error', $e->getMessage());
  230.  
  231.             // undo all pending operations
  232.             TTransaction::rollback();
  233.         }
  234.     }
  235.  
  236.     /**
  237.      * method onDelete()
  238.      * executed whenever the user clicks at the delete button
  239.      * Ask if the user really wants to delete the record
  240.      */
  241.     function onDelete($param) {
  242.         // define the delete action
  243.         $action = new TAction(array($this, 'Delete'));
  244.         $action->setParameters($param); // pass the key parameter ahead
  245.         // shows a dialog to the user
  246.         new TQuestion('Você quer deletar?', $action);
  247.     }
  248.  
  249.     /**
  250.      * method Delete()
  251.      * Delete a record
  252.      */
  253.     function Delete($param) {
  254.         try {
  255.             // get the parameter $key
  256.             $key = $param['key'];
  257.  
  258.             // open a transaction with database 'connection'
  259.             TTransaction::open('connection');
  260.  
  261.             // instantiates object Teste
  262.             $object = new Teste($key);
  263.  
  264.             // deletes the object from the database
  265.             $object->delete();
  266.  
  267.             // close the transaction
  268.             TTransaction::close();
  269.  
  270.             $this->onClear();// limpar dados do formulario e localstore
  271.             // reload the listing
  272.             $this->onReload($param);
  273.             // shows the success message
  274.             new TMessage('info', "Record Deleted");
  275.         } catch (Exception $e) { // in case of exception
  276.             // shows the exception error message
  277.             new TMessage('error', $e->getMessage());
  278.             // undo all pending operations
  279.             TTransaction::rollback();
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * method show()
  285.      * Shows the page e seu conteúdo
  286.      */
  287.     function show() {
  288.         // check if the datagrid is already loaded
  289.         if (!$this->loaded) {
  290.             $this->onReload(func_get_arg(0));
  291.         }
  292.         parent::show();
  293.     }
  294.  
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement