Pouca coisa pra desenhar meia dúzia de controles...
Código: Selecionar todos
<?php
/**
* FuncionarioForm Form
* @author <your name here>
*/
class FuncionarioForm extends TPage
{
protected $form; // form
private $formFields = [];
private static $database = 'exemplos';
private static $activeRecord = 'Funcionario';
private static $primaryKey = 'id';
private static $formName = 'list_Funcionario';
/**
* Form constructor
* @param $param Request
*/
public function __construct( $param )
{
parent::__construct();
// creates the form
$this->form = new BootstrapFormBuilder(self::$formName);
// define the form title
$this->form->setFormTitle('Cadastro de funcionário');
$id = new TEntry('id');
$dt_nascimento = new TDate('dt_nascimento');
$nome = new TEntry('nome');
$sobrenome = new TEntry('sobrenome');
$funcao_id = new TDBCombo('funcao_id', 'exemplos', 'Funcao', 'id', '{nome}','nome asc' );
$cidade_estado_pais_id = new TDBCombo('cidade_estado_pais_id', 'exemplos', 'Pais', 'id', '{nome}','nome asc' );
$cidade_estado_id = new TCombo('cidade_estado_id');
$cidade_id = new TCombo('cidade_id');
$cep = new TEntry('cep');
$endereco = new TText('endereco');
$cidade_estado_pais_id->setChangeAction(new TAction([$this,'onChangecidade_estado_pais_id']));
$cidade_estado_id->setChangeAction(new TAction([$this,'onChangecidade_estado_id']));
$dt_nascimento->addValidation('Data de nascimento', new TRequiredValidator());
$nome->addValidation('Nome', new TRequiredValidator());
$sobrenome->addValidation('Sobrenome', new TRequiredValidator());
$funcao_id->addValidation('Funcao id', new TRequiredValidator());
$cidade_estado_pais_id->addValidation('País', new TRequiredValidator());
$cidade_id->addValidation('Cidade', new TRequiredValidator());
$dt_nascimento->setMask('dd/mm/yyyy');
$id->setEditable(false);
$dt_nascimento->setDatabaseMask('yyyy-mm-dd');
$nome->autofocus = 'autofocus';
$id->setSize(100);
$cep->setSize('100%');
$nome->setSize('100%');
$sobrenome->setSize('100%');
$funcao_id->setSize('100%');
$cidade_id->setSize('100%');
$dt_nascimento->setSize(110);
$endereco->setSize('100%', 68);
$cidade_estado_id->setSize('100%');
$cidade_estado_pais_id->setSize('100%');
$this->form->addFields([new TLabel('Id:')],[$id],[new TLabel('Data de nascimento:', '#ff0000')],[$dt_nascimento]);
$this->form->addFields([new TLabel('Nome:', '#ff0000')],[$nome],[new TLabel('Sobrenome:', '#ff0000')],[$sobrenome]);
$this->form->addFields([new TLabel('Funcao:', '#ff0000')],[$funcao_id],[],[]);
$this->form->addContent([new TFormSeparator('Endereço', '#333333', '18', '#eeeeee')]);
$this->form->addFields([new TLabel('País:', '#ff0000')],[$cidade_estado_pais_id],[new TLabel('Estado:', '#ff0000')],[$cidade_estado_id]);
$this->form->addFields([new TLabel('Cidade:', '#ff0000')],[$cidade_id],[new TLabel('Cep:', '#ff0000')],[$cep]);
$this->form->addFields([new TLabel('Endereco:', '#ff0000')],[$endereco]);
// create the form actions
$btn_onsave = $this->form->addAction('Salvar', new TAction([$this, 'onSave']), 'fa:floppy-o #ffffff');
$btn_onsave->addStyleClass('btn-primary');
$btn_onclear = $this->form->addAction('Limpar formulário', new TAction([$this, 'onClear']), 'fa:eraser #dd5a43');
// vertical box container
$container = new TVBox;
$container->style = 'width: 100%';
$container->class = 'form-container';
// $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
$container->add($this->form);
parent::add($container);
}
public static function onChangecidade_estado_pais_id($param)
{
try
{
if (isset($param['cidade_estado_pais_id']) && $param['cidade_estado_pais_id'])
{
$criteria = TCriteria::create(['pais_id' => (int) $param['cidade_estado_pais_id']]);
TDBCombo::reloadFromModel(self::$formName, 'cidade_estado_id', 'exemplos', 'Estado', 'id', '{nome}', 'nome asc', $criteria, TRUE);
}
else
{
TCombo::clearField(self::$formName, 'cidade_estado_id');
}
}
catch (Exception $e)
{
new TMessage('error', $e->getMessage());
}
}
public static function onChangecidade_estado_id($param)
{
try
{
if (isset($param['cidade_estado_id']) && $param['cidade_estado_id'])
{
$criteria = TCriteria::create(['estado_id' => (int) $param['cidade_estado_id']]);
TDBCombo::reloadFromModel(self::$formName, 'cidade_id', 'exemplos', 'Cidade', 'id', '{nome}', 'nome asc', $criteria, TRUE);
}
else
{
TCombo::clearField(self::$formName, 'cidade_id');
}
}
catch (Exception $e)
{
new TMessage('error', $e->getMessage());
}
}
public function onSave($param = null)
{
try
{
TTransaction::open(self::$database); // 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
**/
$messageAction = null;
$this->form->validate(); // validate form data
$object = new Funcionario(); // create an empty object
$data = $this->form->getData(); // get form data as array
$object->fromArray( (array) $data); // load the object with data
$object->store(); // save the object
$this->fireEvents($object);
// get the generated {PRIMARY_KEY}
$data->id = $object->id;
$this->form->setData($data); // fill form data
TTransaction::close(); // close the transaction
/**
// To define an action to be executed on the message close event:
$messageAction = new TAction(['className', 'methodName']);
**/
new TMessage('info', AdiantiCoreTranslator::translate('Record saved'), $messageAction);
}
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);
}
public function onEdit( $param )
{
try
{
if (isset($param['key']))
{
$key = $param['key']; // get the parameter $key
TTransaction::open(self::$database); // open a transaction
$object = new Funcionario($key); // instantiates the Active Record
$object->cidade_estado_pais_id = $object->cidade->estado->pais->id;
$object->cidade_estado_id = $object->cidade->estado->id;
$this->form->setData($object); // fill the form
$this->fireEvents($object);
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
}
}
public function onShow()
{
}
public function fireEvents( $object )
{
$obj = new stdClass;
if(get_class($object) == 'stdClass')
{
if(isset($object->cidade_estado_pais_id))
{
$obj->cidade_estado_pais_id = $object->cidade_estado_pais_id;
}
if(isset($object->cidade_estado_id))
{
$obj->cidade_estado_id = $object->cidade_estado_id;
}
if(isset($object->cidade_id))
{
$obj->cidade_id = $object->cidade_id;
}
}
else
{
if(isset($object->cidade->estado->pais->id))
{
$obj->cidade_estado_pais_id = $object->cidade->estado->pais->id;
}
if(isset($object->cidade->estado->id))
{
$obj->cidade_estado_id = $object->cidade->estado->id;
}
if(isset($object->cidade_id))
{
$obj->cidade_id = $object->cidade_id;
}
}
TForm::sendData(self::$formName, $obj);
}
}
Itamar M. Lins Jr.