export.lib.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /* See license terms in /license.txt */
  3. use Ddeboer\DataImport\Writer\ExcelWriter;
  4. use Ddeboer\DataImport\Writer\CsvWriter;
  5. use Ddeboer\DataImport\Workflow;
  6. use Ddeboer\DataImport\Reader\CsvReader;
  7. use Ddeboer\DataImport\Reader\ArrayReader;
  8. use Ddeboer\DataImport\Writer\ArrayWriter;
  9. use Chamilo\CoreBundle\Component\Editor\Connector;
  10. use Chamilo\CoreBundle\Component\Filesystem\Data;
  11. use ChamiloSession as Session;
  12. use MediaAlchemyst\Alchemyst;
  13. use MediaAlchemyst\DriversContainer;
  14. use Neutron\TemporaryFilesystem\Manager;
  15. use Neutron\TemporaryFilesystem\TemporaryFilesystem;
  16. use Symfony\Component\Filesystem\Filesystem;
  17. /**
  18. * This is the export library for Chamilo.
  19. * Include/require it in your code to use its functionality.
  20. * Several functions below are adaptations from functions distributed by www.nexen.net
  21. *
  22. * @package chamilo.library
  23. */
  24. class Export
  25. {
  26. /**
  27. * Constructor
  28. */
  29. public function __construct()
  30. {
  31. }
  32. /**
  33. * Export tabular data to CSV-file
  34. * @param array $data
  35. * @param string $filename
  36. *
  37. * @return mixed csv file | false if no data to export
  38. */
  39. public static function arrayToCsv($data, $filename = 'export')
  40. {
  41. if (empty($data)) {
  42. return false;
  43. }
  44. $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.csv';
  45. $writer = new CsvWriter();
  46. $writer->setStream(fopen($filePath, 'w'));
  47. foreach ($data as $item) {
  48. $item = array_map('trim', $item);
  49. $writer->writeItem($item);
  50. }
  51. $writer->finish();
  52. DocumentManager::file_send_for_download($filePath, true, $filename.'.csv');
  53. exit;
  54. }
  55. /**
  56. * Export tabular data to XLS-file
  57. * @param array $data
  58. * @param string $filename
  59. */
  60. public static function arrayToXls($data, $filename = 'export', $encoding = 'utf-8')
  61. {
  62. $filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx';
  63. $file = new \SplFileObject($filePath, 'w');
  64. $writer = new ExcelWriter($file);
  65. $writer->prepare();
  66. foreach ($data as $index => $row) {
  67. $writer->writeItem($row);
  68. }
  69. $writer->finish();
  70. DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
  71. exit;
  72. }
  73. /**
  74. * Export tabular data to XLS-file (as html table)
  75. * @param array $data
  76. * @param string $filename
  77. */
  78. public static function export_table_xls_html($data, $filename = 'export', $encoding = 'utf-8')
  79. {
  80. $file = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xls';
  81. $handle = fopen($file, 'a+');
  82. $systemEncoding = api_get_system_encoding();
  83. fwrite($handle, '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html" charset="'.$encoding.'" /><body><table>');
  84. foreach ($data as $id => $row) {
  85. foreach ($row as $id2 => $row2) {
  86. $data[$id][$id2] = api_htmlentities($row2);
  87. }
  88. }
  89. foreach ($data as $row) {
  90. $string = implode("</td><td>", $row);
  91. $string = '<tr><td>'.$string.'</td></tr>';
  92. if ($encoding != 'utf-8') {
  93. $string = api_convert_encoding($string, $encoding, $systemEncoding);
  94. }
  95. fwrite($handle, $string."\n");
  96. }
  97. fwrite($handle, '</table></body></html>');
  98. fclose($handle);
  99. DocumentManager::file_send_for_download($file, true, $filename.'.xls');
  100. exit;
  101. }
  102. /**
  103. * Export tabular data to XML-file
  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. * Export hierarchical tabular data to XML-file
  142. * @param array Hierarchical array of data to put in XML, each element presenting a 'name' and a 'value' property
  143. * @param string Name of file to be given to the user
  144. * @param string Name of common tag to place each line in
  145. * @param string Name of the root element. A root element should always be given.
  146. * @param string Encoding in which the data is provided
  147. * @return void Prompts the user for a file download
  148. */
  149. public static function export_complex_table_xml(
  150. $data,
  151. $filename = 'export',
  152. $wrapper_tagname,
  153. $encoding = 'ISO-8859-1'
  154. ) {
  155. $file = api_get_path(SYS_ARCHIVE_PATH).'/'.uniqid('').'.xml';
  156. $handle = fopen($file, 'a+');
  157. fwrite($handle, '<?xml version="1.0" encoding="'.$encoding.'"?>'."\n");
  158. if (!is_null($wrapper_tagname)) {
  159. fwrite($handle, '<'.$wrapper_tagname.'>');
  160. }
  161. $s = self::_export_complex_table_xml_helper($data);
  162. fwrite($handle, $s);
  163. if (!is_null($wrapper_tagname)) {
  164. fwrite($handle, '</'.$wrapper_tagname.'>'."\n");
  165. }
  166. fclose($handle);
  167. DocumentManager::file_send_for_download($file, true, $filename.'.xml');
  168. return false;
  169. }
  170. /**
  171. * Helper for the hierarchical XML exporter
  172. * @param array Hierarhical array composed of elements of type ('name'=>'xyz','value'=>'...')
  173. * @param int Level of recursivity. Allows the XML to be finely presented
  174. * @return string The XML string to be inserted into the root element
  175. */
  176. public static function _export_complex_table_xml_helper($data, $level = 1)
  177. {
  178. if (count($data) < 1) {
  179. return '';
  180. }
  181. $string = '';
  182. foreach ($data as $row) {
  183. $string .= "\n".str_repeat("\t", $level).'<'.$row['name'].'>';
  184. if (is_array($row['value'])) {
  185. $string .= self::_export_complex_table_xml_helper($row['value'], $level + 1)."\n";
  186. $string .= str_repeat("\t", $level).'</'.$row['name'].'>';
  187. } else {
  188. $string .= $row['value'];
  189. $string .= '</'.$row['name'].'>';
  190. }
  191. }
  192. return $string;
  193. }
  194. /**
  195. * @param array $data table to be read with the HTML_table class
  196. */
  197. public static function export_table_pdf($data, $params = array())
  198. {
  199. $table_html = self::convert_array_to_html($data, $params);
  200. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  201. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  202. $pdf = new PDF($params['format'], $params['orientation'], $params);
  203. $pdf->html_to_pdf_with_template($table_html);
  204. }
  205. /**
  206. * @param string $html
  207. * @param array $params
  208. */
  209. public static function export_html_to_pdf($html, $params = array())
  210. {
  211. $params['format'] = isset($params['format']) ? $params['format'] : 'A4';
  212. $params['orientation'] = isset($params['orientation']) ? $params['orientation'] : 'P';
  213. $pdf = new PDF($params['format'], $params['orientation'], $params);
  214. $pdf->html_to_pdf_with_template($html);
  215. }
  216. /**
  217. * @param array $data
  218. * @param array $params
  219. *
  220. * @return string
  221. */
  222. public static function convert_array_to_html($data, $params = array())
  223. {
  224. $headers = $data[0];
  225. unset($data[0]);
  226. $header_attributes = isset($params['header_attributes']) ? $params['header_attributes'] : array();
  227. $table = new HTML_Table(array('class' => 'data_table', 'repeat_header' => '1'));
  228. $row = 0;
  229. $column = 0;
  230. foreach ($headers as $header) {
  231. $table->setHeaderContents($row, $column, $header);
  232. $attributes = array();
  233. if (isset($header_attributes) && isset($header_attributes[$column])) {
  234. $attributes = $header_attributes[$column];
  235. }
  236. if (!empty($attributes)) {
  237. $table->updateCellAttributes($row, $column, $attributes);
  238. }
  239. $column++;
  240. }
  241. $row++;
  242. foreach ($data as &$printable_data_row) {
  243. $column = 0;
  244. foreach ($printable_data_row as &$printable_data_cell) {
  245. $table->setCellContents($row, $column, $printable_data_cell);
  246. //$table->updateCellAttributes($row, $column, $atributes);
  247. $column++;
  248. }
  249. $table->updateRowAttributes($row, $row % 2 ? 'class="row_even"' : 'class="row_odd"', true);
  250. $row++;
  251. }
  252. $table_tp_html = $table->toHtml();
  253. return $table_tp_html;
  254. }
  255. /**
  256. * Export HTML content in a ODF document
  257. * @param string $html
  258. * @param string $name
  259. * @param string $format
  260. *
  261. * @return bool
  262. */
  263. public static function htmlToOdt($html, $name, $format = 'odt')
  264. {
  265. $unoconv = api_get_configuration_value('unoconv.binaries');
  266. if (empty($unoconv)) {
  267. return false;
  268. }
  269. if (!empty($html)) {
  270. $fs = new Filesystem();
  271. $paths = [
  272. 'root_sys' => api_get_path(SYS_PATH),
  273. 'path.temp' => api_get_path(SYS_ARCHIVE_PATH),
  274. ];
  275. $connector = new Connector();
  276. $drivers = new DriversContainer();
  277. $drivers['configuration'] = array(
  278. 'unoconv.binaries' => $unoconv,
  279. 'unoconv.timeout' => 60,
  280. );
  281. $tempFilesystem = TemporaryFilesystem::create();
  282. $manager = new Manager($tempFilesystem, $fs);
  283. $alchemyst = new Alchemyst($drivers, $manager);
  284. $dataFileSystem = new Data($paths, $fs, $connector, $alchemyst);
  285. $content = $dataFileSystem->convertRelativeToAbsoluteUrl($html);
  286. $filePath = $dataFileSystem->putContentInTempFile(
  287. $content,
  288. api_replace_dangerous_char($name),
  289. 'html'
  290. );
  291. $try = true;
  292. while ($try) {
  293. try {
  294. $convertedFile = $dataFileSystem->transcode(
  295. $filePath,
  296. $format
  297. );
  298. $try = false;
  299. DocumentManager::file_send_for_download(
  300. $convertedFile,
  301. false,
  302. $name.'.'.$format
  303. );
  304. } catch (Exception $e) {
  305. // error_log($e->getMessage());
  306. }
  307. }
  308. }
  309. }
  310. }