Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class CustomerForm extends TStandardForm
- {
- protected $form;
- protected $loaded;
- protected $responsible_list;
- public function __construct()
- {
- parent::__construct();
- parent::setDatabase('database');
- parent::setActiveRecord('Customer');
- $this->form = new BootstrapFormBuilder('form_Customer');
- $this->form->setFormTitle('Cliente');
- $id = new TEntry('id');
- $name = new TEntry('name');
- $gender = new TRadioGroup('gender');
- $martial_status = new TCombo('marital_status');
- $rg = new TEntry('rg');
- $cpf = new TEntry('cpf');
- $phone = new TEntry('phone');
- $cellphone = new TEntry('cellphone');
- $email = new TEntry('email');
- $nationality = new TEntry('nationality');
- $origin = new TEntry('origin');
- $note = new TText('note');
- $monthly_payment= new TEntry('monthly_payment');
- $expiration_day = new TEntry('expiration_day');
- $removed_reason = new TText('removed_reason');
- $created_at = new TDate('created_at');
- $id->setSize('30%');
- $id->setEditable(false);
- $name->setSize('100%');
- $gender->setSize('100%');
- $gender->setLayout('horizontal');
- $gender->setUseButton();
- $martial_status->setSize('100%');
- $rg->setSize('100%');
- $cpf->setSize('100%');
- $phone->setSize('100%');
- $cellphone->setSize('100%');
- $email->setSize('100%');
- $nationality->setSize('100%');
- $origin->setSize('100%');
- $note->setSize('100%');
- $note->style = 'resize:none;';
- $monthly_payment->setSize('100%');
- $removed_reason->setSize('100%');
- $removed_reason->style = 'resize:none;';
- $created_at->setSize('40%');
- $created_at->setEditable(false);
- $genders = ['M'=>' Masculino', 'F'=>' Feminino'];
- $gender->addItems($genders);
- $created_at->setValue(date('d/m/Y'));
- $martial_statuses = ['C'=>' Casado(a)','S'=>' Solteiro(a)','D'=>'Divorciado(a)','V'=>'Viúvo(a)'];
- $martial_status->addItems($martial_statuses);
- $rg->setMask('99.999.999-9');
- $cpf->setMask('999.999.999-99');
- $phone->setMask('(99)9999-9999');
- $cellphone->setMask('(99)99999-9999');
- $created_at->setMask('dd/mm/yyyy');
- $created_at->setDatabaseMask('yyyy-mm-dd');
- $name->addValidation('Nome', new TRequiredValidator);
- $email->addValidation('Email', new TEmailValidator);
- $this->form->appendPage('Dados');
- $this->form->addFields([new TLabel('Id')], [$id]);
- $this->form->addFields([new TLabel('Nome')], [$name]);
- $this->form->addFields([new TLabel('RG')], [$rg], [new TLabel('CPF')], [$cpf]);
- $this->form->addFields([new TLabel('Estado Civil')], [$martial_status], [new TLabel('Gênero')], [$gender]);
- $this->form->addFields([new TLabel('Naturalidade')], [$nationality], [new TLabel('Procedência')], [$origin]);
- $this->form->addFields([new TLabel('E-mail')], [$email]);
- $this->form->addFields([new TLabel('Telefone')], [$phone], [new TLabel('Celular')], [$cellphone]);
- $this->form->addFields([new TLabel('Observações')], [$note]);
- $this->form->addFields([new TLabel('Motivo da SaÃda')], [$removed_reason]);
- $this->form->appendPage('Responsáveis');
- $responsible_id = new TEntry('responsible_id');
- $responsible_city_id = new TSeekButton('responsible_city_id');
- $responsible_city_name = new TEntry('responsible_city_name');
- $responsible_name = new TEntry('responsible_name');
- $responsible_phone = new TEntry('responsible_phone');
- $responsible_cellphone = new TEntry('responsible_cellphone');
- $responsible_address = new TEntry('responsible_address');
- $responsible_district = new TEntry('responsible_district');
- $responsible_zipcode = new TEntry('responsible_zipcode');
- $responsible_state = new TDBCombo('responsible_state','database','State','id','name');
- $responsible_id->setSize('30%');
- $responsible_id->setEditable(false);
- $responsible_name->setSize('100%');
- $responsible_city_id->setSize('100');
- $responsible_city_name->setSize('calc(100% - 100px)');
- $responsible_city_name->setEditable(false);
- $responsible_name->setSize('100%');
- $responsible_phone->setSize('100%');
- $responsible_cellphone->setSize('100%');
- $responsible_address->setSize('100%');
- $responsible_district->setSize('100%');
- $responsible_zipcode->setSize('100%');
- $responsible_zipcode->setSize('100%');
- $responsible_state->setSize('100%');
- $responsible_state->setEditable(false);
- $responsible_phone->setMask('(99)9999-9999');
- $responsible_cellphone->setMask('(99)99999-9999');
- $responsible_zipcode->setMask('99.999-999');
- $responsible_city_id->setAction(new TAction(array('CityResponsibleSeek', 'onReload')));
- $this->form->addFields([new TLabel('Id')], [$responsible_id]);
- $this->form->addFields([new TLabel('Nome')], [$responsible_name]);
- $this->form->addFields([new TLabel('Telefone')], [$responsible_phone], [new TLabel('Celular')], [$responsible_cellphone]);
- $this->form->addFields([new TLabel('Cidade')], [$responsible_city_id, $responsible_city_name], [new TLabel('Estado')], [$responsible_state]);
- $this->form->addFields([new TLabel('Endereço')], [$responsible_address]);
- $this->form->addFields([new TLabel('Bairro')], [$responsible_district], [new TLabel('CEP')], [$responsible_zipcode]);
- $action_responsible = new TAction(array($this, 'onResponsibleAdd'));
- $add_responsible = new TButton('add_responsible');
- $add_responsible->setAction($action_responsible, 'Adicionar');
- $add_responsible->setImage('fa:save');
- $this->form->addFields([], [$add_responsible]);
- $this->responsible_list = new BootstrapDatagridWrapper(new TDataGrid);
- $this->responsible_list->datatable = 'true';
- $this->responsible_list->style = 'width: 100%';
- $this->responsible_list->setHeight(320);
- $this->responsible_list->disableDefaultClick();
- $column_edit = new TDataGridColumn('edit', '', 'center', 10);
- $column_delete = new TDataGridColumn('delete', '', 'center', 10);
- $column_id = new TDataGridColumn('id', 'Id', 'left', 80);
- $column_name = new TDataGridColumn('name', 'Nome', 'left');
- $this->responsible_list->addColumn($column_edit);
- $this->responsible_list->addColumn($column_delete);
- $this->responsible_list->addColumn($column_id);
- $this->responsible_list->addColumn($column_name);
- $this->responsible_list->createModel();
- $this->form->addContent([TPanelGroup::pack('', $this->responsible_list)]);
- $this->form->appendPage('Planos');
- $this->form->appendPage('Médicos Acompanhantes');
- $this->form->addAction('Salvar', new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addAction('Novo', new TAction(array($this, 'onClear')), 'fa:eraser red');
- $this->form->addAction('Voltar para Listagem', new TAction(array('CustomerList','onReload')), 'fa:table blue');
- $container = new TVBox;
- $container->style = 'width: 100%;';
- $container->add(new TXMLBreadCrumb('menu.xml', 'CustomerList'));
- $container->add($this->form);
- parent::add($container);
- }
- public function onResponsibleAdd($param)
- {
- try
- {
- TTransaction::open($this->database);
- $data = $this->form->getData();
- if(empty($data->responsible_name))
- throw new Exception('O campo Nome é obrigatório.');
- if(empty($data->responsible_city_id))
- throw new Exception('O campo Cidade é obrigatório.');
- $object = new Responsible((int)$data->responsible_id);
- $object->customer_id= $param['id'];
- $object->name = $data->responsible_name;
- $object->phone = $data->responsible_phone;
- $object->cellphone = $data->responsible_cellphone;
- $object->city_id = $data->responsible_city_id;
- $object->address = $data->responsible_address;
- $object->district = $data->responsible_district;
- $object->zipcode = $data->responsible_zipcode;
- if(isset($param['id']))
- {
- $customer = new $this->activeRecord($param['id']);
- $customer->addResponsible($object);
- $customer->store();
- $customer_responsibles = $customer->getResponsibles();
- $session_responsibles = array();
- foreach($customer_responsibles as $item)
- {
- $session_responsibles[$item->id] = $item->toArray();
- $session_responsibles[$item->id]['id'] = $item->id;
- $session_responsibles[$item->id]['name'] = $item->name;
- }
- TSession::setValue('session_responsibles', $session_responsibles);
- }
- TTransaction::close();
- $this->form->setCurrentPage(1);
- $this->onClear($param);
- $this->form->setData($data);
- $this->onReload($param);
- }
- catch (Exception $e)
- {
- $this->form->setData( $this->form->getData());
- new TMessage('error', $e->getMessage());
- $this->form->setCurrentPage(1);
- }
- }
- public function onDeleteResponsible($param)
- {
- $data = $this->form->getData();
- $this->form->setData($data);
- $this->form->setCurrentPage(1);
- }
- public function onEditResponsible($param)
- {
- $data = $this->form->getData();
- $session_responsibles = TSession::getValue('session_responsibles');
- $responsible = $session_responsibles[ (int) $param['list_responsible_id'] ];
- $data->responsible_id = $param['list_responsible_id'];
- $data->responsible_name = $responsible['name'];
- $this->form->setData($data);
- //$this->onReload($param);
- $this->form->setCurrentPage(1);
- }
- public function onReload($param)
- {
- $session_responsibles = TSession::getValue('session_responsibles');
- $this->responsible_list->clear();
- if($session_responsibles)
- {
- $count = 1;
- foreach($session_responsibles as $key => $value)
- {
- $item_name = 'resp_' . $count;
- $item = new StdClass;
- $action_del = new TAction(array($this, 'onDeleteResponsible'));
- $action_del->setParameter('list_responsible_id', $key);
- $action_edi = new TAction(array($this, 'onEditResponsible'));
- $action_edi->setParameter('list_responsible_id', $key);
- $button_del = new TButton('delete_responsible'.$count);
- $button_del->class = 'btn btn-default btn-sm';
- $button_del->setAction( $action_del, '' );
- $button_del->setImage('fa:trash-o red fa-lg');
- $button_edi = new TButton('edit_responsible'.$count);
- $button_edi->class = 'btn btn-default btn-sm';
- $button_edi->setAction( $action_edi, '' );
- $button_edi->setImage('fa:edit blue fa-lg');
- $item->edit = $button_edi;
- $item->delete = $button_del;
- $this->form->addFields([$button_edi],[$button_del]);
- $item->id = $value['id'];
- $item->name = $value['name'];
- $row = $this->responsible_list->addItem($item);
- $row->onmouseover = '';
- $row->onmouseout = '';
- $count++;
- }
- }
- $this->loaded = true;
- }
- public function onEdit($param)
- {
- try
- {
- if (isset($param['id']))
- {
- $id = $param['id'];
- TTransaction::open($this->database);
- $customer = new $this->activeRecord($id);
- $customer_responsibles = $customer->getResponsibles();
- if($customer->removed == 'N')
- {
- BootstrapFormBuilder::hideField('form_Customer', 'removed_reason');
- }
- $customer->created_at = TDate::date2br($customer->created_at);
- $this->form->setData($customer);
- $session_responsibles = array();
- foreach($customer_responsibles as $item)
- {
- $session_responsibles[$item->id] = $item->toArray();
- $session_responsibles[$item->id]['id'] = $item->id;
- $session_responsibles[$item->id]['name'] = $item->name;
- }
- TSession::setValue('session_responsibles', $session_responsibles);
- TTransaction::close();
- }
- else
- {
- $this->form->clear();
- TSession::setValue('session_responsibles', null);
- }
- }
- catch(Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- public function show()
- {
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- if (func_num_args() > 0)
- {
- $this->onReload( func_get_arg(0) );
- }
- else
- {
- $this->onReload();
- }
- }
- parent::show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement