Informações de Contato não são carregadas pelo metodo onEdit Olá, Estou tentado carregar dados de contato, porém sem sucesso. se alguém puder me ajudar agradeço. Segue fonte abaixo: Pessoa ...
RB
Informações de Contato não são carregadas pelo metodo onEdit  
Olá,

Estou tentado carregar dados de contato, porém sem sucesso.

se alguém puder me ajudar agradeço.

Segue fonte abaixo:

Pessoa
 
  1. <?php
  2. /**
  3. * Pessoa Active Record
  4. * @author <your-name-here>
  5. */
  6. class Pessoa extends TRecord
  7. {
  8. const TABLENAME = 'pessoa';
  9. const PRIMARYKEY= 'id';
  10. const IDPOLICY = 'serial'; // {max, serial}
  11. private $contato;
  12. private $contatoweb;
  13. private $endereco;
  14. /**
  15. * Constructor method
  16. */
  17. public function __construct($id = NULL, $callObjectLoad = TRUE)
  18. {
  19. parent::__construct($id, $callObjectLoad);
  20. parent::addAttribute('nome');
  21. parent::addAttribute('data_nascimento');
  22. parent::addAttribute('organizacao_id');
  23. parent::addAttribute('criacao');
  24. parent::addAttribute('atualizacao');
  25. parent::addAttribute('ativo');
  26. parent::addAttribute('tipo_pessoa_id');
  27. }
  28. /**
  29. * Method addContato
  30. * Add a Contato to the Pessoa
  31. * @param $object Instance of Contato
  32. */
  33. public function addContato(Contato $object)
  34. {
  35. $this->contato[] = $object;
  36. }
  37. /**
  38. * Method getcontato
  39. * Return the Pessoa' Contato's
  40. * @return Collection of Contato
  41. */
  42. public function getContato()
  43. {
  44. return $this->contato;
  45. }
  46. /**
  47. * Method addContatoWeb
  48. * Add a ContatoWeb to the Pessoa
  49. * @param $object Instance of ContatoWeb
  50. */
  51. public function addContatoWeb(ContatoWeb $object)
  52. {
  53. $this->contatoweb[] = $object;
  54. }
  55. /**
  56. * Method getContatoWebs
  57. * Return the Pessoa' ContatoWeb's
  58. * @return Collection of ContatoWeb
  59. */
  60. public function getContatoWeb()
  61. {
  62. return $this->contatoweb;
  63. }
  64. /**
  65. * Method addEndereco
  66. * Add a Endereco to the Pessoa
  67. * @param $object Instance of Endereco
  68. */
  69. public function addEndereco(Endereco $object)
  70. {
  71. $this->endereco[] = $object;
  72. }
  73. /**
  74. * Method getEnderecos
  75. * Return the Pessoa' Endereco's
  76. * @return Collection of Endereco
  77. */
  78. public function getEndereco()
  79. {
  80. return $this->endereco;
  81. }
  82. /**
  83. * Reset aggregates
  84. */
  85. public function clearParts()
  86. {
  87. $this->contato = array();
  88. $this->contatoweb = array();
  89. $this->endereco = array();
  90. }
  91. /**
  92. * Load the object and its aggregates
  93. * @param $id object ID
  94. */
  95. public function load($id)
  96. {
  97. $this->contato = parent::loadComposite('Contato', 'pessoa_id', $id);
  98. $this->contatoweb = parent::loadComposite('ContatoWeb', 'pessoa_id', $id);
  99. $this->endereco = parent::loadAggregate('Endereco', 'PessoaEndereco', 'pessoa_id', 'endereco_id', $id);
  100. // load the object itself
  101. return parent::load($id);
  102. }
  103. /**
  104. * Store the object and its aggregates
  105. */
  106. public function store()
  107. {
  108. // store the object itself
  109. parent::store();
  110. parent::saveComposite('Contato', 'pessoa_id', $this->id, $this->contato);
  111. parent::saveComposite('ContatoWeb', 'pessoa_id', $this->id, $this->contatoweb);
  112. parent::saveAggregate('PessoaEndereco', 'pessoa_id', 'endereco_id', $this->id, $this->endereco);
  113. }
  114. /**
  115. * Delete the object and its aggregates
  116. * @param $id object ID
  117. */
  118. public function delete($id = NULL)
  119. {
  120. $id = isset($id) ? $id : $this->id;
  121. parent::deleteComposite('Contato', 'pessoa_id', $id);
  122. parent::deleteComposite('ContatoWeb', 'pessoa_id', $id);
  123. parent::deleteComposite('PessoaEndereco', 'pessoa_id', $id);
  124. // delete the object itself
  125. parent::delete($id);
  126. }
  127. }
  128. ?>



Grid
 
  1. <?php
  2. /**
  3. * DatagridQuickView
  4. */
  5. class DatagridCheckView extends TPage
  6. {
  7. private $datagrid;
  8. private $pageNavigation;
  9. private $loaded;
  10. private $form;
  11. private $editar;
  12. private $order_nome;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->form = new TQuickForm('form_cliente');
  17. $check_gender = new TCheckGroup('check_gender');
  18. $check_gender->addItems( array('F'=>'Fisico', 'J'=>'Juridico') );
  19. $check_gender->setLayout('horizontal');
  20. $this->form->addQuickField('', $check_gender);
  21. $this->datagrid = new TDataGrid;
  22. //$this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  23. $this->datagrid->style="width: 100%"; // Responsive
  24. $id = new TDataGridColumn('id','Cód.','left');
  25. $nome = new TDataGridColumn('pessoa->nome','Nome','left');
  26. $genero = new TDataGridColumn('fisica->genero_id','Sexo','');
  27. $data_nascimento = new TDataGridColumn('pessoa->data_nascimento','Data Nascto','left');
  28. $civil = new TDataGridColumn('fisica->civil->descricao','Estado Cívil','left');
  29. $etnia = new TDataGridColumn('fisica->etnia->descricao','Etnia','left');
  30. $cpf = new TDataGridColumn('fisica->cpf','CPF','left');
  31. $rg = new TDataGridColumn('fisica->rg','RG','left');
  32. $telefone = new TDataGridColumn('contato->telefone','Telefone','left');
  33. $email = new TDataGridColumn('contatoweb->email','E-mail','left');
  34. $site = new TDataGridColumn('contatoweb->site','Site','left');
  35. $editar = new TDataGridColumn('editar','','left');
  36. $deletar = new TDataGridColumn('deletar','','left');
  37. $this->datagrid->addColumn($id);
  38. $this->datagrid->addColumn($nome);
  39. $this->datagrid->addColumn($genero);
  40. $this->datagrid->addColumn($data_nascimento);
  41. $this->datagrid->addColumn($civil);
  42. $this->datagrid->addColumn($etnia);
  43. $this->datagrid->addColumn($cpf);
  44. $this->datagrid->addColumn($rg);
  45. $this->datagrid->addColumn($telefone);
  46. //$this->datagrid->addColumn($cep);
  47. //$this->datagrid->addColumn($logradouro);
  48. //$this->datagrid->addColumn($bairro);
  49. //$this->datagrid->addColumn($cidade);
  50. //$this->datagrid->addColumn($uf);
  51. $this->datagrid->addColumn($email);
  52. $this->datagrid->addColumn($site);
  53. $this->datagrid->addColumn($editar);
  54. $this->datagrid->addColumn($deletar);
  55. // define the transformer method over image
  56. $data_nascimento->setTransformer( function($value, $object, $row) {
  57. $date = new DateTime($value);
  58. return $date->format('d/m/Y');
  59. });
  60. $genero->setTransformer( function($value, $object, $row) {
  61. switch ($value) {
  62. case '1':
  63. return 'Masculino';
  64. break;
  65. case '2':
  66. return 'Feminino';
  67. break;
  68. }
  69. });
  70. $email->setTransformer( function($value, $object, $row) {
  71. switch ($value) {
  72. case !empty($value):
  73. return $value;
  74. break;
  75. default:
  76. return '---';
  77. break;
  78. }
  79. });
  80. // creates the datagrid actions
  81. $order1 = new TAction(array($this, 'onReload'));
  82. $order2 = new TAction(array($this, 'onReload'));
  83. $this->order_nome = $nome;
  84. // define the ordering parameters
  85. $order1->setParameter('order', 'id');
  86. $order2->setParameter('order', $this->order_nome);
  87. // assign the ordering actions
  88. $id->setAction($order1);
  89. $nome->setAction($order2);
  90. $bt1a = new TButton('novo');
  91. $bt1b = new TButton('refresh');
  92. $bt1a->setAction(new TAction(array('ClienteFisicoBuilder','onClear')));
  93. $bt1b->setAction(new TAction(array($this, 'onReload')), 'refresh');
  94. $bt1a->setLabel('Novo');
  95. $bt1b->setLabel('atualizar');
  96. $bt1a->setImage('ico_edit.png');
  97. $bt1b->setImage('bs:edit green');
  98. $hbox1 = new THBox;
  99. $hbox1->addRowSet( $bt1a, $bt1b );
  100. $frame1 = new TFrame;
  101. $frame1->style='border:none;';
  102. //$frame1->setLegend('Bootstrap Glyphicons');
  103. $frame1->add($hbox1);
  104. //$this->form->addField($btn_novo);
  105. // creates the datagrid model
  106. $this->datagrid->createModel();
  107. // creates the page navigation
  108. $this->pageNavigation = new TPageNavigation;
  109. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  110. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  111. // wrap the page content using vertical box
  112. $vbox = new TVBox;
  113. $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  114. $vbox->add($this->form);
  115. $vbox->add($this->datagrid);
  116. $vbox->add($frame1);
  117. $this->form->setFields(array($bt1a,$bt1b));
  118. $vbox->add($this->pageNavigation);
  119. parent::add($vbox);
  120. }
  121. /**
  122. * Load the data into the datagrid
  123. */
  124. public function onReload($param = NULL)
  125. {
  126. $this->datagrid->clear();
  127. try{
  128. //abre a transação com a base
  129. TTransaction::open('centinel');
  130. //cria um repositório para carregar 'Cliente '
  131. $cliente = new TRepository('Cliente');
  132. $limit = 10;
  133. //cria um critério para filtrar os dados conforme usuário logado
  134. $criteria = new TCriteria;
  135. // default order
  136. if (empty($param['order']))
  137. {
  138. $param['order'] = 'id';
  139. $param['direction'] = 'desc';
  140. }
  141. $get_session = TSession::getValue('organizacion_id');//pega id da empresa na seção do usuário
  142. $criteria->setProperties($param); // order, offset
  143. $criteria->add(new TFilter('organizacao_id', '=', $get_session));
  144. $criteria->add(new TFilter('ativo', '=', 1));
  145. $criteria->add(new TFilter('tipo_pessoa_id','=',1));
  146. $criteria->setProperty('limit',$limit);
  147. $objects = $cliente->load($criteria);
  148. if($objects)
  149. {
  150. foreach($objects as $object)
  151. {
  152. //cria os botões de ação e adiciona a datagrid
  153. $edit = new TImage('fa:edit blue');
  154. $action = new TAction(array('ClienteFisicoBuilder', 'onEdit'));
  155. $action->setParameter('key',$object->id);
  156. $object->editar = $edit;
  157. $a = new TActionLink($edit, $action);
  158. $del = new TImage('fa:trash red');
  159. $action1 = new TAction(array($this, 'onDelete'));
  160. $action1->setParameter('key',$object->id);
  161. $object->deletar = $del;
  162. $b = new TActionLink($del, $action1);
  163. $object->editar = $a;
  164. $object->deletar = $b;
  165. $this->datagrid->addItem($object);
  166. }
  167. }
  168. // reset the criteria for record count
  169. $criteria->resetProperties();
  170. $count = $cliente->count($criteria);
  171. $this->pageNavigation->setCount($count); // count of records
  172. $this->pageNavigation->setProperties($param); // order, page
  173. $this->pageNavigation->setLimit($limit); // limit
  174. TTransaction::close();
  175. $this->loaded = TRUE;
  176. }
  177. catch (Exception $e)
  178. {
  179. new TMessage('error',$e->getMessage());
  180. TTransaction::rollback();
  181. }
  182. }
  183. /**
  184. * shows the page
  185. */
  186. public function show(){
  187. // check if the datagrid is already loaded
  188. if (!$this->loaded){
  189. $this->onReload( func_get_arg(0) );
  190. }
  191. parent::show();
  192. }
  193. function onDelete($param)
  194. {
  195. // define the next action
  196. $action3 = new TAction(array($this, 'onInative'));
  197. $action3->setParameters($param); // pass 'key' parameter ahead
  198. // shows a dialog to the user
  199. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'),$action3);
  200. }
  201. function onInative($param)
  202. {
  203. try
  204. {
  205. TTransaction::open('centinel');
  206. TTransaction::setLoggerFunction(function($param)
  207. {
  208. print $param.'<br/>';
  209. });
  210. $organizacao_id = TSession::getValue('organizacion_id');
  211. $atualizacao = date('Y-m-d H:m:s');
  212. $key = $param['key'];
  213. $criteria = new TCriteria;
  214. $criteria->add(new TFilter('id', '=', $key));
  215. $criteria->add(new TFilter('organizacao_id', '=', $organizacao_id));
  216. $repository = new TRepository('Cliente');
  217. $clientes = $repository->load($criteria);
  218. foreach ($clientes as $cliente)
  219. {
  220. $cliente->ativo = 0;
  221. $cliente->atualizacao = $atualizacao;
  222. $cliente->store();
  223. }
  224. TTransaction::close();
  225. new TMessage('info',TAdiantiCoreTranslator::translate('Records deleted'), new TAction(array($this, 'onReload')));
  226. }
  227. catch (Exception $e)
  228. {
  229. new TMessage('error', $e->getMessage());
  230. }
  231. }
  232. }
  233. ?>



formulário
 
  1. <?php
  2. class ClienteFisicoBuilder extends TPage
  3. {
  4. protected $form;
  5. protected $table_contato;
  6. protected $frame_contato;
  7. protected $deta_row;
  8. protected $deta_row_1;
  9. protected $table_endereco;
  10. protected $frame_endereco;
  11. protected $cep;
  12. public function __construct($param){
  13. parent::__construct($param);
  14. parent::include_css('app/resources/builder.css');
  15. $this->form = new BootstrapFormBuilder('frm_cliente_fisico');
  16. $this->form->setFormTitle('Cliente');
  17. $this->form->class='frm_cliente_fisico';
  18. $dados_cliente = new TLabel('Dados Pessoais');
  19. $dados_cliente->class='lbl_titulo';
  20. $this->form->addContent( [$dados_cliente] );
  21. $lbl_id = new TLabel('#');
  22. $id = new TEntry('id');
  23. $id->setEditable(false);
  24. $label_nome = new TLabel('Nome');
  25. $nome = new TEntry('nome');
  26. $nome->addValidation('Nome', new TRequiredValidator);
  27. $lbl_data_nascto = new TLabel('Data Nascimento');
  28. $data_nascimento = new TDate('data_nascimento');
  29. $data_nascimento->addValidation('Data Nascimento', new TRequiredValidator);
  30. $data_nascimento->setMask('dd/mm/yyyy'); // define date mask
  31. $lbl_estado_civil = new TLabel('Estado Cívil');
  32. $estado_civil = new TDBCombo('estado_civil_id','centinel', 'Civil', 'id', 'descricao');
  33. $estado_civil->setDefaultOption(false);
  34. $lbl_genero = new TLabel('Genero');
  35. $items = array();
  36. $items['1'] ='Masculino';
  37. $items['2'] ='Feminino';
  38. $genero_id = new TCombo('genero_id');
  39. $genero_id->addItems($items);
  40. $genero_id->setDefaultOption(false);
  41. $lbl_id_cep = new TLabel('lbl_id_cep');
  42. $lbl_cpf = new TLabel('CPF');
  43. $cpf = new TEntry('cpf');
  44. $cpf->setMask('000.000.000.00');
  45. $cpf->addValidation('CPF', new TCPFValidator, new TMaxValueValidator,array(14));
  46. $lbl_rg = new TLabel('RG');
  47. $rg = new TEntry('rg');
  48. $rg->setMask('00.000.000.00');
  49. $rg->setMaxLength(12);
  50. /*
  51. $lbl_cnh = new TLabel('CNH','#1F4E78');
  52. $cnh = new TEntry('cnh');
  53. $cnh->setMask('00.000.000.00');
  54. $cnh->setMaxLength(12);
  55. $lbl_cnh->style='text-align:left;float:left;';
  56. */
  57. $lbl_etnia = new TLabel('Etnia');
  58. $etnia_id = new TDBCombo('etnia_id','centinel','Etnia','id','descricao');
  59. $etnia_id->setDefaultOption(false);
  60. //adiciona campos ao formulário
  61. $this->form->addFields( [$lbl_id,$label_nome,$lbl_data_nascto,$lbl_estado_civil] );
  62. $this->form->addFields( [$id,$nome,$data_nascimento,$estado_civil] );
  63. $this->form->addFields( [$lbl_genero,$lbl_etnia,$lbl_cpf,$lbl_rg] );
  64. $this->form->addFields( [$genero_id,$etnia_id,$cpf,$rg] );
  65. //cria dados de contato
  66. $contato = new TLabel('Contato');
  67. $contato->class='lbl_titulo';
  68. $lbl_ddi = new TLabel('DDI');
  69. $lbl_dd = new TLabel('DDD');
  70. $lbl_tipo_telefone = new TLabel('Tipo Telefone');
  71. $lbl_telefone = new TLabel('Telefone');
  72. $lbl_operadora = new TLabel('Operadora');
  73. $lbl_email = new TLabel('Email');
  74. $email = new TEntry('email');
  75. //$email->addValidation('email', new TEmailValidator); // email field
  76. $lbl_site = new TLabel('Site');
  77. $site = new TEntry('site');
  78. //Cria tabela contato adiciona campos
  79. $this->table_contato = new TTable('table_contato');
  80. //$this->table_contato->style='font-family:Arial Narrow;';
  81. $this->table_contato->width='100%';
  82. $this->table_contato->addSection('thead');
  83. $this->frame_contato = new TFrame;
  84. $this->frame_contato->style='border:none;float:lef; margin-left:-15px;';
  85. //$frame_contato->setLegend('Contato');
  86. $this->frame_contato->add($contato);
  87. $this->frame_contato->add($this->table_contato);
  88. $this->table_contato->addRowSet([$lbl_ddi,$lbl_dd,$lbl_tipo_telefone,$lbl_telefone,$lbl_operadora]);
  89. $this->form->addContent([$this->frame_contato]);
  90. //adiciona contato web
  91. $this->form->addFields( [$lbl_email,$lbl_site] );
  92. $this->form->addFields( [$email,$site] );
  93. //cria dados de correspondência
  94. $lbl_endereco = new TLabel('Endereco');
  95. $lbl_endereco->class='lbl_titulo';
  96. $lbl_cep = new TLabel('Cep');
  97. $lbl_id_cep = new TLabel('#');
  98. $lbl_logradouro = new TLabel('Logradouro');
  99. $lbl_bairro = new TLabel('Bairro');
  100. $lbl_cidade = new TLabel('Cidade');
  101. $lbl_uf = new TLabel('UF');
  102. $lbl_tipo_endereco_id = new TLabel('Tipo Endereco');
  103. $lbl_numero = new TLabel('Número');
  104. $lbl_complemento = new TLabel('Complemento');
  105. $this->detail_row = 0;
  106. $this->detail_row_1 = 0;
  107. $this->table_endereco = new TTable();
  108. $this->table_endereco->width='100%';
  109. $this->table_endereco->addSection('thead');
  110. $this->frame_endereco = new TFrame;
  111. $this->frame_endereco->style='border:none;float:lef; margin-left:-15px;';
  112. $this->frame_endereco->add($lbl_endereco);
  113. $this->frame_endereco->add($this->table_endereco);
  114. $this->table_endereco->addRowSet([$lbl_id_cep,$lbl_cep,$lbl_logradouro,$lbl_bairro,$lbl_cidade,$lbl_uf,$lbl_tipo_endereco_id,$lbl_numero,$lbl_complemento]);
  115. $this->form->addContent([$this->frame_endereco]);
  116. $lbl_id->class='lbl_text';
  117. $label_nome->class='lbl_text';
  118. $lbl_data_nascto->class='lbl_text';
  119. $lbl_estado_civil->class='lbl_text';
  120. $lbl_genero->class='lbl_text';
  121. $lbl_cpf->class='lbl_text';
  122. $lbl_rg->class='lbl_text';
  123. $lbl_etnia->class='lbl_text';
  124. $lbl_ddi->class='lbl_text';
  125. $lbl_dd->class='lbl_text';
  126. $lbl_tipo_telefone->class='lbl_text';
  127. $lbl_telefone->class='lbl_text';
  128. $lbl_operadora->class='lbl_text';
  129. $lbl_email->class='lbl_text';
  130. $lbl_site->class='lbl_text';
  131. $lbl_cep->class='lbl_text';
  132. $lbl_id_cep->class='lbl_text';
  133. $lbl_logradouro->class='lbl_text';
  134. $lbl_bairro->class='lbl_text';
  135. $lbl_bairro->class='lbl_text';
  136. $lbl_cidade->class='lbl_text';
  137. $lbl_uf->class='lbl_text';
  138. $lbl_numero->class='lbl_text';
  139. $lbl_tipo_endereco_id->class='lbl_text';
  140. $lbl_complemento->class='lbl_text';
  141. $id->class='tentry';
  142. $lbl_id->setSize('10%');
  143. $label_nome->setSize('30%');
  144. $lbl_data_nascto->setSize('20%');
  145. $lbl_estado_civil->setSize('20%');
  146. $lbl_genero->setSize('20%');
  147. $lbl_cpf->setSize('20%');
  148. $lbl_rg->setSize('20%');
  149. $lbl_etnia->setSize('20%');
  150. $lbl_ddi->setSize('40');
  151. $lbl_dd->setSize('40');
  152. $lbl_tipo_telefone->setSize('100');
  153. $lbl_telefone->setSize('100');
  154. $lbl_email->setSize('42%');
  155. $lbl_site->setSize('42%');
  156. $lbl_id_cep->setSize('30');
  157. $lbl_cep->setSize('70');
  158. $lbl_logradouro->setSize('150');
  159. $lbl_bairro->setSize('150');
  160. $lbl_cidade->setSize('150');
  161. $lbl_uf->setSize('40');
  162. $lbl_numero->setSize('70');
  163. $lbl_complemento->setSize('150');
  164. $lbl_tipo_endereco_id->setSize('100');
  165. $id->setSize('10%');
  166. $nome->setSize('30%');
  167. $data_nascimento->setSize('20%');
  168. $estado_civil->setSize('20%');
  169. $genero_id->setSize('20%');
  170. $cpf->setSize('20%');
  171. $rg->setSize('20%');
  172. //$lbl_cnh->setSize('19%');
  173. //$cnh->setSize('19%');
  174. $etnia_id->setSize('20%');
  175. $email->setSize('42%');
  176. $site->setSize('42%');
  177. $table_endereco = new TTable;
  178. $table_endereco->style='border 1px solid #333;';
  179. $table_endereco->width='100%';
  180. $table_endereco->addSection('thead');
  181. $table_endereco->addSection('tbody');
  182. $this->form->addAction('Salvar', new TAction(array($this, 'onSave')), 'ico_save.png');
  183. $this->form->addAction('Listar', new TAction(array('DatagridCheckView', 'onReload')), 'fa:table blue');
  184. //wrap the page content using vertical box
  185. $vbox = new TVBox;
  186. $vbox->add(new TXMLBreadCrumb('menu.xml', 'DatagridCheckView'));
  187. $vbox->add($this->form);
  188. parent::add($vbox);
  189. }
  190. public static function onSave($param)
  191. {
  192. $organizacao_id = TSession::getValue('organizacion_id'); // pega id da empresa
  193. $atualizacao = date('Y-m-d H:m:s');
  194. try
  195. {
  196. TTransaction::open('centinel'); // open a transaction
  197. TTransaction::setLoggerFunction(function($param)
  198. {
  199. print $param.'<br/>';
  200. });
  201. $data_actual = date('Y-m-d H:i:s');
  202. $pessoa = new Pessoa;
  203. $pessoa->nome = $param['nome'];
  204. $pessoa->data_nascimento = TDate::date2us($param['data_nascimento']);
  205. $pessoa->organizacao_id = $organizacao_id;
  206. $pessoa->tipo_pessoa_id = 1;
  207. $pessoa->criacao = $data_actual;
  208. $pessoa->atualizacao = $data_actual;
  209. $pessoa->store(); // save the object
  210. $pessoa_organizacao = new PessoaOrganizacao;
  211. $pessoa_organizacao->pessoa_id = $pessoa->id;
  212. $pessoa_organizacao->organizacao_id = $organizacao_id;
  213. $pessoa_organizacao->store();
  214. $cliente = new Cliente;
  215. $cliente->pessoa_id = $pessoa->id;
  216. $cliente->organizacao_id = $organizacao_id;
  217. $cliente->ativo = 1 ;
  218. $cliente->criacao = $data_actual;
  219. $cliente->atualizacao = $data_actual;
  220. $cliente->tipo_pessoa_id = 1;
  221. $cliente->store();
  222. if($cliente)
  223. {
  224. $fisica = new Fisica;
  225. $fisica->pessoa_id = $pessoa->id;
  226. $fisica->cpf = str_replace(".", "", $param['cpf']);
  227. $fisica->rg = str_replace(".", "", $param['rg']);
  228. $fisica->genero_id = $param['genero_id'];
  229. $fisica->civil_id = $param['estado_civil_id'];
  230. //$fisica->organizacao_id = $organizacao_id;
  231. $fisica->etnia_id = $param['etnia_id'];
  232. $fisica->store();
  233. }
  234. if($fisica)
  235. {
  236. if(!empty($param['ddi']) And is_array($param['telefone']))
  237. {
  238. foreach($param['telefone'] as $row =>$contatos)
  239. {
  240. if (!empty($contatos))
  241. {
  242. //Adiciona contato
  243. $contato = new Contato;
  244. $contato->pessoa_id = $fisica->pessoa_id;
  245. $contato->ddi = $param['ddi'][$row];
  246. $contato->ddd = $param['dd'][$row];
  247. $contato->telefone = $param['telefone'][$row];
  248. $contato->tipo_telefone_id = $param['tipo_telefone_id'][$row];
  249. $contato->operadora_id = $param['operadora_id'][$row];
  250. $contato->store();
  251. }
  252. else{
  253. //Adiciona contato
  254. $contato = new Contato;
  255. $contato->pessoa_id = $fisica->pessoa_id;
  256. $contato->ddi = '0';
  257. $contato->ddd = '0';
  258. $contato->telefone = '0';
  259. $contato->store();
  260. }
  261. }
  262. }
  263. }
  264. //adiciona contato web
  265. if(!empty($param['email']) or $param['site'])
  266. {
  267. $contato_web = new ContatoWeb;
  268. $contato_web->pessoa_id = $pessoa->id;
  269. $contato_web->email = $param['email'];
  270. $contato_web->site = $param['site'];
  271. $contato_web->store();
  272. }
  273. else{
  274. $contato_web = new ContatoWeb;
  275. $contato_web->pessoa_id = $pessoa->id;
  276. $contato_web->email = '0';
  277. $contato_web->site = '0';
  278. $contato_web->store();
  279. }
  280. if(!empty($param['id_endereco']) And is_array($param['id_endereco']))
  281. {
  282. // show form values inside a window
  283. //$win = TWindow::create('Object id', 0.2, 0.3);
  284. foreach($param['id_endereco'] as $row =>$enderecos)
  285. {
  286. if (!empty($enderecos))
  287. {
  288. //$win->add('<pre>'.str_replace("\n", '<br/>', print_r($enderecos['id'], true) ).'</pre>' );
  289. $pessoa_endereco = new PessoaEndereco;
  290. $pessoa_endereco->pessoa_id = $pessoa->id;
  291. $pessoa_endereco->endereco_id = $param['id_endereco'][$row];
  292. $pessoa_endereco->tipo_endereco_id = $param['tipo_endereco_id'][$row];
  293. $pessoa_endereco->numero = $param['numero'][$row];
  294. $pessoa_endereco->complemento = $param['compl'][$row];
  295. $pessoa_endereco->criacao = $data_actual;
  296. $pessoa_endereco->atualizacao = $data_actual;
  297. $pessoa_endereco->store();
  298. }
  299. }
  300. //$win->show();
  301. }
  302. else{
  303. }
  304. $data = new stdClass;
  305. $data->id = $cliente->id;
  306. TForm::sendData('frm_cliente_fisico', $data);
  307. TTransaction::close();// close the transaction
  308. }
  309. catch (Exception $e) // in case of exception
  310. {
  311. new TMessage('error', $e->getMessage());
  312. TForm::sendData('frm_cliente_fisico', $param);// keep form data
  313. TTransaction::rollback();
  314. }
  315. }
  316. //method onCep()
  317. public static function onCep($param)
  318. {
  319. $endereco_cep = $param['_field_id'];
  320. $cep_endereco = $param['_field_value'];
  321. $endereco_cep = explode('_', $endereco_cep);
  322. $unique_id = end($endereco_cep);
  323. $retorno = Endereco::BuscaCepWeb($cep_endereco);
  324. if (!empty($retorno))
  325. {
  326. $response = new stdClass;
  327. $response->{'id_endereco_'.$unique_id} = $retorno['id'];
  328. $response->{'logradouro_'.$unique_id} = $retorno['logradouro'];
  329. $response->{'bairro_'.$unique_id} = $retorno['bairro'];
  330. $response->{'cidade_'.$unique_id} = $retorno['localidade'];
  331. $response->{'uf_'.$unique_id} = $retorno['uf'];
  332. //$response->ibge = strtoupper( $retorno['ibge']);
  333. //$response->gia = strtoupper( $retorno['gia']);
  334. TForm::sendData('frm_cliente_fisico', $response);
  335. TScript::create('$("input[name=numero]").focus();');
  336. }
  337. else
  338. {
  339. $endereco = Endereco::BuscaCepBanco($cep_endereco);
  340. if($endereco)
  341. {
  342. $retorno = new stdClass;
  343. $retorno->{'id_endereco_'.$unique_id} = $endereco['id'];
  344. $retorno->{'logradouro_'.$unique_id} = $endereco['logradouro'];
  345. $retorno->{'bairro_'.$unique_id} = $endereco['bairro'];
  346. $retorno->{'cidade_'.$unique_id} = $endereco['cidade'];
  347. $retorno->{'uf_'.$unique_id} = $endereco['uf'];
  348. TForm::sendData('frm_cliente_fisico', $retorno);
  349. TScript::create('$("input[name=numero]").focus();');
  350. }
  351. else
  352. {
  353. new TMessage("error", "CEP - Em Branco ou Inválido");
  354. }
  355. }
  356. }
  357. public function addContatoRow($param)
  358. {
  359. $uniqid = mt_rand(1000000, 9999999);
  360. $ddi = new TEntry('ddi[]');
  361. $ddi->setValue('55');
  362. $ddi->addValidation('DDI', new TMaxValueValidator,array(2));
  363. $dd = new TEntry('dd[]');
  364. $dd->setValue('011');
  365. $dd->addValidation('DDD', new TMaxValueValidator,array(3));
  366. $tipo_telefone_id = new TDBCombo('tipo_telefone_id[]','centinel','TipoTelefone','id','descricao');
  367. $tipo_telefone_id->setDefaultOption(false);
  368. $telefone = new TEntry('telefone[]');
  369. $telefone->setMask('9999-9999');
  370. $operadora_id = new TDBCombo('operadora_id[]','centinel','Operadora','id','nome');
  371. $operadora_id->setDefaultOption(false);
  372. //set ids
  373. $ddi->setId('ddi_'.$uniqid);
  374. $dd->setId('dd_'.$uniqid);
  375. $telefone->setId('telefone_'.$uniqid);
  376. $tipo_telefone_id->setId('tipo_telefone_id_'.$uniqid);
  377. $operadora_id->setId('operadora_id_'.$uniqid);
  378. // create delete button
  379. $del = new TImage('fa:trash-o blue');
  380. $del->onclick = 'ttable_remove_row(this)';
  381. $row = $this->table_contato->addRowSet([$ddi,$dd,$tipo_telefone_id,$telefone,$operadora_id,$del]);
  382. $this->detail_row ++;
  383. $ddi->setSize('40');
  384. $dd->setSize('40');
  385. $tipo_telefone_id->setSize('100');
  386. $telefone->setSize('100');
  387. $operadora_id ->setSize('100');
  388. }
  389. public function addEnderecoRow($param)
  390. {
  391. $uniqid = mt_rand(1000000, 9999999);
  392. /*
  393. $btn_cep = new TButton('btn_cep');
  394. $btn_cep->setSize(42);
  395. $btn_cep->setAction(new TAction(array($this,'onCep')),'');
  396. $btn_cep->setImage('fa:search blue');
  397. $this->form->addField($btn_cep);
  398. */
  399. $this->cep = new TEntry('cep[]');
  400. $this->cep->setMask('00000-000');
  401. $this->cep->addValidation('CEP', new TRequiredValidator, new TMaxValueValidator,array(8));
  402. $this->form->addField($this->cep);
  403. $input_cep = new TAction(array($this,'onCep'));
  404. $this->cep->setExitAction($input_cep);
  405. $id_endereco = new TEntry('id_endereco[]');
  406. $id_endereco->setEditable(false);
  407. $logradouro = new TEntry('logradouro[]');
  408. $bairro = new TEntry('bairro[]');
  409. $cidade = new TEntry('cidade[]');
  410. $uf = new TEntry('uf[]');
  411. $numero = new TEntry('numero[]');
  412. $tipo_endereco_id = new TDBCombo('tipo_endereco_id[]','centinel','TipoEndereco','id','descricao');
  413. $tipo_endereco_id->setDefaultOption(false);
  414. $complemento = new TEntry('compl[]');
  415. // set row counter
  416. $this->cep->{'data-row_1'} = $this->detail_row_1;
  417. $id_endereco->{'data-row_1'} = $this->detail_row_1;
  418. $logradouro->{'data-row_1'} = $this->detail_row_1;
  419. $bairro->{'data-row_1'} = $this->detail_row_1;
  420. $cidade->{'data-row_1'} = $this->detail_row_1;
  421. $numero->{'data-row_1'} = $this->detail_row_1;
  422. $uf->{'data-row_1'} = $this->detail_row_1;
  423. $tipo_endereco_id->{'data-row_1'} = $this->detail_row_1;
  424. $complemento->{'data-row_1'} = $this->detail_row_1;
  425. //set ids
  426. //$btn_cep ->setId('btn_cep)'.$uniqid);
  427. $this->cep ->setId('cep_'.$uniqid);
  428. $id_endereco ->setId('id_endereco_'.$uniqid);
  429. $logradouro ->setId('logradouro_'.$uniqid);
  430. $bairro ->setId('bairro_'.$uniqid);
  431. $cidade ->setId('cidade_'.$uniqid);
  432. $uf ->setId('uf_'.$uniqid);
  433. $numero ->setId('numero_'.$uniqid);
  434. $tipo_endereco_id->setId('tipo_endereco_id_'.$uniqid);
  435. $complemento->setId('compl_'.$uniqid);
  436. // create delete button
  437. $del_endereco = new TImage('fa:trash-o blue');
  438. $del_endereco->onclick = 'ttable_remove_row(this)';
  439. $row_end = $this->table_endereco->addRowSet([$id_endereco,$this->cep,$logradouro,$bairro,$cidade,$uf,$tipo_endereco_id,$numero,$complemento,$del_endereco]);
  440. $row_end->{'data-row'} = $this->detail_row_1;
  441. $id_endereco->setSize('30');
  442. $this->cep->setSize('70');
  443. $logradouro->setSize('150');
  444. $bairro->setSize('150');
  445. $cidade->setSize('150');
  446. $uf->setSize('40');
  447. $numero->setSize('70');
  448. $complemento->setSize('150');
  449. $tipo_endereco_id->setSize('100');
  450. $this->detail_row_1 ++;
  451. }
  452. public function onClear($param)
  453. {
  454. $this->table_contato->addSection('tbody');
  455. $this->addContatoRow(new stdClass);
  456. //criar e adicionar botão
  457. $add = new TButton('clone');
  458. $add->setLabel('Adicionar');
  459. $add->setImage('fa:plus-circle blue');
  460. $add->addFunction('ttable_clone_previous_row(this)');
  461. //adiciona botão a tabela
  462. $row = $this->table_contato->addRowSet([$add]);
  463. //adciona tabela endereco
  464. $this->table_endereco->addSection('tbody');
  465. $this->addEnderecoRow(new stdClass);
  466. //criar e adicionar botão
  467. $add_endereco = new TButton('clone');
  468. $add_endereco->setLabel('Adicionar');
  469. $add_endereco->setImage('fa:plus-circle blue');
  470. $add_endereco->addFunction('ttable_clone_previous_row(this)');
  471. //adiciona botão a tabela
  472. $row_end = $this->table_endereco->addRowSet([$add_endereco]);
  473. }
  474. function onEdit($param){
  475. try{
  476. if (isset($param['key']))
  477. {
  478. // get the parameter $key
  479. $key = $param['key'];
  480. // open a transaction with database 'centinel'
  481. TTransaction::open('centinel');
  482. // instantiates object Pessoa
  483. $cliente = new Cliente($key);
  484. // fill the form with the active record data
  485. $data->id = $cliente->id;
  486. $pessoa = new Pessoa($cliente->pessoa_id);
  487. print_r($pessoa->getContato());
  488. print_r($pessoa->getEndereco());
  489. $fisica = Fisica::where('pessoa_id','=',$cliente->pessoa_id)->load();
  490. $fisica_doc = $fisica[0];
  491. $this->form->setData($fisica_doc);
  492. if ($pessoa)
  493. {
  494. $this->table_contato->addSection('tbody');
  495. foreach ($pessoa->getContato() as $contato)
  496. {
  497. $this->addContatoRow($contato);
  498. }
  499. // create add button
  500. $add = new TButton('clone');
  501. $add->setLabel('Adicionar');
  502. $add->setImage('fa:plus-circle blue');
  503. $add->addFunction('ttable_clone_previous_row(this)');
  504. // add buttons in table
  505. $this->table_contato->addRowSet([$add]);
  506. }
  507. else
  508. {
  509. $this->onClear($param);
  510. }
  511. $contato_web = ContatoWeb::where('pessoa_id','=',$cliente->pessoa_id)->load();
  512. $contatoweb = $contato_web[0];
  513. $this->form->setData($contatoweb);
  514. //filtra endereco
  515. $criteria_pessoa_endereco = new TCriteria;
  516. $criteria_pessoa_endereco->add( new TFilter( 'pessoa_id', '=', $cliente->pessoa_id));
  517. $pessoa_enderecos = PessoaEndereco::getObjects($criteria_pessoa_endereco);
  518. if ($pessoa_enderecos)
  519. {
  520. $this->table_endereco->addSection('tbody');
  521. foreach ($pessoa_enderecos as $pessoa_endereco)
  522. {
  523. $this->addEnderecoRow($pessoa_endereco);
  524. }
  525. // create add button
  526. $addEnder = new TButton('addEnd');
  527. $addEnder->setLabel('Adicionar');
  528. $addEnder->setImage('fa:plus-circle blue');
  529. $addEnder->addFunction('ttable_clone_previous_row(this)');
  530. //add buttons in table
  531. $this->table_endereco->addRowSet([$addEnder]);
  532. }
  533. else
  534. {
  535. $this->onClear($param);
  536. }
  537. $this->form->setData($pessoa);
  538. $this->form->setData($data);
  539. // close the transaction
  540. TTransaction::close();
  541. }
  542. else
  543. {
  544. $this->form->clear();
  545. }
  546. }
  547. catch (Exception $e) // in case of exception
  548. {
  549. // shows the exception error message
  550. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  551. // undo all pending operations
  552. TTransaction::rollback();
  553. }
  554. }
  555. }
  556. ?>




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 (2)


NR

Veja o exemplo do tutor:
adianti.com.br/framework_files/tutor/index.php?class=SaleMultiValueF

Você deve verificar o objeto recebido como parâmetro nas funções addContatoRow e addEnderecoRow para preencher os campos. Ex:
 
  1. <?php
  2. function addContatoRow($contato)
  3. {
  4. ...
  5. $ddi = new TEntry('ddi[]');
  6. if (!empty($contato->ddi))
  7. {
  8. $ddi->setValue($contato->ddi);
  9. }
  10. ...
  11. }
  12. ?>
RB

Nataniel,

Mais uma vez muito obrigado, não tinha me atentado a este detalhe.