Skip to content

Commit

Permalink
Merge pull request #1486 from claroline/csv-serialize-test
Browse files Browse the repository at this point in the history
Csv serialize test
  • Loading branch information
ngodfraind committed Mar 30, 2016
2 parents 7df5135 + cf5e65b commit 0ebb6a5
Show file tree
Hide file tree
Showing 26 changed files with 606 additions and 104 deletions.
19 changes: 18 additions & 1 deletion Controller/API/User/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Claroline\CoreBundle\Library\Security\Collection\GroupCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as EXT;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;

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

/**
* @Post("/groups/{group}/import/members", name="group_members_import", options={ "method_prefix" = false })
* @View(serializerGroups={"api_user"})
*
* @param Group $group
*
* @return Response
*/
public function importMembersAction(Group $group)
{
$this->throwsExceptionIfNotAdmin();

return $this->groupManager->importMembers(file_get_contents($this->request->files->get('csv')), $group);
}

private function isAdmin()
{
return $this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN');
Expand Down Expand Up @@ -336,7 +353,7 @@ private function throwExceptionIfNotGranted($action, $groups)
throw new AccessDeniedException("You can't do the action [{$action}] on the user list {$groupList}");
}
}

/**
* @View()
* @ApiDoc(
Expand Down
5 changes: 3 additions & 2 deletions Controller/API/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use FOS\RestBundle\Controller\Annotations\NamePrefix;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\Get;

/**
* @NamePrefix("api_")
Expand Down Expand Up @@ -113,6 +114,7 @@ public function getUsersAction()
* description="Returns the users list",
* views = {"user"}
* )
* @Get("/users/page/{page}/limit/{limit}/search", name="search_users", options={ "method_prefix" = false })
*/
public function getSearchUsersAction($page, $limit)
{
Expand Down Expand Up @@ -141,6 +143,7 @@ public function getSearchUsersAction($page, $limit)
* description="Returns the searchable user fields",
* views = {"user"}
* )
* @Get("/users/fields")
*/
public function getUserSearchableFieldsAction()
{
Expand Down Expand Up @@ -428,8 +431,6 @@ public function removeUsersFromGroupAction(Group $group)
public function csvRemoveUserAction()
{
$this->throwsExceptionIfNotAdmin();
//pleaaaaaaaase find me my lord
$csvFile = null;

$this->userManager->csvRemove($this->request->files->get('csv'));
}
Expand Down
4 changes: 4 additions & 0 deletions Library/Exporter/ExporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Claroline\CoreBundle\Library\Exporter;

/**
* @deprecated
* This is now supported by the view layer of the fos_rest bundle
*/
interface ExporterInterface
{
public function export(array $titles, array $data);
Expand Down
38 changes: 38 additions & 0 deletions Library/View/ExporterViewHandler.php
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;
}
}
49 changes: 49 additions & 0 deletions Library/View/Serializer/Csv.php
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;
}
}
58 changes: 58 additions & 0 deletions Library/View/Serializer/Excel.php
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;
}
}
Loading

0 comments on commit 0ebb6a5

Please sign in to comment.