Skip to content

Commit

Permalink
#24: Add Export to CSV button
Browse files Browse the repository at this point in the history
  • Loading branch information
obounjerte committed Aug 4, 2021
1 parent 00ee39b commit 1de8582
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 73 deletions.
171 changes: 121 additions & 50 deletions Controller/SQLIContentListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@

use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
use eZ\Publish\API\Repository\FieldTypeService;
use eZ\Publish\API\Repository\Repository;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchAdapter;
use eZ\Publish\Core\Pagination\Pagerfanta\LocationSearchHitAdapter;
use eZ\Publish\Core\Pagination\Pagerfanta\Pagerfanta;
use EzSystems\EzPlatformAdminUiBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Symfony\Component\HttpFoundation\StreamedResponse;

class SQLIContentListController extends Controller
{
private $searchService;
private $contentTypeService;
private $fieldTypeService;
private $repository;

/**
* SQLIContentListController constructor.
* @param $searchService
* @param $contentTypeService
*/
public function __construct(SearchService $searchService, ContentTypeService $contentTypeService, Repository $repository)
{
public function __construct(
SearchService $searchService,
ContentTypeService $contentTypeService,
Repository $repository,
FieldTypeService $fieldTypeService
) {
$this->searchService = $searchService;
$this->contentTypeService = $contentTypeService;
$this->repository = $repository;
$this->fieldTypeService = $fieldTypeService;
}

/**
Expand All @@ -42,7 +52,6 @@ public function __construct(SearchService $searchService, ContentTypeService $co
*/
public function listAction($contentTypeIdentifier = false, int $page = 1)
{

$query = new LocationQuery();

$criterions = [
Expand All @@ -67,52 +76,114 @@ public function listAction($contentTypeIdentifier = false, int $page = 1)
$contentTypes[$group->identifier] = $this->contentTypeService->loadContentTypes($group);
}

return $this->render('@SQLIEzToolbox/ContentList/all_content_list.html.twig', [
'totalCount' => $paginator->getNbResults(),
'articles' => $paginator,
'contentTypes' => $contentTypes,
]);

// $contentTypes = [];
// $contentTypeGroups = $this->contentTypeService->loadContentTypeGroups();
// foreach ($contentTypeGroups as $group) {
// $contentTypes[$group->identifier] = $this->contentTypeService->loadContentTypes($group);
// }
// dump($contentTypes);
//
// $query = new LocationQuery();
// $param = array();
// foreach ($contentTypes["Content"] as $contentType) {
// array_push($param, $contentType->identifier);
// }
//
// $query->query = new Criterion\LogicalAnd([new Criterion\ContentTypeIdentifier($param)]);
// $query->performCount = true;
//// $query->limit = 0; //count only does not return data
//// $query->limit = ;
//
// $this->searchService = $this->repository->getSearchService();
// $results = $this->searchService->findContent($query);
//
// $paginator = new Pagerfanta(
// new LocationSearchAdapter($query, $this->searchService)
// );
// $paginator->setMaxPerPage(8);
// $paginator->setCurrentPAge($page);
//
// dump($query);
// dump($results);
// foreach ($paginator as $item) {
// dump($item);
// }
// dump($paginator);
//
// return $this->render('@SQLIEzToolbox/ContentList/all_content_list.html.twig', [
// 'contentTypeGroups' =>$contentTypeGroups,
// 'contentTypes' => $contentTypes,
// 'count' => (int)$results->totalCount,
// 'contents' => $paginator,
// ]);
return $this->render(
'@SQLIEzToolbox/ContentList/all_content_list.html.twig',
[
'totalCount' => $paginator->getNbResults(),
'articles' => $paginator,
'contentTypes' => $contentTypes,
]
);
}

/**
*
*/
public function getPaginator($contentTypeIdentifier, $maxPerPage = 8)
{
$query = new LocationQuery();

$criterions = [
new Criterion\Visibility(Criterion\Visibility::VISIBLE),
];

if ($contentTypeIdentifier) {
$criterions[] = new Criterion\ContentTypeIdentifier($contentTypeIdentifier);
}

$query->query = new Criterion\LogicalAnd($criterions);

$paginator = new Pagerfanta(
new LocationSearchAdapter($query, $this->searchService)
);

if ($maxPerPage !== false) {
$paginator->setMaxPerPage(8);
}
return $paginator;
}

/**
*
* @throws NotFoundException
* @throws UnauthorizedException
* @throws InvalidArgumentException
*/
public function exportCSV(
$contentTypeIdentifier,
ContentTypeService $contentTypeService,
FieldTypeService $fieldTypeService
) {
$response = new StreamedResponse();

$query = new LocationQuery();
$query->filter = new Criterion\ContentTypeIdentifier($contentTypeIdentifier);
$result = $this->searchService->findContent($query, ['useAlwaysAvailable' => true]);

$response->setCallback(
function () use ($contentTypeService, $contentTypeIdentifier, $fieldTypeService, $result) {
$fp = fopen('php://output', 'w');

// Build Headers
$headers = [];
$contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
array_push($headers, $fieldDefinition->identifier);
}

// Builder Columns
$contents = array();
foreach ($result->searchHits as $searchHit) {
array_push(
$contents,
$searchHit->valueObject
);
}

fputcsv($fp, $headers);

$columns = [];
foreach ($contents as $content) {
$column = [];
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
$fieldType = $fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
$field = $content->getField($fieldDefinition->identifier);
$valueHash = $fieldType->toHash($field->value);
if (is_array($valueHash)) {
$result = "";
array_walk_recursive(
$valueHash,
function ($elt) use (&$result) {
$result .= strval($elt) . "\n";
}
);
array_push($column, $result);
} else {
array_push($column, $valueHash);
}
}
array_push($columns, $column);
fputcsv($fp, array_values($column), ';', '"');
}

fclose($fp);
}
);

$filename = 'Content_List' . $contentTypeIdentifier;
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
$response->headers->set('Content-Disposition', "attachment; filename=\"export-{$filename}.csv\"");
$response->setStatusCode(200);
return $response;
}
}
8 changes: 8 additions & 0 deletions Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ sqli_eztoolbox_all_content_list:
contentTypeIdentifier: false
_controller: SQLI\EzToolboxBundle\Controller\SQLIContentListController::listAction

