export.lib.inc.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. * This is the export library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * Several functions below are adaptations from functions distributed by www.nexen.net
  8. *
  9. * @package chamilo.library
  10. */
  11. /**
  12. * Code
  13. */
  14. require_once 'document.lib.php';
  15. require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
  16. /**
  17. *
  18. * @package chamilo.library
  19. */
  20. class Export
  21. {
  22. private function __construct()
  23. {
  24. }
  25. /**
  26. *
  27. * @deprecated use export_table_csv_utf8 instead
  28. */
  29. public static function export_table_csv ($data, $filename = 'export')
  30. {
  31. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
  32. $handle = @fopen($file, 'a+');
  33. if (is_array($data)) {
  34. foreach ($data as $index => $row) {
  35. $line = '';
  36. if (is_array($row)) {
  37. foreach($row as $value) {
  38. $line .= '"'.str_replace('"', '""', $value).'";';
  39. }
  40. }
  41. @fwrite($handle, $line."\n");
  42. }
  43. }
  44. @fclose($handle);
  45. DocumentManager :: file_send_for_download($file, true, $filename.'.csv');
  46. return false;
  47. }
  48. /**
  49. * Export tabular data to CSV-file
  50. * @param array $data
  51. * @param string $filename
  52. */
  53. public static function export_table_csv_utf8($data, $filename = 'export')
  54. {
  55. if(empty($data)) {
  56. return false;
  57. }
  58. $path = Chamilo::temp_file();
  59. $converter = new Utf8Encoder(null, true);
  60. $file = FileWriter::create($path, $converter);
  61. $file = CsvWriter::create($file);
  62. foreach ($data as $row) {
  63. $file->put($row);
  64. }
  65. $file->close();
  66. DocumentManager::file_send_for_download($path, true, $filename.'.csv');
  67. unlink($path);
  68. exit;
  69. }
  70. /**
  71. * Export tabular data to XLS-file
  72. * @param array $data
  73. * @param string $filename
  74. */
  75. public static function export_table_xls($data, $filename = 'export')
  76. {
  77. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  78. $handle = fopen($file, 'a+');
  79. foreach ($data as $row) {
  80. fwrite($handle, implode("\t", $row)."\n");
  81. }
  82. fclose($handle);
  83. DocumentManager::file_send_for_download($file, true, $filename.'.xls');
  84. }
  85. /**
  86. * Export tabular data to XML-file
  87. * @param array Simple array of data to put in XML
  88. * @param string Name of file to be given to the user
  89. * @param string Name of common tag to place each line in
  90. * @param string Name of the root element. A root element should always be given.
  91. * @param string Encoding in which the data is provided
  92. */
  93. public static function export_table_xml ($data, $filename = 'export', $item_tagname = 'item', $wrapper_tagname = null, $encoding = null)
  94. {
  95. if (empty($encoding)) {
  96. $encoding = api_get_system_encoding();
  97. }
  98. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  99. $handle = fopen($file, 'a+');
  100. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  101. if (!is_null($wrapper_tagname)) {
  102. fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
  103. }
  104. foreach ($data as $row) {
  105. fwrite($handle, '<'.$item_tagname.'>'."\n");
  106. foreach ($row as $key => $value) {
  107. fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
  108. }
  109. fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
  110. }
  111. if (!is_null($wrapper_tagname)) {
  112. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  113. }
  114. fclose($handle);
  115. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  116. return false;
  117. }
  118. /**
  119. * Export hierarchical tabular data to XML-file
  120. * @param array Hierarchical array of data to put in XML, each element presenting a 'name' and a 'value' property
  121. * @param string Name of file to be given to the user
  122. * @param string Name of common tag to place each line in
  123. * @param string Name of the root element. A root element should always be given.
  124. * @param string Encoding in which the data is provided
  125. * @return void Prompts the user for a file download
  126. */
  127. public static function export_complex_table_xml ($data, $filename = 'export', $wrapper_tagname, $encoding = 'ISO-8859-1')
  128. {
  129. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  130. $handle = fopen($file, 'a+');
  131. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  132. if (!is_null($wrapper_tagname)) {
  133. fwrite($handle, '<'.$wrapper_tagname.'>');
  134. }
  135. $s = self::_export_complex_table_xml_helper($data);
  136. fwrite($handle,$s);
  137. if (!is_null($wrapper_tagname)) {
  138. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  139. }
  140. fclose($handle);
  141. DocumentManager :: file_send_for_download($file, true, $filename.'.xml');
  142. return false;
  143. }
  144. /**
  145. * Helper for the hierarchical XML exporter
  146. * @param array Hierarhical array composed of elements of type ('name'=>'xyz','value'=>'...')
  147. * @param int Level of recursivity. Allows the XML to be finely presented
  148. * @return string The XML string to be inserted into the root element
  149. */
  150. public static function _export_complex_table_xml_helper ($data, $level = 1)
  151. {
  152. if (count($data)<1) { return '';}
  153. $string = '';
  154. foreach ($data as $row) {
  155. $string .= "\n".str_repeat("\t",$level).'<'.$row['name'].'>';
  156. if (is_array($row['value'])) {
  157. $string .= self::_export_complex_table_xml_helper($row['value'],$level+1)."\n";
  158. $string .= str_repeat("\t",$level).'</'.$row['name'].'>';
  159. } else {
  160. $string .= $row['value'];
  161. $string .= '</'.$row['name'].'>';
  162. }
  163. }
  164. return $string;
  165. }
  166. /**
  167. *
  168. * @param array table in array format to be read with the HTML_table class
  169. */
  170. public static function export_table_pdf($data, $params = array())
  171. {
  172. $table_html = self::convert_array_to_html($data, $params);
  173. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  174. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  175. $pdf = new PDF($params['format'], $params['orientation'], $params);
  176. $pdf->html_to_pdf_with_template($table_html);
  177. }
  178. /**
  179. * @param string $html
  180. * @param array $params
  181. */
  182. public static function export_html_to_pdf($html, $params = array())
  183. {
  184. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  185. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  186. $pdf = new PDF($params['format'], $params['orientation'], $params);
  187. $pdf->html_to_pdf_with_template($html);
  188. }
  189. /**
  190. * @param array $data
  191. * @param array $params
  192. * @return string
  193. */
  194. public static function convert_array_to_html($data, $params = array())
  195. {
  196. $headers = $data[0];
  197. unset($data[0]);
  198. $header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array();
  199. $table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1'));
  200. $row = 0;
  201. $column = 0;
  202. foreach ($headers as $header) {
  203. $table->setHeaderContents($row, $column, $header);
  204. $attributes = array();
  205. if (isset($header_attributes) && isset($header_attributes[$column])) {
  206. $attributes = $header_attributes[$column];
  207. }
  208. if (!empty($attributes)) {
  209. $table->updateCellAttributes($row, $column, $attributes);
  210. }
  211. $column++;
  212. }
  213. $row++;
  214. foreach ($data as &$printable_data_row) {
  215. $column = 0;
  216. foreach ($printable_data_row as &$printable_data_cell) {
  217. $table->setCellContents($row, $column, $printable_data_cell);
  218. //$table->updateCellAttributes($row, $column, $atributes);
  219. $column++;
  220. }
  221. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  222. $row++;
  223. }
  224. $table_tp_html = $table->toHtml();
  225. return $table_tp_html;
  226. }
  227. }