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

 
  1. <?php
  2. class SystemUserForm extends TPage
  3. {
  4. protected $form; // form
  5. protected $program_list;
  6. protected $datagrid;
  7. protected $loaded;
  8. /**
  9. * Class constructor
  10. * Creates the page and the registration form
  11. */
  12. function __construct()
  13. {
  14. parent::__construct();
  15. parent::include_css('app/resources/builder.css');
  16. parent::include_css('app/resources/blue_modificado.css');
  17. parent::include_css('app/resources/custom-table.css');
  18. $organizacao_id = TSession::getValue('organizacion_id'); // pega id da empresa
  19. // creates the form
  20. $this->form = new BootstrapFormBuilder('form_System_user');
  21. $this->form->setFormTitle( _t('User') );
  22. // create the form fields
  23. $lbl_id = new TLabel('Id');
  24. $lbl_name = new TLabel(_t('Name'));
  25. //$lbl_login = new TLabel(_t('Login'));
  26. $lbl_password = new TLabel(_t('Password'));
  27. $lbl_confirm_password = new TLabel(_t('Password confirmation'));
  28. $lbl_email = new TLabel(_t('Email'));
  29. $lbl_unit = new TLabel(_t('Main unit'));
  30. $lbl_front_page = new TLabel(_t('Front page'));
  31. $lbl_id->class = 'lbl_text';
  32. $lbl_name->class = 'lbl_text';
  33. //$lbl_login->class = 'lbl_text';
  34. $lbl_password->class = 'lbl_text';
  35. $lbl_confirm_password->class = 'lbl_text';
  36. $lbl_email->class = 'lbl_text';
  37. $lbl_unit->class = 'lbl_text';
  38. $lbl_front_page->class = 'lbl_text';
  39. $id = new TEntry('id');
  40. $name = new TEntry('name');
  41. //$login = new TEntry('login');
  42. $password = new TPassword('password');
  43. $repassword = new TPassword('repassword');
  44. $email = new TEntry('email');
  45. $filter1 = new TCriteria;
  46. $filter1->add(new TFilter('organizacao_id','=',$organizacao_id));
  47. $unit_id = new TDBCombo('system_unit_id','permission','SystemOrganization','id','name');
  48. $program_id = new ">TDBSeekButton('program_id', 'permission', 'form_System_user', 'SystemProgram', 'name', 'program_id', 'program_name');
  49. $program_name = new TEntry('program_name');
  50. //alterado para grids
  51. //$groups = new TDBCheckGroup('groups','permission','SystemGroup','id','name');
  52. $frontpage_id = new ">TDBSeekButton('frontpage_id', 'permission', 'form_System_user', 'SystemProgram', 'name', 'frontpage_id', 'frontpage_name');
  53. $frontpage_name = new TEntry('frontpage_name');
  54. $filter = new TCriteria;
  55. $filter->add(new TFilter('organizacao_id','<','0'));
  56. $organizacao = new TDBCheckGroup('organizacao','centinel','Organizacao','id','juridica_nome','id',$filter);
  57. $units = new TDBCheckGroup('units','permission','SystemUnit','id','name');
  58. $organizacao->setLayout('horizontal');
  59. if ($organizacao->getLabels())
  60. {
  61. foreach ($units->getLabels() as $label)
  62. {
  63. $label->setSize(200);
  64. }
  65. }
  66. $units->setLayout('horizontal');
  67. if ($units->getLabels())
  68. {
  69. foreach ($units->getLabels() as $label)
  70. {
  71. $label->setSize(200);
  72. }
  73. }
  74. $btn = $this->form->addAction( _t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  75. $btn->class = 'btn btn-sm btn-primary';
  76. $this->form->addAction( _t('Clear'), new TAction(array($this, 'onEdit')), 'fa:eraser red');
  77. $this->form->addAction( _t('Back'), new TAction(array('SystemUserList','onReload')), 'fa:arrow-circle-o-left blue');
  78. $add_button = TButton::create('add', array($this,'onAddProgram'), _t('Add'), 'fa:plus green');
  79. $this->form->addField($program_id);
  80. $this->form->addField($program_name);
  81. $this->form->addField($add_button);
  82. $this->program_list = new TQuickGrid;
  83. $this->program_list->setHeight(180);
  84. $this->program_list->makeScrollable();
  85. $this->program_list->style='width: 100%';
  86. $this->program_list->id = 'program_list';
  87. $this->program_list->disableDefaultClick();
  88. $this->program_list->addQuickColumn('', 'delete', 'center', '5%');
  89. $this->program_list->addQuickColumn('Id', 'id', 'left', '10%');
  90. $this->program_list->addQuickColumn(_t('Program'), 'name', 'left', '85%');
  91. $this->program_list->createModel();
  92. $hbox = new THBox;
  93. $hbox->add($program_id);
  94. $hbox->add($program_name, 'display:initial');
  95. $hbox->add($add_button);
  96. $hbox->style = 'margin: 4px';
  97. $vbox = new TVBox;
  98. $vbox->style='width:100%';
  99. $vbox->add( $hbox );
  100. $vbox->add($this->program_list);
  101. // define the sizes
  102. //$id->setSize('50%');
  103. $name->setSize('100%');
  104. $password->setSize('100%');
  105. $repassword->setSize('100%');
  106. $email->setSize('100%');
  107. $unit_id->setSize('100%');
  108. $frontpage_id->setSize('60');
  109. $frontpage_name->setSize('calc(100% - 60px)');
  110. $program_id->setSize('30');
  111. $program_name->setSize('calc(100% - 200px)');
  112. // outros
  113. $id->setEditable(false);
  114. $program_name->setEditable(false);
  115. $frontpage_name->setEditable(false);
  116. // validations
  117. $name->addValidation(_t('Name'), new TRequiredValidator);
  118. $email->addValidation('Email', new TEmailValidator);
  119. $this->form->addFields( [$lbl_id], [$id]);
  120. $this->form->addFields( [$lbl_name], [$name] );
  121. $this->form->addFields( [$lbl_email], [$email] );
  122. $this->form->addFields( [$lbl_unit], [$unit_id], [$lbl_front_page], [$frontpage_id, $frontpage_name] );
  123. $this->form->addFields( [$lbl_password], [$password], [$lbl_confirm_password], [$repassword] );
  124. $this->form->addFields( [new TFormSeparator('')]);
  125. $dados_unit = new TLabel(_t('Units'));
  126. $dados_unit->class ='lbl_titulo';
  127. $this->form->addContent( [$dados_unit] );
  128. $this->form->addFields( [$units] );
  129. $this->form->addFields( [new TFormSeparator('')] );
  130. $dados_grupo = new TLabel(_t('Groups'));
  131. $dados_grupo->class ='lbl_titulo';
  132. $this->form->addContent( [$dados_grupo] );
  133. //lista de grupos
  134. $list1 = new TDBSortList('list1', 'permission', 'SystemGroup', 'id', 'name','id asc' );
  135. $list2 = new TSortList('list2');
  136. $list1->style = 'float:left;width:250px;height:250px';
  137. $list2->style = 'float:left;width:250px;height:250px';
  138. $list1->connectTo($list2);
  139. $list2->connectTo($list1);
  140. // connect the change method
  141. $list1->setChangeAction(new TAction(array($this, 'onChangeActionList')));
  142. // creates the action button
  143. $button1 = new TButton('action1');
  144. $button1->setAction(new TAction(array($this, 'onSend')), '');
  145. $button1->setImage('fa:check-circle-o');
  146. // creates the action button
  147. $button2 = new TButton('action2');
  148. $button2->setAction(new TAction(array($this, 'onSend')), '');
  149. $button2->setImage('fa:check-circle-o');
  150. // creates the action button
  151. $button3 = new TButton('action3');
  152. $button3->setAction(new TAction(array($this, 'onSend')), '');
  153. $button3->setImage('fa:check-circle-o');
  154. // creates the action button
  155. $button4 = new TButton('action4');
  156. $button4->setAction(new TAction(array($this, 'onSend')), 'Send');
  157. $button4->setImage('fa:check-circle-o');
  158. $table1 = new TTable;
  159. $table2 = new TTable;
  160. $row = $table1->addRow();
  161. $row->addCell($list1);
  162. $table2->addRowSet($button1);
  163. $table2->addRowSet($button2);
  164. $table2->addRowSet($button3);
  165. $row->addCell($table2);
  166. $row->addCell($list2);
  167. $table1->addRow()->addCell($button4);
  168. $this->form->setFields(array($list1,$list2, $button1,$button2,$button3,$button4));
  169. $this->form->addFields([$table1]);
  170. $dados_programs = new TLabel(_t('Programs'));
  171. $dados_programs->class ='lbl_titulo';
  172. $this->form->addContent( [$dados_programs] );
  173. $this->form->addFields( [$vbox] );
  174. $container = new TVBox;
  175. $container->style = 'width: 90%';
  176. $container->add(new TXMLBreadCrumb('menu.xml', 'SystemUserList'));
  177. $container->add($this->form);
  178. // add the container to the page
  179. parent::add($container);
  180. }
  181. /**
  182. * Remove program from session
  183. */
  184. public static function deleteProgram($param)
  185. {
  186. $programs = TSession::getValue('program_list');
  187. unset($programs[ $param['id'] ]);
  188. TSession::setValue('program_list', $programs);
  189. }
  190. /**
  191. * method onSave()
  192. * Executed whenever the user clicks at the save button
  193. */
  194. public static function onSave($param)
  195. {
  196. try
  197. {
  198. // open a transaction with database 'permission'
  199. TTransaction::open('permission');
  200. $object = new SystemUser;
  201. $object->fromArray( $param );
  202. $senha = $object->password;
  203. /*
  204. if( empty($object->login) )
  205. {
  206. throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required', _t('Login')));
  207. }
  208. */
  209. if( empty($object->id) )
  210. {
  211. if (SystemUser::newFromLogin($object->login) instanceof SystemUser)
  212. {
  213. throw new Exception(_t('An user with this login is already registered'));
  214. }
  215. if ( empty($object->password) )
  216. {
  217. throw new Exception(TAdiantiCoreTranslator::translate('The field ^1 is required', _t('Password')));
  218. }
  219. $object->active = 'Y';
  220. }
  221. if( $object->password )
  222. {
  223. if( $object->password !== $param['repassword'] )
  224. throw new Exception(_t('The passwords do not match'));
  225. $object->password = md5($object->password);
  226. }
  227. else
  228. {
  229. unset($object->password);
  230. }
  231. $object->store();
  232. $object->clearParts();
  233. if( !empty($param['groups']) )
  234. {
  235. foreach( $param['groups'] as $group_id )
  236. {
  237. $object->addSystemUserGroup( new SystemGroup($group_id) );
  238. }
  239. }
  240. if( !empty($param['units']) )
  241. {
  242. foreach( $param['units'] as $unit_id )
  243. {
  244. $object->addSystemUserUnit( new SystemUnit($unit_id) );
  245. }
  246. }
  247. $programs = TSession::getValue('program_list');
  248. if (!empty($programs))
  249. {
  250. foreach ($programs as $program)
  251. {
  252. $object->addSystemUserProgram( new SystemProgram( $program['id'] ) );
  253. }
  254. }
  255. $data = new stdClass;
  256. $data->id = $object->id;
  257. TForm::sendData('form_System_user', $data);
  258. // close the transaction
  259. TTransaction::close();
  260. // shows the success message
  261. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  262. }
  263. catch (Exception $e) // in case of exception
  264. {
  265. // shows the exception error message
  266. new TMessage('error', $e->getMessage());
  267. // undo all pending operations
  268. TTransaction::rollback();
  269. }
  270. }
  271. /**
  272. * method onEdit()
  273. * Executed whenever the user clicks at the edit button da datagrid
  274. */
  275. function onEdit($param)
  276. {
  277. try
  278. {
  279. if (isset($param['key']))
  280. {
  281. // get the parameter $key
  282. $key=$param['key'];
  283. // open a transaction with database 'permission'
  284. TTransaction::open('permission');
  285. // instantiates object System_user
  286. $object = new SystemUser($key);
  287. unset($object->password);
  288. $units = array();
  289. if( $units_db = $object->getSystemUserUnits() )
  290. {
  291. foreach( $units_db as $unit )
  292. {
  293. $units[] = $unit->id;
  294. }
  295. }
  296. $data = array();
  297. foreach ($object->getSystemUserPrograms() as $program)
  298. {
  299. $data[$program->id] = $program->toArray();
  300. $item = new stdClass;
  301. $item->id = $program->id;
  302. $item->name = $program->name;
  303. $i = new TElement('i');
  304. $i->{'class'} = 'fa fa-trash red';
  305. $btn = new TElement('a');
  306. $btn->{'onclick'} = "__adianti_ajax_exec('class=SystemUserForm&method=deleteProgram&id={$program->id}');$(this).closest('tr').remove();";
  307. $btn->{'class'} = 'btn btn-default btn-sm';
  308. $btn->add( $i );
  309. $item->delete = $btn;
  310. $tr = $this->program_list->addItem($item);
  311. $tr->{'style'} = 'width: 100%;display: inline-table;';
  312. }
  313. $object->units = $units;
  314. // fill the form with the active record data
  315. $this->form->setData($object);
  316. // close the transaction
  317. TTransaction::close();
  318. TSession::setValue('program_list', $data);
  319. }
  320. else
  321. {
  322. $this->form->clear();
  323. TSession::setValue('program_list', null);
  324. }
  325. }
  326. catch (Exception $e) // in case of exception
  327. {
  328. // shows the exception error message
  329. new TMessage('error', $e->getMessage());
  330. // undo all pending operations
  331. TTransaction::rollback();
  332. }
  333. }
  334. /**
  335. * Add a program
  336. */
  337. public static function onAddProgram($param)
  338. {
  339. try
  340. {
  341. $id = $param['program_id'];
  342. $program_list = TSession::getValue('program_list');
  343. if (!empty($id) AND empty($program_list[$id]))
  344. {
  345. TTransaction::open('permission');
  346. $program = SystemProgram::find($id);
  347. $program_list[$id] = $program->toArray();
  348. TSession::setValue('program_list', $program_list);
  349. TTransaction::close();
  350. $i = new TElement('i');
  351. $i->{'class'} = 'fa fa-trash red';
  352. $btn = new TElement('a');
  353. $btn->{'onclick'} = "__adianti_ajax_exec(\'class=SystemGroupForm&method=deleteProgram&id=$id\');$(this).closest(\'tr\').remove();";
  354. $btn->{'class'} = 'btn btn-default btn-sm';
  355. $btn->add($i);
  356. $tr = new TTableRow;
  357. $tr->{'class'} = 'tdatagrid_row_odd';
  358. $tr->{'style'} = 'width: 100%;display: inline-table;';
  359. $cell = $tr->addCell( $btn );
  360. $cell->{'style'}='text-align:center';
  361. $cell->{'class'}='tdatagrid_cell';
  362. $cell->{'width'} = '5%';
  363. $cell = $tr->addCell( $program->id );
  364. $cell->{'class'}='tdatagrid_cell';
  365. $cell->{'width'} = '10%';
  366. $cell = $tr->addCell( $program->name );
  367. $cell->{'class'}='tdatagrid_cell';
  368. $cell->{'width'} = '85%';
  369. TScript::create("tdatagrid_add_serialized_row('program_list', '$tr');");
  370. $data = new stdClass;
  371. $data->program_id = '';
  372. $data->program_name = '';
  373. TForm::sendData('form_System_user', $data);
  374. }
  375. }
  376. catch (Exception $e)
  377. {
  378. new TMessage('error', $e->getMessage());
  379. }
  380. }
  381. static function onChangeAction($param)
  382. {
  383. try
  384. {
  385. TTransaction::open('centinel'); //base de dados
  386. if ($organizacao_id)//se existe parâmetro da primeira combo(nome da primeira combo)
  387. {
  388. $criteria = TCriteria::create( ['organizacao_id' => $organizacao_id ] );
  389. //organizacao_id = parametro chave estrangeira da tabela master
  390. // formname,field(nome do campo),database , nome da model,key,value,ordercolumn = NULL,criteria, startEmpty = FALSE
  391. TDBCombo::reloadFromModel('form_System_user','organizacao_id','centinel','Organizacao','id','{juridica_nome}', 'juridica_nome',$criteria,TRUE);
  392. }
  393. else
  394. {
  395. TCombo::clearField('form_System_user', 'organizacao'); //reload do usuário form_System_user na check "organizacao_id"
  396. }
  397. TTransaction::close();
  398. }
  399. catch (Exception $e)
  400. {
  401. new TMessage('error', $e->getMessage());
  402. }
  403. }
  404. /**
  405. * Executed when a user change some item in list
  406. */
  407. public static function onChangeActionList($param)
  408. {
  409. new TMessage('info', 'Change action<br>'.
  410. 'List1: ' . implode(',', $param['list1']) . '<br>' .
  411. 'List2: ' . implode(',', $param['list2']));
  412. }
  413. /**
  414. * Send data
  415. */
  416. public function onSend($param)
  417. {
  418. // get form data
  419. $data = $this->form->getData();
  420. // put the data back to the form
  421. $this->form->setData($data);
  422. // creates a string with the form element's values
  423. $message = 'List 1: ' . implode(',', $data->list1) . '<br>';
  424. $message.= 'List 2 : ' . implode(',', $data->list2) . '<br>';
  425. // show the message
  426. new TMessage('info', $message);
  427. }
  428. }

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

Ao usar a função setFields somente os campos informados serão considerados pela classe, demais campos serão ignorados.
 
  1. <?php
  2. //$this->form->setFields(array($list1,$list2, $button1,$button2,$button3,$button4));
  3. $this->form->setFields(array_merge($this->form->getFields(),array($list1,$list2, $button1,$button2,$button3,$button4)));
  4. ?>
RB

Nataniel,

Funcionou perfeito, valeu ....

Muito obrigado.