Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Gerando arquivo CSV com o nome das Colunas da Tabela e conteúdo. Ola a todos! Preciso gerar um arquivo CSV baseado em uma consulta que envolver varias tabelas (6) e após essa consulta gerar um arquivo csv. Mas estou com problemas na hora de usar a classe TPage com a function onExportCSV() exemplo do TUTOR. A classe gera o arquivo mas nao gera com os cabeçalhos que vem do banco e se possivel a minha busca envolve ...
AJ
Gerando arquivo CSV com o nome das Colunas da Tabela e conteúdo.  
Ola a todos! Preciso gerar um arquivo CSV baseado em uma consulta que envolver varias tabelas (6) e após essa consulta gerar um arquivo csv.
Mas estou com problemas na hora de usar a classe TPage com a function onExportCSV() exemplo do TUTOR. A classe gera o arquivo mas nao gera com os cabeçalhos que vem do banco e se possivel a minha busca envolve
  1. <?php   $repository = new TRepository('AvaliacaoExames'); ?>
mais de uma Tabela eu preciso fazer uma QUERY envolendo outras tabelas. Deixo minha classe.
  1. <?php
  2. /**
  3.  * CustomerDataGridView
  4.  *
  5.  * @version    1.0
  6.  * @package    samples
  7.  * @subpackage tutor
  8.  * @author    Apolonio Santiago da Silva Junior
  9.  * @copyright  Copyright (c) 2006-2014 iCriaçoes Ltd. (http://www.icriacoes.com.br)
  10.  * @license    http://www.adianti.com.br/framework-license
  11.  */
  12. class CSVPaciente extends TPage
  13. {
  14.     private $form;      // search form
  15.     private $datagrid;  // listing
  16.     private $pageNavigation;
  17.     private $loaded;
  18.     
  19.     /**
  20.      * Class constructor
  21.      * Creates the page, the search form and the listing
  22.      */
  23.     public function __construct()
  24.     {
  25.         parent::__construct();
  26.         new TSession;
  27.         
  28.         // creates the form
  29.         $this->form = new TForm('form_search_paciente');
  30.         
  31.         // create the form fields
  32.         $paciente   = new TEntry('codPaciente');
  33.         $tipoExame = new TEntry('codTipoExame');
  34.         
  35.         $paciente->setSize(170);
  36.         $tipoExame->setSize(126);
  37.         
  38.         $paciente->setValue(TSession::getValue('codPaciente'));
  39.         $tipoExame->setValue(TSession::getValue('codTipoExame'));
  40.         
  41.         $table = new TTable;
  42.         
  43.         $row $table->addRow();
  44.         $cell=$row->addCell('');
  45.         $cell->width80;
  46.         $row->addCell($paciente);
  47.         
  48.         $cell=$row->addCell('');
  49.         $row->addCell($tipoExame);
  50.         $this->form->add($table);
  51.         
  52.         // creates the action button
  53.         $button1=new TButton('find');
  54.         $button1->setAction(new TAction(array($this'onSearch')), 'Buscar');
  55.         $button1->setImage('ico_find.png');
  56.       //  $button2=new TButton('new');
  57.       //  $button2->setAction(new TAction(array('CustomerFormView', 'onEdit')), 'New');
  58.       //  $button2->setImage('ico_new.png');
  59.         
  60.         $button3=new TButton('csv');
  61.         $button3->setAction(new TAction(array($this'onExportCSV')), 'CSV');
  62.         $button3->setImage('ico_print.png');
  63.         
  64.         $row->addCell($button1);
  65.         //$row->addCell($button2);
  66.         $row->addCell($button3);
  67.         
  68.         //$this->form->setFields(array($name, $city_name, $button1, $button2, $button3));
  69.         $this->form->setFields(array($paciente$tipoExame$button1$button3));
  70.         
  71.         // creates a DataGrid
  72.         $this->datagrid = new TQuickGrid;
  73.         $this->datagrid->setHeight(200);
  74.         // creates the datagrid columns
  75.         $this->datagrid->addQuickColumn('Id''codExame''right'40, new TAction(array($this'onReload')), array('order''codExame'));
  76.         $this->datagrid->addQuickColumn('Paciente''codPaciente''left'140, new TAction(array($this'onReload')), array('order''codPaciente'));
  77.         $this->datagrid->addQuickColumn('Avaliador''codAvaliador''left'170, new TAction(array($this'onReload')), array('order''codAvaliador'));
  78.         $this->datagrid->addQuickColumn('Exame''codTipoExame''left'140, new TAction(array($this'onReload')), array('order''codTipoExame'));
  79.         $this->datagrid->addQuickColumn('Medida''medida''left'140);
  80.         $this->datagrid->addQuickColumn('Conclusao''conclusao''left'190);
  81.         $this->datagrid->addQuickColumn('Parecer''parecer''left'190);
  82.         $this->datagrid->addQuickColumn('Data''dataExames''left'120);
  83.         $this->datagrid->addQuickColumn('Wells''codWells''left'120);
  84.         // creates two datagrid actions
  85.       //  $this->datagrid->addQuickAction('Edit', new TDataGridAction(array('CustomerFormView', 'onEdit')), 'id', 'ico_edit.png');
  86.         $this->datagrid->addQuickAction('Delete', new TDataGridAction(array($this'onDelete')), 'codExame''ico_delete.png');
  87.         
  88.         // create the datagrid model
  89.         $this->datagrid->createModel();
  90.         
  91.         // creates the page navigation
  92.         $this->pageNavigation = new TPageNavigation;
  93.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  94.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  95.         
  96.         // creates the page structure using a vertical box
  97.         $vbox = new TVBox;
  98.         $vbox->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  99.         $vbox->add($this->form);
  100.         $vbox->add($this->datagrid);
  101.         $vbox->add($this->pageNavigation);
  102.         
  103.         // add the box inside the page
  104.         parent::add($vbox);
  105.     }
  106.     
  107.     /**
  108.      * method onSearch()
  109.      * Register the filter in the session when the user performs a search
  110.      */
  111.     function onSearch()
  112.     {
  113.         // get the search form data
  114.         $data $this->form->getData();
  115.         
  116.         // check if the user has filled the form
  117.         if (isset($data->codPaciente) AND ($data->codTipoExame))
  118.         {
  119.             // creates a filter using what the user has typed
  120.             $filter = new TFilter('codPaciente''like'"%{$data->codPaciente}%");
  121.             
  122.             // stores the filter in the session
  123.             TSession::setValue('codPaciente'$filter);
  124.             TSession::setValue('codTipoExame',   $data->codTipoExame);
  125.             
  126.         }
  127.         else
  128.         {
  129.             TSession::setValue('codPaciente'NULL);
  130.             TSession::setValue('codTipoExame',   '');
  131.         }
  132.         
  133.         
  134.         // check if the user has filled the form
  135.         if ($data->codTipoExame)
  136.         {
  137.             // creates a filter using what the user has typed
  138.             $filter = new TFilter('(SELECT descricaoTipoExame from system_tipo_exames)''like'"{$data->codTipoExame}%");
  139.             
  140.             // stores the filter in the session
  141.             TSession::setValue('codTipoExame'$filter);
  142.             TSession::setValue('codTipoExame'$data->codTipoExame);
  143.         }
  144.         else
  145.         {
  146.             TSession::setValue('codPaciente'NULL);
  147.             TSession::setValue('codPaciente''');
  148.         }
  149.         
  150.         // fill the form with data again
  151.         $this->form->setData($data);
  152.         
  153.         $param=array();
  154.         $param['offset']    =0;
  155.         $param['first_page']=1;
  156.         $this->onReload($param);
  157.     }
  158.     
  159.     /**
  160.      * method onReload()
  161.      * Load the datagrid with the database objects
  162.      */
  163.     function onReload($param NULL)
  164.     {
  165.         try
  166.         {
  167.             // open a transaction with database 'samples'
  168.             TTransaction::open('permission');
  169.             
  170.             // creates a repository for Customer
  171.             $repository = new TRepository('AvaliacaoExames');
  172.             $limit 20;
  173.             
  174.             // creates a criteria
  175.             $criteria = new TCriteria;
  176.             
  177.             $newparam $param// define new parameters
  178.             if (isset($newparam['order']) AND $newparam['order'] == 'system_tipo_exames->descricaoTipoExame')
  179.             {
  180.                 $newparam['order'] = '(select descricaoTipoExame from system_tipo_exames)';
  181.             }
  182.             
  183.             // default order
  184.             if (empty($newparam['order']))
  185.             {
  186.                 $newparam['order'] = 'codExame';
  187.                 $newparam['direction'] = 'asc';
  188.             }
  189.             
  190.             $criteria->setProperties($newparam); // order, offset
  191.             $criteria->setProperty('limit'$limit);
  192.             
  193.             if (TSession::getValue('codPaciente'))
  194.             {
  195.                 // add the filter stored in the session to the criteria
  196.                 $criteria->add(TSession::getValue('codPaciente'));
  197.             }
  198.             
  199.             if (TSession::getValue('codAvaliador'))
  200.             {
  201.                 // add the filter stored in the session to the criteria
  202.                 $criteria->add(TSession::getValue('codAvaliador'));
  203.             }   
  204.             
  205.             // load the objects according to criteria
  206.             $paciente $repository->load$criteriaFALSE);
  207.             $this->datagrid->clear();
  208.             
  209.             if ($paciente)
  210.             {
  211.                 foreach ($paciente as $pacientes)
  212.                 {
  213.                     // add the object inside the datagrid
  214.                     $this->datagrid->addItem($pacientes);
  215.                 }
  216.             }
  217.        
  218.             
  219.             // reset the criteria for record count
  220.             $criteria->resetProperties();
  221.             $count$repository->count($criteria);
  222.             
  223.             $this->pageNavigation->setCount($count); // count of records
  224.             $this->pageNavigation->setProperties($param); // order, page
  225.             $this->pageNavigation->setLimit($limit); // limit
  226.             
  227.             // close the transaction
  228.             TTransaction::close();
  229.             $this->loaded true;
  230.         }
  231.         catch (Exception $e// in case of exception
  232.         {
  233.             // shows the exception error message
  234.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  235.             // undo all pending operations
  236.             TTransaction::rollback();
  237.         }
  238.     }
  239.     
  240.     function onExportCSV()
  241.     {
  242.         $this->onSearch();
  243.         try
  244.         {
  245.             // open a transaction with database 'samples'
  246.             TTransaction::open('permission');
  247.             
  248.             // creates a repository for Customer
  249.             $repository = new TRepository('AvaliacaoExames');
  250.             
  251.             // creates a criteria
  252.             $criteria = new TCriteria;
  253.             
  254.             if (TSession::getValue('codPaciente'))
  255.             {
  256.                 // add the filter stored in the session to the criteria
  257.                 $criteria->add(TSession::getValue('codPaciente'));
  258.             }
  259.             
  260.             if (TSession::getValue('codAvaliador'))
  261.             {
  262.                 // add the filter stored in the session to the criteria
  263.                 $criteria->add(TSession::getValue('codAvaliador'));
  264.             }
  265.             
  266.             $csv '';
  267.             // load the objects according to criteria
  268.             $customers $repository->load($criteria);
  269.             if ($customers)
  270.             {
  271.                 $csv .= $customer.'id'.';'.
  272.                             $customer.'Paciente'.';'.
  273.                             $customer.'Avaliador'.';'.
  274.                             $customer.'Exame'.';'.
  275.                             $customer.'Tipo'.';'.
  276.                             $customer.'Medida'.';'.
  277.                             $customer.'Conclusao'.';'.
  278.                             $customer.'Parecer'."\n";
  279.                 foreach ($customers as $customer)
  280.                 {
  281.                     $csv .= $customer->codExame.';'.
  282.                             $customer->codPaciente.';'.
  283.                             $customer->codAvaliador.';'.
  284.                             $customer->codTipoExame.';'.
  285.                             $customer->medida.';'.
  286.                             $customer->conclusao.';'.
  287.                             $customer->parecer.';'.
  288.                             $customer->dataExames."\n";
  289.                 }
  290.                 file_put_contents('app/output/exames.csv'$csv);
  291.                 TPage::openFile('app/output/exames.csv');
  292.             }
  293.             // close the transaction
  294.             TTransaction::close();
  295.         }
  296.         catch (Exception $e// in case of exception
  297.         {
  298.             // shows the exception error message
  299.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  300.             // undo all pending operations
  301.             TTransaction::rollback();
  302.         }
  303.     }
  304.     
  305.     /**
  306.      * method onDelete()
  307.      * executed whenever the user clicks at the delete button
  308.      * Ask if the user really wants to delete the record
  309.      */
  310.     function onDelete($param)
  311.     {
  312.         // define the next action
  313.         $action1 = new TAction(array($this'Delete'));
  314.         $action1->setParameters($param); // pass 'key' parameter ahead
  315.         
  316.         // shows a dialog to the user
  317.         new TQuestion('Você deseja realmente apagar o exame ?'$action1);
  318.     }
  319.     
  320.     /**
  321.      * method Delete()
  322.      * Delete a record
  323.      */
  324.     function Delete($param)
  325.     {
  326.         try
  327.         {
  328.             // get the parameter $key
  329.             $key=$param['key'];
  330.             
  331.             // open a transaction with database 'samples'
  332.             TTransaction::open('permission');
  333.             
  334.             // instantiates object Customer
  335.             $customer = new AvaliacaoExames($key);
  336.             // deletes the object from the database
  337.             $customer->delete();
  338.             
  339.             // close the transaction
  340.             TTransaction::close();
  341.             
  342.             // reload the listing
  343.             $this->onReload($param);
  344.             // shows the success message
  345.             new TMessage('info'"Exame apagado com sucesso!");
  346.         }
  347.         catch (Exception $e// in case of exception
  348.         {
  349.             // shows the exception error message
  350.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  351.             // undo all pending operations
  352.             TTransaction::rollback();
  353.         }
  354.     }
  355.     /**
  356.      * method show()
  357.      * Shows the page
  358.      */
  359.     function show()
  360.     {
  361.         // check if the datagrid is already loaded
  362.         if (!$this->loaded)
  363.         {
  364.             $this->onReloadfunc_get_arg(0) );
  365.         }
  366.         parent::show();
  367.     }
  368. }
  369. ?>


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)


