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.
#####################
#####################
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of QueriesList
- *
- * @author udson
- */
- class QueriesList extends TPage {
- private $form;
- private $loaded;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- if (TSession::getValue('logged') !== TRUE)
- {
- //throw new Exception('Sessão expirada, favor logar novamente');
- TApplication::gotoPage('FormLogin', '');
- }
- // creates the form
- $this->form = new TForm('form_Queries_Report');
- $this->form->class = 'tform';
- // creates a table
- $table = new TTable;
- $table-> width = '100%';
- // add the table inside the form
- $this->form->add($table);
- // create the form fields
- $start_date = new TDate('start_date');
- $start_date->setMask('yyyy-mm-dd');
- $start_date->setSize(80);
- $start_time_hour = new TCombo('start_time_hour');
- for ($n=0; $n<=24; $n++)
- {
- $itens1[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
- }
- $start_time_hour->addItems($itens1);
- $start_time_hour->setSize(45);
- $start_time_hour->setValue('');
- $start_time_min = new TCombo('start_time_min');
- for ($n=0; $n<=59; $n++)
- {
- $itens2[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
- }
- $start_time_min->addItems($itens2);
- $start_time_min->setSize(45);
- $start_time_min->setValue('');
- $end_date = new TDate('end_date');
- $end_date->setMask('yyyy-mm-dd');
- $end_date->setSize(80);
- $end_time_hour = new TCombo('end_time_hour');
- for ($n=0; $n<=24; $n++)
- {
- $itens3[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
- }
- $end_time_hour->addItems($itens3);
- $end_time_hour->setSize(45);
- $end_time_hour->setValue('');
- $end_time_min = new TCombo('end_time_min');
- for ($n=0; $n<=59; $n++)
- {
- $itens4[$n] = str_pad($n, 2, '0', STR_PAD_LEFT);
- }
- $end_time_min->addItems($itens4);
- $end_time_min->setSize(45);
- $end_time_min->setValue('');
- $number = new TEntry('number');
- $number->setSize(120);
- $operator = new TEntry('operator');
- $operator->setSize(80);
- $ip = new TEntry('ip');
- $ip->setSize(120);
- $rate = new TEntry('rate');
- $rate->setSize(80);
- $source = new TCombo('source');
- $source_options= array("HTTP" => "HTTP","SIP" => "SIP");
- $source->addItems($source_options);
- $source->setSize(80);
- $customer = new TDBMultiSearch('customer', 'easyconsulta', 'Customer', 'id', 'customer');
- $customer->setOperator('like');
- $customer->setSize(250);
- $customer->setMaxSize(1);
- $customer->setMinLength(3);
- // create an action button (save)
- $search=new TButton('search');
- $search->setAction(new TAction(array($this, 'onSearch')), 'Buscar');
- $search->setImage('ico_find.png');
- $csv=new TButton('CSV');
- $csv->setAction(new TAction(array($this, 'onExportCSV')), 'CSV');
- $csv->setImage('ico_print.png');
- //$pdf=new TButton('PDF');
- //$pdf->setAction(new TAction(array($this, 'onPDF')), 'PDF');
- //$pdf->setImage('ico_print.png');
- // add a row for the field name
- $row = $table->addRowSet(new TLabel('Filtro'), '');
- $row->class = 'tformtitle'; // CSS class
- $row = $table->addRow();
- $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);
- $row = $table->addRow();
- $row->addMultiCell('Ip: ', $ip, 'Tarifa: ', $rate, 'Origem: ', $source, /*'Cliente:', $customer,*/ $search, $csv);
- // add a row for the form action
- $row = $table->addRowSet();
- $row->class = 'tformaction';
- $row->addMultiCell(' ');
- // define wich are the form fields
- $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));
- //
- // creates a DataGrid
- $this->datagrid = new TQuickGrid;
- $this->datagrid->setHeight(400);
- $this->datagrid->makeScrollable();
- $cod = $this->datagrid->addQuickColumn('Cod.', 'id', 'center', 100 , new TAction(array($this, 'onReload')), array('order', 'id'));
- //$customer = $this->datagrid->addQuickColumn('Cliente', 'id_customer', 'center', 150 , new TAction(array($this, 'onReload')), array('order', 'id_customer'));
- $date_time = $this->datagrid->addQuickColumn('Data / Hora', 'date_time', 'center', 150 , new TAction(array($this, 'onReload')), array('order', 'date_time'));
- $number = $this->datagrid->addQuickColumn('Número', 'number', 'center', 150, new TAction(array($this, 'onReload')), array('order', 'number'));
- $operator = $this->datagrid->addQuickColumn('Operadora', 'operator', 'center', 80 , new TAction(array($this, 'onReload')), array('order', 'operator'));
- $ip = $this->datagrid->addQuickColumn('Ip', 'ip', 'center', 150, new TAction(array($this, 'onReload')), array('order', 'ip'));
- $rate = $this->datagrid->addQuickColumn('Tarifa', 'rate', 'center', 80, new TAction(array($this, 'onReload')), array('order', 'rate'));
- $source = $this->datagrid->addQuickColumn('Origem', 'source', 'center', 85, new TAction(array($this, 'onReload')), array('order', 'source'));
- $rate->setTransformer(array($this,'transformerCurrency'));
- //$customer->setTransformer(array($this,'transformerCustomer'));
- //$this->datagrid->addQuickAction('Editar', new TDataGridAction(array('CdrForm', 'onEdit')), 'id', 'ico_edit.png');
- //$this->datagrid->addQuickAction('Deletar', new TDataGridAction(array($this, 'onDelete')), 'id', 'ico_delete.png');
- $this->datagrid->createModel();
- $this->datagrid_footer = new TQuickGrid;
- $this->datagrid_footer->addQuickColumn('', 'total_records', 'left', 700);
- $this->datagrid_footer->addQuickColumn('', 'count', 'right', 165);
- $this->datagrid_footer->createModel();
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // wrap the page content using vertical box
- $vbox = new TVBox;
- if( TSession::getValue('user_type') == "Admin" ){
- $vbox->add( TMenuBar::newFromXML('app/templates/theme1/AdminMenu.xml') );
- $vbox->add(new TXMLBreadCrumb('app/templates/theme1/AdminMenu.xml', __CLASS__));
- } else {
- $vbox->add( TMenuBar::newFromXML('app/templates/theme1/UserMenu.xml') );
- $vbox->add(new TXMLBreadCrumb('app/templates/theme1/UserMenu.xml', __CLASS__));
- }
- $vbox->add( "<br />" );
- $vbox->add($this->form);
- $vbox->add($this->datagrid);
- $vbox->add($this->datagrid_footer);
- $vbox->add($this->pageNavigation);
- parent::add($vbox);
- }
- public function transformerCurrency($value, $object, $row)
- {
- if ($value){
- $value = "R$ " . number_format($value, 4, ',', '.');
- } else {
- $value=NULL;
- }
- return "<span style='color:#006400'> $value </span>";
- }
- public function transformerCustomer ($value, $object, $row){
- try{
- TTransaction::open('easyconsulta');
- $customer = new Customer($value);
- //return $customer->customer;
- return "<span style='color:#FF0000; font-size: 70%; font-weight: bold;'>$customer->customer </span>";
- TTransaction::close();
- } catch (Exception $e) {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- }
- }
- function onSearch($param)
- {
- $data = $this->form->getData();
- $this->form->setData($data);
- TSession::setValue('ql-start_date', NULL);
- TSession::setValue('ql-end_date', NULL);
- TSession::setValue('ql-number', NULL);
- TSession::setValue('ql-operator', NULL);
- TSession::setValue('ql-ip', NULL);
- TSession::setValue('ql-rate', NULL);
- TSession::setValue('ql-source', NULL);
- //TSession::setValue('ql-id_customer', NULL);
- if (!$data->start_time_hour){
- $data->start_time_hour = "00";
- }
- if (!$data->start_time_min){
- $data->start_time_min = "00";
- }
- if (!$data->end_time_hour){
- $data->end_time_hour = "23";
- }
- if (!$data->end_time_min){
- $data->end_time_min = "59";
- }
- if($data->start_date){
- $start_date = " date_time >= '" . $data->start_date . " " . $data->start_time_hour . ":" . $data->start_time_min . "' ";
- TSession::setValue('ql-start_date', $start_date);
- } else {
- $start_date = " date_time >= CURDATE() ";
- TSession::setValue('ql-start_date', $start_date);
- }
- if($data->end_date):
- $start_date = " date_time <= '" . $data->end_date . " " . $data->end_time_hour . ":" . $data->end_time_min . "' ";
- TSession::setValue('ql-end_date', $start_date);
- endif;
- if($data->number):
- $number = " number = '" . $data->number . "' ";
- TSession::setValue('ql-number', $number);
- endif;
- if($data->operator):
- $operator = " operator = '" . $data->operator . "' ";
- TSession::setValue('ql-operator', $operator);
- endif;
- if($data->ip):
- $ip = " ip = '" . $data->ip . "' ";
- TSession::setValue('ql-ip', $ip);
- endif;
- if($data->rate):
- $rate = " rate = '" . $data->rate . "' ";
- TSession::setValue('ql-rate', $rate);
- endif;
- if($data->source):
- $source = " source = '" . $data->source . "' ";
- TSession::setValue('ql-source', $source);
- endif;
- /*
- if($data->customer):
- foreach ( $data->customer as $key => $value ) {
- 1634_customer = $key;
- }
- $customer = " id_customer = '" . 1634_customer . "' ";
- TSession::setValue('ql-id_customer', $customer);
- endif;
- *
- */
- //var_dump($data->customer);
- if( TSession::getValue('user_type') == "User" ):
- 1634_customer = " id_customer = '" . TSession::getValue('id_customer') . "' ";
- TSession::setValue('ql-id_customer', 1634_customer);
- endif;
- $param=array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- function onReload($param=NULL)
- {
- try
- {
- $limit = 100;
- $criteria = new TCriteria;
- $newparam = $param;
- if (empty($newparam['order']))
- {
- $newparam['order'] = 'date_time';
- $newparam['direction'] = 'desc';
- } else {
- $newparam['direction'] = 'asc';
- }
- if (empty($newparam['offset']))
- {
- $newparam['offset'] = 0;
- }
- $criteria->setProperties($newparam);
- $criteria->setProperty('limit', $limit);
- if ( TSession::getValue('ql-start_date') ){
- $where[] = TSession::getValue('ql-start_date');
- }
- if ( TSession::getValue('ql-end_date') ){
- $where[] = TSession::getValue('ql-end_date');
- }
- if ( TSession::getValue('ql-number') ){
- $where[] = TSession::getValue('ql-number');
- }
- if ( TSession::getValue('ql-operator') ){
- $where[] = TSession::getValue('ql-operator');
- }
- if ( TSession::getValue('ql-ip') ){
- $where[] = TSession::getValue('ql-ip');
- }
- if ( TSession::getValue('ql-rate') ){
- $where[] = TSession::getValue('ql-rate');
- }
- if ( TSession::getValue('ql-source') ){
- $where[] = TSession::getValue('ql-source');
- }
- if ( TSession::getValue('ql-id_customer') ){
- $where[] = TSession::getValue('ql-id_customer');
- }
- $sql = "SELECT * FROM query_records ";
- $sql_count = "SELECT id FROM query_records ";
- if( isset( $where ) ){
- $sql .= ' WHERE '.implode( ' AND ',$where ) . "order by " . $newparam['order'] . " " . $newparam['direction'] . " limit " . $newparam['offset'] . "," . $limit . " ";
- $sql_count .= ' WHERE '.implode( ' AND ',$where );
- }
- //echo $sql . "<br />";
- TTransaction::open('easyconsulta');
- $conn = TTransaction::get();
- $result = $conn->query("$sql");
- $queries = $result->fetchAll(PDO::FETCH_OBJ);
- $result = $conn->query("$sql_count");
- $row = $result->fetchAll(PDO::FETCH_OBJ);
- $count = $result->rowCount();
- $this->datagrid->clear();
- $this->datagrid_footer->clear();
- if ($queries)
- {
- foreach ($queries as $query)
- {
- $this->datagrid->addItem($query);
- }
- }
- $criteria->resetProperties();
- $this->pageNavigation->setCount($count);
- $this->pageNavigation->setProperties($param);
- $this->pageNavigation->setLimit($limit);
- $registers = new stdClass();
- $registers->total_records = 'Total de registros: ';
- $registers->count = $count;
- $this->datagrid_footer->addItem($registers);
- TTransaction::close();
- $this->loaded=TRUE;
- } catch (Exception $e) {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- function onExportCSV()
- {
- $file ="app/output/queries.csv";
- $line = '';
- if(file_exists($file)) unlink($file);
- if ( TSession::getValue('ql-start_date') ){
- $where[] = TSession::getValue('ql-start_date');
- }
- if ( TSession::getValue('ql-end_date') ){
- $where[] = TSession::getValue('ql-end_date');
- }
- if ( TSession::getValue('ql-number') ){
- $where[] = TSession::getValue('ql-number');
- }
- if ( TSession::getValue('ql-operator') ){
- $where[] = TSession::getValue('ql-operator');
- }
- if ( TSession::getValue('ql-ip') ){
- $where[] = TSession::getValue('ql-ip');
- }
- if ( TSession::getValue('ql-rate') ){
- $where[] = TSession::getValue('ql-rate');
- }
- if ( TSession::getValue('ql-source') ){
- $where[] = TSession::getValue('ql-source');
- }
- /*
- if ( TSession::getValue('ql-id_customer') ){
- $where[] = TSession::getValue('ql-id_customer');
- }
- *
- */
- $sql = "SELECT * FROM query_records ";
- $sql_count = "SELECT id FROM query_records ";
- if( isset( $where ) ){
- $sql .= ' WHERE '.implode( ' AND ',$where );
- }
- //echo $sql . "<br />";
- TTransaction::open('easyconsulta');
- $conn = TTransaction::get();
- $result = $conn->query("$sql");
- $queries = $result->fetchAll(PDO::FETCH_OBJ);
- if($queries){
- $header="Item".';'.
- "Date / Hora".';'.
- "Numero".';'.
- "Operadora".';'.
- "Ip".';'.
- "Tarifa".';'.
- "Origem"."\\n";
- $this->onWriteCSV($file,$header);
- $item = 1;
- foreach ($queries as $query){
- $line = $item .';'.
- $query->date_time .';'.
- $query->number .';'.
- $query->operator .';'.
- $query->ip .';'.
- $query->rate .';'.
- $query->source . "\\n";
- $this->onWriteCSV($file,$line);
- $item++;
- }
- TPage::openFile($file);
- } else {
- new TMessage('info', 'Não existem dados para este filstro' . '.',NULL,'Status');
- }
- TTransaction::close();
- }
- function onWriteCSV($file,$line){
- $fp = fopen($file, "a");
- $write = fwrite($fp, $line);
- fclose($fp);
- }
- function show(){
- if (!$this->loaded)
- {
- $this->onSearch( func_get_arg(0) );
- parent::show();
- }
- }
- }
Troque onSearch() por onReload() dentro do show()