Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. <?php
  2.  
  3. namespace FacturaScripts\Core\Lib;
  4.  
  5. use FacturaScripts\Core\Base\ExportInterface;
  6.  
  7. class CSVExport implements ExportInterface
  8. {
  9.  
  10. use \FacturaScripts\Core\Base\Utils;
  11.  
  12. public function newDoc($model)
  13. {
  14. $tableData = [];
  15.  
  16. foreach ((array) $model as $key => $value) {
  17. if (is_string($value)) {
  18. $tableData[] = [$key, $this->fixHtml($value)];
  19. }
  20. }
  21.  
  22. $fp = fopen('doc.csv', 'w');
  23.  
  24. // Encabezado
  25. fputcsv($fp, ['key', 'value']);
  26.  
  27. // Cuerpo
  28. foreach ($tableData as $tableRow) {
  29. fputcsv($fp, $tableRow);
  30. }
  31.  
  32. fclose($fp);
  33. }
  34.  
  35. public function newListDoc($cursor, $columns)
  36. {
  37. $tableCols = [];
  38. $tableData = [];
  39.  
  40. if (!empty($cursor)) {
  41. // obtenemos las columnas
  42. foreach ($columns as $col) {
  43. $tableCols[$col->widget->fieldName] = $col->widget->fieldName;
  44. }
  45.  
  46. foreach ($cursor as $key => $row) {
  47. foreach ($tableCols as $col) {
  48. $value = $row->{$col};
  49.  
  50. if (is_string($value)) {
  51. $value = $this->fixHtml($value);
  52. }
  53.  
  54. $tableData[$key][] = $value;
  55. }
  56. }
  57.  
  58. $fp = fopen('list.csv', 'w');
  59.  
  60. // Encabezado
  61. fputcsv($fp, $tableCols);
  62.  
  63. // Cuerpo
  64. foreach ($tableData as $tableRow) {
  65. fputcsv($fp, $tableRow);
  66. }
  67.  
  68. fclose($fp);
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement