[11/06/2024] Les premières modifs + installation de quelques modules indispensables

This commit is contained in:
2024-06-11 14:57:59 +02:00
parent 5ac5653ae5
commit 77cf2c7cc6
1626 changed files with 171457 additions and 131 deletions

View File

@@ -0,0 +1,197 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Admin;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Admin\BaseAdminOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Service\LibraryImageService;
/**
* @Route("/open_api/library/image", name="library_image")
*/
class ImageController extends BaseAdminOpenApiController
{
/**
* @Route("", name="_create", methods="POST")
*
* @OA\Post(
* path="/library/image",
* tags={ "Library image"},
* summary="Create a new image",
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* property="title",
* type="string",
* ),
* @OA\Property(
* property="locale",
* type="string",
* ),
* @OA\Property(
* property="image",
* type="string",
* format="binary"
* )
* )
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function createImage(
Request $request,
ModelFactory $modelFactory,
LibraryImageService $libraryImageService
) {
$locale = $this->findLocale($request);
$image = $libraryImageService->createImage(
$request->files->get('image'),
$request->request->get('title'),
$locale
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryImage', $image, $locale));
}
// Method POST because patch doesn't work with multipart/form-data
/**
* @Route("/{imageId}", name="_update", methods="POST", requirements={"imageId"="\d+"})
*
* @OA\Post(
* path="/library/image/{imageId}",
* tags={ "Library image"},
* summary="Update an image",
* @OA\Parameter(
* name="imageId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\RequestBody(
* required=true,
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* property="title",
* type="string",
* ),
* @OA\Property(
* property="locale",
* type="string",
* ),
* @OA\Property(
* property="image",
* type="string",
* format="binary"
* )
* )
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function updateImage(
$imageId,
Request $request,
ModelFactory $modelFactory,
LibraryImageService $libraryImageService
) {
$locale = $this->findLocale($request);
$image = $libraryImageService->updateImage(
$imageId,
$request->files->get('image'),
$request->request->get('title'),
$locale
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryImage', $image, $locale));
}
/**
* @Route("/{imageId}", name="_delete", methods="DELETE", requirements={"imageId"="\d+"})
*
* @OA\Delete(
* path="/library/image/{imageId}",
* tags={ "Library image"},
* summary="Delete an image",
* @OA\Parameter(
* name="imageId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="204",
* description="Success"
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function deleteImage(
$imageId,
LibraryImageService $libraryImageService
) {
$libraryImageService->deleteImage($imageId);
return new JsonResponse('Success', 204);
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getAdminEditionLang()->getLocale();
}
return $locale;
}
}

View File

@@ -0,0 +1,118 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Admin;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Admin\BaseAdminOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\LibraryImageTag;
use TheliaLibrary\Model\LibraryTagQuery;
use TheliaLibrary\Service\LibraryImageTagService;
/**
* @Route("/open_api/library/image_tag", name="library_image_tag")
*/
class ImageTagController extends BaseAdminOpenApiController
{
/**
* @Route("", name="_associate", methods="POST")
*
* @OA\Post(
* path="/library/image_tag",
* tags={ "Library tag"},
* summary="Associate a tag to an image",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(
* property="imageId",
* type="integer",
* ),
* @OA\Property(
* property="tagId",
* type="integer",
* )
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryImageTag")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function createAssociation(
Request $request,
ModelFactory $modelFactory,
LibraryImageTagService $libraryImageTagService
) {
$data = json_decode($request->getContent(), true);
/** @var LibraryImageTag $openApiLibraryImageTag */
$openApiLibraryImageTag = $modelFactory->buildModel('LibraryImageTag', $data);
$openApiLibraryImageTag->validate(self::GROUP_UPDATE);
$image = $libraryImageTagService->associateImage(
$openApiLibraryImageTag->getImageId(),
$openApiLibraryImageTag->getTagId(),
);
$query = LibraryTagQuery::create();
$tag = $query->findOneById($openApiLibraryImageTag->getTagId());
return OpenApiService::jsonResponse(['imageTag' => $modelFactory->buildModel('LibraryImageTag', $image), 'tag' => $modelFactory->buildModel('LibraryTag', $tag)]);
}
/**
* @Route("/{imageTagId}", name="_delete_association", methods="DELETE", requirements={"imageTagId"="\d+"})
*
* @OA\Delete(
* path="/library/image_tag/{imageTagId}",
* tags={ "Library tag"},
* summary="Delete an association",
* @OA\Parameter(
* name="imageTagId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="204",
* description="Success"
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function deleteAssociation(
$imageTagId,
LibraryImageTagService $libraryImageTagService
) {
$libraryImageTagService->deleteImageAssociation($imageTagId);
return new JsonResponse('Success', 204);
}
}

View File

@@ -0,0 +1,198 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Admin;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Admin\BaseAdminOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\Api\LibraryItemImage;
use TheliaLibrary\Service\LibraryItemImageService;
/**
* @Route("/open_api/library/item_image", name="library_item_image")
*/
class ItemImageController extends BaseAdminOpenApiController
{
/**
* @Route("", name="_associate", methods="POST")
*
* @OA\Post(
* path="/library/item_image",
* tags={ "Library image"},
* summary="Associate an image to an item",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(
* property="imageId",
* type="integer",
* ),
* @OA\Property(
* property="itemType",
* type="string",
* ),
* @OA\Property(
* property="itemId",
* type="integer",
* ),
* @OA\Property(
* property="code",
* type="string",
* ),
* @OA\Property(
* property="visible",
* type="boolean",
* )
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryItemImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function createAssociation(
Request $request,
ModelFactory $modelFactory,
LibraryItemImageService $libraryItemImageService
) {
$data = json_decode($request->getContent(), true);
/** @var LibraryItemImage $openApiLibraryItemImage */
$openApiLibraryItemImage = $modelFactory->buildModel('LibraryItemImage', $data);
$openApiLibraryItemImage->validate(self::GROUP_UPDATE);
$image = $libraryItemImageService->associateImage(
$openApiLibraryItemImage->getImageId(),
$openApiLibraryItemImage->getItemType(),
$openApiLibraryItemImage->getItemId(),
$openApiLibraryItemImage->getCode(),
$openApiLibraryItemImage->isVisible(),
$openApiLibraryItemImage->getPosition()
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryItemImage', $image));
}
/**
* @Route("/{itemImageId}", name="_update_association", methods="PATCH", requirements={"itemImageId"="\d+"})
*
* @OA\Patch(
* path="/library/item_image/{itemImageId}",
* tags={ "Library image"},
* summary="Update an association",
* @OA\Parameter(
* name="itemImageId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(
* property="visible",
* type="boolean",
* ),
* @OA\Property(
* property="code",
* type="string",
* ),
* @OA\Property(
* property="position",
* type="integer",
* ),
* @OA\Property(
* property="positionMovement",
* type="string",
* enum={"up", "down"}
* )
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryItemImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function updateAssociation(
$itemImageId,
Request $request,
ModelFactory $modelFactory,
LibraryItemImageService $libraryItemImageService
) {
$data = json_decode($request->getContent(), true);
$image = $libraryItemImageService->updateImageAssociation(
$itemImageId,
$data['code'] ?? null,
$data['visible'] ?? null,
$data['position'] ?? null,
$data['positionMovement'] ?? null
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryItemImage', $image));
}
/**
* @Route("/{itemImageId}", name="_delete_association", methods="DELETE", requirements={"itemImageId"="\d+"})
*
* @OA\Delete(
* path="/library/item_image/{itemImageId}",
* tags={ "Library image"},
* summary="Delete an association",
* @OA\Parameter(
* name="itemImageId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="204",
* description="Success"
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function deleteAssociation(
$itemImageId,
LibraryItemImageService $libraryItemImageService
) {
$libraryItemImageService->deleteImageAssociation($itemImageId);
return new JsonResponse('Success', 204);
}
}

View File

@@ -0,0 +1,216 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Admin;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Admin\BaseAdminOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\Base\LibraryTagQuery;
use TheliaLibrary\Model\LibraryTag;
use TheliaLibrary\Service\LibraryTagService;
/**
* @Route("/open_api/library/tag", name="library_tag")
*/
class TagController extends BaseAdminOpenApiController
{
/**
* @Route("", name="_view", methods="GET")
* @OA\Get(
* path="/library/tag",
* tags={"Library tag"},
* summary="Get tags",
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryTag")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getTags(Request $request, ModelFactory $modelFactory)
{
$query = LibraryTagQuery::create();
$locale = $this->findLocale($request);
return OpenApiService::jsonResponse(array_map(
function (LibraryTag $tag) use ($modelFactory, $locale) {
/** @var \TheliaLibrary\Model\Api\LibraryTag $tagModel */
$tagModel = $modelFactory->buildModel('LibraryTag', $tag, $locale);
return $tagModel;
},
iterator_to_array($query->find())
));
}
/**
* @Route("", name="_create", methods="POST")
*
* @OA\Post(
* path="/library/tag",
* tags={ "Library tag"},
* summary="Create a new tag",
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(
* property="title",
* type="string"
* ),
* @OA\Property(
* property="colorCode",
* default="#000000",
* type="string"
* ),
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryTag")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function createTag(
Request $request,
ModelFactory $modelFactory,
LibraryTagService $libraryTagService
) {
$locale = $this->findLocale($request);
$tag = $libraryTagService->createTag(
$request->request->get('title'),
$request->request->get('colorCode'),
$locale
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryTag', $tag, $locale));
}
/**
* @Route("/{tagId}", name="_update", methods="POST", requirements={"tagId"="\d+"})
* @OA\Post(
* path="/library/tag/{tagId}",
* tags={ "Library tag"},
* summary="update a tag",
* @OA\Parameter(
* name="tagId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* @OA\Property(
* property="title",
* type="string"
* ),
* @OA\Property(
* property="colorCode",
* default="#000000",
* type="string"
* ),
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryTag")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function updateTag(
$tagId,
Request $request,
ModelFactory $modelFactory,
LibraryTagService $libraryTagService
) {
$locale = $this->findLocale($request);
$tag = $libraryTagService->updateTag(
$tagId,
$request->request->get('title'),
$request->request->get('colorCode'),
$locale
);
return OpenApiService::jsonResponse($modelFactory->buildModel('LibraryTag', $tag, $locale));
}
/**
* @Route("/{tagId}", name="_delete", methods="DELETE", requirements={"tagId"="\d+"})
*
* @OA\Delete(
* path="/library/tag/{tagId}",
* tags={ "Library tag"},
* summary="Delete a tag",
* @OA\Parameter(
* name="tagId",
* in="path",
* required=true,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="204",
* description="Success"
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function deleteTag(
$tagId,
LibraryTagService $libraryTagService
) {
$libraryTagService->deleteTag($tagId);
return new JsonResponse('Success', 204);
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getAdminEditionLang()->getLocale();
}
return $locale;
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Tools\URL;
use TheliaLibrary\Service\ImageService;
/**
* @Route("/image-library", name="image_library_")
*/
class ImageController extends BaseFrontController
{
/**
* @Route("/{identifier}/{region}/{size}/{rotation}/{quality}.{format}", name="view")
*/
public function getImage(
$identifier,
$region,
$size,
$rotation,
$quality,
$format,
ImageService $imageService
) {
$imagePath = $imageService->geFormattedImage($identifier, $region, $size, $rotation, $quality, $format);
return new BinaryFileResponse($imagePath);
}
/**
* @Route("/{identifier}/info.json", name="info")
*/
public function getImageInformation(
$identifier,
ImageService $imageService
): Response {
$image = $imageService->openImage($identifier);
$size = $image->getSize();
$maxSize = $imageService->getMaxSize($image);
return new Response(
json_encode(
[
'@context' => 'http://iiif.io/api/image/3/context.json',
'id' => URL::getInstance()->absoluteUrl('image-library/'.$identifier),
'type' => 'ImageService3',
'protocol' => 'http://iiif.io/api/image',
'profile' => 'level2',
'width' => $size->getWidth(),
'height' => $size->getHeight(),
'maxWidth' => $maxSize->getWidth(),
'maxHeight' => $maxSize->getHeight(),
]
),
200,
[
'Content-Type' => 'application/ld+json;profile="http://iiif.io/api/image/3/context.json"',
]
);
}
}

