Added name duplication check when creating a variable

This commit is contained in:
franck
2013-09-02 01:25:11 +02:00
parent 928bf7b638
commit 1558f6af5f
10 changed files with 169 additions and 75 deletions

View File

@@ -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()) {

View File

@@ -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);

View File

@@ -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));
}
}

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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));
}
}
}

View File

@@ -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";
}
}