From eaad1afdff8295940c30c5dcd70e711d8e8bd868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chans=C3=A9aume?= Date: Mon, 21 Jul 2014 15:01:49 +0200 Subject: [PATCH] Added possibility to change the main product reference --- core/lib/Thelia/Action/Product.php | 2 +- .../Controller/Admin/ProductController.php | 1 + core/lib/Thelia/Controller/BaseController.php | 4 ++-- core/lib/Thelia/Form/ProductCreationForm.php | 14 ++++++------- .../Thelia/Form/ProductModificationForm.php | 20 +++++++++++++++++++ .../default/includes/product-general-tab.html | 4 +--- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/core/lib/Thelia/Action/Product.php b/core/lib/Thelia/Action/Product.php index 9cef94218..5bdbca2f3 100644 --- a/core/lib/Thelia/Action/Product.php +++ b/core/lib/Thelia/Action/Product.php @@ -92,7 +92,7 @@ class Product extends BaseAction implements EventSubscriberInterface $product ->setDispatcher($event->getDispatcher()) - + ->setRef($event->getRef()) ->setLocale($event->getLocale()) ->setTitle($event->getTitle()) ->setDescription($event->getDescription()) diff --git a/core/lib/Thelia/Controller/Admin/ProductController.php b/core/lib/Thelia/Controller/Admin/ProductController.php index ba78c830e..afc5da7f8 100644 --- a/core/lib/Thelia/Controller/Admin/ProductController.php +++ b/core/lib/Thelia/Controller/Admin/ProductController.php @@ -158,6 +158,7 @@ class ProductController extends AbstractSeoCrudController $changeEvent ->setLocale($formData['locale']) + ->setRef($formData['ref']) ->setTitle($formData['title']) ->setChapo($formData['chapo']) ->setDescription($formData['description']) diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 424469876..b039887c6 100644 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -218,13 +218,13 @@ abstract class BaseController extends ContainerAware if ($form->get("error_message")->getData() != null) { $errorMessage = $form->get("error_message")->getData(); } else { - $errorMessage = sprintf("Missing or invalid data: %s", $this->getErrorMessages($form)); + $errorMessage = sprintf($this->getTranslator()->trans("Missing or invalid data: %s"), $this->getErrorMessages($form)); } throw new FormValidationException($errorMessage); } } else { - throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod)); + throw new FormValidationException(sprintf($this->getTranslator()->trans("Wrong form method, %s expected."), $expectedMethod)); } } diff --git a/core/lib/Thelia/Form/ProductCreationForm.php b/core/lib/Thelia/Form/ProductCreationForm.php index d74529509..565a7dbf5 100644 --- a/core/lib/Thelia/Form/ProductCreationForm.php +++ b/core/lib/Thelia/Form/ProductCreationForm.php @@ -22,17 +22,15 @@ class ProductCreationForm extends BaseForm { protected function buildForm($change_mode = false) { - $ref_constraints = array(new NotBlank()); - - if (! $change_mode) { - $ref_constraints[] = new Callback(array( - "methods" => array(array($this, "checkDuplicateRef")) - )); - } $this->formBuilder ->add("ref", "text", array( - "constraints" => $ref_constraints, + "constraints" => array( + new NotBlank(), + new Callback(array( + "methods" => array(array($this, "checkDuplicateRef")) + )) + ), "label" => Translator::getInstance()->trans('Product reference *'), "label_attr" => array("for" => "ref") )) diff --git a/core/lib/Thelia/Form/ProductModificationForm.php b/core/lib/Thelia/Form/ProductModificationForm.php index 894399d94..e580a28c2 100644 --- a/core/lib/Thelia/Form/ProductModificationForm.php +++ b/core/lib/Thelia/Form/ProductModificationForm.php @@ -12,9 +12,12 @@ namespace Thelia\Form; +use Propel\Runtime\ActiveQuery\Criteria; use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\ExecutionContextInterface; use Thelia\Core\Translation\Translator; +use Thelia\Model\ProductQuery; class ProductModificationForm extends ProductCreationForm { @@ -49,6 +52,23 @@ class ProductModificationForm extends ProductCreationForm $this->addStandardDescFields(array('title', 'locale')); } + public function checkDuplicateRef($value, ExecutionContextInterface $context) + { + $data = $context->getRoot()->getData(); + + $count = ProductQuery::create() + ->filterById($data['id'], Criteria::NOT_EQUAL) + ->filterByRef($value)->count(); + + if ($count > 0) { + $context->addViolation( + Translator::getInstance()->trans( + "A product with reference %ref already exists. Please choose another reference.", + array('%ref' => $value) + )); + } + } + public function getName() { return "thelia_product_modification"; diff --git a/templates/backOffice/default/includes/product-general-tab.html b/templates/backOffice/default/includes/product-general-tab.html index 8068ff7fd..7723fef44 100644 --- a/templates/backOffice/default/includes/product-general-tab.html +++ b/templates/backOffice/default/includes/product-general-tab.html @@ -40,9 +40,7 @@ {$label} :
- {* If ref's 'value is not empty the field is disable and the value is sent with an hidden field *} - - {if $value}{/if} +
{/form_field}