Skip to content

Commit

Permalink
fix(bo): fix img path for bo
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed May 16, 2024
1 parent 1d5a4a9 commit 388198d
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 26 deletions.
8 changes: 8 additions & 0 deletions admin-dev/themes/new-theme/scss/pages/_attribute.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
height: 25px;
border: solid 1px $black;
}

.image-container {
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
margin: auto;
}
7 changes: 7 additions & 0 deletions src/Adapter/Attribute/CommandHandler/AddAttributeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use PrestaShop\PrestaShop\Adapter\Attribute\Repository\AttributeRepository;
use PrestaShop\PrestaShop\Adapter\Attribute\Validate\AttributeValidator;
use PrestaShop\PrestaShop\Adapter\File\Uploader\AttributeFileUploader;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\AddAttributeCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\CommandHandler\AddAttributeHandlerInterface;
Expand All @@ -45,6 +46,7 @@ class AddAttributeHandler implements AddAttributeHandlerInterface
public function __construct(
private readonly AttributeRepository $attributeRepository,
private readonly AttributeValidator $attributeValidator,
private readonly AttributeFileUploader $attributeFileUploader
) {
}

Expand All @@ -68,6 +70,11 @@ public function handle(AddAttributeCommand $command): AttributeId

$id = $this->attributeRepository->add($attribute);

$this->attributeFileUploader->upload(
$command->getPathName(),
$id->getValue()
);

return $id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use PrestaShop\PrestaShop\Adapter\Attribute\Repository\AttributeRepository;
use PrestaShop\PrestaShop\Adapter\Attribute\Validate\AttributeValidator;
use PrestaShop\PrestaShop\Adapter\Domain\LocalizedObjectModelTrait;
use PrestaShop\PrestaShop\Adapter\File\Uploader\AttributeFileUploader;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\EditAttributeCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\CommandHandler\EditAttributeHandlerInterface;
Expand All @@ -45,7 +46,8 @@ class EditAttributeHandler implements EditAttributeHandlerInterface

public function __construct(
private AttributeRepository $attributeRepository,
private AttributeValidator $attributeValidator
private AttributeValidator $attributeValidator,
private AttributeFileUploader $attributeFileUploader
) {
}

Expand Down Expand Up @@ -76,6 +78,11 @@ public function handle(EditAttributeCommand $command): void
$propertiesToUpdate[] = 'id_shop_list';
}

if (null !== $command->getPathName()) {
$this->attributeFileUploader->deleteOldFile($command->getAttributeId()->getValue());
$this->attributeFileUploader->upload($command->getPathName(), $command->getAttributeId()->getValue());
}

$this->attributeValidator->validate($attribute);
$this->attributeRepository->partialUpdate($attribute, $propertiesToUpdate);
}
Expand Down
52 changes: 52 additions & 0 deletions src/Adapter/File/Uploader/AttributeFileUploader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Adapter\File\Uploader;

use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\AttributeFileUploaderInterface;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeUploadFailedException;
use PrestaShop\PrestaShop\Core\File\Exception\FileException;