#Export Contents in CSV
sqli_eztoolbox_export:
path: /sqli-admin/all_content_list/{page}/{contentTypeIdentifier}/export
defaults:
page: 1
contentTypeIdentifier: false
_controller: SQLI\EzToolboxBundle\Controller\SQLIContentListController::exportCSV

# FosRestBundle used for api rest
api:
resource: api_routes.yaml
Expand Down
14 changes: 14 additions & 0 deletions Resources/translations/sqli_admin+intl-icu.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#Content List
num_of_content: >-
{totalCount, plural,
=0 {No Content Found}
one {1 Content Found}
other {# Contents Found}
}
content_list.title: 'Content List'
content_list.content_name: 'Content name'
content_list.content_type: 'Content Type'
content_list.modified: 'Modified'
content_list.published: 'Published'
14 changes: 14 additions & 0 deletions Resources/translations/sqli_admin+intl-icu.fr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#Content List
content_list.num_of_content: >-
{totalCount, plural,
=0 {Aucun Content Trouvé }
one {1 Content Trouvé}
other {# Contents Trouvés}
}
content_list.title: 'Liste de Contents'
content_list.content_name: 'Nom du Content'
content_list.content_type: 'Type du Content'
content_list.modified: 'Dernière modification'
content_listpublished: 'Date De publication'

7 changes: 0 additions & 7 deletions Resources/translations/sqli_admin+intl-icu.yaml

This file was deleted.

30 changes: 14 additions & 16 deletions Resources/views/ContentList/all_content_list.html.twig
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{% extends '@ezdesign/ui/layout.html.twig' %}

{% block title %}{{ 'Content List'|trans }}{% endblock %}
{% block title %}{{ 'Content List' }}{% endblock %}

{%- block breadcrumbs -%}
{% include '@ezdesign/ui/breadcrumbs.html.twig' with { items: [
{ value: 'url.list'|trans|desc('Content List') }
{ value: 'content_list.title'|trans|desc('Content List') }
]} %}
{%- endblock -%}

{%- block page_title -%}
{% include '@ezdesign/ui/page_title.html.twig' with {
title: 'url.list'|trans|desc('Content List'),
title: 'content_list.title'|trans|desc('Content List'),
icon_name: 'article'
} %}
{%- endblock -%}
Expand Down Expand Up @@ -45,20 +45,25 @@
{% endfor %}
{% endfor %}
</div>
{% if app.request.get('contentTypeIdentifier') != false %}
<button class="btn btn-secondary" type="button" id="exportCSV">
<a class="text-decoration-none text-reset" href="{{ path('sqli_eztoolbox_all_content_list', { 'contentTypeIdentifier': app.request.get('contentTypeIdentifier') }) }}">Export CSV</a>
</button>
{% endif %}
</div>
</div>
<div class="ez-table-header">
<div class="ez-table-header__headline">{{ "Content List"|trans }} --
{{ "num_of_content"|trans({'totalCount' : totalCount})}}
<div class="ez-table-header__headline">{{ "Content List"}} --
{{ 'num_of_content' | trans({'totalCount' : totalCount}, 'sqli_admin') }}
</div>
</div>
<table class="table">
<thead>
<tr>
<th>{{ 'Content name'|trans }}</th>
<th>{{ 'Content Type'|trans }}</th>
<th>{{ 'Modified'|trans }}</th>
<th>{{ 'Published'|trans }}</th>
<th>{{ 'content_list.content_name' | trans({}, 'sqli_admin') }}</th>
<th>{{ 'content_list.content_type' | trans({}, 'sqli_admin') }}</th>
<th>{{ 'content_list.modified' | trans({}, 'sqli_admin') }}</th>
<th>{{ 'content_list.published' | trans({}, 'sqli_admin') }}</th>
</tr>
</thead>
<tbody>
Expand All @@ -75,11 +80,4 @@
{{ pagerfanta(articles, 'ez') }}
</section>

<script>
$(".dropdown-menu a ").click(function () {
let x = $(this).text();
alert(x);
});
</script>

{%- endblock -%}

0 comments on commit 1de8582

Please sign in to comment.