Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add texture in attribute color #36068

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
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,13 @@ public function handle(AddAttributeCommand $command): AttributeId

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

if (null !== $command->getTextureFilePath()) {
$this->attributeFileUploader->upload(
$command->getTextureFilePath(),
$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->getTextureFilePath()) {
$this->attributeFileUploader->deleteOldFile($command->getAttributeId()->getValue());
$this->attributeFileUploader->upload($command->getTextureFilePath(), $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 (file_exists($filePath)) {
move_uploaded_file($filePath, _PS_IMG_DIR_ . 'co/' . $id . '.jpg');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
move_uploaded_file($filePath, _PS_IMG_DIR_ . 'co/' . $id . '.jpg');
move_uploaded_file($filePath, _PS_COL_IMG_DIR_ . $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');
Comment on lines +48 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (file_exists(_PS_IMG_DIR_ . 'co/' . $id . '.jpg')) {
unlink(_PS_IMG_DIR_ . 'co/' . $id . '.jpg');
if (file_exists(_PS_COL_IMG_DIR_ . $id . '.jpg')) {
unlink(_PS_COL_IMG_DIR_ . $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 setTextureFilePath(
string $pathName,
): void {
$this->pathName = $pathName;
}

public function getTextureFilePath(): ?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 setTextureFilePath(
string $pathName,
): void {
$this->pathName = $pathName;
}

public function getTextureFilePath(): ?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 @@ -32,6 +32,7 @@
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\AddAttributeCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\EditAttributeCommand;
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\ValueObject\AttributeId;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Handles data of submitted Attribute Group form.
Expand All @@ -56,13 +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']
));
$data['shop_association'],
);

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

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

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

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

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

$updateCommand->setTextureFilePath(
$file->getPathname()
);
}

$this->commandBus->handle($updateCommand);
}
}
78 changes: 78 additions & 0 deletions src/Core/Grid/Data/Factory/AttributeGridDataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\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;

/**
* Decorates database records for grid presentation
*/
final class AttributeGridDataFactory implements GridDataFactoryInterface
{
/**
* @param GridDataFactoryInterface $attributeDataFactory
*/
public function __construct(
private readonly GridDataFactoryInterface $attributeDataFactory,
private readonly ImageFolderProvider $imageFolderProvider
) {
}

/**
* {@inheritdoc}
*/
public function getData(SearchCriteriaInterface $searchCriteria)
{
$records = $this->attributeDataFactory->getData($searchCriteria);
$modifiedRecords = $this->modifyRecords($records->getRecords()->all());

return new GridData(
new RecordCollection($modifiedRecords),
$records->getRecordsTotal(),
$records->getQuery()
);
}

/**
* @param array $records
*
* @return array
*/
private function modifyRecords(array $records): array
{
foreach ($records as &$record) {
if (file_exists(_PS_IMG_DIR_ . 'co/' . (int) $record['id_attribute'] . '.jpg')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (file_exists(_PS_IMG_DIR_ . 'co/' . (int) $record['id_attribute'] . '.jpg')) {
if (file_exists(_PS_COL_IMG_DIR_ . (int) $record['id_attribute'] . '.jpg')) {

$record['texture'] = $this->imageFolderProvider->getUrl() . '/' . (int) $record['id_attribute'] . '.jpg';
}
}

return $records;
}
}
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: ~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't necessary, no?