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