PageRenderTime 492ms CodeModel.GetById 108ms RepoModel.GetById 1ms app.codeStats 0ms

/application/datamapper/csv.php

https://bitbucket.org/cledisonf/ignitercms
PHP | 225 lines | 139 code | 24 blank | 62 comment | 25 complexity | 291f4cdd6a72a0c249f59190fd8d9160 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, CC-BY-SA-4.0
  1. <?php
  2. /**
  3. * CSV Extension for DataMapper classes.
  4. *
  5. * Quickly import and export a set of DataMapper models to-and-from CSV files.
  6. *
  7. * @license MIT License
  8. * @package DMZ-Included-Extensions
  9. * @category DMZ
  10. * @author Phil DeJarnett
  11. * @link http://www.overzealous.com/dmz/pages/extensions/csv.html
  12. * @version 1.0
  13. */
  14. // --------------------------------------------------------------------------
  15. /**
  16. * DMZ_CSV Class
  17. *
  18. * @package DMZ-Included-Extensions
  19. */
  20. class DMZ_CSV {
  21. /**
  22. * Convert a DataMapper model into an associative array.
  23. *
  24. * @param DataMapper $object The DataMapper Object to export
  25. * @param mixed filename The filename to export to, or a file pointer. If this is a file pointer, it will not be closed.
  26. * @param array $fields Array of fields to include. If empty, includes all database columns.
  27. * @param bool $include_header If FALSE the header is not exported with the CSV. Not recommended if planning to import this data.
  28. * @return bool TRUE on success, or FALSE on failure.
  29. */
  30. function csv_export($object, $filename, $fields = '', $include_header = TRUE)
  31. {
  32. // determine the correct field set.
  33. if(empty($fields))
  34. {
  35. $fields = $object->fields;
  36. }
  37. $success = TRUE;
  38. // determine if we need to open the file or not.
  39. if(is_string($filename))
  40. {
  41. // open the file, if possible.
  42. $fp = fopen($filename, 'w');
  43. if($fp === FALSE)
  44. {
  45. log_message('error', 'CSV Extension: Unable to open file ' . $filename);
  46. return FALSE;
  47. }
  48. }
  49. else
  50. {
  51. // assume file pointer.
  52. $fp = $filename;
  53. }
  54. if($include_header)
  55. {
  56. // Print out header line
  57. $success = fputcsv($fp, $fields);
  58. }
  59. if($success)
  60. {
  61. foreach($object as $o)
  62. {
  63. // convert each object into an array
  64. $result = array();
  65. foreach($fields as $f)
  66. {
  67. $result[] = $o->{$f};
  68. }
  69. // output CSV-formatted line
  70. $success = fputcsv($fp, $result);
  71. if(!$success)
  72. {
  73. // stop on first failure.
  74. break;
  75. }
  76. }
  77. }
  78. if(is_string($filename))
  79. {
  80. fclose($fp);
  81. }
  82. return $success;
  83. }
  84. /**
  85. * Import objects from a CSV file.
  86. *
  87. * Completely empty rows are automatically skipped, as are rows that
  88. * start with a # sign (assumed to be comments).
  89. *
  90. * @param DataMapper $object The type of DataMapper Object to import
  91. * @param mixed $filename Name of CSV file, or a file pointer.
  92. * @param array $fields If empty, the database fields are used. Otherwise used to limit what fields are saved.
  93. * @param boolean $header_row If true, the first line is assumed to be a header row. Defaults to true.
  94. * @param mixed $callback A callback method for each row. Can return FALSE on failure to save, or 'stop' to stop the import.
  95. * @return array Array of imported objects, or FALSE if unable to import.
  96. */
  97. function csv_import($object, $filename, $fields = '', $header_row = TRUE, $callback = NULL)
  98. {
  99. $class = get_class($object);
  100. if(empty($fields))
  101. {
  102. $fields = $object->fields;
  103. }
  104. // determine if we need to open the file or not.
  105. if(is_string($filename))
  106. {
  107. // open the file, if possible.
  108. $fp = fopen($filename, 'r');
  109. if($fp === FALSE)
  110. {
  111. log_message('error', 'CSV Extension: Unable to open file ' . $filename);
  112. return FALSE;
  113. }
  114. }
  115. else
  116. {
  117. // assume file pointer.
  118. $fp = $filename;
  119. }
  120. if(empty($callback))
  121. {
  122. $result = array();
  123. }
  124. else
  125. {
  126. $result = 0;
  127. }
  128. $columns = NULL;
  129. while(($data = fgetcsv($fp)) !== FALSE)
  130. {
  131. // get column names
  132. if(is_null($columns))
  133. {
  134. if($header_row)
  135. {
  136. // store header row for column names
  137. $columns = $data;
  138. // only include columns in $fields
  139. foreach($columns as $index => $name)
  140. {
  141. if( ! in_array($name, $fields))
  142. {
  143. // mark column as false to skip
  144. $columns[$index] = FALSE;
  145. }
  146. }
  147. continue;
  148. }
  149. else
  150. {
  151. $columns = $fields;
  152. }
  153. }
  154. // skip on comments and empty rows
  155. if(empty($data) || $data[0][0] == '#' || implode('', $data) == '')
  156. {
  157. continue;
  158. }
  159. // create the object to save
  160. $o = new $class();
  161. foreach($columns as $index => $key)
  162. {
  163. if(count($data) <= $index)
  164. {
  165. // more header columns than data columns
  166. break;
  167. }
  168. // skip columns that were determined to not be needed above.
  169. if($key === FALSE)
  170. {
  171. continue;
  172. }
  173. // finally, it's OK to save the data column.
  174. $o->{$key} = $data[$index];
  175. }
  176. if( empty($callback))
  177. {
  178. $result[] = $o;
  179. }
  180. else
  181. {
  182. $test = call_user_func($callback, $o);
  183. if($test === 'stop')
  184. {
  185. break;
  186. }
  187. if($test !== FALSE)
  188. {
  189. $result++;
  190. }
  191. }
  192. }
  193. if(is_string($filename))
  194. {
  195. fclose($fp);
  196. }
  197. return $result;
  198. }
  199. }
  200. /* End of file csv.php */
  201. /* Location: ./application/datamapper/csv.php */