Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. <?php
  2.  
  3. class AcaoEtapaFotosDetalhe extends TPage {
  4.  
  5. private $form;
  6. private $datagrid;
  7.  
  8. public function __construct() {
  9. parent::__construct();
  10.  
  11. $this->form = new BootstrapFormBuilder('detalhe_acaoetapafotos');
  12. $this->form->setFormTitle('Detalhe de Fotos de Etapa da Ação');
  13. $this->form->class = 'detalhe_acaoetapafotos';
  14.  
  15. $id = new THidden('id');
  16. $acaoetapa_id = new THidden('acaoetapa_id');
  17. $acaoetapa_id->setValue(filter_input(INPUT_GET, 'fk'));
  18.  
  19. TTransaction::open('database');
  20. $nomeEtapa = new AcaoEtapaRecord(filter_input(INPUT_GET, 'fk'));
  21. TTransaction::close();
  22.  
  23. $nomeEtapa = new TLabel('<b>' . $nomeEtapa->nomeetapa . '</b>');
  24.  
  25. $descricaofoto = new TText('descricaofoto');
  26. $nomearquivo = new TFile('nomearquivo');
  27. $nomearquivo->setProperty('accept', 'image/jpeg');
  28. $latitude = new TEntry('latitude');
  29. $longitude = new TEntry('longitude');
  30.  
  31. //$nomearquivo->setMaxLength(50);
  32. $latitude->setMaxLength(20);
  33. $longitude->setMaxLength(20);
  34.  
  35. $latitude->{'placeholder'} = '90º';
  36. $longitude->{'placeholder'} = '90º';
  37.  
  38. $acaoetapa_id->addValidation('Ação Etapa' , new TRequiredValidator);
  39. $nomearquivo->addValidation('Foto' , new TRequiredValidator);
  40. $descricaofoto->addValidation('Descrição da Foto' , new TRequiredValidator);
  41.  
  42. $action1 = new TAction(array($this, 'onSave'));
  43. $action1->setParameter('key', '' . filter_input(INPUT_GET, 'key') . '');
  44. $action1->setParameter('fk', '' . filter_input(INPUT_GET, 'fk') . '');
  45. $action1->setParameter('did', '' . filter_input(INPUT_GET, 'did') . '');
  46.  
  47. $action2 = new TAction(array('AcaoEtapaDetalhe', 'onReload'));
  48. $action2->setParameter('key', '' . filter_input(INPUT_GET, 'key') . '');
  49. $action2->setParameter('fk', '' . filter_input(INPUT_GET, 'did') . '');
  50.  
  51. $this->form->addFields([$id]);
  52. $this->form->addFields([$acaoetapa_id]);
  53. $this->form->addFields([new TLabel('Nome da Etapa <i>*</i>')], [$nomeEtapa]);
  54. $this->form->addFields([new TLabel('Descrição da Foto <i>*</i>')], [$descricaofoto]);
  55. $this->form->addFields([new TLabel('Foto')], [$nomearquivo]);
  56. $this->form->addFields([new TLabel('Latitude')], [$latitude]);
  57. $this->form->addFields([new TLabel('Longitude')], [$longitude]);
  58.  
  59. $this->form->addFields([new TLabel('')], [TElement::tag('label', '<i>* Campos obrigatórios</i>' ) ]);
  60.  
  61. $this->form->addAction('Salvar', $action1, 'fa:save')->class = 'btn btn-sm btn-primary';
  62. $this->form->addAction('Voltar', $action2, 'fa:arrow-left')->class = 'btn btn-sm btn-primary';
  63.  
  64. $this->datagrid = new TDatagridTables;
  65.  
  66. $dgacaoetapa_id = new TDataGridColumn('nome_acaoEtapa', 'Ação Etapa', 'left', 200);
  67. $dgdescricaofoto = new TDataGridColumn('descricaofoto', 'Descrição da Foto', 'left', 200);
  68. $dgnomearquivo = new TDataGridColumn('icone_preview', 'Foto', 'left', 200);
  69. $dglatitude = new TDataGridColumn('latitude', 'Latitude', 'left', 200);
  70. $dglongitude = new TDataGridColumn('longitude', 'Longitude', 'left', 200);
  71.  
  72. $this->datagrid->addColumn($dgacaoetapa_id);
  73. $this->datagrid->addColumn($dgdescricaofoto);
  74. $this->datagrid->addColumn($dgnomearquivo);
  75. $this->datagrid->addColumn($dglatitude);
  76. $this->datagrid->addColumn($dglongitude);
  77.  
  78. $actionEdit = new TDataGridAction(array($this, 'onEdit'));
  79. $actionEdit->setButtonClass("btn btn-default");
  80. $actionEdit->setLabel('Editar');
  81. $actionEdit->setImage("fa:pencil-square-o blue fa-lg");
  82. $actionEdit->setField('id');
  83. $actionEdit->setFk('acaoetapa_id');
  84. $actionEdit->setParameter('did', filter_input(INPUT_GET,'did'));
  85.  
  86. $actionDelete = new TDataGridAction(array($this, 'onDelete'));
  87. $actionDelete->setButtonClass("btn btn-default");
  88. $actionDelete->setLabel('Deletar');
  89. $actionDelete->setImage("fa:trash-o red fa-lg");
  90. $actionDelete->setField('id');
  91. $actionDelete->setFk('acaoetapa_id');
  92. $actionDelete->setParameter('did', filter_input(INPUT_GET,'did'));
  93.  
  94. $this->datagrid->addAction($actionEdit);
  95. $this->datagrid->addAction($actionDelete);
  96.  
  97. $this->datagrid->createModel();
  98.  
  99. $container = new TVBox;
  100. $container->style = "width: 100%";
  101.  
  102. $container->add($this->form);
  103. $container->add(TPanelGroup::pack(NULL, $this->datagrid));
  104.  
  105. parent::add($container);
  106. }
  107.  
  108. function onReload()
  109. {
  110. TTransaction::open('database');
  111.  
  112. $repository = new TRepository('AcaoEtapaFotosRecord');
  113. $criteria = new TCriteria;
  114.  
  115. $criteria->add(new TFilter('acaoetapa_id', '=', filter_input(INPUT_GET, 'fk')));
  116.  
  117. $objects = $repository->load($criteria, FALSE);
  118.  
  119. $this->datagrid->clear();
  120.  
  121. if (!empty($objects)) {
  122. foreach ($objects as $object) {
  123. $this->datagrid->addItem($object);
  124. }
  125. }
  126.  
  127. TTransaction::close();
  128.  
  129. $this->loaded = true;
  130. }
  131.  
  132. function onDelete($param = NULL)
  133. {
  134. if (isset($param["key"])) {
  135.  
  136. $param = [
  137. "key" => $param[ "key" ],
  138. "fk" => $param[ "fk" ],
  139. "did" => $param[ "did" ],
  140. ];
  141.  
  142. $action_ok = new TAction([$this, "Delete"]);
  143. $action_cancel = new TAction([$this, "onReload"]);
  144.  
  145. $action_ok->setParameters( $param );
  146. $action_cancel->setParameters( $param );
  147.  
  148. $this->onReload( $param );
  149.  
  150. new TQuestion("Deseja remover o registro?", $action_ok, $action_cancel);
  151. }
  152.  
  153. }
  154.  
  155. function Delete($param = NULL)
  156. {
  157. $key = $param['key'];
  158.  
  159. try {
  160. TTransaction::open('database');
  161.  
  162. $object = new AcaoEtapaFotosRecord($key);
  163. $object->delete();
  164.  
  165. $this->onReload( $param );
  166.  
  167. new TMessage("info", "Registro deletado com sucesso!");
  168.  
  169. TTransaction::close();
  170. } catch (Exception $ex) {
  171. new TMessage('error', $ex->getMessage());
  172.  
  173. TTransaction::rollback();
  174. }
  175.  
  176. $this->onReload();
  177. }
  178.  
  179.  
  180. function onSave($param = NULL)
  181. {
  182.  
  183. try {
  184. TTransaction::open('database');
  185.  
  186. $this->form->validate();
  187.  
  188. $object = $this->form->getData('AcaoEtapaFotosRecord');
  189.  
  190. $source_file = 'tmp/' . $object->nomearquivo;
  191. $extensao = strtolower(pathinfo(strtolower($object->nomearquivo), PATHINFO_EXTENSION));
  192.  
  193. if ($extensao == 'jpg' || $extensao == 'jpeg') {
  194. $object->nomearquivo = 'temp';
  195. $object->store();
  196.  
  197. $caminho = 'app/images/fotos_etapas/foto_' . $object->id . '.' . $extensao;
  198.  
  199. $object->nomearquivo = 'foto_' . $object->id . '.' . $extensao;
  200.  
  201. rename($source_file, $caminho);
  202.  
  203. $object->store();
  204.  
  205. $action = new TAction( [ "AcaoEtapaFotosDetalhe", "onReload" ] );
  206. $action->setParameters( ["fk" => $param[ "fk" ], "did" => $param[ "did" ] ] );
  207.  
  208. new TMessage("info", "Registro salvo com sucesso!", $action);
  209. } else {
  210. new TMessage('error', 'Apenas imagens do tipo JPG e JPEG!');
  211. }
  212. TTransaction::close();
  213. } catch (Exception $ex) {
  214. new TMessage('error', $ex->getMessage());
  215.  
  216. TTransaction::rollback();
  217. }
  218. }
  219.  
  220. function onEdit($param = NULL)
  221. {
  222.  
  223. try {
  224.  
  225. if (isset($param['key'])) {
  226.  
  227. $key = $param['key'];
  228.  
  229. TTransaction::open('database');
  230.  
  231. $obj = new AcaoEtapaFotosRecord($key);
  232.  
  233. $this->form->setData($obj);
  234.  
  235. TTransaction::close();
  236.  
  237. }
  238.  
  239. } catch (Exception $ex) {
  240.  
  241. new TMessage("Error", "Ocorreu um erro ao tentar carregar o registro para edição!<br><br>" . $ex->getMessage());
  242.  
  243. TTransaction::rollback();
  244.  
  245. }
  246.  
  247. }
  248.  
  249. function show()
  250. {
  251.  
  252. $this->onReload();
  253.  
  254. parent::show();
  255.  
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement