PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/views/helpers/csv.php

https://github.com/jobzesage/Adres
PHP | 334 lines | 202 code | 52 blank | 80 comment | 39 complexity | cc534a581030ad3588bee3654a0f3877 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause-No-Nuclear-License-2014, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * CSV helper for cakePHP. Compatible with version 1.1.x.x and higher.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * Licensed under The MIT License
  8. *
  9. * @copyright Adam Royle
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. */
  12. class CsvHelper extends Helper {
  13. var $delimiter = ',';
  14. var $enclosure = '"';
  15. var $filename = null;
  16. var $line = array();
  17. var $buffer;
  18. /**
  19. * This option preserves leading zeros on numeric data when opened in Excel.
  20. * Use it ONLY when the csv file is going to be opened in Excel, as it uses
  21. * non-standard syntax, and will probably break in other systems.
  22. */
  23. var $preserveLeadingZerosInExcel = false;
  24. var $_tmpFile = false;
  25. function CsvHelper() {
  26. $this->clear();
  27. }
  28. /**
  29. * Adds a multi-dimensional array to the buffer.
  30. *
  31. * @param array $data Multi-dimensional array
  32. * @param boolean $addFieldNames Add a row of field names before adding data
  33. * @access public
  34. */
  35. function addGrid($data, $addFieldNames = true, $fieldList = null) {
  36. if (!$data) {
  37. return false;
  38. }
  39. if (@is_array(reset($row = reset($data)))) {
  40. // Array generated by cakePHP model
  41. // eg.
  42. // $data = array(array('Model' => array('field_name' => 'field value')), array('Model' => array('field_name' => 'field value')))
  43. $defaultModel = key($row);
  44. if ($this->filename === null) {
  45. $this->setFilename(Inflector::pluralize($defaultModel));
  46. }
  47. if ($fieldList) {
  48. $fields = array();
  49. foreach ($fieldList as $fieldName) {
  50. if (strpos($fieldName, '.')) {
  51. list($modelName, $fieldName) = explode('.', $fieldName);
  52. } else {
  53. $modelName = $defaultModel;
  54. }
  55. $fields[] = array(Inflector::humanize($modelName), $fieldName);
  56. }
  57. if ($addFieldNames){
  58. foreach ($fields as $field) {
  59. if ($field[0] != $defaultModel) {
  60. $this->addField($field[0].' '.Inflector::humanize($field[1]));
  61. } else {
  62. $this->addField(Inflector::humanize($field[1]));
  63. }
  64. }
  65. $this->endRow();
  66. }
  67. foreach ($data as $row) {
  68. foreach ($fields as $field) {
  69. @$this->addField($row[$field[0]][$field[1]]);
  70. }
  71. $this->endRow();
  72. }
  73. } else {
  74. if ($addFieldNames){
  75. foreach (reset($row) as $key => $value) {
  76. $this->addField(Inflector::humanize($key));
  77. }
  78. $this->endRow();
  79. }
  80. foreach ($data as $row) {
  81. $this->addRow($row[$defaultModel]);
  82. }
  83. }
  84. } else {
  85. // Regular 2-dimensional array (with or without keys).
  86. // eg.
  87. // $data = array(array('field_name' => 'field_value'), array('field_name' => 'field_value'))
  88. // or
  89. // $data = array(array('field value'), array('field value'))
  90. if ($fieldList) {
  91. if ($addFieldNames){
  92. foreach ($fieldList as $fieldName) {
  93. $this->addField(Inflector::humanize($fieldName));
  94. }
  95. $this->endRow();
  96. }
  97. foreach ($data as $row) {
  98. foreach ($fieldList as $fieldName) {
  99. @$this->addField($row[$fieldName]);
  100. }
  101. $this->endRow();
  102. }
  103. } else {
  104. if ($addFieldNames) {
  105. foreach (reset($data) as $key => $value) {
  106. $this->addField(Inflector::humanize($key));
  107. }
  108. $this->endRow();
  109. }
  110. foreach ($data as $row) {
  111. $this->addRow($row);
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Adds a single field value to the buffer. You must call $csv->endRow() to commit fields to the buffer.
  118. *
  119. * @param string $value Field value
  120. * @access public
  121. */
  122. function addField($value) {
  123. $this->line[] = $value;
  124. }
  125. /**
  126. * Commits the row of fields that were added by addField()
  127. *
  128. * @access public
  129. */
  130. function endRow() {
  131. $this->addRow($this->line);
  132. $this->line = array();
  133. }
  134. /**
  135. * Adds a single row to the buffer.
  136. *
  137. * @param array $row Data to be added
  138. * @access public
  139. */
  140. function addRow($row) {
  141. $row = (Array) $row;
  142. if ($this->preserveLeadingZerosInExcel) {
  143. // convert the number to a string formula
  144. foreach ($row as $key => $value){
  145. if (strlen($value) > 1 && $value[0] == '0' && is_numeric($value)) {
  146. $row[$key] = '="'.$value.'"';
  147. }
  148. }
  149. }
  150. fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
  151. }
  152. /**
  153. * Outputs headers
  154. *
  155. * @param string $filename Filename to save as
  156. * @access public
  157. */
  158. function renderHeaders($filename = null) {
  159. if (is_string($filename)) {
  160. $this->setFilename($filename);
  161. }
  162. if ($this->filename === null) {
  163. $this->filename = 'Data.csv';
  164. }
  165. if ($this->filename) {
  166. header("Content-disposition:attachment;filename=".$this->filename);
  167. }
  168. header("Content-type:application/vnd.ms-excel");
  169. }
  170. /**
  171. * Sets the output filename. Automatically appends .csv if necessary.
  172. *
  173. * @param string $filename Filename to save as
  174. * @access public
  175. */
  176. function setFilename($filename) {
  177. $this->filename = $filename;
  178. if (strtolower(substr($this->filename, -4)) != '.csv') {
  179. $this->filename .= '.csv';
  180. }
  181. }
  182. /**
  183. * Returns CSV string and clears internal buffer. Outputs headers() if necessary.
  184. *
  185. * @param mixed $outputHeaders Boolean to determine if should output headers, or a string to set the filename
  186. * @param string $to_encoding Encoding to use
  187. * @param string $from_encoding
  188. * @return string String CSV formatted string
  189. * @access public
  190. */
  191. function render($outputHeaders = true, $to_encoding = null, $from_encoding = "auto") {
  192. if ($outputHeaders) {
  193. if (is_string($outputHeaders)) {
  194. $this->setFilename($outputHeaders);
  195. }
  196. $this->renderHeaders();
  197. }
  198. if ($this->_tmpFile) {
  199. rewind($this->buffer);
  200. $output = '';
  201. while (!feof($this->buffer)) {
  202. $output .= fread($this->buffer, 8192);
  203. }
  204. fclose($this->buffer);
  205. } else {
  206. rewind($this->buffer);
  207. $output = stream_get_contents($this->buffer);
  208. }
  209. // get around excel bug (http://support.microsoft.com/kb/323626/)
  210. if (substr($output,0,2) == 'ID') {
  211. $pos = strpos($output, $this->delimiter);
  212. if ($pos === false) {
  213. $pos = strpos($output, "\n");
  214. }
  215. if ($pos !== false) {
  216. $output = $this->enclosure . substr($output, 0, $pos) . $this->enclosure . substr($output, $pos);
  217. }
  218. }
  219. if ($to_encoding) {
  220. $output = mb_convert_encoding($output, $to_encoding, $from_encoding);
  221. }
  222. $this->clear();
  223. return $this->output($output);
  224. }
  225. /**
  226. * Clears internal buffer. This is called automatically by CsvHelper::render()
  227. *
  228. * @access public
  229. */
  230. function clear() {
  231. $this->line = array();
  232. $this->buffer = @fopen('php://temp/maxmemory:'.(5*1024*1024), 'r+');
  233. if ($this->buffer === false) {
  234. $this->_tmpFile = true;
  235. $this->buffer = tmpfile();
  236. }
  237. }
  238. }
  239. /**
  240. * A PHP4 implementation of the equivalent PHP5 function
  241. *
  242. * See (http://www.php.net/manual/en/function.fputcsv.php) for details
  243. */
  244. if (!function_exists('fputcsv')) {
  245. function fputcsv(&$handle, $fields = array(), $delimiter = ',', $enclosure = '"') {
  246. $str = '';
  247. $escape_char = '\\';
  248. foreach ($fields as $value) {
  249. settype($value, 'string');
  250. if (strpos($value, $delimiter) !== false ||
  251. strpos($value, $enclosure) !== false ||
  252. strpos($value, "\n") !== false ||
  253. strpos($value, "\r") !== false ||
  254. strpos($value, "\t") !== false ||
  255. strpos($value, ' ') !== false) {
  256. $str2 = $enclosure;
  257. $escaped = 0;
  258. $len = strlen($value);
  259. for ($i=0;$i<$len;$i++) {
  260. if ($value[$i] == $escape_char) {
  261. $escaped = 1;
  262. } else if (!$escaped && $value[$i] == $enclosure) {
  263. $str2 .= $enclosure;
  264. } else {
  265. $escaped = 0;
  266. }
  267. $str2 .= $value[$i];
  268. }
  269. $str2 .= $enclosure;
  270. $str .= $str2.$delimiter;
  271. } else {
  272. $str .= $value.$delimiter;
  273. }
  274. }
  275. $str = substr($str,0,-1);
  276. $str .= "\n";
  277. return fwrite($handle, $str);
  278. }
  279. }
  280. ?>