export.lib.inc.php 7.8 KB

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