export.lib.inc.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /* See license terms in /license.txt */
  3. use Chamilo\CoreBundle\Component\Editor\Connector;
  4. use MediaAlchemyst\Alchemyst;
  5. use MediaAlchemyst\DriversContainer;
  6. use Neutron\TemporaryFilesystem\Manager;
  7. use Neutron\TemporaryFilesystem\TemporaryFilesystem;
  8. use Sonata\Exporter\Handler;
  9. use Sonata\Exporter\Source\ArraySourceIterator;
  10. use Sonata\Exporter\Writer\CsvWriter;
  11. use Sonata\Exporter\Writer\XlsWriter;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. /**
  14. * This is the export library for Chamilo.
  15. * Include/require it in your code to use its functionality.
  16. */
  17. class Export
  18. {
  19. /**
  20. * Constructor.
  21. */
  22. public function __construct()
  23. {
  24. }
  25. /**
  26. * Export tabular data to CSV-file.
  27. *
  28. * @param array $data
  29. * @param string $filename
  30. * @param bool $writeOnly Whether to only write on disk or also send for download
  31. * @param string $enclosure
  32. *
  33. * @return mixed csv raw data | false if no data to export | string file path if success in $writeOnly mode
  34. */
  35. public static function arrayToCsv($data, $filename = 'export', $writeOnly = false, $enclosure = '"')
  36. {
  37. if (empty($data)) {
  38. return false;
  39. }
  40. $enclosure = !empty($enclosure) ? $enclosure : '"';
  41. $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
  42. $writer = new CsvWriter($filePath, ';', $enclosure, true);
  43. $source = new ArraySourceIterator($data);
  44. $handler = Handler::create($source, $writer);
  45. $handler->export();
  46. if (!$writeOnly) {
  47. DocumentManager::file_send_for_download($filePath, true, $filename.'.csv');
  48. exit;
  49. }
  50. return $filePath;
  51. }
  52. /**
  53. * Export tabular data to XLS-file.
  54. *
  55. * @param array $data
  56. * @param string $filename
  57. */
  58. public static function arrayToXls($data, $filename = 'export')
  59. {
  60. if (empty($data)) {
  61. return false;
  62. }
  63. $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx';
  64. $writer = new XlsWriter($filePath);
  65. $source = new ArraySourceIterator($data);
  66. $handler = Handler::create($source, $writer);
  67. $handler->export();
  68. DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
  69. exit;
  70. }
  71. /**
  72. * Export tabular data to XLS-file (as html table).
  73. *
  74. * @param array $data
  75. * @param string $filename
  76. */
  77. public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
  78. {
  79. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  80. $handle = fopen($file, 'a+');
  81. $systemEncoding = api_get_system_encoding();
  82. fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>');
  83. foreach ($data as $id => $row) {
  84. foreach ($row as $id2 => $row2) {
  85. $data[$id][$id2] = api_htmlentities($row2);
  86. }
  87. }
  88. foreach ($data as $row) {
  89. $string = implode("</td><td>", $row);
  90. $string = '<tr><td>'.$string.'</td></tr>';
  91. if ($encoding != 'utf-8') {
  92. $string = api_convert_encoding($string, $encoding, $systemEncoding);
  93. }
  94. fwrite($handle, $string."\n");
  95. }
  96. fwrite($handle, '</table></body></html>');
  97. fclose($handle);
  98. DocumentManager::file_send_for_download($file, true, $filename.'.xls');
  99. exit;
  100. }
  101. /**
  102. * Export tabular data to XML-file.
  103. *
  104. * @param array Simple array of data to put in XML
  105. * @param string Name of file to be given to the user
  106. * @param string Name of common tag to place each line in
  107. * @param string Name of the root element. A root element should always be given.
  108. * @param string Encoding in which the data is provided
  109. */
  110. public static function arrayToXml(
  111. $data,
  112. $filename = 'export',
  113. $item_tagname = 'item',
  114. $wrapper_tagname = null,
  115. $encoding = null
  116. ) {
  117. if (empty($encoding)) {
  118. $encoding = api_get_system_encoding();
  119. }
  120. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  121. $handle = fopen($file, 'a+');
  122. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  123. if (!is_null($wrapper_tagname)) {
  124. fwrite($handle, "\t".'<'.$wrapper_tagname.'>'."\n");
  125. }
  126. foreach ($data as $row) {
  127. fwrite($handle, '<'.$item_tagname.'>'."\n");
  128. foreach ($row as $key => $value) {
  129. fwrite($handle, "\t\t".'<'.$key.'>'.$value.'</'.$key.'>'."\n");
  130. }
  131. fwrite($handle, "\t".'</'.$item_tagname.'>'."\n");
  132. }
  133. if (!is_null($wrapper_tagname)) {
  134. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  135. }
  136. fclose($handle);
  137. DocumentManager::file_send_for_download($file, true, $filename.'.xml');
  138. exit;
  139. }
  140. /**
  141. * @param array $data table to be read with the HTML_table class
  142. */
  143. public static function export_table_pdf($data, $params = [])
  144. {
  145. $table_html = self::convert_array_to_html($data, $params);
  146. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  147. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  148. $pdf = new PDF($params['format'], $params['orientation'], $params);
  149. $pdf->html_to_pdf_with_template($table_html);
  150. }
  151. /**
  152. * @param string $html
  153. * @param array $params
  154. */
  155. public static function export_html_to_pdf($html, $params = [])
  156. {
  157. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  158. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  159. $pdf = new PDF($params['format'], $params['orientation'], $params);
  160. $pdf->html_to_pdf_with_template($html);
  161. }
  162. /**
  163. * @param array $data
  164. * @param array $params
  165. *
  166. * @return string
  167. */
  168. public static function convert_array_to_html($data, $params = [])
  169. {
  170. $headers = $data[0];
  171. unset($data[0]);
  172. $header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : [];
  173. $table = new HTML_Table(['class' => 'data_table', 'repeat_header' => '1']);
  174. $row = 0;
  175. $column = 0;
  176. foreach ($headers as $header) {
  177. $table->setHeaderContents($row, $column, $header);
  178. $attributes = [];
  179. if (isset($header_attributes) && isset($header_attributes[$column])) {
  180. $attributes = $header_attributes[$column];
  181. }
  182. if (!empty($attributes)) {
  183. $table->updateCellAttributes($row, $column, $attributes);
  184. }
  185. $column++;
  186. }
  187. $row++;
  188. foreach ($data as &$printable_data_row) {
  189. $column = 0;
  190. foreach ($printable_data_row as &$printable_data_cell) {
  191. $table->setCellContents($row, $column, $printable_data_cell);
  192. //$table->updateCellAttributes($row, $column, $atributes);
  193. $column++;
  194. }
  195. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  196. $row++;
  197. }
  198. $table_tp_html = $table->toHtml();
  199. return $table_tp_html;
  200. }
  201. /**
  202. * Export HTML content in a ODF document.
  203. *
  204. * @param string $html
  205. * @param string $name
  206. * @param string $format
  207. *
  208. * @return bool
  209. */
  210. public static function htmlToOdt($html, $name, $format = 'odt')
  211. {
  212. $unoconv = api_get_configuration_value('unoconv.binaries');
  213. if (empty($unoconv)) {
  214. return false;
  215. }
  216. if (!empty($html)) {
  217. $fs = new Filesystem();
  218. $paths = [
  219. 'root_sys' => api_get_path(SYS_PATH),
  220. 'path.temp' => api_get_path(SYS_ARCHIVE_PATH),
  221. ];
  222. $connector = new Connector();
  223. $drivers = new DriversContainer();
  224. $drivers['configuration'] = [
  225. 'unoconv.binaries' => $unoconv,
  226. 'unoconv.timeout' => 60,
  227. ];
  228. $tempFilesystem = TemporaryFilesystem::create();
  229. $manager = new Manager($tempFilesystem, $fs);
  230. $alchemyst = new Alchemyst($drivers, $manager);
  231. $dataFileSystem = new Data($paths, $fs, $connector, $alchemyst);
  232. $content = $dataFileSystem->convertRelativeToAbsoluteUrl($html);
  233. $filePath = $dataFileSystem->putContentInTempFile(
  234. $content,
  235. api_replace_dangerous_char($name),
  236. 'html'
  237. );
  238. $try = true;
  239. while ($try) {
  240. try {
  241. $convertedFile = $dataFileSystem->transcode(
  242. $filePath,
  243. $format
  244. );
  245. $try = false;
  246. DocumentManager::file_send_for_download(
  247. $convertedFile,
  248. false,
  249. $name.'.'.$format
  250. );
  251. } catch (Exception $e) {
  252. // error_log($e->getMessage());
  253. }
  254. }
  255. }
  256. }
  257. }