Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.58 KB | None | 0 0
  1. <?php
  2. class CustomerForm extends TStandardForm
  3. {
  4. protected $form;
  5. protected $loaded;
  6. protected $responsible_list;
  7.  
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. parent::setDatabase('database');
  12. parent::setActiveRecord('Customer');
  13.  
  14. $this->form = new BootstrapFormBuilder('form_Customer');
  15. $this->form->setFormTitle('Cliente');
  16.  
  17. $id = new TEntry('id');
  18. $name = new TEntry('name');
  19. $gender = new TRadioGroup('gender');
  20. $martial_status = new TCombo('marital_status');
  21. $rg = new TEntry('rg');
  22. $cpf = new TEntry('cpf');
  23. $phone = new TEntry('phone');
  24. $cellphone = new TEntry('cellphone');
  25. $email = new TEntry('email');
  26. $nationality = new TEntry('nationality');
  27. $origin = new TEntry('origin');
  28. $note = new TText('note');
  29. $monthly_payment= new TEntry('monthly_payment');
  30. $expiration_day = new TEntry('expiration_day');
  31. $removed_reason = new TText('removed_reason');
  32. $created_at = new TDate('created_at');
  33.  
  34. $id->setSize('30%');
  35. $id->setEditable(false);
  36. $name->setSize('100%');
  37. $gender->setSize('100%');
  38. $gender->setLayout('horizontal');
  39. $gender->setUseButton();
  40. $martial_status->setSize('100%');
  41. $rg->setSize('100%');
  42. $cpf->setSize('100%');
  43. $phone->setSize('100%');
  44. $cellphone->setSize('100%');
  45. $email->setSize('100%');
  46. $nationality->setSize('100%');
  47. $origin->setSize('100%');
  48. $note->setSize('100%');
  49. $note->style = 'resize:none;';
  50. $monthly_payment->setSize('100%');
  51. $removed_reason->setSize('100%');
  52. $removed_reason->style = 'resize:none;';
  53. $created_at->setSize('40%');
  54. $created_at->setEditable(false);
  55.  
  56. $genders = ['M'=>' Masculino', 'F'=>' Feminino'];
  57. $gender->addItems($genders);
  58. $created_at->setValue(date('d/m/Y'));
  59.  
  60. $martial_statuses = ['C'=>' Casado(a)','S'=>' Solteiro(a)','D'=>'Divorciado(a)','V'=>'Viúvo(a)'];
  61. $martial_status->addItems($martial_statuses);
  62.  
  63. $rg->setMask('99.999.999-9');
  64. $cpf->setMask('999.999.999-99');
  65. $phone->setMask('(99)9999-9999');
  66. $cellphone->setMask('(99)99999-9999');
  67. $created_at->setMask('dd/mm/yyyy');
  68. $created_at->setDatabaseMask('yyyy-mm-dd');
  69.  
  70. $name->addValidation('Nome', new TRequiredValidator);
  71. $email->addValidation('Email', new TEmailValidator);
  72.  
  73. $this->form->appendPage('Dados');
  74.  
  75. $this->form->addFields([new TLabel('Id')], [$id]);
  76. $this->form->addFields([new TLabel('Nome')], [$name]);
  77. $this->form->addFields([new TLabel('RG')], [$rg], [new TLabel('CPF')], [$cpf]);
  78. $this->form->addFields([new TLabel('Estado Civil')], [$martial_status], [new TLabel('Gênero')], [$gender]);
  79. $this->form->addFields([new TLabel('Naturalidade')], [$nationality], [new TLabel('Procedência')], [$origin]);
  80. $this->form->addFields([new TLabel('E-mail')], [$email]);
  81. $this->form->addFields([new TLabel('Telefone')], [$phone], [new TLabel('Celular')], [$cellphone]);
  82. $this->form->addFields([new TLabel('Observações')], [$note]);
  83. $this->form->addFields([new TLabel('Motivo da Saída')], [$removed_reason]);
  84.  
  85. $this->form->appendPage('Responsáveis');
  86.  
  87. $responsible_id = new TEntry('responsible_id');
  88. $responsible_city_id = new TSeekButton('responsible_city_id');
  89. $responsible_city_name = new TEntry('responsible_city_name');
  90. $responsible_name = new TEntry('responsible_name');
  91. $responsible_phone = new TEntry('responsible_phone');
  92. $responsible_cellphone = new TEntry('responsible_cellphone');
  93. $responsible_address = new TEntry('responsible_address');
  94. $responsible_district = new TEntry('responsible_district');
  95. $responsible_zipcode = new TEntry('responsible_zipcode');
  96. $responsible_state = new TDBCombo('responsible_state','database','State','id','name');
  97.  
  98. $responsible_id->setSize('30%');
  99. $responsible_id->setEditable(false);
  100. $responsible_name->setSize('100%');
  101. $responsible_city_id->setSize('100');
  102. $responsible_city_name->setSize('calc(100% - 100px)');
  103. $responsible_city_name->setEditable(false);
  104. $responsible_name->setSize('100%');
  105. $responsible_phone->setSize('100%');
  106. $responsible_cellphone->setSize('100%');
  107. $responsible_address->setSize('100%');
  108. $responsible_district->setSize('100%');
  109. $responsible_zipcode->setSize('100%');
  110. $responsible_zipcode->setSize('100%');
  111. $responsible_state->setSize('100%');
  112. $responsible_state->setEditable(false);
  113.  
  114. $responsible_phone->setMask('(99)9999-9999');
  115. $responsible_cellphone->setMask('(99)99999-9999');
  116. $responsible_zipcode->setMask('99.999-999');
  117.  
  118. $responsible_city_id->setAction(new TAction(array('CityResponsibleSeek', 'onReload')));
  119.  
  120. $this->form->addFields([new TLabel('Id')], [$responsible_id]);
  121. $this->form->addFields([new TLabel('Nome')], [$responsible_name]);
  122. $this->form->addFields([new TLabel('Telefone')], [$responsible_phone], [new TLabel('Celular')], [$responsible_cellphone]);
  123. $this->form->addFields([new TLabel('Cidade')], [$responsible_city_id, $responsible_city_name], [new TLabel('Estado')], [$responsible_state]);
  124. $this->form->addFields([new TLabel('Endereço')], [$responsible_address]);
  125. $this->form->addFields([new TLabel('Bairro')], [$responsible_district], [new TLabel('CEP')], [$responsible_zipcode]);
  126.  
  127. $action_responsible = new TAction(array($this, 'onResponsibleAdd'));
  128. $add_responsible = new TButton('add_responsible');
  129. $add_responsible->setAction($action_responsible, 'Adicionar');
  130. $add_responsible->setImage('fa:save');
  131.  
  132. $this->form->addFields([], [$add_responsible]);
  133.  
  134. $this->responsible_list = new BootstrapDatagridWrapper(new TDataGrid);
  135. $this->responsible_list->datatable = 'true';
  136. $this->responsible_list->style = 'width: 100%';
  137. $this->responsible_list->setHeight(320);
  138. $this->responsible_list->disableDefaultClick();
  139.  
  140. $column_edit = new TDataGridColumn('edit', '', 'center', 10);
  141. $column_delete = new TDataGridColumn('delete', '', 'center', 10);
  142. $column_id = new TDataGridColumn('id', 'Id', 'left', 80);
  143. $column_name = new TDataGridColumn('name', 'Nome', 'left');
  144.  
  145. $this->responsible_list->addColumn($column_edit);
  146. $this->responsible_list->addColumn($column_delete);
  147. $this->responsible_list->addColumn($column_id);
  148. $this->responsible_list->addColumn($column_name);
  149.  
  150. $this->responsible_list->createModel();
  151.  
  152. $this->form->addContent([TPanelGroup::pack('', $this->responsible_list)]);
  153.  
  154. $this->form->appendPage('Planos');
  155.  
  156. $this->form->appendPage('Médicos Acompanhantes');
  157.  
  158. $this->form->addAction('Salvar', new TAction(array($this, 'onSave')), 'fa:floppy-o');
  159. $this->form->addAction('Novo', new TAction(array($this, 'onClear')), 'fa:eraser red');
  160. $this->form->addAction('Voltar para Listagem', new TAction(array('CustomerList','onReload')), 'fa:table blue');
  161.  
  162. $container = new TVBox;
  163. $container->style = 'width: 100%;';
  164. $container->add(new TXMLBreadCrumb('menu.xml', 'CustomerList'));
  165. $container->add($this->form);
  166. parent::add($container);
  167. }
  168.  
  169. public function onResponsibleAdd($param)
  170. {
  171. try
  172. {
  173. TTransaction::open($this->database);
  174.  
  175. $data = $this->form->getData();
  176.  
  177. if(empty($data->responsible_name))
  178. throw new Exception('O campo Nome é obrigatório.');
  179. if(empty($data->responsible_city_id))
  180. throw new Exception('O campo Cidade é obrigatório.');
  181.  
  182. $object = new Responsible((int)$data->responsible_id);
  183. $object->customer_id= $param['id'];
  184. $object->name = $data->responsible_name;
  185. $object->phone = $data->responsible_phone;
  186. $object->cellphone = $data->responsible_cellphone;
  187. $object->city_id = $data->responsible_city_id;
  188. $object->address = $data->responsible_address;
  189. $object->district = $data->responsible_district;
  190. $object->zipcode = $data->responsible_zipcode;
  191.  
  192. if(isset($param['id']))
  193. {
  194. $customer = new $this->activeRecord($param['id']);
  195. $customer->addResponsible($object);
  196. $customer->store();
  197. $customer_responsibles = $customer->getResponsibles();
  198.  
  199. $session_responsibles = array();
  200.  
  201. foreach($customer_responsibles as $item)
  202. {
  203. $session_responsibles[$item->id] = $item->toArray();
  204. $session_responsibles[$item->id]['id'] = $item->id;
  205. $session_responsibles[$item->id]['name'] = $item->name;
  206. }
  207.  
  208. TSession::setValue('session_responsibles', $session_responsibles);
  209. }
  210.  
  211. TTransaction::close();
  212.  
  213. $this->form->setCurrentPage(1);
  214.  
  215. $this->onClear($param);
  216.  
  217. $this->form->setData($data);
  218.  
  219. $this->onReload($param);
  220. }
  221. catch (Exception $e)
  222. {
  223. $this->form->setData( $this->form->getData());
  224.  
  225. new TMessage('error', $e->getMessage());
  226.  
  227. $this->form->setCurrentPage(1);
  228. }
  229. }
  230.  
  231. public function onDeleteResponsible($param)
  232. {
  233. $data = $this->form->getData();
  234.  
  235. $this->form->setData($data);
  236.  
  237. $this->form->setCurrentPage(1);
  238. }
  239.  
  240. public function onEditResponsible($param)
  241. {
  242. $data = $this->form->getData();
  243.  
  244. $session_responsibles = TSession::getValue('session_responsibles');
  245.  
  246. $responsible = $session_responsibles[ (int) $param['list_responsible_id'] ];
  247.  
  248. $data->responsible_id = $param['list_responsible_id'];
  249. $data->responsible_name = $responsible['name'];
  250.  
  251. $this->form->setData($data);
  252.  
  253. //$this->onReload($param);
  254.  
  255. $this->form->setCurrentPage(1);
  256. }
  257.  
  258. public function onReload($param)
  259. {
  260. $session_responsibles = TSession::getValue('session_responsibles');
  261.  
  262. $this->responsible_list->clear();
  263.  
  264. if($session_responsibles)
  265. {
  266. $count = 1;
  267.  
  268. foreach($session_responsibles as $key => $value)
  269. {
  270. $item_name = 'resp_' . $count;
  271. $item = new StdClass;
  272.  
  273. $action_del = new TAction(array($this, 'onDeleteResponsible'));
  274. $action_del->setParameter('list_responsible_id', $key);
  275.  
  276. $action_edi = new TAction(array($this, 'onEditResponsible'));
  277. $action_edi->setParameter('list_responsible_id', $key);
  278.  
  279. $button_del = new TButton('delete_responsible'.$count);
  280. $button_del->class = 'btn btn-default btn-sm';
  281. $button_del->setAction( $action_del, '' );
  282. $button_del->setImage('fa:trash-o red fa-lg');
  283.  
  284. $button_edi = new TButton('edit_responsible'.$count);
  285. $button_edi->class = 'btn btn-default btn-sm';
  286. $button_edi->setAction( $action_edi, '' );
  287. $button_edi->setImage('fa:edit blue fa-lg');
  288.  
  289. $item->edit = $button_edi;
  290. $item->delete = $button_del;
  291.  
  292. $this->form->addFields([$button_edi],[$button_del]);
  293.  
  294. $item->id = $value['id'];
  295. $item->name = $value['name'];
  296.  
  297. $row = $this->responsible_list->addItem($item);
  298. $row->onmouseover = '';
  299. $row->onmouseout = '';
  300.  
  301. $count++;
  302. }
  303. }
  304. $this->loaded = true;
  305. }
  306.  
  307. public function onEdit($param)
  308. {
  309. try
  310. {
  311. if (isset($param['id']))
  312. {
  313. $id = $param['id'];
  314.  
  315. TTransaction::open($this->database);
  316.  
  317. $customer = new $this->activeRecord($id);
  318. $customer_responsibles = $customer->getResponsibles();
  319.  
  320. if($customer->removed == 'N')
  321. {
  322. BootstrapFormBuilder::hideField('form_Customer', 'removed_reason');
  323. }
  324.  
  325. $customer->created_at = TDate::date2br($customer->created_at);
  326.  
  327. $this->form->setData($customer);
  328.  
  329. $session_responsibles = array();
  330.  
  331. foreach($customer_responsibles as $item)
  332. {
  333. $session_responsibles[$item->id] = $item->toArray();
  334. $session_responsibles[$item->id]['id'] = $item->id;
  335. $session_responsibles[$item->id]['name'] = $item->name;
  336. }
  337.  
  338. TSession::setValue('session_responsibles', $session_responsibles);
  339.  
  340. TTransaction::close();
  341. }
  342. else
  343. {
  344. $this->form->clear();
  345.  
  346. TSession::setValue('session_responsibles', null);
  347. }
  348. }
  349. catch(Exception $e)
  350. {
  351. new TMessage('error', $e->getMessage());
  352. }
  353. }
  354.  
  355. public function show()
  356. {
  357. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  358. {
  359. if (func_num_args() > 0)
  360. {
  361. $this->onReload( func_get_arg(0) );
  362. }
  363. else
  364. {
  365. $this->onReload();
  366. }
  367. }
  368. parent::show();
  369. }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement