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. * Class constructor
  20. * Creates the page, the search form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. new TSession;
  26. // creates the form
  27. $this->form = new TForm('form_search_paciente');
  28. // create the form fields
  29. $paciente = new TEntry('codPaciente');
  30. $tipoExame = new TEntry('codTipoExame');
  31. $paciente->setSize(170);
  32. $tipoExame->setSize(126);
  33. $paciente->setValue(TSession::getValue('codPaciente'));
  34. $tipoExame->setValue(TSession::getValue('codTipoExame'));
  35. $table = new TTable;
  36. $row = $table->addRow();
  37. $cell=$row->addCell('');
  38. $cell->width= 80;
  39. $row->addCell($paciente);
  40. $cell=$row->addCell('');
  41. $row->addCell($tipoExame);
  42. $this->form->add($table);
  43. // creates the action button
  44. $button1=new TButton('find');
  45. $button1->setAction(new TAction(array($this, 'onSearch')), 'Buscar');
  46. $button1->setImage('ico_find.png');
  47. // $button2=new TButton('new');
  48. // $button2->setAction(new TAction(array('CustomerFormView', 'onEdit')), 'New');
  49. // $button2->setImage('ico_new.png');
  50. $button3=new TButton('csv');
  51. $button3->setAction(new TAction(array($this, 'onExportCSV')), 'CSV');
  52. $button3->setImage('ico_print.png');
  53. $row->addCell($button1);
  54. //$row->addCell($button2);
  55. $row->addCell($button3);
  56. //$this->form->setFields(array($name, $city_name, $button1, $button2, $button3));
  57. $this->form->setFields(array($paciente, $tipoExame, $button1, $button3));
  58. // creates a DataGrid
  59. $this->datagrid = new TQuickGrid;
  60. $this->datagrid->setHeight(200);
  61. // creates the datagrid columns
  62. $this->datagrid->addQuickColumn('Id', 'codExame', 'right', 40, new TAction(array($this, 'onReload')), array('order', 'codExame'));
  63. $this->datagrid->addQuickColumn('Paciente', 'codPaciente', 'left', 140, new TAction(array($this, 'onReload')), array('order', 'codPaciente'));
  64. $this->datagrid->addQuickColumn('Avaliador', 'codAvaliador', 'left', 170, new TAction(array($this, 'onReload')), array('order', 'codAvaliador'));
  65. $this->datagrid->addQuickColumn('Exame', 'codTipoExame', 'left', 140, new TAction(array($this, 'onReload')), array('order', 'codTipoExame'));
  66. $this->datagrid->addQuickColumn('Medida', 'medida', 'left', 140);
  67. $this->datagrid->addQuickColumn('Conclusao', 'conclusao', 'left', 190);
  68. $this->datagrid->addQuickColumn('Parecer', 'parecer', 'left', 190);
  69. $this->datagrid->addQuickColumn('Data', 'dataExames', 'left', 120);
  70. $this->datagrid->addQuickColumn('Wells', 'codWells', 'left', 120);
  71. // creates two datagrid actions
  72. // $this->datagrid->addQuickAction('Edit', new TDataGridAction(array('CustomerFormView', 'onEdit')), 'id', 'ico_edit.png');
  73. $this->datagrid->addQuickAction('Delete', new TDataGridAction(array($this, 'onDelete')), 'codExame', 'ico_delete.png');
  74. // create the datagrid model
  75. $this->datagrid->createModel();
  76. // creates the page navigation
  77. $this->pageNavigation = new TPageNavigation;
  78. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  79. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  80. // creates the page structure using a vertical box
  81. $vbox = new TVBox;
  82. $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  83. $vbox->add($this->form);
  84. $vbox->add($this->datagrid);
  85. $vbox->add($this->pageNavigation);
  86. // add the box inside the page
  87. parent::add($vbox);
  88. }
  89. /**
  90. * method onSearch()
  91. * Register the filter in the session when the user performs a search
  92. */
  93. function onSearch()
  94. {
  95. // get the search form data
  96. $data = $this->form->getData();
  97. // check if the user has filled the form
  98. if (isset($data->codPaciente) AND ($data->codTipoExame))
  99. {
  100. // creates a filter using what the user has typed
  101. $filter = new TFilter('codPaciente', 'like', "%{$data->codPaciente}%");
  102. // stores the filter in the session
  103. TSession::setValue('codPaciente', $filter);
  104. TSession::setValue('codTipoExame', $data->codTipoExame);
  105. }
  106. else
  107. {
  108. TSession::setValue('codPaciente', NULL);
  109. TSession::setValue('codTipoExame', '');
  110. }
  111. // check if the user has filled the form
  112. if ($data->codTipoExame)
  113. {
  114. // creates a filter using what the user has typed
  115. $filter = new TFilter('(SELECT descricaoTipoExame from system_tipo_exames)', 'like', "{$data->codTipoExame}%");
  116. // stores the filter in the session
  117. TSession::setValue('codTipoExame', $filter);
  118. TSession::setValue('codTipoExame', $data->codTipoExame);
  119. }
  120. else
  121. {
  122. TSession::setValue('codPaciente', NULL);
  123. TSession::setValue('codPaciente', '');
  124. }
  125. // fill the form with data again
  126. $this->form->setData($data);
  127. $param=array();
  128. $param['offset'] =0;
  129. $param['first_page']=1;
  130. $this->onReload($param);
  131. }
  132. /**
  133. * method onReload()
  134. * Load the datagrid with the database objects
  135. */
  136. function onReload($param = NULL)
  137. {
  138. try
  139. {
  140. // open a transaction with database 'samples'
  141. TTransaction::open('permission');
  142. // creates a repository for Customer
  143. $repository = new TRepository('AvaliacaoExames');
  144. $limit = 20;
  145. // creates a criteria
  146. $criteria = new TCriteria;
  147. $newparam = $param; // define new parameters
  148. if (isset($newparam['order']) AND $newparam['order'] == 'system_tipo_exames->descricaoTipoExame')
  149. {
  150. $newparam['order'] = '(select descricaoTipoExame from system_tipo_exames)';
  151. }
  152. // default order
  153. if (empty($newparam['order']))
  154. {
  155. $newparam['order'] = 'codExame';
  156. $newparam['direction'] = 'asc';
  157. }
  158. $criteria->setProperties($newparam); // order, offset
  159. $criteria->setProperty('limit', $limit);
  160. if (TSession::getValue('codPaciente'))
  161. {
  162. // add the filter stored in the session to the criteria
  163. $criteria->add(TSession::getValue('codPaciente'));
  164. }
  165. if (TSession::getValue('codAvaliador'))
  166. {
  167. // add the filter stored in the session to the criteria
  168. $criteria->add(TSession::getValue('codAvaliador'));
  169. }
  170. // load the objects according to criteria
  171. $paciente = $repository->load( $criteria, FALSE);
  172. $this->datagrid->clear();
  173. if ($paciente)
  174. {
  175. foreach ($paciente as $pacientes)
  176. {
  177. // add the object inside the datagrid
  178. $this->datagrid->addItem($pacientes);
  179. }
  180. }
  181. // reset the criteria for record count
  182. $criteria->resetProperties();
  183. $count= $repository->count($criteria);
  184. $this->pageNavigation->setCount($count); // count of records
  185. $this->pageNavigation->setProperties($param); // order, page
  186. $this->pageNavigation->setLimit($limit); // limit
  187. // close the transaction
  188. TTransaction::close();
  189. $this->loaded = true;
  190. }
  191. catch (Exception $e) // in case of exception
  192. {
  193. // shows the exception error message
  194. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  195. // undo all pending operations
  196. TTransaction::rollback();
  197. }
  198. }
  199. function onExportCSV()
  200. {
  201. $this->onSearch();
  202. try
  203. {
  204. // open a transaction with database 'samples'
  205. TTransaction::open('permission');
  206. // creates a repository for Customer
  207. $repository = new TRepository('AvaliacaoExames');
  208. // creates a criteria
  209. $criteria = new TCriteria;
  210. if (TSession::getValue('codPaciente'))
  211. {
  212. // add the filter stored in the session to the criteria
  213. $criteria->add(TSession::getValue('codPaciente'));
  214. }
  215. if (TSession::getValue('codAvaliador'))
  216. {
  217. // add the filter stored in the session to the criteria
  218. $criteria->add(TSession::getValue('codAvaliador'));
  219. }
  220. $csv = '';
  221. // load the objects according to criteria
  222. $customers = $repository->load($criteria);
  223. if ($customers)
  224. {
  225. $csv .= $customer.'id'.';'.
  226. $customer.'Paciente'.';'.
  227. $customer.'Avaliador'.';'.
  228. $customer.'Exame'.';'.
  229. $customer.'Tipo'.';'.
  230. $customer.'Medida'.';'.
  231. $customer.'Conclusao'.';'.
  232. $customer.'Parecer'."\n";
  233. foreach ($customers as $customer)
  234. {
  235. $csv .= $customer->codExame.';'.
  236. $customer->codPaciente.';'.
  237. $customer->codAvaliador.';'.
  238. $customer->codTipoExame.';'.
  239. $customer->medida.';'.
  240. $customer->conclusao.';'.
  241. $customer->parecer.';'.
  242. $customer->dataExames."\n";
  243. }
  244. file_put_contents('app/output/exames.csv', $csv);
  245. TPage::openFile('app/output/exames.csv');
  246. }
  247. // close the transaction
  248. TTransaction::close();
  249. }
  250. catch (Exception $e) // in case of exception
  251. {
  252. // shows the exception error message
  253. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  254. // undo all pending operations
  255. TTransaction::rollback();
  256. }
  257. }
  258. /**
  259. * method onDelete()
  260. * executed whenever the user clicks at the delete button
  261. * Ask if the user really wants to delete the record
  262. */
  263. function onDelete($param)
  264. {
  265. // define the next action
  266. $action1 = new TAction(array($this, 'Delete'));
  267. $action1->setParameters($param); // pass 'key' parameter ahead
  268. // shows a dialog to the user
  269. new TQuestion('Você deseja realmente apagar o exame ?', $action1);
  270. }
  271. /**
  272. * method Delete()
  273. * Delete a record
  274. */
  275. function Delete($param)
  276. {
  277. try
  278. {
  279. // get the parameter $key
  280. $key=$param['key'];
  281. // open a transaction with database 'samples'
  282. TTransaction::open('permission');
  283. // instantiates object Customer
  284. $customer = new AvaliacaoExames($key);
  285. // deletes the object from the database
  286. $customer->delete();
  287. // close the transaction
  288. TTransaction::close();
  289. // reload the listing
  290. $this->onReload($param);
  291. // shows the success message
  292. new TMessage('info', "Exame apagado com sucesso!");
  293. }
  294. catch (Exception $e) // in case of exception
  295. {
  296. // shows the exception error message
  297. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  298. // undo all pending operations
  299. TTransaction::rollback();
  300. }
  301. }
  302. /**
  303. * method show()
  304. * Shows the page
  305. */
  306. function show()
  307. {
  308. // check if the datagrid is already loaded
  309. if (!$this->loaded)
  310. {
  311. $this->onReload( func_get_arg(0) );
  312. }
  313. parent::show();
  314. }
  315. }
  316. ?>


Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework 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. foreach ($customers as $customer)
  6. {
  7. $csv .= $customer->codExame.';'.
  8. $customer->codPaciente.';'.
  9. $customer->codAvaliador.';'.
  10. $customer->codTipoExame.';'.
  11. $customer->medida.';'.
  12. $customer->conclusao.';'.
  13. $customer->parecer.';'.
  14. $customer->dataExames."\n";
  15. }
  16. }
  17. ?>