-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1486 from claroline/csv-serialize-test
Csv serialize test
- Loading branch information
Showing
26 changed files
with
606 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Claroline\CoreBundle\Library\View; | ||
|
||
use FOS\RestBundle\View\View; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
||
class ExporterViewHandler | ||
{ | ||
public function createResponse(ViewHandler $handler, View $view, Request $request, $format) | ||
{ | ||
$data = $view->getData(); | ||
$context = $view->getSerializationContext(); | ||
$container = $handler->getContainer(); | ||
$format = $view->getFormat(); | ||
$file = $container->get('claroline.library.view.serializer.serializer')->serialize($data, $format); | ||
$response = new StreamedResponse(); | ||
|
||
$response->setCallBack( | ||
function () use ($file) { | ||
readfile($file); | ||
} | ||
); | ||
|
||
$response->headers->set('Content-Transfer-Encoding', 'octet-stream'); | ||
$response->headers->set('Content-Type', 'application/force-download'); | ||
$response->headers->set('Content-Disposition', 'attachment; filename=file.' . $format); | ||
|
||
switch ($format) { | ||
case 'csv': $response->headers->set('Content-Type', 'text/csv'); break; | ||
case 'xls': $response->headers->set('Content-Type', 'application/vnd.ms-excel'); break; | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Claroline Connect package. | ||
* | ||
* (c) Claroline Consortium <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Claroline\CoreBundle\Library\View\Serializer; | ||
|
||
use JMS\DiExtraBundle\Annotation as DI; | ||
|
||
/** | ||
* @DI\Service("claroline.library.view.serializer.csv") | ||
*/ | ||
class Csv | ||
{ | ||
private $tmpLogPath; | ||
|
||
/** | ||
* @DI\InjectParams({ | ||
* "tmp" = @DI\Inject("%claroline.param.platform_generated_archive_path%"), | ||
* }) | ||
*/ | ||
public function __construct($tmp) | ||
{ | ||
$this->tmpLogPath = $tmp; | ||
} | ||
|
||
public function export(array $titles, array $data) | ||
{ | ||
$tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . '.csv'; | ||
file_put_contents($this->tmpLogPath, $tmpFile . "\n", FILE_APPEND); | ||
$fp = fopen($tmpFile, 'w'); | ||
|
||
fputcsv($fp, $titles); | ||
|
||
foreach($data as $item) { | ||
fputcsv($fp, $item); | ||
} | ||
|
||
fclose($fp); | ||
|
||
return $tmpFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Claroline Connect package. | ||
* | ||
* (c) Claroline Consortium <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Claroline\CoreBundle\Library\View\Serializer; | ||
|
||
use JMS\DiExtraBundle\Annotation as DI; | ||
|
||
/** | ||
* @DI\Service("claroline.library.view.serializer.xls") | ||
*/ | ||
class Excel | ||
{ | ||
private $tmpLogPath; | ||
|
||
/** | ||
* @DI\InjectParams({ | ||
* "tmp" = @DI\Inject("%claroline.param.platform_generated_archive_path%"), | ||
* }) | ||
*/ | ||
public function __construct($tmp) | ||
{ | ||
$this->tmpLogPath = $tmp; | ||
} | ||
|
||
/** | ||
* http://www.the-art-of-web.com/php/dataexport/ | ||
*/ | ||
public function export(array $titles, array $data) | ||
{ | ||
//titles row | ||
$excel = implode("\t", $titles) . "\r\n"; | ||
|
||
foreach ($data as $row) { | ||
array_walk($row, function(&$str) { | ||
$str = preg_replace("/\t/", "\\t", $str); | ||
$str = preg_replace("/\r?\n/", "\\n", $str); | ||
if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; | ||
}); | ||
|
||
|
||
$excel .= implode("\t", $row) . "\r\n"; | ||
} | ||
|
||
$tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . ".xls"; | ||
file_put_contents($this->tmpLogPath, $tmpFile . "\n", FILE_APPEND); | ||
file_put_contents($tmpFile, $excel); | ||
|
||
return $tmpFile; | ||
} | ||
} |
Oops, something went wrong.