Skip to content

Commit 0ebb6a5

Browse files
committed
Merge pull request #1486 from claroline/csv-serialize-test
Csv serialize test
2 parents 7df5135 + cf5e65b commit 0ebb6a5

File tree

26 files changed

+606
-104
lines changed

26 files changed

+606
-104
lines changed

Controller/API/User/GroupController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3131
use Claroline\CoreBundle\Library\Security\Collection\GroupCollection;
3232
use Sensio\Bundle\FrameworkExtraBundle\Configuration as EXT;
33+
use FOS\RestBundle\Controller\Annotations\Get;
34+
use FOS\RestBundle\Controller\Annotations\Post;
3335

3436
/**
3537
* @NamePrefix("api_")
@@ -307,6 +309,21 @@ public function getEditGroupFormAction(Group $group)
307309
return $this->apiManager->handleFormView('ClarolineCoreBundle:API:User\editGroupForm.html.twig', $form, $options);
308310
}
309311

312+
/**
313+
* @Post("/groups/{group}/import/members", name="group_members_import", options={ "method_prefix" = false })
314+
* @View(serializerGroups={"api_user"})
315+
*
316+
* @param Group $group
317+
*
318+
* @return Response
319+
*/
320+
public function importMembersAction(Group $group)
321+
{
322+
$this->throwsExceptionIfNotAdmin();
323+
324+
return $this->groupManager->importMembers(file_get_contents($this->request->files->get('csv')), $group);
325+
}
326+
310327
private function isAdmin()
311328
{
312329
return $this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN');
@@ -336,7 +353,7 @@ private function throwExceptionIfNotGranted($action, $groups)
336353
throw new AccessDeniedException("You can't do the action [{$action}] on the user list {$groupList}");
337354
}
338355
}
339-
356+
340357
/**
341358
* @View()
342359
* @ApiDoc(

Controller/API/User/UserController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use FOS\RestBundle\Controller\Annotations\NamePrefix;
4040
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
4141
use FOS\RestBundle\Controller\Annotations\Post;
42+
use FOS\RestBundle\Controller\Annotations\Get;
4243

4344
/**
4445
* @NamePrefix("api_")
@@ -113,6 +114,7 @@ public function getUsersAction()
113114
* description="Returns the users list",
114115
* views = {"user"}
115116
* )
117+
* @Get("/users/page/{page}/limit/{limit}/search", name="search_users", options={ "method_prefix" = false })
116118
*/
117119
public function getSearchUsersAction($page, $limit)
118120
{
@@ -141,6 +143,7 @@ public function getSearchUsersAction($page, $limit)
141143
* description="Returns the searchable user fields",
142144
* views = {"user"}
143145
* )
146+
* @Get("/users/fields")
144147
*/
145148
public function getUserSearchableFieldsAction()
146149
{
@@ -428,8 +431,6 @@ public function removeUsersFromGroupAction(Group $group)
428431
public function csvRemoveUserAction()
429432
{
430433
$this->throwsExceptionIfNotAdmin();
431-
//pleaaaaaaaase find me my lord
432-
$csvFile = null;
433434

434435
$this->userManager->csvRemove($this->request->files->get('csv'));
435436
}

Library/Exporter/ExporterInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Claroline\CoreBundle\Library\Exporter;
1313

14+
/**
15+
* @deprecated
16+
* This is now supported by the view layer of the fos_rest bundle
17+
*/
1418
interface ExporterInterface
1519
{
1620
public function export(array $titles, array $data);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Claroline\CoreBundle\Library\View;
4+
5+
use FOS\RestBundle\View\View;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
use Symfony\Component\HttpFoundation\StreamedResponse;
9+
10+
class ExporterViewHandler
11+
{
12+
public function createResponse(ViewHandler $handler, View $view, Request $request, $format)
13+
{
14+
$data = $view->getData();
15+
$context = $view->getSerializationContext();
16+
$container = $handler->getContainer();
17+
$format = $view->getFormat();
18+
$file = $container->get('claroline.library.view.serializer.serializer')->serialize($data, $format);
19+
$response = new StreamedResponse();
20+
21+
$response->setCallBack(
22+
function () use ($file) {
23+
readfile($file);
24+
}
25+
);
26+
27+
$response->headers->set('Content-Transfer-Encoding', 'octet-stream');
28+
$response->headers->set('Content-Type', 'application/force-download');
29+
$response->headers->set('Content-Disposition', 'attachment; filename=file.' . $format);
30+
31+
switch ($format) {
32+
case 'csv': $response->headers->set('Content-Type', 'text/csv'); break;
33+
case 'xls': $response->headers->set('Content-Type', 'application/vnd.ms-excel'); break;
34+
}
35+
36+
return $response;
37+
}
38+
}

Library/View/Serializer/Csv.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.csv")
18+
*/
19+
class Csv
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+
public function export(array $titles, array $data)
34+
{
35+
$tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . '.csv';
36+
file_put_contents($this->tmpLogPath, $tmpFile . "\n", FILE_APPEND);
37+
$fp = fopen($tmpFile, 'w');
38+
39+
fputcsv($fp, $titles);
40+
41+
foreach($data as $item) {
42+
fputcsv($fp, $item);
43+
}
44+
45+
fclose($fp);
46+
47+
return $tmpFile;
48+
}
49+
}

Library/View/Serializer/Excel.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)