class AttributeFileUploader implements AttributeFileUploaderInterface
{
public function upload(?string $filePath, int $id): void
{
try {
if(null !== $filePath) {
move_uploaded_file($filePath, _PS_IMG_DIR_ . 'co/' . $id . '.jpg');
}
} catch (FileException $e) {
throw new AttributeUploadFailedException((sprintf('Failed to copy the file %s.', $filePath)));
}
}

public function deleteOldFile(int $id): void
{
if (file_exists(_PS_IMG_DIR_ . 'co/' . $id . '.jpg')) {
unlink(_PS_IMG_DIR_ . 'co/' . $id . '.jpg');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute;

interface AttributeFileUploaderInterface
{
/**
* @param string $filePath
* @param int $id
*/
public function upload(string $filePath, int $id): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class AddAttributeCommand
*/
private $associatedShopIds;

/**
* @var string|null
*/
private $pathName;

/**
* @param int $attributeGroupId
* @param array $localizedValue
Expand Down Expand Up @@ -105,6 +110,20 @@ public function getAssociatedShopIds(): array
return $this->associatedShopIds;
}

/**
* @param string $pathName
*/
public function setTextureInformation(
string $pathName,
): void {
$this->pathName = $pathName;
}

public function getPathName(): ?string
{
return $this->pathName;
}

/**
* Asserts that attribute group's names are valid.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class EditAttributeCommand

private ?string $color;

private ?string $pathName;

/**
* @var int[]
*/
Expand Down Expand Up @@ -125,6 +127,20 @@ public function setAssociatedShopIds(array $associatedShopIds): self
return $this;
}

/**
* @param string $pathName
*/
public function setTextureInformation(
string $pathName,
): void {
$this->pathName = $pathName;
}

public function getPathName(): ?string
{
return $this->pathName ?? null;
}

/**
* Asserts that attribute group's names are valid.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception;

class AttributeUploadFailedException extends AttributeException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\EditAttributeCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\ValueObject\AttributeId;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;

/**
* Handles data of submitted Attribute Group form.
Expand All @@ -58,14 +57,24 @@ public function __construct(CommandBusInterface $commandBus)
*/
public function create(array $data)
{
/** @var AttributeId $attributeId */
$attributeId = $this->commandBus->handle(new AddAttributeCommand(
$addAttributeCommand = new AddAttributeCommand(
$data['attribute_group'],
$data['name'],
$data['color'] ?? '',
$data['shop_association'],
));
$this->handleUploadedFile($data['texture'], $attributeId->getValue());
);

if (isset($data['texture'])) {
/** @var UploadedFile $file */
$file = $data['texture'];

$addAttributeCommand->setTextureInformation(
$file->getPathname()
);
}

/** @var AttributeId $attributeId */
$attributeId = $this->commandBus->handle($addAttributeCommand);

return $attributeId->getValue();
}
Expand All @@ -81,17 +90,15 @@ public function update($id, array $data)
->setColor($data['color'])
->setAssociatedShopIds($data['shop_association']);

$this->commandBus->handle($updateCommand);
if (isset($data['texture'])) {
/** @var UploadedFile $file */
$file = $data['texture'];

// delete previous img if exist
if (file_exists(_PS_IMG_DIR_ . 'co' . '/' . $id . '.jpg')) {
unlink(_PS_IMG_DIR_ . 'co' . '/' . $id . '.jpg');
$updateCommand->setTextureInformation(
$file->getPathname()
);
}
$this->handleUploadedFile($data['texture'], $id);
}

protected function handleUploadedFile(UploadedFile $file, int $id): void
{
$file->move(_PS_IMG_DIR_ . 'co', $id . '.jpg');
$this->commandBus->handle($updateCommand);
}
}
13 changes: 6 additions & 7 deletions src/Core/Grid/Data/Factory/AttributeGridDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

namespace PrestaShop\PrestaShop\Core\Grid\Data\Factory;

use PrestaShop\PrestaShop\Adapter\Shop\Url\ImageFolderProvider;
use PrestaShop\PrestaShop\Core\Grid\Data\GridData;
use PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection;
use PrestaShop\PrestaShop\Core\Grid\Search\SearchCriteriaInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Decorates database records for grid presentation
Expand All @@ -41,16 +41,15 @@ final class AttributeGridDataFactory implements GridDataFactoryInterface
*/
private $attributeDataFactory;


private UrlGeneratorInterface $urlGenerator;
private ImageFolderProvider $imageFolderProvider;

/**
* @param GridDataFactoryInterface $attributeDataFactory
*/
public function __construct(GridDataFactoryInterface $attributeDataFactory, UrlGeneratorInterface $urlGenerator)
public function __construct(GridDataFactoryInterface $attributeDataFactory, ImageFolderProvider $imageFolderProvider)
{
$this->attributeDataFactory = $attributeDataFactory;
$this->urlGenerator = $urlGenerator;
$this->imageFolderProvider = $imageFolderProvider;
}

/**
Expand All @@ -76,8 +75,8 @@ public function getData(SearchCriteriaInterface $searchCriteria)
private function modifyRecords(array $records): array
{
foreach ($records as &$record) {
if (file_exists(_PS_IMG_DIR_ . 'co' . '/' . (int) $record['id_attribute'] . '.jpg')) {
$record['texture'] = $this->urlGenerator->generate('assets', ['path' => _PS_IMG_DIR_ . 'co' . '/' . (int) $record['id_attribute'] . '.jpg']);
if (file_exists(_PS_IMG_DIR_ . 'co/' . (int) $record['id_attribute'] . '.jpg')) {
$record['texture'] = $this->imageFolderProvider->getUrl() . '/' . (int) $record['id_attribute'] . '.jpg';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ services:
PrestaShop\PrestaShop\Adapter\Attribute\CommandHandler\EditAttributeHandler: ~
PrestaShop\PrestaShop\Adapter\Attribute\QueryHandler\GetAttributeForEditingHandler: ~
PrestaShop\PrestaShop\Adapter\Attribute\Validate\AttributeValidator: ~
PrestaShop\PrestaShop\Adapter\File\Uploader\AttributeFileUploader: ~
PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\AttributeFileUploaderInterface: ~
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ services:
- "@=service('prestashop.adapter.legacy.context').getContext().link"
- 'img/p'

prestashop.adapter.shop.url.attribute_image_folder_provider:
class: 'PrestaShop\PrestaShop\Adapter\Shop\Url\ImageFolderProvider'
arguments:
- "@=service('prestashop.adapter.legacy.context').getContext().link"
- 'img/co'

prestashop.adapter.shop.url.category_image_folder_provider:
class: 'PrestaShop\PrestaShop\Adapter\Shop\Url\ImageFolderProvider'
arguments:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ services:
class: 'PrestaShop\PrestaShop\Core\Grid\Data\Factory\AttributeGridDataFactory'
arguments:
- '@prestashop.core.grid.data.factory.attribute'
- '@prestashop.core.admin.url_generator'
- '@prestashop.adapter.shop.url.attribute_image_folder_provider'

prestashop.core.grid.data.factory.attribute_group:
class: '%prestashop.core.grid.data.factory.doctrine_grid_data_factory%'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*#}
{% if record['texture'] is defined %}
<img src="{{ record['texture'] }}" />
<div class="attributes-color-container">
<img class="image-container" src="{{ record['texture'] }}" />
</div>
{% else %}
<div style="background-color:{{ record[column.options.field] }};" class="attributes-color-container"></div>
{% endif %}

0 comments on commit 388198d

Please sign in to comment.