View File

@@ -0,0 +1,148 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front;
use Propel\Runtime\ActiveQuery\ModelCriteria;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Model\ConfigQuery;
use Thelia\Model\ProductImage;
use Thelia\Tools\URL;
use TheliaLibrary\Service\ImageService;
use TheliaMain\PropelResolver;
/**
* @Route("/legacy-image-library", name="legacy_image_library_")
*/
class LegacyImageController extends BaseFrontController
{
/**
* @Route("/{itemType}_image_{imageId}/{region}/{size}/{rotation}/{quality}.{format}", name="view")
*/
public function getImage(
$itemType,
$imageId,
$region,
$size,
$rotation,
$quality,
$format,
ImageService $imageService
) {
$sourceFilePath = $this->getSourceFilePath($itemType, $imageId);
if (!\in_array(strtolower($format), ['jpg', 'jpeg', 'png', 'gif', 'jp2', 'webp'])) {
throw new HttpException(400, 'Bad format value');
}
$formattedImagePath = THELIA_WEB_DIR.'legacy-image-library'.DS.$itemType.'_image_'.$imageId.DS.$region.DS.$size.DS.$rotation;
if (!is_dir($formattedImagePath)) {
if (!@mkdir($formattedImagePath, 0755, true)) {
throw new \RuntimeException(sprintf('Failed to create %s file in cache directory', $formattedImagePath));
}
}
$formattedImagePath .= DS.$quality.'.'.$format;
if (file_exists($formattedImagePath)) {
return new BinaryFileResponse($formattedImagePath);
}
$image = $imageService->getImagineInstance()->open($sourceFilePath);
$image = $imageService->applyRegion($image, $region);
$image = $imageService->applySize($image, $size);
$image = $imageService->applyRotation($image, $rotation, $format);
$image = $imageService->applyQuality($image, $quality);
$image->save($formattedImagePath);
return new BinaryFileResponse($formattedImagePath);
}
/**
* @Route("/{itemType}_image_{imageId}/info.json", name="info")
*/
public function getImageInformation(
$itemType,
$imageId,
ImageService $imageService
): Response {
$sourceFilePath = $this->getSourceFilePath($itemType, $imageId);
$image = $imageService->getImagineInstance()->open($sourceFilePath);
$size = $image->getSize();
$maxSize = $imageService->getMaxSize($image);
return new Response(
json_encode(
[
'@context' => 'http://iiif.io/api/image/3/context.json',
'id' => URL::getInstance()->absoluteUrl('legacy-image-library/'.$itemType.'_image_'.$imageId),
'type' => 'ImageService3',
'protocol' => 'http://iiif.io/api/image',
'profile' => 'level2',
'width' => $size->getWidth(),
'height' => $size->getHeight(),
'maxWidth' => $maxSize->getWidth(),
'maxHeight' => $maxSize->getHeight(),
]
),
200,
[
'Content-Type' => 'application/ld+json;profile="http://iiif.io/api/image/3/context.json"',
]
);
}
private function getSnakeFromCamelCase($camelCase): string
{
$pattern = '/(?<=\\w)(?=[A-Z])|(?<=[a-z])(?=[0-9])/';
$snakeCase = preg_replace($pattern, '_', $camelCase);
return strtolower($snakeCase);
}
private function getSourceFilePath($itemType, $imageId)
{
$tableMapClass = PropelResolver::getTableMapByTableName($this->getSnakeFromCamelCase($itemType));
$tableMap = new $tableMapClass();
/** @var ModelCriteria $queryClass */
$queryClass = $tableMap->getClassName().'ImageQuery';
/** @var ProductImage $image */
$image = $queryClass::create()
->filterById($imageId)
->filterByVisible(1)
->findOne();
if (null === $image) {
return new Response(null, 404);
}
$baseSourceFilePath = ConfigQuery::read('images_library_path');
if ($baseSourceFilePath === null) {
$baseSourceFilePath = THELIA_LOCAL_DIR.'media'.DS.'images';
} else {
$baseSourceFilePath = THELIA_ROOT.$baseSourceFilePath;
}
return sprintf(
'%s/%s/%s',
$baseSourceFilePath,
$itemType,
$image->getFile()
);
}
}

