Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * LivroForm Form
- * @author <your name here>
- */
- class LivroForm extends TPage
- {
- protected $form; // form
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new TQuickForm('form_Livro');
- $this->form->class = 'tform'; // change CSS class
- $this->form->style = 'display: table;width:100%'; // change style
- // define the form title
- $this->form->setFormTitle('Livro');
- // create the form fields
- $id = new TEntry('id');
- $titulo = new TEntry('titulo');
- $autor = new TEntry('autor');
- $dt_compra = new TDate('dt_compra');
- $valor = new TEntry('valor');
- $dt_compra->setMask('dd/mm/yyyy');
- $valor->setNumericMask(2,',', '.', true);
- // add the fields
- $this->form->addQuickField('Id', $id, 100 );
- $this->form->addQuickField('Titulo', $titulo, 200 );
- $this->form->addQuickField('Autor', $autor, 200 );
- $this->form->addQuickField('Dt Compra', $dt_compra, 100 );
- $this->form->addQuickField('Valor', $valor, 100 );
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('samples'); // open a transaction
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
- $this->form->validate(); // validate form data
- $object = new Livro; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->dt_compra = DateTime::createFromFormat('d/m/Y', $object->dt_compra)->format( 'Y-m-d' );
- $object->store(); // save the object
- // get the generated id
- $data->id = $object->id;
- $this->form->setData($data); // fill form data
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear();
- }
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('samples'); // open a transaction
- $object = new Livro($key); // instantiates the Active Record
- $object->dt_compra = DateTime::createFromFormat('Y-m-d', $object->dt_compra)->format( 'd/m/Y' );
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement