diff --git a/core/lib/Thelia/Action/Config.php b/core/lib/Thelia/Action/Config.php index a4a2425a6..fa683f1d4 100644 --- a/core/lib/Thelia/Action/Config.php +++ b/core/lib/Thelia/Action/Config.php @@ -53,6 +53,8 @@ class Config extends BaseAction implements EventSubscriberInterface ->setValue($event->getValue()) ->setLocale($event->getLocale()) ->setTitle($event->getTitle()) + ->setHidden($event->getHidden()) + ->setSecured($event->getSecured()) ->save() ; @@ -69,18 +71,19 @@ class Config extends BaseAction implements EventSubscriberInterface { $search = ConfigQuery::create(); - if (null !== $config = $search->findOneById($event->getConfigId()) - && - $event->getValue() != $config->getValue()) { + if (null !== $config = $search->findOneById($event->getConfigId())) { - $config - ->setDispatcher($this->getDispatcher()) + if ($event->getValue() !== $config->getValue()) { - ->setValue($event->getValue()) - ->save() - ; + $config + ->setDispatcher($this->getDispatcher()) - $event->setConfig($config); + ->setValue($event->getValue()) + ->save() + ; + + $event->setConfig($config); + } } } @@ -122,6 +125,7 @@ class Config extends BaseAction implements EventSubscriberInterface */ public function delete(ConfigDeleteEvent $event) { + if (null !== ($config = ConfigQuery::create()->findOneById($event->getConfigId()))) { if (! $config->getSecured()) { diff --git a/core/lib/Thelia/Controller/Admin/VariablesController.php b/core/lib/Thelia/Controller/Admin/VariablesController.php index 82c4c39d9..89e9fb326 100644 --- a/core/lib/Thelia/Controller/Admin/VariablesController.php +++ b/core/lib/Thelia/Controller/Admin/VariablesController.php @@ -83,7 +83,9 @@ class VariablesController extends BaseAdminController ->setValue($data['value']) ->setLocale($data["locale"]) ->setTitle($data['title']) - ; + ->setHidden($data['hidden']) + ->setSecured($data['secured']) + ; $this->dispatch(TheliaEvents::CONFIG_CREATE, $createEvent); @@ -288,7 +290,7 @@ class VariablesController extends BaseAdminController if (null !== $response = $this->checkAuth("admin.configuration.variables.delete")) return $response; // Get the config id, and dispatch the delet request - $event = new ConfigDeleteEvent($this->getRequest()->get('id')); + $event = new ConfigDeleteEvent($this->getRequest()->get('variable_id')); $this->dispatch(TheliaEvents::CONFIG_DELETE, $event); diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index bede9f08c..c7c9f6f14 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -120,6 +120,29 @@ class BaseController extends ContainerAware return $request->getSession(); } + /** + * Get all errors that occured in a form + * + * @param \Symfony\Component\Form\Form $form + * @return string the error string + */ + private function getErrorMessages(\Symfony\Component\Form\Form $form) { + + $errors = ''; + + foreach ($form->getErrors() as $key => $error) { + $errors .= $error->getMessage() . ', '; + } + + foreach ($form->all() as $child) { + if (!$child->isValid()) { + $errors .= $this->getErrorMessages($child) . ', '; + } + } + + return rtrim($errors, ', '); + } + /** * Validate a BaseForm * @@ -138,10 +161,12 @@ class BaseController extends ContainerAware if ($form->isValid()) { return $form; - } else { - throw new FormValidationException("Missing or invalid data"); } - } else { + else { + throw new FormValidationException(sprintf("Missing or invalid data: %s", $this->getErrorMessages($form))); + } + } + else { throw new FormValidationException(sprintf("Wrong form method, %s expected.", $expectedMethod)); } } diff --git a/core/lib/Thelia/Core/Event/ConfigChangeEvent.php b/core/lib/Thelia/Core/Event/ConfigChangeEvent.php index 7a4acb64f..e7da059ee 100644 --- a/core/lib/Thelia/Core/Event/ConfigChangeEvent.php +++ b/core/lib/Thelia/Core/Event/ConfigChangeEvent.php @@ -29,8 +29,6 @@ class ConfigChangeEvent extends ConfigCreateEvent { protected $config_id; - protected $hidden; - protected $secured; protected $description; protected $chapo; protected $postscriptum; @@ -52,30 +50,6 @@ class ConfigChangeEvent extends ConfigCreateEvent return $this; } - public function getHidden() - { - return $this->hidden; - } - - public function setHidden($hidden) - { - $this->hidden = $hidden; - - return $this; - } - - public function getSecured() - { - return $this->secured; - } - - public function setSecured($secured) - { - $this->secured = $secured; - - return $this; - } - public function getDescription() { return $this->description; diff --git a/core/lib/Thelia/Core/Event/ConfigCreateEvent.php b/core/lib/Thelia/Core/Event/ConfigCreateEvent.php index c5561fb98..79440c491 100644 --- a/core/lib/Thelia/Core/Event/ConfigCreateEvent.php +++ b/core/lib/Thelia/Core/Event/ConfigCreateEvent.php @@ -30,6 +30,8 @@ class ConfigCreateEvent extends ConfigEvent protected $value; protected $locale; protected $title; + protected $hidden; + protected $secured; // Use event_name to prevent conflict with Event::name property. public function getEventName() @@ -79,4 +81,28 @@ class ConfigCreateEvent extends ConfigEvent return $this; } + + public function getHidden() + { + return $this->hidden; + } + + public function setHidden($hidden) + { + $this->hidden = $hidden; + + return $this; + } + + public function getSecured() + { + return $this->secured; + } + + public function setSecured($secured) + { + $this->secured = $secured; + + return $this; + } } diff --git a/core/lib/Thelia/Core/Event/ConfigDeleteEvent.php b/core/lib/Thelia/Core/Event/ConfigDeleteEvent.php index dd4e3f8fe..6cc39438c 100644 --- a/core/lib/Thelia/Core/Event/ConfigDeleteEvent.php +++ b/core/lib/Thelia/Core/Event/ConfigDeleteEvent.php @@ -27,8 +27,22 @@ use Thelia\Model\Config; class ConfigDeleteEvent extends ConfigEvent { + protected $config_id; + public function __construct($config_id) { $this->setConfigId($config_id); } + + public function getConfigId() + { + return $this->config_id; + } + + public function setConfigId($config_id) + { + $this->config_id = $config_id; + + return $this; + } } diff --git a/core/lib/Thelia/Form/VariableCreationForm.php b/core/lib/Thelia/Form/VariableCreationForm.php index e9f78a01a..5ac0b47ea 100644 --- a/core/lib/Thelia/Form/VariableCreationForm.php +++ b/core/lib/Thelia/Form/VariableCreationForm.php @@ -22,31 +22,39 @@ /*************************************************************************************/ namespace Thelia\Form; -use Symfony\Component\Validator\Constraints\NotBlank; -use Thelia\Model\LangQuery; -use Propel\Runtime\ActiveQuery\Criteria; +use Symfony\Component\Validator\Constraints; +use Thelia\Model\ConfigQuery; +use Symfony\Component\Validator\ExecutionContextInterface; class VariableCreationForm extends BaseForm { - protected function buildForm() + protected function buildForm($change_mode = false) { + $name_constraints = array(new Constraints\NotBlank()); + + if (!$change_mode) { + $name_constraints[] = new Constraints\Callback(array( + "methods" => array(array($this, "checkDuplicateName")) + )); + } + $this->formBuilder ->add("name", "text", array( - "constraints" => array( - new NotBlank() - ) + "constraints" => $name_constraints )) ->add("title", "text", array( "constraints" => array( - new NotBlank() + new Constraints\NotBlank() ) )) ->add("locale", "hidden", array( "constraints" => array( - new NotBlank() + new Constraints\NotBlank() ) )) ->add("value", "text", array()) + ->add("hidden", "hidden", array()) + ->add("secured", "hidden", array()) ; } @@ -54,4 +62,14 @@ class VariableCreationForm extends BaseForm { return "thelia_variable_creation"; } + + public function checkDuplicateName($value, ExecutionContextInterface $context) + { + $config = ConfigQuery::create()->findOneByName($value); + + if ($config) { + $context->addViolation(sprintf("A variable with name \"%s\" already exists.", $value)); + } + } + } diff --git a/core/lib/Thelia/Form/VariableModificationForm.php b/core/lib/Thelia/Form/VariableModificationForm.php index dfae754e8..28589a9a4 100644 --- a/core/lib/Thelia/Form/VariableModificationForm.php +++ b/core/lib/Thelia/Form/VariableModificationForm.php @@ -31,7 +31,7 @@ class VariableModificationForm extends BaseDescForm { protected function buildForm() { - parent::buildForm(); + parent::buildForm(true); $this->formBuilder ->add("id", "hidden", array( @@ -49,11 +49,11 @@ class VariableModificationForm extends BaseDescForm ->add("value", "text", array()) ->add("hidden", "hidden", array()) ->add("secured", "hidden", array()) - ; + ; } public function getName() { - return "thelia_variable_creation"; + return "thelia_variable_modification"; } } diff --git a/templates/admin/default/variable-edit.html b/templates/admin/default/variable-edit.html index 9f0633a01..257084711 100644 --- a/templates/admin/default/variable-edit.html +++ b/templates/admin/default/variable-edit.html @@ -1,4 +1,4 @@ -{check_auth context="admin" roles="ADMIN" permissions="admin.configuration.variables.edit" login_tpl="admin/login"} +{check_auth context="admin" roles="ADMIN" permissions="admin.configuration.variables.edit" login_tpl="/admin/login"} {$page_title={intl l='Edit a system variable'}} @@ -11,9 +11,9 @@ {loop name="config_edit" type="config" hidden="*" id="$variable_id" backend_context="1" lang="$edition_language"} @@ -30,7 +30,7 @@
{form name="thelia.admin.variable.modification"} -
+ {* Be sure to get the variable ID, even if the form could not be validated *} @@ -40,7 +40,7 @@ {form_hidden_fields form=$form} {form_field form=$form field='success_url'} - + {/form_field} {* We do not allow creation of hidden variables *} @@ -50,7 +50,7 @@ {/form_field} {form_field form=$form field='hidden'} - + {/form_field} {form_field form=$form field='locale'} @@ -94,7 +94,7 @@
- {form_field form=$form field='value'} + {form_field form=$form field='secured'}