View File

@@ -0,0 +1,224 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front\OpenApi;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Front\BaseFrontOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\LibraryImage;
use TheliaLibrary\Model\LibraryImageQuery;
use TheliaLibrary\Model\LibraryItemImageQuery;
/**
* @Route("/open_api/library/image", name="front_library_image")
*/
class ImageController extends BaseFrontOpenApiController
{
/**
* @Route("", name="_get", methods="GET")
*
* @OA\Get(
* path="/library/image",
* tags={"Library image"},
* summary="Get images",
* @OA\Parameter(
* name="id",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemType",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="title",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="code",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="onlyVisible",
* in="query",
* @OA\Schema(
* type="boolean",
* default="true"
* )
* ),
* @OA\Parameter(
* name="offset",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="limit",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="tagId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="width",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="height",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="locale",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getImage(
Request $request,
ModelFactory $modelFactory
) {
$locale = $this->findLocale($request);
$imageQuery = LibraryImageQuery::create()->orderById(Criteria::DESC);
if (null !== $id = $request->get('id')) {
$imageQuery->filterById($id);
}
if (null !== $title = $request->get('title')) {
$imageQuery->useLibraryImageI18nQuery()
->filterByLocale($locale)
->filterByTitle("%$title%", Criteria::LIKE)
->endUse();
}
$itemImageQuery = null;
if (null !== $itemId = $request->get('itemId')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByItemId($itemId);
}
if (null !== $itemType = $request->get('itemType')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByItemType($itemType);
}
if (null !== $code = $request->get('code')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByCode($code);
}
if (true === $request->get('onlyVisible')) {
$itemImageQuery = $this->getOrInitItemJoin($imageQuery, $itemImageQuery)->filterByVisible(true);
}
if (null !== $itemImageQuery) {
$itemImageQuery->orderByPosition();
$itemImageQuery->endUse();
}
if (null !== $tagId = $request->get('tagId')) {
$itemImageQuery = $imageQuery->useLibraryImageTagQuery()->filterByTagId($tagId)->endUse();
}
if (null !== $limit = $request->get('limit', 20)) {
$imageQuery->limit($limit);
}
if (null !== $offset = $request->get('offset', 0)) {
$imageQuery->offset($offset);
}
$width = $request->get('width');
$height = $request->get('height');
return OpenApiService::jsonResponse(array_map(
function (LibraryImage $image) use ($modelFactory, $locale, $width, $height) {
/** @var \TheliaLibrary\Model\Api\LibraryImage $imageModel */
$imageModel = $modelFactory->buildModel('LibraryImage', $image, $locale);
$imageModel->setWidth($width);
$imageModel->setHeight($height);
return $imageModel;
},
iterator_to_array($imageQuery->find())
));
}
protected function getOrInitItemJoin($query, $itemImageQuery = null): LibraryItemImageQuery
{
if (null !== $itemImageQuery) {
return $itemImageQuery;
}
return $query->useLibraryItemImageQuery();
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getLang()->getLocale();
}
return $locale;
}
}

