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 mais de uma Tabela eu preciso fazer uma QUERY envolendo outras tabelas. Deixo minha classe.
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
- <?php $repository = new TRepository('AvaliacaoExames'); ?>
- <?php
- /**
- * CustomerDataGridView
- *
- * @version 1.0
- * @package samples
- * @subpackage tutor
- * @author Apolonio Santiago da Silva Junior
- * @copyright Copyright (c) 2006-2014 iCriaçoes Ltd. (http://www.icriacoes.com.br)
- * @license http://www.adianti.com.br/framework-license
- */
- class CSVPaciente extends TPage
- {
- private $form; // search form
- private $datagrid; // listing
- private $pageNavigation;
- private $loaded;
- /**
- * Class constructor
- * Creates the page, the search form and the listing
- */
- public function __construct()
- {
- parent::__construct();
- new TSession;
- // creates the form
- $this->form = new TForm('form_search_paciente');
- // create the form fields
- $paciente = new TEntry('codPaciente');
- $tipoExame = new TEntry('codTipoExame');
- $paciente->setSize(170);
- $tipoExame->setSize(126);
- $paciente->setValue(TSession::getValue('codPaciente'));
- $tipoExame->setValue(TSession::getValue('codTipoExame'));
- $table = new TTable;
- $row = $table->addRow();
- $cell=$row->addCell('');
- $cell->width= 80;
- $row->addCell($paciente);
- $cell=$row->addCell('');
- $row->addCell($tipoExame);
- $this->form->add($table);
- // creates the action button
- $button1=new TButton('find');
- $button1->setAction(new TAction(array($this, 'onSearch')), 'Buscar');
- $button1->setImage('ico_find.png');
- // $button2=new TButton('new');
- // $button2->setAction(new TAction(array('CustomerFormView', 'onEdit')), 'New');
- // $button2->setImage('ico_new.png');
- $button3=new TButton('csv');
- $button3->setAction(new TAction(array($this, 'onExportCSV')), 'CSV');
- $button3->setImage('ico_print.png');
- $row->addCell($button1);
- //$row->addCell($button2);
- $row->addCell($button3);
- //$this->form->setFields(array($name, $city_name, $button1, $button2, $button3));
- $this->form->setFields(array($paciente, $tipoExame, $button1, $button3));
- // creates a DataGrid
- $this->datagrid = new TQuickGrid;
- $this->datagrid->setHeight(200);
- // creates the datagrid columns
- $this->datagrid->addQuickColumn('Id', 'codExame', 'right', 40, new TAction(array($this, 'onReload')), array('order', 'codExame'));
- $this->datagrid->addQuickColumn('Paciente', 'codPaciente', 'left', 140, new TAction(array($this, 'onReload')), array('order', 'codPaciente'));
- $this->datagrid->addQuickColumn('Avaliador', 'codAvaliador', 'left', 170, new TAction(array($this, 'onReload')), array('order', 'codAvaliador'));
- $this->datagrid->addQuickColumn('Exame', 'codTipoExame', 'left', 140, new TAction(array($this, 'onReload')), array('order', 'codTipoExame'));
- $this->datagrid->addQuickColumn('Medida', 'medida', 'left', 140);
- $this->datagrid->addQuickColumn('Conclusao', 'conclusao', 'left', 190);
- $this->datagrid->addQuickColumn('Parecer', 'parecer', 'left', 190);
- $this->datagrid->addQuickColumn('Data', 'dataExames', 'left', 120);
- $this->datagrid->addQuickColumn('Wells', 'codWells', 'left', 120);
- // creates two datagrid actions
- // $this->datagrid->addQuickAction('Edit', new TDataGridAction(array('CustomerFormView', 'onEdit')), 'id', 'ico_edit.png');
- $this->datagrid->addQuickAction('Delete', new TDataGridAction(array($this, 'onDelete')), 'codExame', 'ico_delete.png');
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // creates the page structure using a vertical box
- $vbox = new TVBox;
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- $vbox->add($this->datagrid);
- $vbox->add($this->pageNavigation);
- // add the box inside the page
- parent::add($vbox);
- }
- /**
- * method onSearch()
- * Register the filter in the session when the user performs a search
- */
- function onSearch()
- {
- // get the search form data
- $data = $this->form->getData();
- // check if the user has filled the form
- if (isset($data->codPaciente) AND ($data->codTipoExame))
- {
- // creates a filter using what the user has typed
- $filter = new TFilter('codPaciente', 'like', "%{$data->codPaciente}%");
- // stores the filter in the session
- TSession::setValue('codPaciente', $filter);
- TSession::setValue('codTipoExame', $data->codTipoExame);
- }
- else
- {
- TSession::setValue('codPaciente', NULL);
- TSession::setValue('codTipoExame', '');
- }
- // check if the user has filled the form
- if ($data->codTipoExame)
- {
- // creates a filter using what the user has typed
- $filter = new TFilter('(SELECT descricaoTipoExame from system_tipo_exames)', 'like', "{$data->codTipoExame}%");
- // stores the filter in the session
- TSession::setValue('codTipoExame', $filter);
- TSession::setValue('codTipoExame', $data->codTipoExame);
- }
- else
- {
- TSession::setValue('codPaciente', NULL);
- TSession::setValue('codPaciente', '');
- }
- // fill the form with data again
- $this->form->setData($data);
- $param=array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'samples'
- TTransaction::open('permission');
- // creates a repository for Customer
- $repository = new TRepository('AvaliacaoExames');
- $limit = 20;
- // creates a criteria
- $criteria = new TCriteria;
- $newparam = $param; // define new parameters
- if (isset($newparam['order']) AND $newparam['order'] == 'system_tipo_exames->descricaoTipoExame')
- {
- $newparam['order'] = '(select descricaoTipoExame from system_tipo_exames)';
- }
- // default order
- if (empty($newparam['order']))
- {
- $newparam['order'] = 'codExame';
- $newparam['direction'] = 'asc';
- }
- $criteria->setProperties($newparam); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('codPaciente'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('codPaciente'));
- }
- if (TSession::getValue('codAvaliador'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('codAvaliador'));
- }
- // load the objects according to criteria
- $paciente = $repository->load( $criteria, FALSE);
- $this->datagrid->clear();
- if ($paciente)
- {
- foreach ($paciente as $pacientes)
- {
- // add the object inside the datagrid
- $this->datagrid->addItem($pacientes);
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count= $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- function onExportCSV()
- {
- $this->onSearch();
- try
- {
- // open a transaction with database 'samples'
- TTransaction::open('permission');
- // creates a repository for Customer
- $repository = new TRepository('AvaliacaoExames');
- // creates a criteria
- $criteria = new TCriteria;
- if (TSession::getValue('codPaciente'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('codPaciente'));
- }
- if (TSession::getValue('codAvaliador'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('codAvaliador'));
- }
- $csv = '';
- // load the objects according to criteria
- $customers = $repository->load($criteria);
- if ($customers)
- {
- $csv .= $customer.'id'.';'.
- $customer.'Paciente'.';'.
- $customer.'Avaliador'.';'.
- $customer.'Exame'.';'.
- $customer.'Tipo'.';'.
- $customer.'Medida'.';'.
- $customer.'Conclusao'.';'.
- $customer.'Parecer'."\n";
- foreach ($customers as $customer)
- {
- $csv .= $customer->codExame.';'.
- $customer->codPaciente.';'.
- $customer->codAvaliador.';'.
- $customer->codTipoExame.';'.
- $customer->medida.';'.
- $customer->conclusao.';'.
- $customer->parecer.';'.
- $customer->dataExames."\n";
- }
- file_put_contents('app/output/exames.csv', $csv);
- TPage::openFile('app/output/exames.csv');
- }
- // close the transaction
- TTransaction::close();
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onDelete()
- * executed whenever the user clicks at the delete button
- * Ask if the user really wants to delete the record
- */
- function onDelete($param)
- {
- // define the next action
- $action1 = new TAction(array($this, 'Delete'));
- $action1->setParameters($param); // pass 'key' parameter ahead
- // shows a dialog to the user
- new TQuestion('Você deseja realmente apagar o exame ?', $action1);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param)
- {
- try
- {
- // get the parameter $key
- $key=$param['key'];
- // open a transaction with database 'samples'
- TTransaction::open('permission');
- // instantiates object Customer
- $customer = new AvaliacaoExames($key);
- // deletes the object from the database
- $customer->delete();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload($param);
- // shows the success message
- new TMessage('info', "Exame apagado com sucesso!");
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method show()
- * Shows the page
- */
- function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded)
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
- ?>
Troque essa parte
Por esta