Advertisement
lordjackson

GestorProgramaForm

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