FC

Troque essa parte

  1. <?php
  2. if ($customers)
  3.             {
  4.                 $csv .= $customer.'id'.';'.
  5.                             $customer.'Paciente'.';'.
  6.                             $customer.'Avaliador'.';'.
  7.                             $customer.'Exame'.';'.
  8.                             $customer.'Tipo'.';'.
  9.                             $customer.'Medida'.';'.
  10.                             $customer.'Conclusao'.';'.
  11.                             $customer.'Parecer'."n";
  12.                 foreach ($customers as $customer)
  13.                 {
  14.                     $csv .= $customer->codExame.';'.
  15.                             $customer->codPaciente.';'.
  16.                             $customer->codAvaliador.';'.
  17.                             $customer->codTipoExame.';'.
  18.                             $customer->medida.';'.
  19.                             $customer->conclusao.';'.
  20.                             $customer->parecer.';'.
  21.                             $customer->dataExames."n";
  22.                 } 
  23. }
  24. ?>


Por esta

  1. <?php
  2. if ($customers)
  3.             {
  4.                 $csv 'id;Paciente;Avaliador;Exame;Tipo;Medida;Conclusao;Parecer'."\n";
  5.                 
  6.                 foreach ($customers as $customer)
  7.                 {
  8.                     $csv .= $customer->codExame.';'.
  9.                             $customer->codPaciente.';'.
  10.                             $customer->codAvaliador.';'.
  11.                             $customer->codTipoExame.';'.
  12.                             $customer->medida.';'.
  13.                             $customer->conclusao.';'.
  14.                             $customer->parecer.';'.
  15.                             $customer->dataExames."\n";
  16.                 } 
  17. }
  18. ?>