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: ...
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:

 
  1. <?php
  2. /**
  3. * ClientesLista Listing
  4. * @author <your name here>
  5. */
  6. class ClientesLista extends TPage
  7. {
  8. private $form; // form
  9. private $datagrid; // listing
  10. private $pageNavigation;
  11. private $formgrid;
  12. private $loaded;
  13. private $deleteButton;
  14. /**
  15. * Class constructor
  16. * Creates the page, the form and the listing
  17. */
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. // creates the form
  22. $this->form = new BootstrapFormBuilder('form_search_Clientes');
  23. $this->form->setFormTitle('Clientes');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $nome = new TEntry('nome');
  27. $pessoa_tp_id = new TDBCombo('pessoa_tp_id', 'gestoros', 'PessoaTp', 'id', 'nome');
  28. $cpfcnpj = new TEntry('cpfcnpj');
  29. $criteria = new TCriteria;
  30. $ativo = new THidden('ativo');
  31. $ativo = '1';
  32. $criteria->add(new TFilter('ativo_id', '=', $ativo));
  33. //$cidades_id = new TDBUniqueSearch('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome');
  34. $cidades_id = new TDBCombo('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome', $criteria);
  35. //$cidades_id->setValue('TODAS');
  36. $ativo_id = new TDBCombo('ativo_id', 'gestoros', 'Ativo', 'id', 'nome');
  37. $nome->style = "text-transform: uppercase";
  38. // add the fields
  39. $this->form->addFields( [ new TLabel('Id') ], [ $id ] );
  40. $this->form->addFields( [ new TLabel('Nome') ], [ $nome ] );
  41. $this->form->addFields( [ new TLabel('Pessoa') ], [ $pessoa_tp_id ] );
  42. $this->form->addFields( [ new TLabel('CPF/CNPJ') ], [ $cpfcnpj ] );
  43. $this->form->addFields( [ new TLabel('Cidade') ], [ $cidades_id ] );
  44. $this->form->addFields( [ new TLabel('Ativo') ], [ $ativo_id ] );
  45. // set sizes
  46. $id->setSize('100%');
  47. $nome->setSize('100%');
  48. $pessoa_tp_id->setSize('100%');
  49. $cpfcnpj->setSize('100%');
  50. $cidades_id->setSize('100%');
  51. $ativo_id->setSize('100%');
  52. // keep the form filled during navigation with session data
  53. $this->form->setData( TSession::getValue(__CLASS__ . '_filter_data') );
  54. // add the search form actions
  55. $btn = $this->form->addAction(_t('Find'), new TAction([$this, 'onSearch']), 'fa:search');
  56. $btn->class = 'btn btn-sm btn-primary';
  57. $this->form->addActionLink(_t('New'), new TAction(['ClientesForm', 'onEdit']), 'fa:plus green');
  58. // creates a Datagrid
  59. $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  60. $this->datagrid->style = 'width: 100%';
  61. $this->datagrid->datatable = 'true';
  62. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  63. $this->datagrid->enablePopover('Clique para alterar', 'Cliente: <b> {id} </b>');
  64. // creates the datagrid columns
  65. $column_id = new TDataGridColumn('id', 'Id', 'right', 10);
  66. $column_nome = new TDataGridColumn('nome', 'Nome', 'left');
  67. $column_pessoa_tp_id = new TDataGridColumn('pessoa_tp->nome', 'Pessoa', 'left');
  68. $column_cpfcnpj = new TDataGridColumn('cpfcnpj', 'CPF/CNPJ', 'left');
  69. $column_cidades_id = new TDataGridColumn('cidades->nome', 'Cidade', 'right');
  70. $column_ativo_id = new TDataGridColumn('ativo_id', 'Ativo', 'right');
  71. // add the columns to the DataGrid
  72. $this->datagrid->addColumn($column_id);
  73. $this->datagrid->addColumn($column_nome);
  74. $this->datagrid->addColumn($column_pessoa_tp_id);
  75. $this->datagrid->addColumn($column_cpfcnpj);
  76. $this->datagrid->addColumn($column_cidades_id);
  77. $this->datagrid->addColumn($column_ativo_id);
  78. $column_ativo_id->setTransformer( function($value, $object, $row) {
  79. $class = ($value=='2') ? 'danger' : 'success';
  80. $label = ($value=='2') ? _t('No') : _t('Yes');
  81. $div = new TElement('span');
  82. $div->class="label label-{$class}";
  83. $div->style="text-shadow:none; font-size:12px; font-weight:lighter";
  84. $div->add($label);
  85. return $div;
  86. });
  87. // creates the datagrid column actions
  88. $column_id->setAction(new TAction([$this, 'onReload']), ['order' => 'id']);
  89. $column_nome->setAction(new TAction([$this, 'onReload']), ['order' => 'nome']);
  90. //Abindo de acordo com tipo de pessoa
  91. $action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  92. //$action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  93. $action2 = new TDataGridAction([$this, 'onDelete'], ['id'=>'{id}']);
  94. $this->datagrid->addAction($action1, _t('Edit'), 'far:edit blue');
  95. $this->datagrid->addAction($action2 ,_t('Delete'), 'far:trash-alt red');
  96. $action_servico = new TDataGridAction(array($this, 'AlterarCliente'));
  97. $action_servico->setLabel('Alterar Cliente');
  98. $action_servico->setImage('fa:donate blue fa-lg');
  99. $action_servico->setField('id');
  100. $this->datagrid->addAction($action_servico);
  101. // create the datagrid model
  102. $this->datagrid->createModel();
  103. // creates the page navigation
  104. $this->pageNavigation = new TPageNavigation;
  105. $this->pageNavigation->setAction(new TAction([$this, 'onReload']));
  106. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  107. // vertical box container
  108. $container = new TVBox;
  109. $container->style = 'width: 100%';
  110. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  111. $container->add($this->form);
  112. $container->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
  113. parent::add($container);
  114. }
  115. /**
  116. * Inline record editing
  117. * @param $param Array containing:
  118. * key: object ID value
  119. * field name: object attribute to be updated
  120. * value: new attribute content
  121. */
  122. public function onInlineEdit($param)
  123. {
  124. try
  125. {
  126. // get the parameter $key
  127. $field = $param['field'];
  128. $key = $param['key'];
  129. $value = $param['value'];
  130. TTransaction::open('gestoros'); // open a transaction with database
  131. $object = new Clientes($key); // instantiates the Active Record
  132. $object->{$field} = $value;
  133. $object->store(); // update the object in the database
  134. TTransaction::close(); // close the transaction
  135. $this->onReload($param); // reload the listing
  136. new TMessage('info', "Record Updated");
  137. }
  138. catch (Exception $e) // in case of exception
  139. {
  140. new TMessage('error', $e->getMessage()); // shows the exception error message
  141. TTransaction::rollback(); // undo all pending operations
  142. }
  143. }
  144. /**
  145. * Register the filter in the session
  146. */
  147. public function onSearch()
  148. {
  149. // get the search form data
  150. $data = $this->form->getData();
  151. // clear session filters
  152. TSession::setValue(__CLASS__.'_filter_id', NULL);
  153. TSession::setValue(__CLASS__.'_filter_nome', NULL);
  154. TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', NULL);
  155. TSession::setValue(__CLASS__.'_filter_cpfcnpj', NULL);
  156. TSession::setValue(__CLASS__.'_filter_cidades_id', NULL);
  157. TSession::setValue(__CLASS__.'_filter_ativo_id', NULL);
  158. if (isset($data->id) AND ($data->id)) {
  159. $filter = new TFilter('id', '=', $data->id); // create the filter
  160. TSession::setValue(__CLASS__.'_filter_id', $filter); // stores the filter in the session
  161. }
  162. if (isset($data->nome) AND ($data->nome)) {
  163. $filter = new TFilter('nome', 'like', "%{$data->nome}%"); // create the filter
  164. TSession::setValue(__CLASS__.'_filter_nome', $filter); // stores the filter in the session
  165. }
  166. if (isset($data->pessoa_tp_id) AND ($data->pessoa_tp_id)) {
  167. $filter = new TFilter('pessoa_tp_id', '=', $data->pessoa_tp_id); // create the filter
  168. TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', $filter); // stores the filter in the session
  169. }
  170. if (isset($data->cpfcnpj) AND ($data->cpfcnpj)) {
  171. $filter = new TFilter('cpfcnpj', 'like', "%{$data->cpfcnpj}%"); // create the filter
  172. TSession::setValue(__CLASS__.'_filter_cpfcnpj', $filter); // stores the filter in the session
  173. }
  174. if (isset($data->cidades_id) AND ($data->cidades_id)) {
  175. $filter = new TFilter('cidades_id', '=', $data->cidades_id); // create the filter
  176. TSession::setValue(__CLASS__.'_filter_cidades_id', $filter); // stores the filter in the session
  177. }
  178. if (isset($data->ativo_id) AND ($data->ativo_id)) {
  179. $filter = new TFilter('ativo_id', '=', $data->ativo_id); // create the filter
  180. TSession::setValue(__CLASS__.'_filter_ativo_id', $filter); // stores the filter in the session
  181. }
  182. // fill the form with data again
  183. $this->form->setData($data);
  184. // keep the search data in the session
  185. TSession::setValue(__CLASS__ . '_filter_data', $data);
  186. $param = array();
  187. $param['offset'] =0;
  188. $param['first_page']=1;
  189. $this->onReload($param);
  190. }
  191. /**
  192. * Load the datagrid with data
  193. */
  194. public function onReload($param = NULL)
  195. {
  196. try
  197. {
  198. // open a transaction with database 'gestoros'
  199. TTransaction::open('gestoros');
  200. // creates a repository for Clientes
  201. $repository = new TRepository('Clientes');
  202. $limit = 10;
  203. // creates a criteria
  204. $criteria = new TCriteria;
  205. // default order
  206. if (empty($param['order']))
  207. {
  208. $param['order'] = 'id';
  209. $param['direction'] = 'asc';
  210. }
  211. $criteria->setProperties($param); // order, offset
  212. $criteria->setProperty('limit', $limit);
  213. //Desabilita o click do datagrid
  214. $this->datagrid->disableDefaultClick();
  215. if (TSession::getValue(__CLASS__.'_filter_id')) {
  216. $criteria->add(TSession::getValue(__CLASS__.'_filter_id')); // add the session filter
  217. }
  218. if (TSession::getValue(__CLASS__.'_filter_nome')) {
  219. $criteria->add(TSession::getValue(__CLASS__.'_filter_nome')); // add the session filter
  220. }
  221. if (TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')) {
  222. $criteria->add(TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')); // add the session filter
  223. }
  224. if (TSession::getValue(__CLASS__.'_filter_cpfcnpj')) {
  225. $criteria->add(TSession::getValue(__CLASS__.'_filter_cpfcnpj')); // add the session filter
  226. }
  227. if (TSession::getValue(__CLASS__.'_filter_cidades_id')) {
  228. $criteria->add(TSession::getValue(__CLASS__.'_filter_cidades_id')); // add the session filter
  229. }
  230. if (TSession::getValue(__CLASS__.'_filter_ativo_id')) {
  231. $criteria->add(TSession::getValue(__CLASS__.'_filter_ativo_id')); // add the session filter
  232. }
  233. // load the objects according to criteria
  234. $objects = $repository->load($criteria, FALSE);
  235. if (is_callable($this->transformCallback))
  236. {
  237. call_user_func($this->transformCallback, $objects, $param);
  238. }
  239. $this->datagrid->clear();
  240. if ($objects)
  241. {
  242. // iterate the collection of active records
  243. foreach ($objects as $object)
  244. {
  245. // add the object inside the datagrid
  246. $this->datagrid->addItem($object);
  247. }
  248. }
  249. // reset the criteria for record count
  250. $criteria->resetProperties();
  251. $count= $repository->count($criteria);
  252. $this->pageNavigation->setCount($count); // count of records
  253. $this->pageNavigation->setProperties($param); // order, page
  254. $this->pageNavigation->setLimit($limit); // limit
  255. // close the transaction
  256. TTransaction::close();
  257. $this->loaded = true;
  258. }
  259. catch (Exception $e)
  260. {
  261. new TMessage('error', $e->getMessage());
  262. TTransaction::rollback();
  263. }
  264. }
  265. /**
  266. * Ask before deletion
  267. */
  268. public static function onDelete($param)
  269. {
  270. // define the delete action
  271. $action = new TAction([__CLASS__, 'Delete']);
  272. $action->setParameters($param); // pass the key parameter ahead
  273. // shows a dialog to the user
  274. new TQuestion(AdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  275. }
  276. /**
  277. * Delete a record
  278. */
  279. public static function Delete($param)
  280. {
  281. try
  282. {
  283. $key=$param['key']; // get the parameter $key
  284. TTransaction::open('gestoros'); // open a transaction with database
  285. $object = new Clientes($key, FALSE); // instantiates the Active Record
  286. $object->delete(); // deletes the object from the database
  287. TTransaction::close(); // close the transaction
  288. $pos_action = new TAction([__CLASS__, 'onReload']);
  289. new TMessage('info', AdiantiCoreTranslator::translate('Record deleted'), $pos_action); // success message
  290. }
  291. catch (Exception $e) // in case of exception
  292. {
  293. new TMessage('error', $e->getMessage()); // shows the exception error message
  294. TTransaction::rollback(); // undo all pending operations
  295. }
  296. }
  297. /**
  298. * method show()
  299. * Shows the page
  300. */
  301. public function show()
  302. {
  303. // check if the datagrid is already loaded
  304. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  305. {
  306. if (func_num_args() > 0)
  307. {
  308. $this->onReload( func_get_arg(0) );
  309. }
  310. else
  311. {
  312. $this->onReload();
  313. }
  314. }
  315. parent::show();
  316. }
  317. /**
  318. * Ação antes da Edição
  319. */
  320. public function AlterarCliente($param)
  321. {
  322. try
  323. {
  324. TTransaction::open('gestoros');
  325. if (isset($param['key']))
  326. {
  327. $key = $param['key'];
  328. $cliente = new Clientes($key);
  329. if ($cliente->pessoa_tp_id == '2')
  330. {
  331. //$action_edit = new TAction(array('ClientesForm', 'onEdit'));
  332. $action_edit = new TDataGridAction(['ClientesFormPJ', 'onEdit'], ['id'=>'{id}']);
  333. }
  334. else
  335. {
  336. //$action_edit = new TAction(array('ClientesFormPJ', 'onEdit'));
  337. $action_edit = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  338. }
  339. }
  340. TTransaction::close(); // close the transaction
  341. }
  342. catch (exception $e)
  343. {
  344. new TMessage('error', $e->getMessage());
  345. }
  346. }
  347. }
  348. ?>


Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (4)


PS

Minha chamada:

 
  1. <?php
  2. $action_servico = new TDataGridAction(array($this, 'AlterarCliente'));
  3. $action_servico->setLabel('Alterar Cliente');
  4. $action_servico->setImage('fa:donate blue fa-lg');
  5. $action_servico->setField('id');
  6. $this->datagrid->addAction($action_servico);
  7. ?>


Minha função:

 
  1. <?php
  2. public function AlterarCliente($param)
  3. {
  4. try
  5. {
  6. TTransaction::open('gestoros');
  7. if (isset($param['key']))
  8. {
  9. $key = $param['key'];
  10. $cliente = new Clientes($key);
  11. if ($cliente->pessoa_tp_id == '2')
  12. {
  13. $action_edit = new TDataGridAction(['ClientesFormPJ', 'onEdit'], ['id'=>'{id}']);
  14. }
  15. else
  16. {
  17. $action_edit = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  18. }
  19. }
  20. TTransaction::close(); // close the transaction
  21. }
  22. catch (exception $e)
  23. {
  24. new TMessage('error', $e->getMessage());
  25. }
  26. }
  27. ?>

PS

Pessoal, ajuda urgente.
Alguém pode dar uma força ?
NR

 
  1. <?php
  2. if ($cliente->pessoa_tp_id == '2')
  3. {
  4. //$action_edit = new TDataGridAction(['ClientesFormPJ', 'onEdit'], ['id'=>'{id}']);
  5. TApplication::loadPage('ClientesFormPJ', 'onEdit', ['id'=>$key]);
  6. }
  7. else
  8. {
  9. //$action_edit = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  10. TApplication::loadPage('ClientesForm', 'onEdit', ['id'=>$key]);
  11. }
  12. ?>
PS

Nataniel Rabaioli, parabéns e muito obrigado, ficou show.
Irei compartilhar meu código aqui, caso mais algém tenha essa dúvida.

 
  1. <?php
  2. /**
  3. * ClientesLista Listing
  4. * @author <your name here>
  5. */
  6. class ClientesLista extends TPage
  7. {
  8. private $form; // form
  9. private $datagrid; // listing
  10. private $pageNavigation;
  11. private $formgrid;
  12. private $loaded;
  13. private $deleteButton;
  14. /**
  15. * Class constructor
  16. * Creates the page, the form and the listing
  17. */
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. // creates the form
  22. $this->form = new BootstrapFormBuilder('form_search_Clientes');
  23. $this->form->setFormTitle('Clientes');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $nome = new TEntry('nome');
  27. $pessoa_tp_id = new TDBCombo('pessoa_tp_id', 'gestoros', 'PessoaTp', 'id', 'nome');
  28. $cpfcnpj = new TEntry('cpfcnpj');
  29. $criteria = new TCriteria;
  30. $ativo = new THidden('ativo');
  31. $ativo = '1';
  32. $criteria->add(new TFilter('ativo_id', '=', $ativo));
  33. //$cidades_id = new TDBUniqueSearch('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome');
  34. $cidades_id = new TDBCombo('cidades_id', 'gestoros', 'Cidades', 'id', 'nome', 'nome', $criteria);
  35. //$cidades_id->setValue('TODAS');
  36. $ativo_id = new TDBCombo('ativo_id', 'gestoros', 'Ativo', 'id', 'nome');
  37. $nome->style = "text-transform: uppercase";
  38. // add the fields
  39. $this->form->addFields( [ new TLabel('Id') ], [ $id ] );
  40. $this->form->addFields( [ new TLabel('Nome') ], [ $nome ] );
  41. $this->form->addFields( [ new TLabel('Pessoa') ], [ $pessoa_tp_id ] );
  42. $this->form->addFields( [ new TLabel('CPF/CNPJ') ], [ $cpfcnpj ] );
  43. $this->form->addFields( [ new TLabel('Cidade') ], [ $cidades_id ] );
  44. $this->form->addFields( [ new TLabel('Ativo') ], [ $ativo_id ] );
  45. // set sizes
  46. $id->setSize('100%');
  47. $nome->setSize('100%');
  48. $pessoa_tp_id->setSize('100%');
  49. $cpfcnpj->setSize('100%');
  50. $cidades_id->setSize('100%');
  51. $ativo_id->setSize('100%');
  52. // keep the form filled during navigation with session data
  53. $this->form->setData( TSession::getValue(__CLASS__ . '_filter_data') );
  54. // add the search form actions
  55. $btn = $this->form->addAction(_t('Find'), new TAction([$this, 'onSearch']), 'fa:search');
  56. $btn->class = 'btn btn-sm btn-primary';
  57. $this->form->addActionLink(_t('New'), new TAction(['ClientesForm', 'onEdit']), 'fa:plus green');
  58. // creates a Datagrid
  59. $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  60. $this->datagrid->style = 'width: 100%';
  61. $this->datagrid->datatable = 'true';
  62. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  63. $this->datagrid->enablePopover('Clique para alterar', 'Cliente: <b> {id} </b>');
  64. // creates the datagrid columns
  65. $column_id = new TDataGridColumn('id', 'Id', 'right', 10);
  66. $column_nome = new TDataGridColumn('nome', 'Nome', 'left');
  67. $column_pessoa_tp_id = new TDataGridColumn('pessoa_tp_id', 'Pessoa', 'left');
  68. $column_cpfcnpj = new TDataGridColumn('cpfcnpj', 'CPF/CNPJ', 'left');
  69. $column_cidades_id = new TDataGridColumn('cidades->nome', 'Cidade', 'right');
  70. $column_ativo_id = new TDataGridColumn('ativo_id', 'Ativo', 'right');
  71. // add the columns to the DataGrid
  72. $this->datagrid->addColumn($column_id);
  73. $this->datagrid->addColumn($column_nome);
  74. $this->datagrid->addColumn($column_pessoa_tp_id);
  75. $this->datagrid->addColumn($column_cpfcnpj);
  76. $this->datagrid->addColumn($column_cidades_id);
  77. $this->datagrid->addColumn($column_ativo_id);
  78. $column_ativo_id->setTransformer( function($value, $object, $row) {
  79. $class = ($value=='2') ? 'danger' : 'success';
  80. $label = ($value=='2') ? _t('No') : _t('Yes');
  81. $div = new TElement('span');
  82. $div->class="label label-{$class}";
  83. $div->style="text-shadow:none; font-size:12px; font-weight:lighter";
  84. $div->add($label);
  85. return $div;
  86. });
  87. // creates the datagrid column actions
  88. $column_id->setAction(new TAction([$this, 'onReload']), ['order' => 'id']);
  89. $column_nome->setAction(new TAction([$this, 'onReload']), ['order' => 'nome']);
  90. //Abindo de acordo com tipo de pessoa
  91. if ($pessoa_tp_id == '1')
  92. {
  93. $action1 = new TDataGridAction(['ClientesFormPJ', 'onEdit'], ['id'=>'{id}']);
  94. } else {
  95. $action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  96. }
  97. //$action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  98. $action2 = new TDataGridAction([$this, 'onDelete'], ['id'=>'{id}']);
  99. //$action1 = new TDataGridAction(['ClientesForm', 'onEdit'], ['id'=>'{id}']);
  100. $this->datagrid->addAction($action1, _t('Edit'), 'far:edit blue');
  101. $this->datagrid->addAction($action2 ,_t('Delete'), 'far:trash-alt red');
  102. //Altera o Cliente conforme tipo de pessoa
  103. //$action_servico = new TDataGridAction(['ClientesForm', 'onEdit']);
  104. $action_servico = new TDataGridAction(array($this, 'AlterarCliente'));
  105. $action_servico->setLabel('Alterar Cliente');
  106. $action_servico->setImage('fa:donate blue fa-lg');
  107. $action_servico->setField('id');
  108. $this->datagrid->addAction($action_servico);
  109. // create the datagrid model
  110. $this->datagrid->createModel();
  111. // creates the page navigation
  112. $this->pageNavigation = new TPageNavigation;
  113. $this->pageNavigation->setAction(new TAction([$this, 'onReload']));
  114. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  115. // vertical box container
  116. $container = new TVBox;
  117. $container->style = 'width: 100%';
  118. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  119. $container->add($this->form);
  120. $container->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
  121. parent::add($container);
  122. }
  123. /**
  124. * Inline record editing
  125. * @param $param Array containing:
  126. * key: object ID value
  127. * field name: object attribute to be updated
  128. * value: new attribute content
  129. */
  130. public function onInlineEdit($param)
  131. {
  132. try
  133. {
  134. // get the parameter $key
  135. $field = $param['field'];
  136. $key = $param['key'];
  137. $value = $param['value'];
  138. TTransaction::open('gestoros'); // open a transaction with database
  139. $object = new Clientes($key); // instantiates the Active Record
  140. $object->{$field} = $value;
  141. $object->store(); // update the object in the database
  142. TTransaction::close(); // close the transaction
  143. $this->onReload($param); // reload the listing
  144. new TMessage('info', "Record Updated");
  145. }
  146. catch (Exception $e) // in case of exception
  147. {
  148. new TMessage('error', $e->getMessage()); // shows the exception error message
  149. TTransaction::rollback(); // undo all pending operations
  150. }
  151. }
  152. /**
  153. * Register the filter in the session
  154. */
  155. public function onSearch()
  156. {
  157. // get the search form data
  158. $data = $this->form->getData();
  159. // clear session filters
  160. TSession::setValue(__CLASS__.'_filter_id', NULL);
  161. TSession::setValue(__CLASS__.'_filter_nome', NULL);
  162. TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', NULL);
  163. TSession::setValue(__CLASS__.'_filter_cpfcnpj', NULL);
  164. TSession::setValue(__CLASS__.'_filter_cidades_id', NULL);
  165. TSession::setValue(__CLASS__.'_filter_ativo_id', NULL);
  166. if (isset($data->id) AND ($data->id)) {
  167. $filter = new TFilter('id', '=', $data->id); // create the filter
  168. TSession::setValue(__CLASS__.'_filter_id', $filter); // stores the filter in the session
  169. }
  170. if (isset($data->nome) AND ($data->nome)) {
  171. $filter = new TFilter('nome', 'like', "%{$data->nome}%"); // create the filter
  172. TSession::setValue(__CLASS__.'_filter_nome', $filter); // stores the filter in the session
  173. }
  174. if (isset($data->pessoa_tp_id) AND ($data->pessoa_tp_id)) {
  175. $filter = new TFilter('pessoa_tp_id', '=', $data->pessoa_tp_id); // create the filter
  176. TSession::setValue(__CLASS__.'_filter_pessoa_tp_id', $filter); // stores the filter in the session
  177. }
  178. if (isset($data->cpfcnpj) AND ($data->cpfcnpj)) {
  179. $filter = new TFilter('cpfcnpj', 'like', "%{$data->cpfcnpj}%"); // create the filter
  180. TSession::setValue(__CLASS__.'_filter_cpfcnpj', $filter); // stores the filter in the session
  181. }
  182. if (isset($data->cidades_id) AND ($data->cidades_id)) {
  183. $filter = new TFilter('cidades_id', '=', $data->cidades_id); // create the filter
  184. TSession::setValue(__CLASS__.'_filter_cidades_id', $filter); // stores the filter in the session
  185. }
  186. if (isset($data->ativo_id) AND ($data->ativo_id)) {
  187. $filter = new TFilter('ativo_id', '=', $data->ativo_id); // create the filter
  188. TSession::setValue(__CLASS__.'_filter_ativo_id', $filter); // stores the filter in the session
  189. }
  190. // fill the form with data again
  191. $this->form->setData($data);
  192. // keep the search data in the session
  193. TSession::setValue(__CLASS__ . '_filter_data', $data);
  194. $param = array();
  195. $param['offset'] =0;
  196. $param['first_page']=1;
  197. $this->onReload($param);
  198. }
  199. /**
  200. * Load the datagrid with data
  201. */
  202. public function onReload($param = NULL)
  203. {
  204. try
  205. {
  206. // open a transaction with database 'gestoros'
  207. TTransaction::open('gestoros');
  208. // creates a repository for Clientes
  209. $repository = new TRepository('Clientes');
  210. $limit = 10;
  211. // creates a criteria
  212. $criteria = new TCriteria;
  213. // default order
  214. if (empty($param['order']))
  215. {
  216. $param['order'] = 'id';
  217. $param['direction'] = 'asc';
  218. }
  219. $criteria->setProperties($param); // order, offset
  220. $criteria->setProperty('limit', $limit);
  221. //Desabilita o click do datagrid
  222. $this->datagrid->disableDefaultClick();
  223. if (TSession::getValue(__CLASS__.'_filter_id')) {
  224. $criteria->add(TSession::getValue(__CLASS__.'_filter_id')); // add the session filter
  225. }
  226. if (TSession::getValue(__CLASS__.'_filter_nome')) {
  227. $criteria->add(TSession::getValue(__CLASS__.'_filter_nome')); // add the session filter
  228. }
  229. if (TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')) {
  230. $criteria->add(TSession::getValue(__CLASS__.'_filter_pessoa_tp_id')); // add the session filter
  231. }
  232. if (TSession::getValue(__CLASS__.'_filter_cpfcnpj')) {
  233. $criteria->add(TSession::getValue(__CLASS__.'_filter_cpfcnpj')); // add the session filter
  234. }
  235. if (TSession::getValue(__CLASS__.'_filter_cidades_id')) {
  236. $criteria->add(TSession::getValue(__CLASS__.'_filter_cidades_id')); // add the session filter
  237. }
  238. if (TSession::getValue(__CLASS__.'_filter_ativo_id')) {
  239. $criteria->add(TSession::getValue(__CLASS__.'_filter_ativo_id')); // add the session filter
  240. }
  241. // load the objects according to criteria
  242. $objects = $repository->load($criteria, FALSE);
  243. if (is_callable($this->transformCallback))
  244. {
  245. call_user_func($this->transformCallback, $objects, $param);
  246. }
  247. $this->datagrid->clear();
  248. if ($objects)
  249. {
  250. // iterate the collection of active records
  251. foreach ($objects as $object)
  252. {
  253. // add the object inside the datagrid
  254. $this->datagrid->addItem($object);
  255. }
  256. }
  257. // reset the criteria for record count
  258. $criteria->resetProperties();
  259. $count= $repository->count($criteria);
  260. $this->pageNavigation->setCount($count); // count of records
  261. $this->pageNavigation->setProperties($param); // order, page
  262. $this->pageNavigation->setLimit($limit); // limit
  263. // close the transaction
  264. TTransaction::close();
  265. $this->loaded = true;
  266. }
  267. catch (Exception $e)
  268. {
  269. new TMessage('error', $e->getMessage());
  270. TTransaction::rollback();
  271. }
  272. }
  273. /**
  274. * Ask before deletion
  275. */
  276. public static function onDelete($param)
  277. {
  278. // define the delete action
  279. $action = new TAction([__CLASS__, 'Delete']);
  280. $action->setParameters($param); // pass the key parameter ahead
  281. // shows a dialog to the user
  282. new TQuestion(AdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  283. }
  284. /**
  285. * Delete a record
  286. */
  287. public static function Delete($param)
  288. {
  289. try
  290. {
  291. $key=$param['key']; // get the parameter $key
  292. TTransaction::open('gestoros'); // open a transaction with database
  293. $object = new Clientes($key, FALSE); // instantiates the Active Record
  294. $object->delete(); // deletes the object from the database
  295. TTransaction::close(); // close the transaction
  296. $pos_action = new TAction([__CLASS__, 'onReload']);
  297. new TMessage('info', AdiantiCoreTranslator::translate('Record deleted'), $pos_action); // success message
  298. }
  299. catch (Exception $e) // in case of exception
  300. {
  301. new TMessage('error', $e->getMessage()); // shows the exception error message
  302. TTransaction::rollback(); // undo all pending operations
  303. }
  304. }
  305. /**
  306. * method show()
  307. * Shows the page
  308. */
  309. public function show()
  310. {
  311. // check if the datagrid is already loaded
  312. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  313. {
  314. if (func_num_args() > 0)
  315. {
  316. $this->onReload( func_get_arg(0) );
  317. }
  318. else
  319. {
  320. $this->onReload();
  321. }
  322. }
  323. parent::show();
  324. }
  325. /**
  326. * Ação antes da Edição
  327. */
  328. public function AlterarCliente($param)
  329. {
  330. try
  331. {
  332. TTransaction::open('gestoros');
  333. if (isset($param['key']))
  334. {
  335. $key = $param['key'];
  336. $cliente = new Clientes($key);
  337. if ($cliente->pessoa_tp_id == '2')
  338. {
  339. //TApplication::loadPage( 'ClientesFormPJ' , 'onEdit' , [ 'key' => $key ]);
  340. AdiantiCoreApplication::loadPage( 'ClientesFormPJ' , 'onEdit' , [ 'key' => $key ]);
  341. }
  342. else
  343. {
  344. TApplication::loadPage( 'ClientesForm' , 'onEdit' , [ 'key' => $key ]);
  345. }
  346. }
  347. TTransaction::close(); // close the transaction
  348. }
  349. catch (exception $e)
  350. {
  351. new TMessage('error', $e->getMessage());
  352. }
  353. }
  354. }
  355. ?>

</your>