PS
Chamar um formulário por meio de uma condicional IF
Bom dia pessoal
Tenho dois forms: ClientesFormPF e ClientesFormPJ
Gostaria de clicar no botão Editar dentro do Grid e conforme o tipo de pesso física ou jurídica chamar o form correspondente, segue meu código abaixo:
Tenho dois forms: ClientesFormPF e ClientesFormPJ
Gostaria de clicar no botão Editar dentro do Grid e conforme o tipo de pesso física ou jurídica chamar o form correspondente, segue meu código abaixo:
- <?php
- /**
- * ClientesLista Listing
- * @author <your name here>
- */
- class ClientesLista extends TPage
- {
- private $form; // form
- private $datagrid; // listing
- private $pageNavigation;
- private $formgrid;
- private $loaded;
- private $deleteButton;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- public function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_search_Clientes');
- $this->form->setFormTitle('Clientes');
- // create the form fields
- $id = new TEntry('id');
- $nome = new TEntry('nome');
- $pessoa_tp_id = new TDBCombo('pessoa_tp_id', 'gestoros', 'PessoaTp', 'id', 'nome');
- $cpfcnpj = new TEntry('cpfcnpj');
- $criteria = new TCriteria;
- $ativo = new THidden('ativo');
- $ativo = '1';
- $criteria->add(new TFilter('ativo_id', '=', $ativo));
- //$cidades_id = new TDBUniqueSearch('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome');
- $cidades_id = new TDBCombo('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome', $criteria);
- //$cidades_id->setValue('TODAS');
- $ativo_id = new TDBCombo('ativo_id', 'gestoros', 'Ativo', 'id', 'nome');
- $nome->style = "text-transform: uppercase";
- // add the fields
- $this->form->addFields( [ new TLabel('Id') ], [ $id ] );
- $this->form->addFields( [ new TLabel('Nome') ], [ $nome ] );
- $this->form->addFields( [ new TLabel('Pessoa') ], [ $pessoa_tp_id ] );
- $this->form->addFields( [ new TLabel('CPF/CNPJ') ], [ $cpfcnpj ] );
- $this->form->addFields( [ new TLabel('Cidade') ], [ $cidades_id ] );
- $this->form->addFields( [ new TLabel('Ativo') ], [ $ativo_id ] );
- // set sizes
- $id->setSize('100%');
- $nome->setSize('100%');
- $pessoa_tp_id->setSize('100%');
- $cpfcnpj->setSize('100%');
- $cidades_id->setSize('100%');
- $ativo_id->setSize('100%');
- // keep the form filled during navigation with session data
- $this->form->setData( TSession::getValue(__CLASS__ . '_filter_data') );
- // add the search form actions
- $btn = $this->form->addAction(_t('Find'), new TAction([$this, 'onSearch']), 'fa:search');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addActionLink(_t('New'), new TAction(['ClientesForm', 'onEdit']), 'fa:plus green');
- // creates a Datagrid
- $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->datatable = 'true';
- // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
- $this->datagrid->enablePopover('Clique para alterar', 'Cliente: <b> {id} </b>');
- // creates the datagrid columns
- $column_id = new TDataGridColumn('id', 'Id', 'right', 10);
- $column_nome = new TDataGridColumn('nome', 'Nome', 'left');
- $column_pessoa_tp_id = new TDataGridColumn('pessoa_tp->nome', 'Pessoa', 'left');
- $column_cpfcnpj = new TDataGridColumn('cpfcnpj', 'CPF/CNPJ', 'left');
- $column_cidades_id = new TDataGridColumn('cidades->nome', 'Cidade', 'right');
- $column_ativo_id = new TDataGridColumn('ativo_id', 'Ativo', 'right');
- // add the columns to the DataGrid
- $this->datagrid->addColumn($column_id);
- $this->datagrid->addColumn($column_nome);
- $this->datagrid->addColumn($column_pessoa_tp_id);
- $this->datagrid->addColumn($column_cpfcnpj);
- $this->datagrid->addColumn($column_cidades_id);
- $this->datagrid->addColumn($column_ativo_id);
- $column_ativo_id->setTransformer( function($value, $object, $row) {
- $class = ($value=='2') ? 'danger' : 'success';
- $label = ($value=='2') ? _t('No') : _t('Yes');
- $div = new TElement('span');
- $div->class="label label-{$class}";
- $div->style="text-shadow:none; font-size:12px; font-weight:lighter";
- $div->add($label);
- return $div;
- });
- // creates the datagrid column actions
- $column_id->setAction(new TAction([$this, 'onReload']), ['order' => 'id']);
- $column_nome->setAction(new TAction([$this, 'onReload']), ['order' => 'nome']);
- //Abindo de acordo com tipo de pessoa
- $action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
- //$action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
- $action2 = new TDataGridAction([$this, 'onDelete'], ['id'=>'{id}']);
- $this->datagrid->addAction($action1, _t('Edit'), 'far:edit blue');
- $this->datagrid->addAction($action2 ,_t('Delete'), 'far:trash-alt red');
- $action_servico = new TDataGridAction(array($this, 'AlterarCliente'));
- $action_servico->setLabel('Alterar Cliente');
- $action_servico->setImage('fa:donate blue fa-lg');
- $action_servico->setField('id');
- $this->datagrid->addAction($action_servico);
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction([$this, 'onReload']));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- $container->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
- parent::add($container);
- }
- /**
- * Inline record editing
- * @param $param Array containing:
- * key: object ID value
- * field name: object attribute to be updated
- * value: new attribute content
- */
- public function onInlineEdit($param)
- {
- try
- {
- // get the parameter $key
- $field = $param['field'];
- $key = $param['key'];
- $value = $param['value'];
- TTransaction::open('gestoros'); // open a transaction with database
- $object = new Clientes($key); // instantiates the Active Record
- $object->{$field} = $value;
- $object->store(); // update the object in the database
- TTransaction::close(); // close the transaction
- $this->onReload($param); // reload the listing
- new TMessage('info', "Record Updated");
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Register the filter in the session
- */
- public function onSearch()
- {
- // get the search form data
- $data = $this->form->getData();
- // clear session filters
- TSession::setValue(__CLASS__.'_filter_id', NULL);
- TSession::setValue(__CLASS__.'_filter_nome', NULL);
- TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', NULL);
- TSession::setValue(__CLASS__.'_filter_cpfcnpj', NULL);
- TSession::setValue(__CLASS__.'_filter_cidades_id', NULL);
- TSession::setValue(__CLASS__.'_filter_ativo_id', NULL);
- if (isset($data->id) AND ($data->id)) {
- $filter = new TFilter('id', '=', $data->id); // create the filter
- TSession::setValue(__CLASS__.'_filter_id', $filter); // stores the filter in the session
- }
- if (isset($data->nome) AND ($data->nome)) {
- $filter = new TFilter('nome', 'like', "%{$data->nome}%"); // create the filter
- TSession::setValue(__CLASS__.'_filter_nome', $filter); // stores the filter in the session
- }
- if (isset($data->pessoa_tp_id) AND ($data->pessoa_tp_id)) {
- $filter = new TFilter('pessoa_tp_id', '=', $data->pessoa_tp_id); // create the filter
- TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', $filter); // stores the filter in the session
- }
- if (isset($data->cpfcnpj) AND ($data->cpfcnpj)) {
- $filter = new TFilter('cpfcnpj', 'like', "%{$data->cpfcnpj}%"); // create the filter
- TSession::setValue(__CLASS__.'_filter_cpfcnpj', $filter); // stores the filter in the session
- }
- if (isset($data->cidades_id) AND ($data->cidades_id)) {
- $filter = new TFilter('cidades_id', '=', $data->cidades_id); // create the filter
- TSession::setValue(__CLASS__.'_filter_cidades_id', $filter); // stores the filter in the session
- }
- if (isset($data->ativo_id) AND ($data->ativo_id)) {
- $filter = new TFilter('ativo_id', '=', $data->ativo_id); // create the filter
- TSession::setValue(__CLASS__.'_filter_ativo_id', $filter); // stores the filter in the session
- }
- // fill the form with data again
- $this->form->setData($data);
- // keep the search data in the session
- TSession::setValue(__CLASS__ . '_filter_data', $data);
- $param = array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- /**
- * Load the datagrid with data
- */
- public function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'gestoros'
- TTransaction::open('gestoros');
- // creates a repository for Clientes
- $repository = new TRepository('Clientes');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- //Desabilita o click do datagrid
- $this->datagrid->disableDefaultClick();
- if (TSession::getValue(__CLASS__.'_filter_id')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_id')); // add the session filter
- }
- if (TSession::getValue(__CLASS__.'_filter_nome')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_nome')); // add the session filter
- }
- if (TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')); // add the session filter
- }
- if (TSession::getValue(__CLASS__.'_filter_cpfcnpj')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_cpfcnpj')); // add the session filter
- }
- if (TSession::getValue(__CLASS__.'_filter_cidades_id')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_cidades_id')); // add the session filter
- }
- if (TSession::getValue(__CLASS__.'_filter_ativo_id')) {
- $criteria->add(TSession::getValue(__CLASS__.'_filter_ativo_id')); // add the session filter
- }
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- if (is_callable($this->transformCallback))
- {
- call_user_func($this->transformCallback, $objects, $param);
- }
- $this->datagrid->clear();
- if ($objects)
- {
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- // add the object inside the datagrid
- $this->datagrid->addItem($object);
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count= $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- TTransaction::rollback();
- }
- }
- /**
- * Ask before deletion
- */
- public static function onDelete($param)
- {
- // define the delete action
- $action = new TAction([__CLASS__, 'Delete']);
- $action->setParameters($param); // pass the key parameter ahead
- // shows a dialog to the user
- new TQuestion(AdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
- }
- /**
- * Delete a record
- */
- public static function Delete($param)
- {
- try
- {
- $key=$param['key']; // get the parameter $key
- TTransaction::open('gestoros'); // open a transaction with database
- $object = new Clientes($key, FALSE); // instantiates the Active Record
- $object->delete(); // deletes the object from the database
- TTransaction::close(); // close the transaction
- $pos_action = new TAction([__CLASS__, 'onReload']);
- new TMessage('info', AdiantiCoreTranslator::translate('Record deleted'), $pos_action); // success message
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method show()
- * Shows the page
- */
- public function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
- {
- if (func_num_args() > 0)
- {
- $this->onReload( func_get_arg(0) );
- }
- else
- {
- $this->onReload();
- }
- }
- parent::show();
- }
- /**
- * Ação antes da Edição
- */
- public function AlterarCliente($param)
- {
- try
- {
- TTransaction::open('gestoros');
- if (isset($param['key']))
- {
- $key = $param['key'];
- $cliente = new Clientes($key);
- if ($cliente->pessoa_tp_id == '2')
- {
- //$action_edit = new TAction(array('ClientesForm', 'onEdit'));
- $action_edit = new TDataGridAction(['ClientesFormPJ', 'onEdit'], ['id'=>'{id}']);
- }
- else
- {
- //$action_edit = new TAction(array('ClientesFormPJ', 'onEdit'));
- $action_edit = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
- }
- }
- TTransaction::close(); // close the transaction
- }
- catch (exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- }
- ?>
Minha chamada:
Minha função:
Pessoal, ajuda urgente.
Alguém pode dar uma força ?
Nataniel Rabaioli, parabéns e muito obrigado, ficou show.
Irei compartilhar meu código aqui, caso mais algém tenha essa dúvida.
</your>