|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Claroline Connect package. |
| 5 | + * |
| 6 | + * (c) Claroline Consortium <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Claroline\CoreBundle\Library\View\Serializer; |
| 13 | + |
| 14 | +use JMS\DiExtraBundle\Annotation as DI; |
| 15 | + |
| 16 | +/** |
| 17 | + * @DI\Service("claroline.library.view.serializer.xls") |
| 18 | + */ |
| 19 | +class Excel |
| 20 | +{ |
| 21 | + private $tmpLogPath; |
| 22 | + |
| 23 | + /** |
| 24 | + * @DI\InjectParams({ |
| 25 | + * "tmp" = @DI\Inject("%claroline.param.platform_generated_archive_path%"), |
| 26 | + * }) |
| 27 | + */ |
| 28 | + public function __construct($tmp) |
| 29 | + { |
| 30 | + $this->tmpLogPath = $tmp; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * http://www.the-art-of-web.com/php/dataexport/ |
| 35 | + */ |
| 36 | + public function export(array $titles, array $data) |
| 37 | + { |
| 38 | + //titles row |
| 39 | + $excel = implode("\t", $titles) . "\r\n"; |
| 40 | + |
| 41 | + foreach ($data as $row) { |
| 42 | + array_walk($row, function(&$str) { |
| 43 | + $str = preg_replace("/\t/", "\\t", $str); |
| 44 | + $str = preg_replace("/\r?\n/", "\\n", $str); |
| 45 | + if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + $excel .= implode("\t", $row) . "\r\n"; |
| 50 | + } |
| 51 | + |
| 52 | + $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . ".xls"; |
| 53 | + file_put_contents($this->tmpLogPath, $tmpFile . "\n", FILE_APPEND); |
| 54 | + file_put_contents($tmpFile, $excel); |
| 55 | + |
| 56 | + return $tmpFile; |
| 57 | + } |
| 58 | +} |
0 commit comments