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

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (1)


PD

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