export.lib.inc.php 7.6 KB

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