Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- ini_set('display_errors', 1);
- ini_set('display_startup_erros', 1);
- error_reporting(E_ALL);
- */
- class PessoaForm extends TPage
- {
- private $form;
- public function __construct()
- {
- parent::__construct();
- $this->form = new BootstrapFormBuilder('form_pessoa');
- $this->form->setFormTitle('Formulário de Pessoa');
- $this->form->class = 'form_pessoa';
- $id = new THidden('id');
- $uf = new TCombo('uf');
- $municipio_id = new TCombo('municipio_id');
- $nome = new TEntry('nome');
- $apelido = new TEntry('apelido');
- $cpf = new TEntry('cpf');
- $datanascimento = new TDate('datanascimento');
- $sexo = new TCombo('sexo');
- $endereco = new TEntry('endereco');
- $bairro = new TEntry('bairro');
- $cep = new TEntry('cep');
- $telefone = new TEntry('telefone');
- $celular = new TEntry('celular');
- $email = new TEntry('email');
- $uf->addItems(\Lib\Funcoes\Util::getUF());
- $actionUF = new TAction(array($this, 'onChangeActionUFMunicipio'));
- $uf->setChangeAction($actionUF);
- //------- SEXO --------
- $sexo_array = array();
- $sexo_array["MASCULINO"] = "MASCULINO";
- $sexo_array["FEMININO"] = "FEMININO";
- $sexo->addItems($sexo_array);
- TTransaction::close();
- //---------------------------
- $nome->setMaxLength(100);
- $apelido->setMaxLength(50);
- $cpf->setMaxLength(20);
- $endereco->setMaxLength(100);
- $bairro->setMaxLength(100);
- $cep->setMaxLength(9);
- $telefone->setMaxLength(20);
- $celular->setMaxLength(20);
- $email->setMaxLength(150);
- $municipio_id->addValidation('Município', new TRequiredValidator);
- $nome->addValidation('Nome', new TRequiredValidator);
- $cpf->addValidation('CPF', new TRequiredValidator);
- $datanascimento->addValidation('Data de Nascimento', new TRequiredValidator);
- $sexo->addValidation('Sexo', new TRequiredValidator);
- $cpf->addValidation('CPF', new TCPFValidator);
- $telefone->setMask('(99) 9999-9999');
- $celular->setMask('(99) 99999-9999');
- $cep->setMask('99999-999');
- $cpf->setMask('99999999999');
- $this->form->addFields([$id]);
- $this->form->addFields([new TLabel('Nome <i>*</i>')], [$nome]);
- $this->form->addFields([new TLabel('Apelido')], [$apelido]);
- $this->form->addFields([new TLabel('CPF <i>*</i>')], [$cpf]);
- $this->form->addFields([new TLabel('UF<i>*</i>')], [$uf]);
- $this->form->addFields([new TLabel('Município <i>*</i>')], [$municipio_id]);
- $this->form->addFields([new TLabel('Data de Nascimento <i>*</i>')], [$datanascimento]);
- $this->form->addFields([new TLabel('Sexo <i>*</i>')], [$sexo]);
- $this->form->addFields([new TLabel('Endereço')], [$endereco]);
- $this->form->addFields([new TLabel('Bairro')], [$bairro]);
- $this->form->addFields([new TLabel('CEP')], [$cep]);
- $this->form->addFields([new TLabel('Telefone')], [$telefone]);
- $this->form->addFields([new TLabel('Celular')], [$celular]);
- $this->form->addFields([new TLabel('E-mail')], [$email]);
- $this->form->addFields([new TLabel('')], [TElement::tag('label', '<i>* Campos obrigatórios</i>')]);
- $this->form->addAction('Salvar', new TAction(array($this, 'onSave')), 'fa:save')->class = 'btn btn-sm btn-primary';
- $this->form->addAction('Voltar', new TAction(array('PessoaList', 'onReload')), 'fa:arrow-left')->class = 'btn btn-sm btn-primary';
- parent::add($this->form);
- }
- public static function onChangeActionUFMunicipio($param)
- {
- $fieldName = 'municipio_id';
- $formName = 'form_pessoa';
- $arrayDados = [];
- if (!empty($param['uf'])) {
- try {
- TTransaction::open('database');
- $repository = new TRepository('MunicipioRecord');
- $criteria = new TCriteria;
- $criteria->add(new TFilter('uf', '=', $param['uf']));
- $objects = $repository->load($criteria);
- if ($objects) {
- foreach ($objects as $object) {
- $arrayDados[$object->id] = $object->nomemunicipio;
- }
- } else {
- new TMessage('INFO', 'Nenhum município encontrado.');
- }
- TTransaction::close();
- } catch (Exception $e) {
- new TMessage('INFO', $e->getMessage());
- }
- }
- TCombo::reload($formName, $fieldName, $arrayDados);
- if(!empty($param['municipio_id']))
- TScript::create("$(document).ready( function() { $('select[name=" . $fieldName . "]').val(". $param['municipio_id'] ."); });");
- }
- function onSave()
- {
- try {
- TTransaction::open('database');
- $this->form->validate();
- $object = $this->form->getData('PessoaRecord');
- $object->datanascimento = \Adianti\Widget\Form\TDate::date2us($object->datanascimento);
- $object->gestorprograma_id = TSession::getValue('gestorprograma_id');
- unset($object->uf);
- $object->store();
- TTransaction::close();
- $action_ok = new TAction(['PessoaList', "onReload"]);
- new TMessage("info", "Registro salvo com sucesso!", $action_ok);
- } catch (Exception $e) {
- $uf = ($this->form->getField('uf'))->getValue();
- $municipio_id = ($this->form->getField('municipio_id'))->getValue();
- $this->onChangeActionUFMunicipio(['uf' => $uf, 'municipio_id' => $municipio_id]);
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- function onEdit($param)
- {
- try {
- if (isset($param['key'])) {
- $key = $param['key'];
- TTransaction::open('database');
- $object = new PessoaRecord($key);
- $object->datanascimento = TDate::date2br($object->datanascimento);
- $objMunicipio = new MunicipioRecord($object->municipio_id);
- $object->uf = $objMunicipio->uf;
- $this->onChangeActionUFMunicipio(['uf' => $object->uf, 'municipio_id' => $object->municipio_id]);
- $this->form->setData($object);
- TTransaction::close();
- }
- } catch (Exception $e) {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage() . "<br/>");
- TTransaction::rollback();
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement