export.lib.inc.php 11 KB

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