Advertisement
lordjackson

PessoaForm

Jul 29th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.80 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. ini_set('display_errors', 1);
  5. ini_set('display_startup_erros', 1);
  6. error_reporting(E_ALL);
  7. */
  8.  
  9. class PessoaForm extends TPage
  10. {
  11.  
  12.     private $form;
  13.  
  14.     public function __construct()
  15.     {
  16.  
  17.         parent::__construct();
  18.  
  19.         $this->form = new BootstrapFormBuilder('form_pessoa');
  20.         $this->form->setFormTitle('Formulário de Pessoa');
  21.         $this->form->class = 'form_pessoa';
  22.  
  23.         $id = new THidden('id');
  24.         $uf = new TCombo('uf');
  25.         $municipio_id = new TCombo('municipio_id');
  26.         $nome = new TEntry('nome');
  27.         $apelido = new TEntry('apelido');
  28.         $cpf = new TEntry('cpf');
  29.         $datanascimento = new TDate('datanascimento');
  30.         $sexo = new TCombo('sexo');
  31.         $endereco = new TEntry('endereco');
  32.         $bairro = new TEntry('bairro');
  33.         $cep = new TEntry('cep');
  34.         $telefone = new TEntry('telefone');
  35.         $celular = new TEntry('celular');
  36.         $email = new TEntry('email');
  37.  
  38.         $uf->addItems(\Lib\Funcoes\Util::getUF());
  39.  
  40.         $actionUF = new TAction(array($this, 'onChangeActionUFMunicipio'));
  41.         $uf->setChangeAction($actionUF);
  42.  
  43.         //------- SEXO --------
  44.  
  45.         $sexo_array = array();
  46.         $sexo_array["MASCULINO"] = "MASCULINO";
  47.         $sexo_array["FEMININO"] = "FEMININO";
  48.  
  49.         $sexo->addItems($sexo_array);
  50.  
  51.         TTransaction::close();
  52.         //---------------------------
  53.  
  54.         $nome->setMaxLength(100);
  55.         $apelido->setMaxLength(50);
  56.         $cpf->setMaxLength(20);
  57.         $endereco->setMaxLength(100);
  58.         $bairro->setMaxLength(100);
  59.         $cep->setMaxLength(9);
  60.         $telefone->setMaxLength(20);
  61.         $celular->setMaxLength(20);
  62.         $email->setMaxLength(150);
  63.  
  64.         $municipio_id->addValidation('Município', new TRequiredValidator);
  65.         $nome->addValidation('Nome', new TRequiredValidator);
  66.         $cpf->addValidation('CPF', new TRequiredValidator);
  67.         $datanascimento->addValidation('Data de Nascimento', new TRequiredValidator);
  68.         $sexo->addValidation('Sexo', new TRequiredValidator);
  69.         $cpf->addValidation('CPF', new TCPFValidator);
  70.  
  71.         $telefone->setMask('(99) 9999-9999');
  72.         $celular->setMask('(99) 99999-9999');
  73.         $cep->setMask('99999-999');
  74.         $cpf->setMask('99999999999');
  75.  
  76.         $this->form->addFields([$id]);
  77.         $this->form->addFields([new TLabel('Nome <i>*</i>')], [$nome]);
  78.         $this->form->addFields([new TLabel('Apelido')], [$apelido]);
  79.         $this->form->addFields([new TLabel('CPF <i>*</i>')], [$cpf]);
  80.         $this->form->addFields([new TLabel('UF<i>*</i>')], [$uf]);
  81.         $this->form->addFields([new TLabel('Município <i>*</i>')], [$municipio_id]);
  82.         $this->form->addFields([new TLabel('Data de Nascimento <i>*</i>')], [$datanascimento]);
  83.         $this->form->addFields([new TLabel('Sexo <i>*</i>')], [$sexo]);
  84.         $this->form->addFields([new TLabel('Endereço')], [$endereco]);
  85.         $this->form->addFields([new TLabel('Bairro')], [$bairro]);
  86.         $this->form->addFields([new TLabel('CEP')], [$cep]);
  87.         $this->form->addFields([new TLabel('Telefone')], [$telefone]);
  88.         $this->form->addFields([new TLabel('Celular')], [$celular]);
  89.         $this->form->addFields([new TLabel('E-mail')], [$email]);
  90.  
  91.         $this->form->addFields([new TLabel('')], [TElement::tag('label', '<i>* Campos obrigatórios</i>')]);
  92.  
  93.         $this->form->addAction('Salvar', new TAction(array($this, 'onSave')), 'fa:save')->class = 'btn btn-sm btn-primary';
  94.         $this->form->addAction('Voltar', new TAction(array('PessoaList', 'onReload')), 'fa:arrow-left')->class = 'btn btn-sm btn-primary';
  95.  
  96.         parent::add($this->form);
  97.  
  98.     }
  99.  
  100.     public static function onChangeActionUFMunicipio($param)
  101.     {
  102.  
  103.         $fieldName = 'municipio_id';
  104.         $formName = 'form_pessoa';
  105.         $arrayDados = [];
  106.  
  107.         if (!empty($param['uf'])) {
  108.  
  109.             try {
  110.  
  111.                 TTransaction::open('database');
  112.  
  113.                 $repository = new TRepository('MunicipioRecord');
  114.  
  115.                 $criteria = new TCriteria;
  116.  
  117.                 $criteria->add(new TFilter('uf', '=', $param['uf']));
  118.  
  119.                 $objects = $repository->load($criteria);
  120.  
  121.                 if ($objects) {
  122.  
  123.                     foreach ($objects as $object) {
  124.                         $arrayDados[$object->id] = $object->nomemunicipio;
  125.                     }
  126.  
  127.                 } else {
  128.  
  129.                     new TMessage('INFO', 'Nenhum município encontrado.');
  130.  
  131.                 }
  132.  
  133.                 TTransaction::close();
  134.  
  135.             } catch (Exception $e) {
  136.  
  137.                 new TMessage('INFO', $e->getMessage());
  138.  
  139.             }
  140.  
  141.         }
  142.  
  143.         TCombo::reload($formName, $fieldName, $arrayDados);
  144.  
  145.         if(!empty($param['municipio_id']))
  146.             TScript::create("$(document).ready( function() { $('select[name=" . $fieldName . "]').val(". $param['municipio_id'] ."); });");
  147.  
  148.     }
  149.  
  150.     function onSave()
  151.     {
  152.  
  153.         try {
  154.  
  155.             TTransaction::open('database');
  156.  
  157.             $this->form->validate();
  158.  
  159.             $object = $this->form->getData('PessoaRecord');
  160.             $object->datanascimento = \Adianti\Widget\Form\TDate::date2us($object->datanascimento);
  161.             $object->gestorprograma_id = TSession::getValue('gestorprograma_id');
  162.  
  163.             unset($object->uf);
  164.  
  165.             $object->store();
  166.  
  167.             TTransaction::close();
  168.  
  169.             $action_ok = new TAction(['PessoaList', "onReload"]);
  170.  
  171.             new TMessage("info", "Registro salvo com sucesso!", $action_ok);
  172.  
  173.         } catch (Exception $e) {
  174.  
  175.             $uf = ($this->form->getField('uf'))->getValue();
  176.             $municipio_id = ($this->form->getField('municipio_id'))->getValue();
  177.  
  178.             $this->onChangeActionUFMunicipio(['uf' => $uf, 'municipio_id' => $municipio_id]);
  179.  
  180.             new TMessage('error', $e->getMessage());
  181.  
  182.             TTransaction::rollback();
  183.  
  184.         }
  185.  
  186.     }
  187.  
  188.     function onEdit($param)
  189.     {
  190.  
  191.         try {
  192.  
  193.             if (isset($param['key'])) {
  194.  
  195.                 $key = $param['key'];
  196.  
  197.                 TTransaction::open('database');
  198.  
  199.                 $object = new PessoaRecord($key);
  200.                 $object->datanascimento = TDate::date2br($object->datanascimento);
  201.  
  202.                 $objMunicipio = new MunicipioRecord($object->municipio_id);
  203.                 $object->uf = $objMunicipio->uf;
  204.  
  205.                 $this->onChangeActionUFMunicipio(['uf' => $object->uf, 'municipio_id' => $object->municipio_id]);
  206.  
  207.                 $this->form->setData($object);
  208.  
  209.                 TTransaction::close();
  210.  
  211.             }
  212.  
  213.         } catch (Exception $e) {
  214.  
  215.  
  216.             new TMessage('error', '<b>Error</b> ' . $e->getMessage() . "<br/>");
  217.  
  218.             TTransaction::rollback();
  219.  
  220.         }
  221.  
  222.     }
  223.  
  224. }
  225.  
  226. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement