Skip to content

Commit

Permalink
[project-base] fixed product edit in development environment (#3544)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Nov 4, 2024
1 parent e38a8be commit 83c7dc6
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 5 deletions.
1 change: 1 addition & 0 deletions assets/js/admin/validation/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ import './validationPromoCode';
import './validationHreflangSetting';
import './validationStore';
import './validationTransportPrices';
import './validationUniqueProductCatnum';
37 changes: 37 additions & 0 deletions assets/js/admin/validation/form/validationUniqueProductCatnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(function ($, window) {
window.ShopsysFrameworkBundleFormConstraintsUniqueProductCatnum = function () {
this.message = null;

this.validate = (value, element) => {
const $catnumInput = $('#' + element.id);

const url = $catnumInput.data('unique-catnum-url');
const currentProductCatnum = $catnumInput.data('current-product-catnum');

if (url === undefined) {
return [];
}

FpJsFormValidator.ajax.sendRequest(
url,
{
catnum: value,
currentProductCatnum
},
(response) => {
const catnumExists = JSON.parse(response);

if (catnumExists) {
$catnumInput.jsFormValidator('showErrors', {
errors: [this.message],
sourceId: 'form-error-' + String(element.id).replace(/_/g, '-')
});
}
}
);

return [];
};
};

})(jQuery, window);
20 changes: 15 additions & 5 deletions src/Form/Admin/Product/ProductFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopsys\FrameworkBundle\Form\Admin\Product\Price\ProductPricesWithVatSelectType;
use Shopsys\FrameworkBundle\Form\Admin\Stock\ProductStockFormType;
use Shopsys\FrameworkBundle\Form\CategoriesType;
use Shopsys\FrameworkBundle\Form\Constraints\UniqueProductCatnum;
use Shopsys\FrameworkBundle\Form\Constraints\UniqueProductParameters;
use Shopsys\FrameworkBundle\Form\DatePickerType;
use Shopsys\FrameworkBundle\Form\DisplayOnlyType;
Expand Down Expand Up @@ -48,6 +49,7 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Validator\Constraints;

class ProductFormType extends AbstractType
Expand All @@ -66,6 +68,7 @@ class ProductFormType extends AbstractType
* @param \Shopsys\FrameworkBundle\Form\Transformers\ProductParameterValueToProductParameterValuesLocalizedTransformer $productParameterValueToProductParameterValuesLocalizedTransformer
* @param \Shopsys\FrameworkBundle\Model\Product\ProductFacade $productFacade
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportFacade $transportFacade
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $urlGenerator
*/
public function __construct(
private readonly BrandFacade $brandFacade,
Expand All @@ -79,6 +82,7 @@ public function __construct(
private readonly ProductParameterValueToProductParameterValuesLocalizedTransformer $productParameterValueToProductParameterValuesLocalizedTransformer,
private readonly ProductFacade $productFacade,
private readonly TransportFacade $transportFacade,
private readonly UrlGeneratorInterface $urlGenerator,
) {
}

Expand Down Expand Up @@ -164,14 +168,20 @@ private function createBasicInformationGroup(
]);

$builderBasicInformationGroup->add('catnum', TextType::class, [
'required' => false,
'required' => true,
'constraints' => [
new Constraints\Length(
['max' => 100, 'maxMessage' => 'Catalog number cannot be longer than {{ limit }} characters'],
),
new Constraints\NotBlank(),
new Constraints\Length(['max' => 100, 'maxMessage' => 'Catalog number cannot be longer than {{ limit }} characters']),
new UniqueProductCatnum(['product' => $product]),
],
'disabled' => $this->isProductMainVariant($product),
'attr' => $disabledItemInMainVariantAttr,
'attr' => array_merge(
$disabledItemInMainVariantAttr,
[
'data-unique-catnum-url' => $this->urlGenerator->generate('admin_product_catnumexists'),
'data-current-product-catnum' => $product !== null ? $product->getCatnum() : '',
],
),
'label' => t('Catalog number'),
])
->add('partno', TextType::class, [
Expand Down
15 changes: 15 additions & 0 deletions src/Form/Constraints/UniqueProductCatnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Form\Constraints;

use Shopsys\FrameworkBundle\Model\Product\Product;
use Symfony\Component\Validator\Constraint;

class UniqueProductCatnum extends Constraint
{
public string $message = 'Product with entered catalog number already exists';

public ?Product $product = null;
}
48 changes: 48 additions & 0 deletions src/Form/Constraints/UniqueProductCatnumValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Form\Constraints;

use Shopsys\FrameworkBundle\Model\Product\ProductFacade;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class UniqueProductCatnumValidator extends ConstraintValidator
{
/**
* @param \Shopsys\FrameworkBundle\Model\Product\ProductFacade $productFacade
*/
public function __construct(
protected readonly ProductFacade $productFacade,
) {
}

/**
* @param mixed $value
* @param \Symfony\Component\Validator\Constraint $constraint
*/
public function validate($value, Constraint $constraint): void
{
if (!$constraint instanceof UniqueProductCatnum) {
throw new UnexpectedTypeException($constraint, UniqueProductCatnum::class);
}

if ($value === null || $value === '') {
return;
}

if ($constraint->product !== null && $value === $constraint->product->getCatnum()) {
return;
}

$productByCatnum = $this->productFacade->findByCatnum($value);

if ($productByCatnum === null) {
return;
}

$this->context->addViolation($constraint->message);
}
}
9 changes: 9 additions & 0 deletions src/Model/Product/ProductFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,13 @@ protected function getChangedNamesByLocale(Product $product, array $originalName

return $changedProductNames;
}

/**
* @param string $catnum
* @return \Shopsys\FrameworkBundle\Model\Product\Product|null
*/
public function findByCatnum(string $catnum): ?Product
{
return $this->productRepository->findByCatnum($catnum);
}
}
14 changes: 14 additions & 0 deletions src/Model/Product/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,18 @@ protected function getSortedProductIds(array $catnumsToSortBy, array $catnumToId

return $sortedProductIds;
}

/**
* @param string $catnum
* @return \Shopsys\FrameworkBundle\Model\Product\Product|null
*/
public function findByCatnum(string $catnum): ?Product
{
$queryBuilder = $this->getProductRepository()
->createQueryBuilder('p')
->andWhere('p.catnum = :catnum')
->setParameter('catnum', $catnum);

return $queryBuilder->getQuery()->getOneOrNullResult();
}
}
3 changes: 3 additions & 0 deletions src/Resources/translations/validators.cs.po
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ msgstr "Název produktu nesmí být delší než {{ limit }} znaků"
msgid "Product parameters are duplicate."
msgstr "Parametry produktů jsou duplicitní."

msgid "Product with entered catalog number already exists"
msgstr "Produkt s tímto katalogovým číslem již existuje"

msgid "Promo code \"%invalidPromoCode%\" is not available for pricing group ID \"%pricingGroupId%\"."
msgstr "Slevový kupón \"%invalidPromoCode%\" není dostupný pro Vaši cenovou skupinu."

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/translations/validators.en.po
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ msgstr ""
msgid "Product parameters are duplicate."
msgstr ""

msgid "Product with entered catalog number already exists"
msgstr ""

msgid "Promo code \"%invalidPromoCode%\" is not available for pricing group ID \"%pricingGroupId%\"."
msgstr ""

Expand Down

0 comments on commit 83c7dc6

Please sign in to comment.