View File

@@ -0,0 +1,197 @@
<?php
/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TheliaLibrary\Controller\Front\OpenApi;
use OpenApi\Annotations as OA;
use OpenApi\Controller\Front\BaseFrontOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Service\OpenApiService;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\Request;
use TheliaLibrary\Model\LibraryItemImage;
use TheliaLibrary\Model\LibraryItemImageQuery;
/**
* @Route("/open_api/library/item_image", name="front_library_item_image")
*/
class ItemImageController extends BaseFrontOpenApiController
{
/**
* @Route("", name="_get", methods="GET")
*
* @OA\Get(
* path="/library/item_image",
* tags={"Library image"},
* summary="Get item images association",
* @OA\Parameter(
* name="itemId",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="itemType",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="code",
* in="query",
* @OA\Schema(
* type="string"
* )
* ),
* @OA\Parameter(
* name="offset",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Parameter(
* name="limit",
* in="query",
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(ref="#/components/schemas/LibraryItemImage")
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getItemImage(
Request $request,
ModelFactory $modelFactory
) {
$locale = $this->findLocale($request);
$itemImageQuery = LibraryItemImageQuery::create();
if (null !== $imageId = $request->get('imageId')) {
$itemImageQuery->filterByImageId($imageId);
}
if (null !== $itemType = $request->get('itemType')) {
$itemImageQuery->filterByItemType($itemType);
}
if (null !== $itemId = $request->get('itemId')) {
$itemImageQuery->filterByItemId($itemId);
}
if (null !== $code = $request->get('code')) {
$itemImageQuery->filterByCode($code);
}
if (null !== $limit = $request->get('limit', 20)) {
$itemImageQuery->limit($limit);
}
if (null !== $offset = $request->get('offset', 0)) {
$itemImageQuery->offset($offset);
}
$itemImageQuery->orderByPosition();
return OpenApiService::jsonResponse(array_map(
function (LibraryItemImage $itemImage) use ($modelFactory, $locale) {
return $modelFactory->buildModel('LibraryItemImage', $itemImage, $locale);
},
iterator_to_array($itemImageQuery->find())
));
}
/**
* @Route("/types", name="_type_list", methods="GET")
*
* @OA\Get(
* path="/library/item_image/types",
* tags={"Library image"},
* summary="Get all item types availables",
* @OA\Parameter(
* name="onlyExisting",
* description="If false basic Thelia types will be added (product, content, ...) even if they have no image associated",
* in="query",
* @OA\Schema(
* type="boolean",
* default="false"
* )
* ),
* @OA\Response(
* response="200",
* description="Success",
* @OA\JsonContent(
* type="array",
* @OA\Items(
* type="string",
* )
* )
* ),
* @OA\Response(
* response="400",
* description="Bad request",
* @OA\JsonContent(ref="#/components/schemas/Error")
* )
* )
*/
public function getItemTypes(
Request $request
) {
$itemTypes = array_map(
function (LibraryItemImage $libraryItemImage) {
return $libraryItemImage->getItemType();
},
iterator_to_array(LibraryItemImageQuery::create()
->groupByItemType()
->find())
);
if (false === $request->get('onlyExisting', false) || 'false' === $request->get('onlyExisting', false)) {
$itemTypes = array_merge(
[
'product',
'category',
'content',
'folder',
],
$itemTypes
);
}
return OpenApiService::jsonResponse(
$itemTypes
);
}
protected function findLocale(Request $request)
{
$locale = $request->get('locale');
if (null == $locale) {
$locale = $request->getSession()->getAdminEditionLang()->getLocale();
}
return $locale;
}
}