export.lib.inc.php 11 KB

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