diff --git a/Controller/SQLIContentListController.php b/Controller/SQLIContentListController.php index e594fc1..8879ba2 100644 --- a/Controller/SQLIContentListController.php +++ b/Controller/SQLIContentListController.php @@ -5,21 +5,26 @@ 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; /** @@ -27,11 +32,16 @@ class SQLIContentListController extends Controller * @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; } /** @@ -42,7 +52,6 @@ public function __construct(SearchService $searchService, ContentTypeService $co */ public function listAction($contentTypeIdentifier = false, int $page = 1) { - $query = new LocationQuery(); $criterions = [ @@ -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; } } \ No newline at end of file diff --git a/Resources/config/routing.yaml b/Resources/config/routing.yaml index cb2b7c0..e7c7c74 100644 --- a/Resources/config/routing.yaml +++ b/Resources/config/routing.yaml @@ -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 diff --git a/Resources/translations/sqli_admin+intl-icu.en.yaml b/Resources/translations/sqli_admin+intl-icu.en.yaml new file mode 100644 index 0000000..3b6d462 --- /dev/null +++ b/Resources/translations/sqli_admin+intl-icu.en.yaml @@ -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' \ No newline at end of file diff --git a/Resources/translations/sqli_admin+intl-icu.fr.yaml b/Resources/translations/sqli_admin+intl-icu.fr.yaml new file mode 100644 index 0000000..af005a5 --- /dev/null +++ b/Resources/translations/sqli_admin+intl-icu.fr.yaml @@ -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' + diff --git a/Resources/translations/sqli_admin+intl-icu.yaml b/Resources/translations/sqli_admin+intl-icu.yaml deleted file mode 100644 index c3e9672..0000000 --- a/Resources/translations/sqli_admin+intl-icu.yaml +++ /dev/null @@ -1,7 +0,0 @@ -#Content List -num_of_content: >- - {totalCount, plural, - =0 {No Content Found} - one {1 Content Found} - other {# Contents Found} - } \ No newline at end of file diff --git a/Resources/views/ContentList/all_content_list.html.twig b/Resources/views/ContentList/all_content_list.html.twig index d81112f..446d611 100644 --- a/Resources/views/ContentList/all_content_list.html.twig +++ b/Resources/views/ContentList/all_content_list.html.twig @@ -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 -%} @@ -45,20 +45,25 @@ {% endfor %} {% endfor %} + {% if app.request.get('contentTypeIdentifier') != false %} + + {% endif %}
{{ 'Content name'|trans }} | -{{ 'Content Type'|trans }} | -{{ 'Modified'|trans }} | -{{ 'Published'|trans }} | +{{ 'content_list.content_name' | trans({}, 'sqli_admin') }} | +{{ 'content_list.content_type' | trans({}, 'sqli_admin') }} | +{{ 'content_list.modified' | trans({}, 'sqli_admin') }} | +{{ 'content_list.published' | trans({}, 'sqli_admin') }} |
---|