Created
September 9, 2018 00:42
-
-
Save devalexandre/926ef90a397b1e9380d9ac2c55dfd208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* categoryForm Form | |
* @author Alexandre E Souza | |
*/ | |
class categoryForm extends TWindow | |
{ | |
protected $form; // form | |
/** | |
* Form constructor | |
* @param $param Request | |
*/ | |
public function __construct( $param ) | |
{ | |
parent::__construct(); | |
// creates the form | |
$this->form = new BootstrapFormBuilder('form_category'); | |
$this->form->setFormTitle('category'); | |
// create the form fields | |
$id = new TEntry('id'); | |
$name = new TEntry('name'); | |
// add the fields | |
$this->form->addFields( [ new TLabel('Id') ], [ $id ] ); | |
$this->form->addFields( [ new TLabel('Name') ], [ $name ] ); | |
// set sizes | |
$id->setSize('100%'); | |
$name->setSize('100%'); | |
if (!empty($id)) | |
{ | |
$id->setEditable(FALSE); | |
} | |
/** samples | |
$fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation | |
$fieldX->setSize( '100%' ); // set size | |
**/ | |
// create the form actions | |
$btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:floppy-o'); | |
$btn->class = 'btn btn-sm btn-primary'; | |
$this->form->addAction(_t('New'), new TAction([$this, 'onEdit']), 'fa:eraser red'); | |
// 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('sample'); // 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 | |
$data = $this->form->getData(); // get form data as array | |
$object = new category; // create an empty object | |
$object->fromArray( (array) $data); // load the object with data | |
$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(TRUE); | |
} | |
/** | |
* 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('sample'); // open a transaction | |
$object = new category($key); // instantiates the Active Record | |
$this->form->setData($object); // fill the form | |
TTransaction::close(); // close the transaction | |
} | |
else | |
{ | |
$this->form->clear(TRUE); | |
} | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); // shows the exception error message | |
TTransaction::rollback(); // undo all pending operations | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment