Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Como filtrar data da Busca para o datagrid Olá, Galera! Peço ajuda dos colegas, preciso parar a data da busca para o datagrid. ...
JA
Como filtrar data da Busca para o datagrid  
Olá, Galera!

Peço ajuda dos colegas, preciso parar a data da busca para o datagrid.

  1. <?php
  2. /**
  3.  * ExemploCheckList Listing
  4.  * @author  <your name here>
  5.  */
  6. class SystemAlunoFrequenciaUpdate extends TPage
  7. {
  8.     private $form// form
  9.     private $datagrid// listing
  10.     private $pageNavigation;
  11.     private $formgrid;
  12.     private $loaded;
  13.     private $deleteButton;
  14.     private static $database 'db_guiansoft';
  15.     private static $activeRecord 'SystemAlunoTurma';
  16.     private static $primaryKey 'id';
  17.     private static $formName 'form_system_aluno_frequencia';
  18.     /**
  19.      * Class constructor
  20.      * Creates the page, the form and the listing
  21.      */
  22.     public function __construct()
  23.     {
  24.         parent::__construct();
  25.         // creates the form
  26.         $this->form = new BootstrapFormBuilder(self::$formName);
  27.         // define the form title
  28.         $this->form->setFormTitle('Frequência');
  29.         $id              = new TEntry('id');
  30.         $periodo_id      = new TDBCombo('periodo_id''db_guiansoft''SystemPeriodo''id''nome');
  31.         $curso_id        = new TDBCombo('curso_id''db_guiansoft''SystemCurso''id''nome');
  32.         $modulo_id       = new TDBCombo('modulo_id''db_guiansoft''SystemModulo''id''nome');
  33.         $turma_id        = new TDBCombo('turma_id''db_guiansoft''SystemTurma''id''nome');
  34.         $situacao_id     = new TDBCombo('situacao_id''db_guiansoft''SystemSituacaoAlunoTurma''id''nome');
  35.         $data_frequencia = new TDate('data_frequencia');
  36.         $id->setSize(100);
  37.         $curso_id->setSize('100%');
  38.         $modulo_id->setSize('100%');
  39.         $turma_id->setSize('100%');
  40.         $situacao_id->setSize('100%');
  41.         $data_frequencia->setSize('100%');
  42.         $this->form->addFields( [new TLabel(('Período'))], [$periodo_id], [new TLabel(('Curso'))], [$curso_id] );
  43.         $this->form->addFields( [new TLabel(('Módulo'))], [$modulo_id], [new TLabel(('Turma'))], [$turma_id] );
  44.         $this->form->addFields( [new TLabel(('Situação'))], [$situacao_id], [new TLabel(('Data'))], [$data_frequencia] );
  45.         // keep the form filled during navigation with session data
  46.         $this->form->setDataTSession::getValue(__CLASS__.'_filter_data') );
  47.         $btn_onsearch $this->form->addAction('Buscar', new TAction([$this'onSearch']), 'fa:search');
  48.         $btn_onsearch->addStyleClass('btn');
  49.         $bt_marcar $this->form->addAction('Presente', new TAction([$this'onSelectedAll']), 'fa:check #ffffff');
  50.         $bt_marcar->addFunction("$('input:checkbox').prop('frequencia_check',true);");
  51.         $bt_marcar->addStyleClass('btn-success');  
  52.         
  53.         $bt_desmarcar $this->form->addAction('Faltou', new TAction([$this'offSelectedAll']), 'fa:check #ffffff');
  54.         $bt_desmarcar->addFunction("$('input:checkbox').prop('frequencia_check',false);");
  55.         $bt_desmarcar->addStyleClass('btn-danger');  
  56.         $btn_onsave $this->form->addAction('Salvar', new TAction([$this'onSearch']), 'fa:floppy-o #ffffff');
  57.         $btn_onsave->addStyleClass('btn-primary');
  58.         // creates a Datagrid
  59.         $this->datagrid = new TDataGrid;
  60.         $this->datagrid = new BootstrapDatagridWrapper($this->datagrid);
  61.         $this->datagrid->style 'width: 100%';
  62.         $this->datagrid->setHeight(320);
  63.         $column_id = new TDataGridColumn('id''Id''center' '70px');
  64.         $column_aluno_id = new TDataGridColumn('SystemAluno->name''Aluno''left');
  65.         $column_data_frequencia = new TDataGridColumn('data_frequencia''Data''left');
  66.         $column_frequencia_check = new TDataGridColumn('frequencia_check''Frequência''left');
  67.         $column_frequencia_check->setTransformer( function($value$object$row) {
  68.             $frequencia_check = new TCheckButton('check_'.$object->id);
  69.             $frequencia_check->setSize('70%');
  70.             $frequencia_check->setFormName(self::$formName);
  71.             $frequencia_check->setValue($value);
  72.             $frequencia_check->setIndexValue('on');
  73.             $frequencia_check->onchange 'mudarCor(this)';
  74.             $this->formgrid->addField($frequencia_check); // important
  75.             return $frequencia_check;
  76.         });
  77.         $order_id = new TAction(array($this'onReload'));
  78.         $order_id->setParameter('order''id');
  79.         $column_id->setAction($order_id);
  80.         $this->datagrid->addColumn($column_id);
  81.         $this->datagrid->addColumn($column_aluno_id);
  82.         $this->datagrid->addColumn($column_data_frequencia);
  83.         $this->datagrid->addColumn($column_frequencia_check);
  84.         // create the datagrid model
  85.         $this->datagrid->createModel();
  86.         // creates the page navigation
  87.         $this->pageNavigation = new TPageNavigation;
  88.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  89.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  90.         $this->datagrid->disableDefaultClick();
  91.         // put datagrid inside a form
  92.         $this->formgrid = new TForm;
  93.         $this->formgrid->add($this->datagrid);
  94.         $panel = new TPanelGroup;
  95.         $panel->add($this->formgrid);
  96.         $panel->addFooter($this->pageNavigation);
  97.         // vertical box container
  98.         $container = new TVBox;
  99.         $container->style 'width: 100%';
  100.         $container->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  101.         $container->add($this->form);
  102.         $container->add($panel);
  103.         parent::add($container);
  104.     }
  105.     /** 
  106.     * method onSelectedAll() 
  107.     * Executed when the user clicks at the marcar todos button 
  108.     */ 
  109.     function onSelectedAll() 
  110.     { 
  111.     $data $this->form->getData(); 
  112.     if($data->aluno_id != ''
  113.     { 
  114.     $data->frequencia_check 'true'
  115.     TButton::enableField('form_system_aluno_frequencia','bt_desmarcar'); 
  116.     //$this->onReload($data); 
  117.     }
  118.     } 
  119.     /** 
  120.     * method offSelectedAll() 
  121.     * Executed when the user clicks at the desmarcar todos button 
  122.     */ 
  123.     function offSelectedAll() 
  124.     { 
  125.     $data $this->form->getData();
  126.     if($data->aluno_id != ''
  127.     {  
  128.     $data->frequencia_check 'false'
  129.     TButton::disableField('form_system_aluno_frequencia','bt_desmarcar'); 
  130.     //$this->onReload($data); 
  131.     }
  132.     }    
  133.     public static function onSave($param)
  134.     {
  135.         $frequencia_check   $param['_field_frequencia_check'];
  136.         $value  $param['_field_value'];
  137.         $column $param['column'];
  138.         $parts  explode('_'$frequencia_check);
  139.         $id     end($parts);
  140.         try
  141.         {
  142.             // open transaction
  143.             TTransaction::open('db_guiansoft');
  144.             $class self::$activeRecord;
  145.             $object $class::find($id);
  146.             if ($object)
  147.             {
  148.                 $object->$column $value;
  149.                 $object->store();
  150.             }
  151.             // close transaction
  152.             TTransaction::close();
  153.         }
  154.         catch (Exception $e)
  155.         {
  156.             // show the exception message
  157.             new TMessage('error'$e->getMessage());
  158.         }
  159.     }
  160.     /**
  161.      * Register the filter in the session
  162.      */
  163.     public function onSearch()
  164.     {
  165.         // get the search form data
  166.         $data $this->form->getData();
  167.         $filters = [];
  168.         TSession::setValue(__CLASS__.'_filter_data'NULL);
  169.         TSession::setValue(__CLASS__.'_filters'NULL);
  170.         if (isset($data->id) AND ($data->id))
  171.         {
  172.             $filters[] = new TFilter('id''='$data->id);// create the filter
  173.         }
  174.         if (isset($data->periodo_id) AND ($data->periodo_id))
  175.         {
  176.             $filters[] = new TFilter('periodo_id''like'"%{$data->periodo_id}%");// create the filter
  177.         }
  178.         if (isset($data->curso_id) AND ($data->curso_id))
  179.         {
  180.             $filters[] = new TFilter('curso_id''like'"%{$data->curso_id}%");// create the filter
  181.         }
  182.         if (isset($data->modulo_id) AND ($data->modulo_id))
  183.         {
  184.             $filters[] = new TFilter('modulo_id''like'"%{$data->modulo_id}%");// create the filter
  185.         }
  186.         if (isset($data->turma_id) AND ($data->turma_id))
  187.         {
  188.             $filters[] = new TFilter('turma_id''like'"%{$data->turma_id}%");// create the filter
  189.         }
  190.         if (isset($data->situacao_id) AND ($data->situacao_id))
  191.         {
  192.             $filters[] = new TFilter('situacao_id''like'"%{$data->situacao_id}%");// create the filter
  193.         }
  194.         // fill the form with data again
  195.         $this->form->setData($data);
  196.         // keep the search data in the session
  197.         TSession::setValue(__CLASS__.'_filter_data'$data);
  198.         TSession::setValue(__CLASS__.'_filters'$filters);
  199.         $param=array();
  200.         $param['offset']    =0;
  201.         $param['first_page']=1;
  202.         $this->onReload($param);
  203.     }
  204.     /**
  205.      * Load the datagrid with data
  206.      */
  207.     public function onReload($param NULL)
  208.     {
  209.         try
  210.         {
  211.             // open a transaction with database 'exemplo1'
  212.             TTransaction::open(self::$database);
  213.             // creates a repository for CheckList
  214.             $repository = new TRepository(self::$activeRecord);
  215.             $limit 20;
  216.             // creates a criteria
  217.             $criteria = new TCriteria;
  218.             if (empty($param['order']))
  219.             {
  220.                 $param['order'] = 'id';
  221.             }
  222.             if (empty($param['direction']))
  223.             {
  224.                 $param['direction'] = 'desc';
  225.             }
  226.             $criteria->setProperties($param); // order, offset
  227.             $criteria->setProperty('limit'$limit);
  228.             if($filters TSession::getValue(__CLASS__.'_filters'))
  229.             {
  230.                 foreach ($filters as $filter)
  231.                 {
  232.                     $criteria->add($filter);
  233.                 }
  234.             }
  235.             // load the objects according to criteria
  236.             $objects $repository->load($criteriaFALSE);
  237.             $this->datagrid->clear();
  238.             if ($objects)
  239.             {
  240.                 // iterate the collection of active records
  241.                 foreach ($objects as $object)
  242.                 {
  243.                     // add the object inside the datagrid
  244.                     $this->datagrid->addItem($object);
  245.                 }
  246.             }
  247.             // reset the criteria for record count
  248.             $criteria->resetProperties();
  249.             $count$repository->count($criteria);
  250.             $this->pageNavigation->setCount($count); // count of records
  251.             $this->pageNavigation->setProperties($param); // order, page
  252.             $this->pageNavigation->setLimit($limit); // limit
  253.             // close the transaction
  254.             TTransaction::close();
  255.             $this->loaded true;
  256.         }
  257.         catch (Exception $e// in case of exception
  258.         {
  259.             // shows the exception error message
  260.             new TMessage('error'$e->getMessage());
  261.             // undo all pending operations
  262.             TTransaction::rollback();
  263.         }
  264.     }
  265.     public function onShow()
  266.     {
  267.     }
  268.     /**
  269.      * method show()
  270.      * Shows the page
  271.      */
  272.     public function show()
  273.     {
  274.         // frequencia_check if the datagrid is already loaded
  275.         if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'],  array('onReload''onSearch')))) )
  276.         {
  277.             if (func_num_args() > 0)
  278.             {
  279.                 $this->onReloadfunc_get_arg(0) );
  280.             }
  281.             else
  282.             {
  283.                 $this->onReload();
  284.             }
  285.         }
  286.         parent::show();
  287.     }
  288. }
  289. ?>


Att,
Jonathas Alves

Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (5)


NR

Consegue detalhar um pouco mais? Não entendi exatamente o que você quer fazer
JA

Olá, Nataniel!

SystemAlunoFrequenciaUpdate faz uma consulta:

Período, Curso, Módulo, Turma, Situação e Data;
OBS: Situação = situação do aluno na turma que pode ser cursando, cancelado, transferido, reprovado ou aprovado.

Data: data do registro da frequencia.

Essas informações para consulta estão na SystemAlunoTurma que retorna no list da SystemAlunoFrequenciaUpdate.

então queria armazenar para cada aluno que esta no list essas informações e se ele esta presente ou faltou.

tem dois botoes um presente em verde e faltou em vermelho ao clicar posso marcar todos como presente ou como faltou.

e em seguida gravo essas informações em SystemAlunoFrequencia.

essa é a ideia

Att,
Jonathas Alves

NR

Quando você clica em Buscar, a função onSearch guarda os dados dos formulário na sessão:
  1. <?php
  2. // onSearch
  3. $data $this->form->getData();
  4. TSession::setValue(__CLASS__.'_filter_data'$data);
  5. ?>

Com isso você consegue recuperar a data em outras funções da classe, como a onReload, onSelectedAll, etc
JA

Olá, Nataniel!
Obrigado pela atenção

Retorna o campo data_frequencia null
object(SystemAlunoTurma)#114 (9) { ["system_aluno":"SystemAlunoTurma":private]=> NULL ["system_periodo":"SystemAlunoTurma":private]=> NULL ["system_curso":"SystemAlunoTurma":private]=> NULL ["system_modulo":"SystemAlunoTurma":private]=> NULL ["system_turma":"SystemAlunoTurma":private]=> NULL ["system_situacao_turma":"SystemAlunoTurma":private]=> NULL ["data":protected]=> array(9) { ["id"]=> string(1) "2" ["matricula"]=> string(8) "20180001" ["aluno_turma_id"]=> string(1) "2" ["periodo_id"]=> string(1) "5" ["curso_id"]=> string(1) "2" ["modulo_id"]=> string(1) "2" ["turma_id"]=> string(1) "2" ["situacao_id"]=> string(1) "5" ["data_frequencia"]=> NULL } ["vdata":protected]=> NULL ["attributes":protected]=> array(0) { } } object(SystemAlunoTurma)#113 (9) { ["system_aluno":"SystemAlunoTurma":private]=> NULL ["system_periodo":"SystemAlunoTurma":private]=> NULL ["system_curso":"SystemAlunoTurma":private]=> NULL ["system_modulo":"SystemAlunoTurma":private]=> NULL ["system_turma":"SystemAlunoTurma":private]=> NULL ["system_situacao_turma":"SystemAlunoTurma":private]=> NULL ["data":protected]=> array(9) { ["id"]=> string(1) "3" ["matricula"]=> string(8) "20180001" ["aluno_turma_id"]=> string(1) "1" ["periodo_id"]=> string(1) "5" ["curso_id"]=> string(1) "2" ["modulo_id"]=> string(1) "2" ["turma_id"]=> string(1) "2" ["situacao_id"]=> string(1) "5" ["data_frequencia"]=> NULL } ["vdata":protected]=> NULL ["attributes":protected]=> array(0) { } }



Fiz essa alteração no código, mais ainda não estou conseguindo pegar o valor da data da frequência.
  1. <?php
  2. /**
  3.      * Register the filter in the session
  4.      */
  5.     public function onSearch()
  6.     {
  7.         // get the search form data
  8.         $data $this->form->getData();
  9.         
  10.         // clear session filters
  11.         TSession::setValue('_filter_perido',   NULL);
  12.         TSession::setValue('_filter_curso',   NULL);
  13.         TSession::setValue('_filter_modulo',   NULL);
  14.         TSession::setValue('_filter_turma',   NULL);
  15.         TSession::setValue('_filter_situacao',   NULL);
  16.         if (isset($data->periodo_id) AND ($data->periodo_id)) {
  17.             $filter = new TFilter('periodo_id''like'"%{$data->periodo_id}%"); // create the filter
  18.             TSession::setValue('_filter_periodo',   $filter); // stores the filter in the session
  19.         }
  20.         if (isset($data->curso_id) AND ($data->curso_id)) {
  21.             $filter = new TFilter('curso_id''like'"%{$data->curso_id}%"); // create the filter
  22.             TSession::setValue('_filter_curso',   $filter); // stores the filter in the session
  23.         }
  24.         if (isset($data->modulo_id) AND ($data->modulo_id)) {
  25.             $filter = new TFilter('modulo_id''like'"%{$data->modulo_id}%"); // create the filter
  26.             TSession::setValue('_filter_modulo',   $filter); // stores the filter in the session
  27.         }
  28.         if (isset($data->turma_id) AND ($data->turma_id)) {
  29.             $filter = new TFilter('turma_id''like'"%{$data->turma_id}%"); // create the filter
  30.             TSession::setValue('_filter_turma',   $filter); // stores the filter in the session
  31.         }
  32.         if (isset($data->situacao_id) AND ($data->situacao_id)) {
  33.             $filter = new TFilter('situacao_id''like'"%{$data->situacao_id}%"); // create the filter
  34.             TSession::setValue('_filter_situacao',   $filter); // stores the filter in the session
  35.         }
  36.         
  37.         // fill the form with data again
  38.         $this->form->setData($data);
  39.         
  40.         // keep the search data in the session
  41.         TSession::setValue('_filter_data'$data);
  42.         
  43.         $param=array();
  44.         $param['offset']    =0;
  45.         $param['first_page']=1;
  46.         $this->onReload($param);
  47.     }
  48.     
  49.     /**
  50.      * Load the datagrid with data
  51.      */
  52.     public function onReload($param NULL)
  53.     {
  54.         try
  55.         {
  56.             // open a transaction with database 'samples'
  57.             TTransaction::open('db_guiansoft');
  58.             TTransaction::setLogger(new TLoggerSTD); // standard output
  59.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  60.             
  61.             // creates a repository for Prospeccao
  62.             $repository = new TRepository('SystemAlunoTurma');
  63.             $limit 10;
  64.             // creates a criteria
  65.             $criteria = new TCriteria;
  66.             
  67.             // default order
  68.             if (empty($param['order']))
  69.             {
  70.                 $param['order'] = 'id';
  71.                 $param['direction'] = 'asc';
  72.             }
  73.             
  74.             $criteria->setProperties($param); // order, offset
  75.             $criteria->setProperty('limit'$limit);
  76.             
  77.             if (TSession::getValue('_filter_periodo')) {
  78.                 $criteria->add(TSession::getValue('_filter_periodo')); // add the session filter
  79.             }
  80.             if (TSession::getValue('_filter_curso')) {
  81.                 $criteria->add(TSession::getValue('_filter_curso')); // add the session filter
  82.             }
  83.             if (TSession::getValue('_filter_modulo')) {
  84.                 $criteria->add(TSession::getValue('_filter_modulo')); // add the session filter
  85.             }
  86.             if (TSession::getValue('_filter_turma')) {
  87.                 $criteria->add(TSession::getValue('_filter_turma')); // add the session filter
  88.             }
  89.             if (TSession::getValue('_filter_situacao')) {
  90.                 $criteria->add(TSession::getValue('_filter_situacao')); // add the session filter
  91.             }
  92.             
  93.             // load the objects according to criteria
  94.             $objects $repository->load($criteriaFALSE);
  95.             if (is_callable($this->transformCallback))
  96.             {
  97.                 call_user_func($this->transformCallback$objects$param);
  98.             }
  99.             
  100.             $this->datagrid->clear();
  101.             if ($objects)
  102.             {
  103.                 // iterate the collection of active records
  104.                 foreach ($objects as $object)
  105.                 {
  106.                     $object->data_frequencia TDate::date2br($object->data_frequencia);
  107.                     // add the object inside the datagrid
  108.                     $this->datagrid->addItem($object);
  109.                     var_dump($object);
  110.                 }
  111.             }
  112.             
  113.             // reset the criteria for record count
  114.             $criteria->resetProperties();
  115.             $count$repository->count($criteria);
  116.             
  117.             $this->pageNavigation->setCount($count); // count of records
  118.             $this->pageNavigation->setProperties($param); // order, page
  119.             $this->pageNavigation->setLimit($limit); // limit
  120.             
  121.             // close the transaction
  122.             TTransaction::close();
  123.             $this->loaded true;
  124.         }
  125.         catch (Exception $e// in case of exception
  126.         {
  127.             // shows the exception error message
  128.             new TMessage('error'$e->getMessage());
  129.             // undo all pending operations
  130.             TTransaction::rollback();
  131.         }
  132.     }
  133. ?>
NR

Jonathas, você está fazendo um var_dump da variável $object, que é uma instância da classe SystemAlunoTurma. Nessa variável você tem os valores de cada registro gravados no banco de dados. Se quiser adicionar outras informaçõe que não estejam no banco de dados, como uma data do formulário, você vai precisar especificar isso:
  1. <?php
  2. // iterate the collection of active records
  3. $dados_sessao TSession::getValue(__CLASS__.'_filter_data'); 
  4. foreach ($objects as $object)
  5. {
  6.      // $object->data_frequencia = TDate::date2br($object->data_frequencia); data frequencia nao existe ou é nula no banco de dados
  7.      $object->data_frequencia $dados_sessao->data_frequencia// pega valor do campo do formulario que foi salvo na sessao apos clicar em Buscar e chamar a funcao onSearch
  8.      // add the object inside the datagrid
  9.      $this->datagrid->addItem($object);
  10.      var_dump($object);
  11. }
  12. ?>