Conheça as melhorias da versão 8.0, 8.1, 8.2!
Clique aqui para saber mais
function show() Caros usto a função show(), pra popular a datagrid no primeiro acesso à página, a função cumpre o papel, porém quando tenho paginação na datagrid, e tento trocar de página esta função faz com que limpe a datagrid. Segue código abaixo. ##################### ...
UP
function show()  
Fechado
Caros usto a função show(), pra popular a datagrid no primeiro acesso à página, a função cumpre o papel, porém quando tenho paginação na datagrid, e tento trocar de página esta função faz com que limpe a datagrid. Segue código abaixo.

#####################
 
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. /**
  8. * Description of QueriesList
  9. *
  10. * @author udson
  11. */
  12. class QueriesList extends TPage {
  13. private $form;
  14. private $loaded;
  15. /**
  16. * Class constructor
  17. * Creates the page and the registration form
  18. */
  19. function __construct()
  20. {
  21. parent::__construct();
  22. if (TSession::getValue('logged') !== TRUE)
  23. {
  24. //throw new Exception('Sessão expirada, favor logar novamente');
  25. TApplication::gotoPage('FormLogin', '');
  26. }
  27. // creates the form
  28. $this->form = new TForm('form_Queries_Report');
  29. $this->form->class = 'tform';
  30. // creates a table
  31. $table = new TTable;
  32. $table-> width = '100%';
  33. // add the table inside the form
  34. $this->form->add($table);
  35. // create the form fields
  36. $start_date = new TDate('start_date');
  37. $start_date->setMask('yyyy-mm-dd');
  38. $start_date->setSize(80);
  39. $start_time_hour = new TCombo('start_time_hour');
  40. for ($n=0; $n<=24; $n++)
  41. {
  42. $itens1[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
  43. }
  44. $start_time_hour->addItems($itens1);
  45. $start_time_hour->setSize(45);
  46. $start_time_hour->setValue('');
  47. $start_time_min = new TCombo('start_time_min');
  48. for ($n=0; $n<=59; $n++)
  49. {
  50. $itens2[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
  51. }
  52. $start_time_min->addItems($itens2);
  53. $start_time_min->setSize(45);
  54. $start_time_min->setValue('');
  55. $end_date = new TDate('end_date');
  56. $end_date->setMask('yyyy-mm-dd');
  57. $end_date->setSize(80);
  58. $end_time_hour = new TCombo('end_time_hour');
  59. for ($n=0; $n<=24; $n++)
  60. {
  61. $itens3[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
  62. }
  63. $end_time_hour->addItems($itens3);
  64. $end_time_hour->setSize(45);
  65. $end_time_hour->setValue('');
  66. $end_time_min = new TCombo('end_time_min');
  67. for ($n=0; $n<=59; $n++)
  68. {
  69. $itens4[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
  70. }
  71. $end_time_min->addItems($itens4);
  72. $end_time_min->setSize(45);
  73. $end_time_min->setValue('');
  74. $number = new TEntry('number');
  75. $number->setSize(120);
  76. $operator = new TEntry('operator');
  77. $operator->setSize(80);
  78. $ip = new TEntry('ip');
  79. $ip->setSize(120);
  80. $rate = new TEntry('rate');
  81. $rate->setSize(80);
  82. $source = new TCombo('source');
  83. $source_options= array("HTTP" => "HTTP","SIP" => "SIP");
  84. $source->addItems($source_options);
  85. $source->setSize(80);
  86. $customer = new TDBMultiSearch('customer', 'easyconsulta', 'Customer', 'id', 'customer');
  87. $customer->setOperator('like');
  88. $customer->setSize(250);
  89. $customer->setMaxSize(1);
  90. $customer->setMinLength(3);
  91. // create an action button (save)
  92. $search=new TButton('search');
  93. $search->setAction(new TAction(array($this, 'onSearch')), 'Buscar');
  94. $search->setImage('ico_find.png');
  95. $csv=new TButton('CSV');
  96. $csv->setAction(new TAction(array($this, 'onExportCSV')), 'CSV');
  97. $csv->setImage('ico_print.png');
  98. //$pdf=new TButton('PDF');
  99. //$pdf->setAction(new TAction(array($this, 'onPDF')), 'PDF');
  100. //$pdf->setImage('ico_print.png');
  101. // add a row for the field name
  102. $row = $table->addRowSet(new TLabel('Filtro'), '');
  103. $row->class = 'tformtitle'; // CSS class
  104. $row = $table->addRow();
  105. $row->addMultiCell('De: ', $start_date, '', $start_time_hour, '', $start_time_min, 'Até: ', '', $end_date, '', $end_time_hour, '', $end_time_min,'Número: ', $number,'Operadora: ', $operator);
  106. $row = $table->addRow();
  107. $row->addMultiCell('Ip:  ', $ip, 'Tarifa: ', $rate, 'Origem: ', $source, /*'Cliente:', $customer,*/ $search, $csv);
  108. // add a row for the form action
  109. $row = $table->addRowSet();
  110. $row->class = 'tformaction';
  111. $row->addMultiCell('  ');
  112. // define wich are the form fields
  113. $this->form->setFields(array($start_date,$start_time_hour,$start_time_min,$end_date,$end_time_hour,$end_time_min,$number,$operator,$ip,$rate,$source,$search,/*$customer,*/$csv));
  114. //
  115. // creates a DataGrid
  116. $this->datagrid = new TQuickGrid;
  117. $this->datagrid->setHeight(400);
  118. $this->datagrid->makeScrollable();
  119. $cod = $this->datagrid->addQuickColumn('Cod.', 'id', 'center', 100 , new TAction(array($this, 'onReload')), array('order', 'id'));
  120. //$customer = $this->datagrid->addQuickColumn('Cliente', 'id_customer', 'center', 150 , new TAction(array($this, 'onReload')), array('order', 'id_customer'));
  121. $date_time = $this->datagrid->addQuickColumn('Data / Hora', 'date_time', 'center', 150 , new TAction(array($this, 'onReload')), array('order', 'date_time'));
  122. $number = $this->datagrid->addQuickColumn('Número', 'number', 'center', 150, new TAction(array($this, 'onReload')), array('order', 'number'));
  123. $operator = $this->datagrid->addQuickColumn('Operadora', 'operator', 'center', 80 , new TAction(array($this, 'onReload')), array('order', 'operator'));
  124. $ip = $this->datagrid->addQuickColumn('Ip', 'ip', 'center', 150, new TAction(array($this, 'onReload')), array('order', 'ip'));
  125. $rate = $this->datagrid->addQuickColumn('Tarifa', 'rate', 'center', 80, new TAction(array($this, 'onReload')), array('order', 'rate'));
  126. $source = $this->datagrid->addQuickColumn('Origem', 'source', 'center', 85, new TAction(array($this, 'onReload')), array('order', 'source'));
  127. $rate->setTransformer(array($this,'transformerCurrency'));
  128. //$customer->setTransformer(array($this,'transformerCustomer'));
  129. //$this->datagrid->addQuickAction('Editar', new TDataGridAction(array('CdrForm', 'onEdit')), 'id', 'ico_edit.png');
  130. //$this->datagrid->addQuickAction('Deletar', new TDataGridAction(array($this, 'onDelete')), 'id', 'ico_delete.png');
  131. $this->datagrid->createModel();
  132. $this->datagrid_footer = new TQuickGrid;
  133. $this->datagrid_footer->addQuickColumn('', 'total_records', 'left', 700);
  134. $this->datagrid_footer->addQuickColumn('', 'count', 'right', 165);
  135. $this->datagrid_footer->createModel();
  136. $this->pageNavigation = new TPageNavigation;
  137. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  138. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  139. // wrap the page content using vertical box
  140. $vbox = new TVBox;
  141. if( TSession::getValue('user_type') == "Admin" ){
  142. $vbox->add( TMenuBar::newFromXML('app/templates/theme1/AdminMenu.xml') );
  143. $vbox->add(new TXMLBreadCrumb('app/templates/theme1/AdminMenu.xml', __CLASS__));
  144. } else {
  145. $vbox->add( TMenuBar::newFromXML('app/templates/theme1/UserMenu.xml') );
  146. $vbox->add(new TXMLBreadCrumb('app/templates/theme1/UserMenu.xml', __CLASS__));
  147. }
  148. $vbox->add( "<br />" );
  149. $vbox->add($this->form);
  150. $vbox->add($this->datagrid);
  151. $vbox->add($this->datagrid_footer);
  152. $vbox->add($this->pageNavigation);
  153. parent::add($vbox);
  154. }
  155. public function transformerCurrency($value, $object, $row)
  156. {
  157. if ($value){
  158. $value = "R$ " . number_format($value, 4, ',', '.');
  159. } else {
  160. $value=NULL;
  161. }
  162. return "<span style='color:#006400'> $value </span>";
  163. }
  164. public function transformerCustomer ($value, $object, $row){
  165. try{
  166. TTransaction::open('easyconsulta');
  167. $customer = new Customer($value);
  168. //return $customer->customer;
  169. return "<span style='color:#FF0000; font-size: 70%; font-weight: bold;'>$customer->customer </span>";
  170. TTransaction::close();
  171. } catch (Exception $e) {
  172. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  173. }
  174. }
  175. function onSearch($param)
  176. {
  177. $data = $this->form->getData();
  178. $this->form->setData($data);
  179. TSession::setValue('ql-start_date', NULL);
  180. TSession::setValue('ql-end_date', NULL);
  181. TSession::setValue('ql-number', NULL);
  182. TSession::setValue('ql-operator', NULL);
  183. TSession::setValue('ql-ip', NULL);
  184. TSession::setValue('ql-rate', NULL);
  185. TSession::setValue('ql-source', NULL);
  186. //TSession::setValue('ql-id_customer', NULL);
  187. if (!$data->start_time_hour){
  188. $data->start_time_hour = "00";
  189. }
  190. if (!$data->start_time_min){
  191. $data->start_time_min = "00";
  192. }
  193. if (!$data->end_time_hour){
  194. $data->end_time_hour = "23";
  195. }
  196. if (!$data->end_time_min){
  197. $data->end_time_min = "59";
  198. }
  199. if($data->start_date){
  200. $start_date = " date_time >= '" . $data->start_date . " " . $data->start_time_hour . ":" . $data->start_time_min . "' ";
  201. TSession::setValue('ql-start_date', $start_date);
  202. } else {
  203. $start_date = " date_time >= CURDATE() ";
  204. TSession::setValue('ql-start_date', $start_date);
  205. }
  206. if($data->end_date):
  207. $start_date = " date_time <= '" . $data->end_date . " " . $data->end_time_hour . ":" . $data->end_time_min . "' ";
  208. TSession::setValue('ql-end_date', $start_date);
  209. endif;
  210. if($data->number):
  211. $number = " number = '" . $data->number . "' ";
  212. TSession::setValue('ql-number', $number);
  213. endif;
  214. if($data->operator):
  215. $operator = " operator = '" . $data->operator . "' ";
  216. TSession::setValue('ql-operator', $operator);
  217. endif;
  218. if($data->ip):
  219. $ip = " ip = '" . $data->ip . "' ";
  220. TSession::setValue('ql-ip', $ip);
  221. endif;
  222. if($data->rate):
  223. $rate = " rate = '" . $data->rate . "' ";
  224. TSession::setValue('ql-rate', $rate);
  225. endif;
  226. if($data->source):
  227. $source = " source = '" . $data->source . "' ";
  228. TSession::setValue('ql-source', $source);
  229. endif;
  230. /*
  231. if($data->customer):
  232. foreach ( $data->customer as $key => $value ) {
  233. 1634_customer = $key;
  234. }
  235. $customer = " id_customer = '" . 1634_customer . "' ";
  236. TSession::setValue('ql-id_customer', $customer);
  237. endif;
  238. *
  239. */
  240. //var_dump($data->customer);
  241. if( TSession::getValue('user_type') == "User" ):
  242. 1634_customer = " id_customer = '" . TSession::getValue('id_customer') . "' ";
  243. TSession::setValue('ql-id_customer', 1634_customer);
  244. endif;
  245. $param=array();
  246. $param['offset'] =0;
  247. $param['first_page']=1;
  248. $this->onReload($param);
  249. }
  250. function onReload($param=NULL)
  251. {
  252. try
  253. {
  254. $limit = 100;
  255. $criteria = new TCriteria;
  256. $newparam = $param;
  257. if (empty($newparam['order']))
  258. {
  259. $newparam['order'] = 'date_time';
  260. $newparam['direction'] = 'desc';
  261. } else {
  262. $newparam['direction'] = 'asc';
  263. }
  264. if (empty($newparam['offset']))
  265. {
  266. $newparam['offset'] = 0;
  267. }
  268. $criteria->setProperties($newparam);
  269. $criteria->setProperty('limit', $limit);
  270. if ( TSession::getValue('ql-start_date') ){
  271. $where[] = TSession::getValue('ql-start_date');
  272. }
  273. if ( TSession::getValue('ql-end_date') ){
  274. $where[] = TSession::getValue('ql-end_date');
  275. }
  276. if ( TSession::getValue('ql-number') ){
  277. $where[] = TSession::getValue('ql-number');
  278. }
  279. if ( TSession::getValue('ql-operator') ){
  280. $where[] = TSession::getValue('ql-operator');
  281. }
  282. if ( TSession::getValue('ql-ip') ){
  283. $where[] = TSession::getValue('ql-ip');
  284. }
  285. if ( TSession::getValue('ql-rate') ){
  286. $where[] = TSession::getValue('ql-rate');
  287. }
  288. if ( TSession::getValue('ql-source') ){
  289. $where[] = TSession::getValue('ql-source');
  290. }
  291. if ( TSession::getValue('ql-id_customer') ){
  292. $where[] = TSession::getValue('ql-id_customer');
  293. }
  294. $sql = "SELECT * FROM query_records ";
  295. $sql_count = "SELECT id FROM query_records ";
  296. if( isset( $where ) ){
  297. $sql .= ' WHERE '.implode( ' AND ',$where ) . "order by " . $newparam['order'] . " " . $newparam['direction'] . " limit " . $newparam['offset'] . "," . $limit . " ";
  298. $sql_count .= ' WHERE '.implode( ' AND ',$where );
  299. }
  300. //echo $sql . "<br />";
  301. TTransaction::open('easyconsulta');
  302. $conn = TTransaction::get();
  303. $result = $conn->query("$sql");
  304. $queries = $result->fetchAll(PDO::FETCH_OBJ);
  305. $result = $conn->query("$sql_count");
  306. $row = $result->fetchAll(PDO::FETCH_OBJ);
  307. $count = $result->rowCount();
  308. $this->datagrid->clear();
  309. $this->datagrid_footer->clear();
  310. if ($queries)
  311. {
  312. foreach ($queries as $query)
  313. {
  314. $this->datagrid->addItem($query);
  315. }
  316. }
  317. $criteria->resetProperties();
  318. $this->pageNavigation->setCount($count);
  319. $this->pageNavigation->setProperties($param);
  320. $this->pageNavigation->setLimit($limit);
  321. $registers = new stdClass();
  322. $registers->total_records = 'Total de registros: ';
  323. $registers->count = $count;
  324. $this->datagrid_footer->addItem($registers);
  325. TTransaction::close();
  326. $this->loaded=TRUE;
  327. } catch (Exception $e) {
  328. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  329. TTransaction::rollback();
  330. }
  331. }
  332. function onExportCSV()
  333. {
  334. $file ="app/output/queries.csv";
  335. $line = '';
  336. if(file_exists($file)) unlink($file);
  337. if ( TSession::getValue('ql-start_date') ){
  338. $where[] = TSession::getValue('ql-start_date');
  339. }
  340. if ( TSession::getValue('ql-end_date') ){
  341. $where[] = TSession::getValue('ql-end_date');
  342. }
  343. if ( TSession::getValue('ql-number') ){
  344. $where[] = TSession::getValue('ql-number');
  345. }
  346. if ( TSession::getValue('ql-operator') ){
  347. $where[] = TSession::getValue('ql-operator');
  348. }
  349. if ( TSession::getValue('ql-ip') ){
  350. $where[] = TSession::getValue('ql-ip');
  351. }
  352. if ( TSession::getValue('ql-rate') ){
  353. $where[] = TSession::getValue('ql-rate');
  354. }
  355. if ( TSession::getValue('ql-source') ){
  356. $where[] = TSession::getValue('ql-source');
  357. }
  358. /*
  359. if ( TSession::getValue('ql-id_customer') ){
  360. $where[] = TSession::getValue('ql-id_customer');
  361. }
  362. *
  363. */
  364. $sql = "SELECT * FROM query_records ";
  365. $sql_count = "SELECT id FROM query_records ";
  366. if( isset( $where ) ){
  367. $sql .= ' WHERE '.implode( ' AND ',$where );
  368. }
  369. //echo $sql . "<br />";
  370. TTransaction::open('easyconsulta');
  371. $conn = TTransaction::get();
  372. $result = $conn->query("$sql");
  373. $queries = $result->fetchAll(PDO::FETCH_OBJ);
  374. if($queries){
  375. $header="Item".';'.
  376. "Date / Hora".';'.
  377. "Numero".';'.
  378. "Operadora".';'.
  379. "Ip".';'.
  380. "Tarifa".';'.
  381. "Origem"."\\n";
  382. $this->onWriteCSV($file,$header);
  383. $item = 1;
  384. foreach ($queries as $query){
  385. $line = $item .';'.
  386. $query->date_time .';'.
  387. $query->number .';'.
  388. $query->operator .';'.
  389. $query->ip .';'.
  390. $query->rate .';'.
  391. $query->source . "\\n";
  392. $this->onWriteCSV($file,$line);
  393. $item++;
  394. }
  395. TPage::openFile($file);
  396. } else {
  397. new TMessage('info', 'Não existem dados para este filstro' . '.',NULL,'Status');
  398. }
  399. TTransaction::close();
  400. }
  401. function onWriteCSV($file,$line){
  402. $fp = fopen($file, "a");
  403. $write = fwrite($fp, $line);
  404. fclose($fp);
  405. }
  406. function show(){
  407. if (!$this->loaded)
  408. {
  409. $this->onSearch( func_get_arg(0) );
  410. parent::show();
  411. }
  412. }
  413. }

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 (1)


PD

Troque onSearch() por onReload() dentro do show()