Advertisement
Guest User

LivroForm

a guest
Apr 27th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.90 KB | None | 0 0
  1. <?php
  2. /**
  3.  * LivroForm Form
  4.  * @author  <your name here>
  5.  */
  6. class LivroForm extends TPage
  7. {
  8.     protected $form; // form
  9.    
  10.     /**
  11.      * Form constructor
  12.      * @param $param Request
  13.      */
  14.     public function __construct( $param )
  15.     {
  16.         parent::__construct();
  17.        
  18.         // creates the form
  19.         $this->form = new TQuickForm('form_Livro');
  20.         $this->form->class = 'tform'; // change CSS class
  21.        
  22.         $this->form->style = 'display: table;width:100%'; // change style
  23.        
  24.         // define the form title
  25.         $this->form->setFormTitle('Livro');
  26.        
  27.  
  28.  
  29.         // create the form fields
  30.         $id = new TEntry('id');
  31.         $titulo = new TEntry('titulo');
  32.         $autor = new TEntry('autor');
  33.         $dt_compra = new TDate('dt_compra');
  34.         $valor = new TEntry('valor');
  35.         $dt_compra->setMask('dd/mm/yyyy');
  36.         $valor->setNumericMask(2,',', '.', true);
  37.  
  38.         // add the fields
  39.         $this->form->addQuickField('Id', $id,  100 );
  40.         $this->form->addQuickField('Titulo', $titulo,  200 );
  41.         $this->form->addQuickField('Autor', $autor,  200 );
  42.         $this->form->addQuickField('Dt Compra', $dt_compra,  100 );
  43.         $this->form->addQuickField('Valor', $valor,  100 );
  44.  
  45.  
  46.  
  47.  
  48.         if (!empty($id))
  49.         {
  50.             $id->setEditable(FALSE);
  51.         }
  52.        
  53.         /** samples
  54.          $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  55.          $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  56.          $fieldX->setSize( 100, 40 ); // set size
  57.          **/
  58.          
  59.         // create the form actions
  60.         $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  61.         $this->form->addQuickAction(_t('New'),  new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  62.        
  63.         // vertical box container
  64.         $container = new TVBox;
  65.         $container->style = 'width: 90%';
  66.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  67.         $container->add($this->form);
  68.        
  69.         parent::add($container);
  70.     }
  71.  
  72.     /**
  73.      * Save form data
  74.      * @param $param Request
  75.      */
  76.     public function onSave( $param )
  77.     {
  78.         try
  79.         {
  80.             TTransaction::open('samples'); // open a transaction
  81.            
  82.             /**
  83.             // Enable Debug logger for SQL operations inside the transaction
  84.             TTransaction::setLogger(new TLoggerSTD); // standard output
  85.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  86.             **/
  87.            
  88.             $this->form->validate(); // validate form data
  89.            
  90.             $object = new Livro;  // create an empty object
  91.             $data = $this->form->getData(); // get form data as array
  92.             $object->fromArray( (array) $data); // load the object with data
  93.             $object->dt_compra = DateTime::createFromFormat('d/m/Y', $object->dt_compra)->format( 'Y-m-d' );
  94.             $object->store(); // save the object
  95.            
  96.             // get the generated id
  97.             $data->id = $object->id;
  98.            
  99.             $this->form->setData($data); // fill form data
  100.             TTransaction::close(); // close the transaction
  101.            
  102.             new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  103.         }
  104.         catch (Exception $e) // in case of exception
  105.         {
  106.             new TMessage('error', $e->getMessage()); // shows the exception error message
  107.             $this->form->setData( $this->form->getData() ); // keep form data
  108.             TTransaction::rollback(); // undo all pending operations
  109.         }
  110.     }
  111.    
  112.     /**
  113.      * Clear form data
  114.      * @param $param Request
  115.      */
  116.     public function onClear( $param )
  117.     {
  118.         $this->form->clear();
  119.     }
  120.    
  121.     /**
  122.      * Load object to form data
  123.      * @param $param Request
  124.      */
  125.     public function onEdit( $param )
  126.     {
  127.         try
  128.         {
  129.             if (isset($param['key']))
  130.             {
  131.                 $key = $param['key'];  // get the parameter $key
  132.                 TTransaction::open('samples'); // open a transaction
  133.                 $object = new Livro($key); // instantiates the Active Record
  134.                 $object->dt_compra = DateTime::createFromFormat('Y-m-d', $object->dt_compra)->format( 'd/m/Y' );
  135.                 $this->form->setData($object); // fill the form
  136.                 TTransaction::close(); // close the transaction
  137.             }
  138.             else
  139.             {
  140.                 $this->form->clear();
  141.             }
  142.         }
  143.         catch (Exception $e) // in case of exception
  144.         {
  145.             new TMessage('error', $e->getMessage()); // shows the exception error message
  146.             TTransaction::rollback(); // undo all pending operations
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement