RB
Form não é preenchido no onEdit ao usar componente TDBSortList
Olá,
Estou tentando usar o componente TDBSortList na tela de usuário, porém ao editar informações não são preenchidas no form, e não estou conseguindo identificar onde estou errando, alguém pode me ajudar /
Segue código
Estou tentando usar o componente TDBSortList na tela de usuário, porém ao editar informações não são preenchidas no form, e não estou conseguindo identificar onde estou errando, alguém pode me ajudar /
Segue código
- <?php
- class SystemUserForm extends TPage
- {
- protected $form; // form
- protected $program_list;
- protected $datagrid;
- protected $loaded;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- parent::include_css('app/resources/builder.css');
- parent::include_css('app/resources/blue_modificado.css');
- parent::include_css('app/resources/custom-table.css');
- $organizacao_id = TSession::getValue('organizacion_id'); // pega id da empresa
- // creates the form
- $this->form = new BootstrapFormBuilder('form_System_user');
- $this->form->setFormTitle( _t('User') );
- // create the form fields
- $lbl_id = new TLabel('Id');
- $lbl_name = new TLabel(_t('Name'));
- //$lbl_login = new TLabel(_t('Login'));
- $lbl_password = new TLabel(_t('Password'));
- $lbl_confirm_password = new TLabel(_t('Password confirmation'));
- $lbl_email = new TLabel(_t('Email'));
- $lbl_unit = new TLabel(_t('Main unit'));
- $lbl_front_page = new TLabel(_t('Front page'));
- $lbl_id->class = 'lbl_text';
- $lbl_name->class = 'lbl_text';
- //$lbl_login->class = 'lbl_text';
- $lbl_password->class = 'lbl_text';
- $lbl_confirm_password->class = 'lbl_text';
- $lbl_email->class = 'lbl_text';
- $lbl_unit->class = 'lbl_text';
- $lbl_front_page->class = 'lbl_text';
- $id = new TEntry('id');
- $name = new TEntry('name');
- //$login = new TEntry('login');
- $password = new TPassword('password');
- $repassword = new TPassword('repassword');
- $email = new TEntry('email');
- $filter1 = new TCriteria;
- $filter1->add(new TFilter('organizacao_id','=',$organizacao_id));
- $unit_id = new TDBCombo('system_unit_id','permission','SystemOrganization','id','name');
- $program_id = new ">TDBSeekButton('program_id', 'permission', 'form_System_user', 'SystemProgram', 'name', 'program_id', 'program_name');
- $program_name = new TEntry('program_name');
- //alterado para grids
- //$groups = new TDBCheckGroup('groups','permission','SystemGroup','id','name');
- $frontpage_id = new ">TDBSeekButton('frontpage_id', 'permission', 'form_System_user', 'SystemProgram', 'name', 'frontpage_id', 'frontpage_name');
- $frontpage_name = new TEntry('frontpage_name');
- $filter = new TCriteria;
- $filter->add(new TFilter('organizacao_id','<','0'));
- $organizacao = new TDBCheckGroup('organizacao','centinel','Organizacao','id','juridica_nome','id',$filter);
- $units = new TDBCheckGroup('units','permission','SystemUnit','id','name');
- $organizacao->setLayout('horizontal');
- if ($organizacao->getLabels())
- {
- foreach ($units->getLabels() as $label)
- {
- $label->setSize(200);
- }
- }
- $units->setLayout('horizontal');
- if ($units->getLabels())
- {
- foreach ($units->getLabels() as $label)
- {
- $label->setSize(200);
- }
- }
- $btn = $this->form->addAction( _t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addAction( _t('Clear'), new TAction(array($this, 'onEdit')), 'fa:eraser red');
- $this->form->addAction( _t('Back'), new TAction(array('SystemUserList','onReload')), 'fa:arrow-circle-o-left blue');
- $add_button = TButton::create('add', array($this,'onAddProgram'), _t('Add'), 'fa:plus green');
- $this->form->addField($program_id);
- $this->form->addField($program_name);
- $this->form->addField($add_button);
- $this->program_list = new TQuickGrid;
- $this->program_list->setHeight(180);
- $this->program_list->makeScrollable();
- $this->program_list->style='width: 100%';
- $this->program_list->id = 'program_list';
- $this->program_list->disableDefaultClick();
- $this->program_list->addQuickColumn('', 'delete', 'center', '5%');
- $this->program_list->addQuickColumn('Id', 'id', 'left', '10%');
- $this->program_list->addQuickColumn(_t('Program'), 'name', 'left', '85%');
- $this->program_list->createModel();
- $hbox = new THBox;
- $hbox->add($program_id);
- $hbox->add($program_name, 'display:initial');
- $hbox->add($add_button);
- $hbox->style = 'margin: 4px';
- $vbox = new TVBox;
- $vbox->style='width:100%';
- $vbox->add( $hbox );
- $vbox->add($this->program_list);
- // define the sizes
- //$id->setSize('50%');
- $name->setSize('100%');
- $password->setSize('100%');
- $repassword->setSize('100%');
- $email->setSize('100%');
- $unit_id->setSize('100%');
- $frontpage_id->setSize('60');
- $frontpage_name->setSize('calc(100% - 60px)');
- $program_id->setSize('30');
- $program_name->setSize('calc(100% - 200px)');
- // outros
- $id->setEditable(false);
- $program_name->setEditable(false);
- $frontpage_name->setEditable(false);
- // validations
- $name->addValidation(_t('Name'), new TRequiredValidator);
- $email->addValidation('Email', new TEmailValidator);
- $this->form->addFields( [$lbl_id], [$id]);
- $this->form->addFields( [$lbl_name], [$name] );
- $this->form->addFields( [$lbl_email], [$email] );
- $this->form->addFields( [$lbl_unit], [$unit_id], [$lbl_front_page], [$frontpage_id, $frontpage_name] );
- $this->form->addFields( [$lbl_password], [$password], [$lbl_confirm_password], [$repassword] );
- $this->form->addFields( [new TFormSeparator('')]);
- $dados_unit = new TLabel(_t('Units'));
- $dados_unit->class ='lbl_titulo';
- $this->form->addContent( [$dados_unit] );
- $this->form->addFields( [$units] );
- $this->form->addFields( [new TFormSeparator('')] );
- $dados_grupo = new TLabel(_t('Groups'));
- $dados_grupo->class ='lbl_titulo';
- $this->form->addContent( [$dados_grupo] );
- //lista de grupos
- $list1 = new TDBSortList('list1', 'permission', 'SystemGroup', 'id', 'name','id asc' );
- $list2 = new TSortList('list2');
- $list1->style = 'float:left;width:250px;height:250px';
- $list2->style = 'float:left;width:250px;height:250px';
- $list1->connectTo($list2);
- $list2->connectTo($list1);
- // connect the change method
- $list1->setChangeAction(new TAction(array($this, 'onChangeActionList')));
- // creates the action button
- $button1 = new TButton('action1');
- $button1->setAction(new TAction(array($this, 'onSend')), '');
- $button1->setImage('fa:check-circle-o');
- // creates the action button
- $button2 = new TButton('action2');
- $button2->setAction(new TAction(array($this, 'onSend')), '');
- $button2->setImage('fa:check-circle-o');
- // creates the action button
- $button3 = new TButton('action3');
- $button3->setAction(new TAction(array($this, 'onSend')), '');
- $button3->setImage('fa:check-circle-o');
- // creates the action button
- $button4 = new TButton('action4');
- $button4->setAction(new TAction(array($this, 'onSend')), 'Send');
- $button4->setImage('fa:check-circle-o');
- $table1 = new TTable;
- $table2 = new TTable;
- $row = $table1->addRow();
- $row->addCell($list1);
- $table2->addRowSet($button1);
- $table2->addRowSet($button2);
- $table2->addRowSet($button3);
- $row->addCell($table2);
- $row->addCell($list2);
- $table1->addRow()->addCell($button4);
- $this->form->setFields(array($list1,$list2, $button1,$button2,$button3,$button4));
- $this->form->addFields([$table1]);
- $dados_programs = new TLabel(_t('Programs'));
- $dados_programs->class ='lbl_titulo';
- $this->form->addContent( [$dados_programs] );
- $this->form->addFields( [$vbox] );
- $container = new TVBox;
- $container->style = 'width: 90%';
- $container->add(new TXMLBreadCrumb('menu.xml', 'SystemUserList'));
- $container->add($this->form);
- // add the container to the page
- parent::add($container);
- }
- /**
- * Remove program from session
- */
- public static function deleteProgram($param)
- {
- $programs = TSession::getValue('program_list');
- unset($programs[ $param['id'] ]);
- TSession::setValue('program_list', $programs);
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- public static function onSave($param)
- {
- try
- {
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- $object = new SystemUser;
- $object->fromArray( $param );
- $senha = $object->password;
- /*
- if( empty($object->login) )
- {
- throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required', _t('Login')));
- }
- */
- if( empty($object->id) )
- {
- if (SystemUser::newFromLogin($object->login) instanceof SystemUser)
- {
- throw new Exception(_t('An user with this login is already registered'));
- }
- if ( empty($object->password) )
- {
- throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required', _t('Password')));
- }
- $object->active = 'Y';
- }
- if( $object->password )
- {
- if( $object->password !== $param['repassword'] )
- throw new Exception(_t('The passwords do not match'));
- $object->password = md5($object->password);
- }
- else
- {
- unset($object->password);
- }
- $object->store();
- $object->clearParts();
- if( !empty($param['groups']) )
- {
- foreach( $param['groups'] as $group_id )
- {
- $object->addSystemUserGroup( new SystemGroup($group_id) );
- }
- }
- if( !empty($param['units']) )
- {
- foreach( $param['units'] as $unit_id )
- {
- $object->addSystemUserUnit( new SystemUnit($unit_id) );
- }
- }
- $programs = TSession::getValue('program_list');
- if (!empty($programs))
- {
- foreach ($programs as $program)
- {
- $object->addSystemUserProgram( new SystemProgram( $program['id'] ) );
- }
- }
- $data = new stdClass;
- $data->id = $object->id;
- TForm::sendData('form_System_user', $data);
- // close the transaction
- TTransaction::close();
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- if (isset($param['key']))
- {
- // get the parameter $key
- $key=$param['key'];
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- // instantiates object System_user
- $object = new SystemUser($key);
- unset($object->password);
- $units = array();
- if( $units_db = $object->getSystemUserUnits() )
- {
- foreach( $units_db as $unit )
- {
- $units[] = $unit->id;
- }
- }
- $data = array();
- foreach ($object->getSystemUserPrograms() as $program)
- {
- $data[$program->id] = $program->toArray();
- $item = new stdClass;
- $item->id = $program->id;
- $item->name = $program->name;
- $i = new TElement('i');
- $i->{'class'} = 'fa fa-trash red';
- $btn = new TElement('a');
- $btn->{'onclick'} = "__adianti_ajax_exec('class=SystemUserForm&method=deleteProgram&id={$program->id}');$(this).closest('tr').remove();";
- $btn->{'class'} = 'btn btn-default btn-sm';
- $btn->add( $i );
- $item->delete = $btn;
- $tr = $this->program_list->addItem($item);
- $tr->{'style'} = 'width: 100%;display: inline-table;';
- }
- $object->units = $units;
- // fill the form with the active record data
- $this->form->setData($object);
- // close the transaction
- TTransaction::close();
- TSession::setValue('program_list', $data);
- }
- else
- {
- $this->form->clear();
- TSession::setValue('program_list', null);
- }
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * Add a program
- */
- public static function onAddProgram($param)
- {
- try
- {
- $id = $param['program_id'];
- $program_list = TSession::getValue('program_list');
- if (!empty($id) AND empty($program_list[$id]))
- {
- TTransaction::open('permission');
- $program = SystemProgram::find($id);
- $program_list[$id] = $program->toArray();
- TSession::setValue('program_list', $program_list);
- TTransaction::close();
- $i = new TElement('i');
- $i->{'class'} = 'fa fa-trash red';
- $btn = new TElement('a');
- $btn->{'onclick'} = "__adianti_ajax_exec(\'class=SystemGroupForm&method=deleteProgram&id=$id\');$(this).closest(\'tr\').remove();";
- $btn->{'class'} = 'btn btn-default btn-sm';
- $btn->add($i);
- $tr = new TTableRow;
- $tr->{'class'} = 'tdatagrid_row_odd';
- $tr->{'style'} = 'width: 100%;display: inline-table;';
- $cell = $tr->addCell( $btn );
- $cell->{'style'}='text-align:center';
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '5%';
- $cell = $tr->addCell( $program->id );
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '10%';
- $cell = $tr->addCell( $program->name );
- $cell->{'class'}='tdatagrid_cell';
- $cell->{'width'} = '85%';
- TScript::create("tdatagrid_add_serialized_row('program_list', '$tr');");
- $data = new stdClass;
- $data->program_id = '';
- $data->program_name = '';
- TForm::sendData('form_System_user', $data);
- }
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- static function onChangeAction($param)
- {
- try
- {
- TTransaction::open('centinel'); //base de dados
- if ($organizacao_id)//se existe parâmetro da primeira combo(nome da primeira combo)
- {
- $criteria = TCriteria::create( ['organizacao_id' => $organizacao_id ] );
- //organizacao_id = parametro chave estrangeira da tabela master
- // formname,field(nome do campo),database , nome da model,key,value,ordercolumn = NULL,criteria, startEmpty = FALSE
- TDBCombo::reloadFromModel('form_System_user','organizacao_id','centinel','Organizacao','id','{juridica_nome}', 'juridica_nome',$criteria,TRUE);
- }
- else
- {
- TCombo::clearField('form_System_user', 'organizacao'); //reload do usuário form_System_user na check "organizacao_id"
- }
- TTransaction::close();
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Executed when a user change some item in list
- */
- public static function onChangeActionList($param)
- {
- new TMessage('info', 'Change action<br>'.
- 'List1: ' . implode(',', $param['list1']) . '<br>' .
- 'List2: ' . implode(',', $param['list2']));
- }
- /**
- * Send data
- */
- public function onSend($param)
- {
- // get form data
- $data = $this->form->getData();
- // put the data back to the form
- $this->form->setData($data);
- // creates a string with the form element's values
- $message = 'List 1: ' . implode(',', $data->list1) . '<br>';
- $message.= 'List 2 : ' . implode(',', $data->list2) . '<br>';
- // show the message
- new TMessage('info', $message);
- }
- }
Ao usar a função setFields somente os campos informados serão considerados pela classe, demais campos serão ignorados.
Nataniel,
Funcionou perfeito, valeu ....
Muito obrigado.