From c15431a468089ebcf95bb2e4005d2dd60775bef9 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 22 Oct 2013 16:53:37 +0200 Subject: [PATCH 01/12] create languages pages --- .../Thelia/Config/Resources/routing/admin.xml | 6 +++ .../Controller/Admin/LangController.php | 43 +++++++++++++++++ templates/admin/default/lang.html | 46 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 core/lib/Thelia/Controller/Admin/LangController.php create mode 100644 templates/admin/default/lang.html diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 7b377b48e..654478642 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -920,6 +920,12 @@ + + + + Thelia\Controller\Admin\LangController::defaultAction + + diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php new file mode 100644 index 000000000..637cea161 --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -0,0 +1,43 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Admin; +use Thelia\Core\Security\AccessManager; +use Thelia\Core\Security\Resource\AdminResources; + + +/** + * Class LangController + * @package Thelia\Controller\Admin + * @author Manuel Raynaud + */ +class LangController extends BaseAdminController +{ + + public function defaultAction() + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::VIEW)) return $response; + + return $this->render('lang'); + } +} \ No newline at end of file diff --git a/templates/admin/default/lang.html b/templates/admin/default/lang.html new file mode 100644 index 000000000..9711c21f1 --- /dev/null +++ b/templates/admin/default/lang.html @@ -0,0 +1,46 @@ +{extends file="admin-layout.tpl"} + +{block name="page-title"}{intl l='Languages an URLs'}{/block} + +{block name="check-resource"}admin.configuration.languages{/block} +{block name="check-access"}view{/block} + +{block name="main-content"} +
+ +
+ + +
+
+
+ + + + + + + + + + {module_include location='modules_table_header'} + + + + + +
+ {intl l="Languages management"} +
{intl l="Title"}{intl l="code ISO 639"}{intl l="locale"}{intl l="locale"}{intl l="Actions"}
+
+
+
+
+
+ +{/block} \ No newline at end of file From 7915552fe695bea47baec990177084c094d1a125 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 22 Oct 2013 18:59:12 +0200 Subject: [PATCH 02/12] create ajax modal for lang edition --- core/lib/Thelia/Config/Resources/config.xml | 2 + .../Thelia/Config/Resources/routing/admin.xml | 5 + .../Controller/Admin/LangController.php | 29 ++++- core/lib/Thelia/Core/Template/Loop/Lang.php | 3 +- core/lib/Thelia/Form/Lang/LangCreateForm.php | 116 ++++++++++++++++++ core/lib/Thelia/Form/Lang/LangUpdateForm.php | 54 ++++++++ .../default/ajax/language-update-modal.html | 64 ++++++++++ templates/admin/default/lang.html | 46 ------- templates/admin/default/languages.html | 105 ++++++++-------- 9 files changed, 324 insertions(+), 100 deletions(-) create mode 100644 core/lib/Thelia/Form/Lang/LangCreateForm.php create mode 100644 core/lib/Thelia/Form/Lang/LangUpdateForm.php create mode 100644 templates/admin/default/ajax/language-update-modal.html delete mode 100644 templates/admin/default/lang.html diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 09a1e60e8..67c856a05 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -153,6 +153,8 @@
+ + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 654478642..bea1428ed 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -926,6 +926,11 @@ Thelia\Controller\Admin\LangController::defaultAction + + Thelia\Controller\Admin\LangController::updateAction + \d+ + + diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index 637cea161..aec455b4a 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -22,8 +22,11 @@ /*************************************************************************************/ namespace Thelia\Controller\Admin; + use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; +use Thelia\Form\Lang\LangUpdateForm; +use Thelia\Model\LangQuery; /** @@ -38,6 +41,30 @@ class LangController extends BaseAdminController { if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::VIEW)) return $response; - return $this->render('lang'); + return $this->render('languages'); + } + + public function updateAction($lang_id) + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response; + + $this->checkXmlHttpRequest(); + + $lang = LangQuery::create()->findPk($lang_id); + + $langForm = new LangUpdateForm($this->getRequest(), 'form', array( + 'id' => $lang->getId(), + 'title' => $lang->getTitle(), + 'code' => $lang->getCode(), + 'locale' => $lang->getLocale(), + 'date_format' => $lang->getDateFormat(), + 'time_format' => $lang->getTimeFormat() + )); + + $this->getParserContext()->addForm($langForm); + + return $this->render('ajax/language-update-modal', array( + 'lang_id' => $lang_id + )); } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Template/Loop/Lang.php b/core/lib/Thelia/Core/Template/Loop/Lang.php index 487acb565..52a866d30 100755 --- a/core/lib/Thelia/Core/Template/Loop/Lang.php +++ b/core/lib/Thelia/Core/Template/Loop/Lang.php @@ -99,7 +99,8 @@ class Lang extends BaseLoop ->set("LOCALE", $result->getLocale()) ->set("URL", $result->getUrl()) ->set("IS_DEFAULT", $result->getByDefault()) - ->set("URL", $result->getUrl()) + ->set("DATE_FORMAT", $result->getDateFormat()) + ->set("TIME_FORMAT", $result->getTimeFormat()) ->set("POSITION", $result->getPosition()) ; diff --git a/core/lib/Thelia/Form/Lang/LangCreateForm.php b/core/lib/Thelia/Form/Lang/LangCreateForm.php new file mode 100644 index 000000000..deef71802 --- /dev/null +++ b/core/lib/Thelia/Form/Lang/LangCreateForm.php @@ -0,0 +1,116 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Form\Lang; +use Symfony\Component\Validator\Constraints\NotBlank; +use Thelia\Form\BaseForm; +use Thelia\Core\Translation\Translator; + + +/** + * Class LangCreateForm + * @package Thelia\Form\Lang + * @author Manuel Raynaud + */ +class LangCreateForm extends BaseForm +{ + + /** + * + * in this function you add all the fields you need for your Form. + * Form this you have to call add method on $this->formBuilder attribute : + * + * $this->formBuilder->add("name", "text") + * ->add("email", "email", array( + * "attr" => array( + * "class" => "field" + * ), + * "label" => "email", + * "constraints" => array( + * new \Symfony\Component\Validator\Constraints\NotBlank() + * ) + * ) + * ) + * ->add('age', 'integer'); + * + * @return null + */ + protected function buildForm() + { + $this->formBuilder + ->add('title', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('Language name'), + 'label_attr' => array( + 'for' => 'title_lang' + ) + )) + ->add('code', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('ISO 639 Code'), + 'label_attr' => array( + 'for' => 'code_lang' + ) + )) + ->add('locale', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('language locale'), + 'label_attr' => array( + 'for' => 'locale_lang' + ) + )) + ->add('date_format', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('date format'), + 'label_attr' => array( + 'for' => 'date_lang' + ) + )) + ->add('time_format', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('time format'), + 'label_attr' => array( + 'for' => 'time_lang' + ) + )) + ; + } + + /** + * @return string the name of you form. This name must be unique + */ + public function getName() + { + return 'thelia_language_create'; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Form/Lang/LangUpdateForm.php b/core/lib/Thelia/Form/Lang/LangUpdateForm.php new file mode 100644 index 000000000..0463335f7 --- /dev/null +++ b/core/lib/Thelia/Form/Lang/LangUpdateForm.php @@ -0,0 +1,54 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Form\Lang; +use Symfony\Component\Validator\Constraints\GreaterThan; +use Symfony\Component\Validator\Constraints\NotBlank; + + +/** + * Class LangUpdateForm + * @package Thelia\Form\Lang + * @author Manuel Raynaud + */ +class LangUpdateForm extends LangCreateForm +{ + + public function buildForm() + { + parent::buildForm(); + + $this->formBuilder + ->add('id', 'hidden', array( + 'constraints' => array( + new NotBlank(), + new GreaterThan(array('value' => 0)) + ) + )); + } + + public function getName() + { + return 'thelia_lang_update'; + } +} \ No newline at end of file diff --git a/templates/admin/default/ajax/language-update-modal.html b/templates/admin/default/ajax/language-update-modal.html new file mode 100644 index 000000000..909b351b0 --- /dev/null +++ b/templates/admin/default/ajax/language-update-modal.html @@ -0,0 +1,64 @@ +{* Update an Address *} + +{form name="thelia.lang.update"} + +{* Capture the dialog body, to pass it to the generic dialog *} +{capture "edit_lang_dialog"} + + {form_hidden_fields form=$form} + + {form_field form=$form field='title'} +
+ + +
+ {/form_field} + + {form_field form=$form field='code'} +
+ + +
+ {/form_field} + + {form_field form=$form field='locale'} +
+ + +
+ {/form_field} + + {form_field form=$form field='date_format'} +
+ + +
+ {/form_field} + + {form_field form=$form field='time_format'} +
+ + +
+ {/form_field} + + + +{/capture} + + {include + file = "includes/generic-create-dialog.html" + + dialog_id = "edit_lang_dialog" + dialog_title = {intl l="Edit a language"} + dialog_body = {$smarty.capture.edit_lang_dialog nofilter} + +dialog_ok_label = {intl l="Edit this language"} +dialog_cancel_label = {intl l="Cancel"} + +form_action = {url path="/admin/configuration/languages/save/{$lang_id}"} +form_enctype = {form_enctype form=$form} +form_error_message = $form_error_message +} + +{/form} \ No newline at end of file diff --git a/templates/admin/default/lang.html b/templates/admin/default/lang.html deleted file mode 100644 index 9711c21f1..000000000 --- a/templates/admin/default/lang.html +++ /dev/null @@ -1,46 +0,0 @@ -{extends file="admin-layout.tpl"} - -{block name="page-title"}{intl l='Languages an URLs'}{/block} - -{block name="check-resource"}admin.configuration.languages{/block} -{block name="check-access"}view{/block} - -{block name="main-content"} -
- -
- - -
-
-
- - - - - - - - - - {module_include location='modules_table_header'} - - - - - -
- {intl l="Languages management"} -
{intl l="Title"}{intl l="code ISO 639"}{intl l="locale"}{intl l="locale"}{intl l="Actions"}
-
-
-
-
-
- -{/block} \ No newline at end of file diff --git a/templates/admin/default/languages.html b/templates/admin/default/languages.html index 868d911be..76249b74a 100644 --- a/templates/admin/default/languages.html +++ b/templates/admin/default/languages.html @@ -19,7 +19,7 @@ {module_include location='languages_top'}
-
+
@@ -38,61 +38,43 @@ {intl l="Language name"} {intl l="ISO 639 Code"} + {intl l="Locale"} + {intl l="date form"} + {intl l="time form"} {intl l="Default"} {intl l="Actions"} + {loop type="lang" name="lang.list" backend_context="1"} - - + {$TITLE} + {$CODE} + {$LOCALE} + {$DATE_FORMAT} + {$TIME_FORMAT}
- +
- +
- -
- - - - - - -
- -
- - -
- -
- - - - - - -
- -
- - -
- + {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.language" access="UPDATE"} + + + + {/loop} + {loop type="auth" name="can_change" role="ADMIN" resource="admin.configuration.language" access="DELETE"} + + + + {/loop}
+ {/loop} - - - - - - - @@ -100,13 +82,18 @@
+ + +
+ +
- +
{intl l="Parameters"}
- +
@@ -124,24 +111,19 @@
- -
- -
-
+
{intl l="Association language/URL"}
-
+
-
+ -
@@ -169,7 +151,6 @@ -
@@ -249,6 +230,7 @@ form_content = {$smarty.capture.delete_dialog nofilter} } +
{/block} {block name="javascript-initialization"} @@ -270,4 +252,23 @@ {javascripts file='assets/js/bootstrap-select/bootstrap-select.js'} {/javascripts} + + {/block} \ No newline at end of file From a2d292f6a398597f14ce2c2783782ee43a9cd22a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 22 Oct 2013 19:17:27 +0200 Subject: [PATCH 03/12] create lang object events --- .../Thelia/Config/Resources/routing/admin.xml | 5 + .../Controller/Admin/LangController.php | 15 ++ .../Core/Event/Lang/LangCreateEvent.php | 141 ++++++++++++++++++ core/lib/Thelia/Core/Event/Lang/LangEvent.php | 76 ++++++++++ .../Core/Event/Lang/LangUpdateEvent.php | 35 +++++ 5 files changed, 272 insertions(+) create mode 100644 core/lib/Thelia/Core/Event/Lang/LangCreateEvent.php create mode 100644 core/lib/Thelia/Core/Event/Lang/LangEvent.php create mode 100644 core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index bea1428ed..aec9e1588 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -931,6 +931,11 @@ \d+ + + Thelia\Controller\Admin\LangController::processUpdateAction + \d+ + + diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index aec455b4a..dd6deeff9 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -67,4 +67,19 @@ class LangController extends BaseAdminController 'lang_id' => $lang_id )); } + + public function processUpdateAction($lang_id) + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response; + + $error_msg = false; + + $langForm = new LangUpdateForm($this->getRequest()); + + try { + $form = $this->validateForm($langForm); + } catch(\Exception $e) { + + } + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangCreateEvent.php b/core/lib/Thelia/Core/Event/Lang/LangCreateEvent.php new file mode 100644 index 000000000..43279b093 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangCreateEvent.php @@ -0,0 +1,141 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; + + +/** + * Class LangCreateEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangCreateEvent extends LangEvent +{ + protected $title; + protected $code; + protected $locale; + protected $date_format; + protected $time_format; + + /** + * @param mixed $code + * + * @return $this + */ + public function setCode($code) + { + $this->code = $code; + + return $this; + } + + /** + * @return mixed + */ + public function getCode() + { + return $this->code; + } + + /** + * @param mixed $date_format + * + * @return $this + */ + public function setDateFormat($date_format) + { + $this->date_format = $date_format; + + return $this; + } + + /** + * @return mixed + */ + public function getDateFormat() + { + return $this->date_format; + } + + /** + * @param mixed $locale + * + * @return $this + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + + /** + * @return mixed + */ + public function getLocale() + { + return $this->locale; + } + + /** + * @param mixed $time_format + * + * @return $this + */ + public function setTimeFormat($time_format) + { + $this->time_format = $time_format; + + return $this; + } + + /** + * @return mixed + */ + public function getTimeFormat() + { + return $this->time_format; + } + + /** + * @param mixed $title + * + * @return $this + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + /** + * @return mixed + */ + public function getTitle() + { + return $this->title; + } + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangEvent.php b/core/lib/Thelia/Core/Event/Lang/LangEvent.php new file mode 100644 index 000000000..f66b6428a --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangEvent.php @@ -0,0 +1,76 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; +use Thelia\Core\Event\ActionEvent; +use Thelia\Model\Lang; + + +/** + * Class LangEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangEvent extends ActionEvent +{ + /** + * @var \Thelia\Model\Lang + */ + protected $lang; + + function __construct(Lang $lang = null) + { + $this->lang = $lang; + } + + /** + * @param \Thelia\Model\Lang $lang + */ + public function setLang(Lang $lang) + { + $this->lang = $lang; + } + + /** + * @return \Thelia\Model\Lang + */ + public function getLang() + { + return $this->lang; + } + + /** + * + * check if lang object is present + * + * @return bool + */ + public function hasLang() + { + return null !== $this->lang; + } + + + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php new file mode 100644 index 000000000..a520dd4c5 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php @@ -0,0 +1,35 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; + + +/** + * Class LangUpdateEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangUpdateEvent extends LangCreateEvent +{ + +} \ No newline at end of file From 38761a727532de8640c8992088e6bc732bf8c076 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 22 Oct 2013 19:56:18 +0200 Subject: [PATCH 04/12] process lang udate action --- core/lib/Thelia/Action/Lang.php | 81 +++++++++++++++++++ core/lib/Thelia/Config/Resources/action.xml | 5 ++ .../Thelia/Config/Resources/routing/admin.xml | 2 +- .../Controller/Admin/LangController.php | 26 +++++- .../Core/Event/Lang/LangUpdateEvent.php | 34 ++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 11 +++ core/lib/Thelia/Model/Lang.php | 41 ++++++++++ 7 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 core/lib/Thelia/Action/Lang.php diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php new file mode 100644 index 000000000..88a544b5f --- /dev/null +++ b/core/lib/Thelia/Action/Lang.php @@ -0,0 +1,81 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangUpdateEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\LangQuery; + + +/** + * Class Lang + * @package Thelia\Action + * @author Manuel Raynaud + */ +class Lang extends BaseAction implements EventSubscriberInterface +{ + + public function update(LangUpdateEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getId())) { + $lang->setDispatcher($this->getDispatcher()); + + $lang->setTitle($event->getTitle()) + ->setLocale($event->getLocale()) + ->setCode($event->getCode()) + ->setDateFormat($event->getDateFormat()) + ->setTimeFormat($event->getTimeFormat()) + ->save(); + + $event->setLang($lang); + } + } + + /** + * Returns an array of event names this subscriber wants to listen to. + * + * The array keys are event names and the value can be: + * + * * The method name to call (priority defaults to 0) + * * An array composed of the method name to call and the priority + * * An array of arrays composed of the method names to call and respective + * priorities, or 0 if unset + * + * For instance: + * + * * array('eventName' => 'methodName') + * * array('eventName' => array('methodName', $priority)) + * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) + * + * @return array The event names to listen to + * + * @api + */ + public static function getSubscribedEvents() + { + return array( + TheliaEvents::LANG_UPDATE => array('update', 128) + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index e5d6c9492..ab8be7d00 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -160,6 +160,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index aec9e1588..607724d5d 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -931,7 +931,7 @@ \d+ - + Thelia\Controller\Admin\LangController::processUpdateAction \d+ diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index dd6deeff9..908bdeadf 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -23,6 +23,8 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\Lang\LangUpdateEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; use Thelia\Form\Lang\LangUpdateForm; @@ -78,8 +80,30 @@ class LangController extends BaseAdminController try { $form = $this->validateForm($langForm); - } catch(\Exception $e) { + $event = new LangUpdateEvent($form->get('id')->getData()); + $event + ->setTitle($form->get('title')->getData()) + ->setCode($form->get('code')->getData()) + ->setLocale($form->get('locale')->getData()) + ->setDateFormat($form->get('date_format')->getData()) + ->setTimeFormat($form->get('time_format')->getData()) + ; + + $this->dispatch(TheliaEvents::LANG_UPDATE, $event); + + if (false === $event->hasLang()) { + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang'))); + } + + $changedObject = $event->getLang(); + $this->adminLogAppend(sprintf("%s %s (ID %s) modified", 'Lang', $changedObject->getTitle(), $changedObject->getId())); + $this->redirectToRoute('/admin/configuration/languages'); + } catch(\Exception $e) { + $error_msg = $e->getMessage(); } + + return $this->render('languages'); } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php index a520dd4c5..f0d4b928d 100644 --- a/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php +++ b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php @@ -31,5 +31,39 @@ namespace Thelia\Core\Event\Lang; */ class LangUpdateEvent extends LangCreateEvent { + /** + * @var int lang id + */ + protected $id; + + /** + * @param int $id + */ + function __construct($id) + { + $this->id = $id; + } + + + /** + * @param int $id + * + * @return $this + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index bc16115f9..7e02adaff 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -688,4 +688,15 @@ final class TheliaEvents * sent for subscribing to the newsletter */ const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe'; + + const LANG_UPDATE = 'action.lang.update'; + + const BEFORE_UPDATELANG = 'action.lang.beforeUpdate'; + const AFTER_UPDATELANG = 'action.lang.afterUpdate'; + + const BEFORE_CREATELANG = 'action.lang.beforeCreate'; + const AFTER_CREATELANG = 'action.lang.afterCreate'; + + const BEFORE_DELETELANG = 'action.lang.beforeDelete'; + const AFTER_DELETELANG = 'action.lang.afterDelete'; } diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index 3c45ade33..054a4e807 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -2,10 +2,14 @@ namespace Thelia\Model; +use Propel\Runtime\Connection\ConnectionInterface; +use Thelia\Core\Event\Lang\LangEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Lang as BaseLang; class Lang extends BaseLang { + use \Thelia\Model\Tools\ModelEventDispatcherTrait; /** * Return the default language object, using a local variable to cache it. * @@ -20,4 +24,41 @@ class Lang extends BaseLang { return $default_lang; } + + public function preInsert(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this)); + + return true; + } + + public function postInsert(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_CREATELANG, new LangEvent($this)); + } + + public function preUpdate(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_UPDATELANG, new LangEvent($this)); + + return true; + } + + public function postUpdate(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_UPDATELANG, new LangEvent($this)); + } + + public function preDelete(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::BEFORE_DELETELANG, new LangEvent($this)); + + return true; + } + + public function postDelete(ConnectionInterface $con = null) + { + $this->dispatchEvent(TheliaEvents::AFTER_DELETELANG, new LangEvent($this)); + } + } From d356974ef739955f70a76542019300da121b094c Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 08:45:02 +0200 Subject: [PATCH 05/12] create lang toggleDefault object --- core/lib/Thelia/Action/Lang.php | 3 +- .../Thelia/Config/Resources/routing/admin.xml | 5 ++ .../Controller/Admin/LangController.php | 5 ++ .../Event/Lang/LangToggleDefaultEvent.php | 69 +++++++++++++++++++ core/lib/Thelia/Core/Event/TheliaEvents.php | 4 ++ templates/admin/default/languages.html | 32 ++++++++- 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 core/lib/Thelia/Core/Event/Lang/LangToggleDefaultEvent.php diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php index 88a544b5f..cbd4d074b 100644 --- a/core/lib/Thelia/Action/Lang.php +++ b/core/lib/Thelia/Action/Lang.php @@ -75,7 +75,8 @@ class Lang extends BaseAction implements EventSubscriberInterface public static function getSubscribedEvents() { return array( - TheliaEvents::LANG_UPDATE => array('update', 128) + TheliaEvents::LANG_UPDATE => array('update', 128), + TheliaEvents::LANG_TOGGLEDEFAULT => array('toggleDefault', 128) ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 607724d5d..744d24ca8 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -936,6 +936,11 @@ \d+ + + Thelia\Controller\Admin\LangController::toggleDefaultAction + \d+ + + diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index 908bdeadf..7c6a08681 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -106,4 +106,9 @@ class LangController extends BaseAdminController return $this->render('languages'); } + + public function toggleDefaultAction($lang_id) + { + + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangToggleDefaultEvent.php b/core/lib/Thelia/Core/Event/Lang/LangToggleDefaultEvent.php new file mode 100644 index 000000000..f31972f01 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangToggleDefaultEvent.php @@ -0,0 +1,69 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; + + +/** + * Class LangToggleDefaultEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangToggleDefaultEvent extends LangEvent +{ + /** + * @var int + */ + protected $lang_id; + + /** + * @param int $lang_id + */ + function __construct($lang_id) + { + $this->lang_id = $lang_id; + } + + /** + * @param int $lang_id + * + * @return $this + */ + public function setLangId($lang_id) + { + $this->lang_id = $lang_id; + + return $this; + } + + /** + * @return int + */ + public function getLangId() + { + return $this->lang_id; + } + + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 7e02adaff..9aede0ae2 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -689,8 +689,12 @@ final class TheliaEvents */ const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe'; + /************ LANG MANAGEMENT ****************************/ + const LANG_UPDATE = 'action.lang.update'; + const LANG_TOGGLEDEFAULT = 'action.lang.toggleDefault'; + const BEFORE_UPDATELANG = 'action.lang.beforeUpdate'; const AFTER_UPDATELANG = 'action.lang.afterUpdate'; diff --git a/templates/admin/default/languages.html b/templates/admin/default/languages.html index 76249b74a..f18bfdcc3 100644 --- a/templates/admin/default/languages.html +++ b/templates/admin/default/languages.html @@ -54,8 +54,8 @@ {$DATE_FORMAT} {$TIME_FORMAT} -
- +
+
@@ -231,6 +231,23 @@ }
+ + {/block} {block name="javascript-initialization"} @@ -269,6 +286,17 @@ $("#loading-event").remove(); }); }); + + $(".lang-default-change").on("switch-change", function(data, value){ + var baseUrl = "{url path='/admin/configuration/languages/toggleDefault'}"; + if(data.value) { + $.ajax({ + url : baseUrl+$(this).data('id') + }).fail(function(){ + $('#toggle-default-failed').modal('show'); + }); + } + }); }); {/block} \ No newline at end of file From eac2c274e78ab356a8b33e2efde3e5e8a691f4b9 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 09:12:05 +0200 Subject: [PATCH 06/12] change http code status on different context --- .../Controller/Admin/BaseAdminController.php | 20 +++++++------- .../Controller/Admin/CountryController.php | 2 +- .../Controller/Admin/LangController.php | 27 +++++++++++++++++++ core/lib/Thelia/Controller/BaseController.php | 4 ++- templates/admin/default/languages.html | 7 ++--- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index fdbb6c1c4..b7d80383f 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -97,14 +97,17 @@ class BaseAdminController extends BaseController * * @return \Symfony\Component\HttpFoundation\Response */ - protected function errorPage($message) + protected function errorPage($message, $status = 500) { if ($message instanceof \Exception) { $message = $this->getTranslator()->trans("Sorry, an error occured: %msg", array('%msg' => $message->getMessage())); } - return $this->render('general_error', array( - "error_message" => $message) + return $this->render('general_error', + array( + "error_message" => $message + ), + $status ); } @@ -366,14 +369,13 @@ class BaseAdminController extends BaseController * Render the given template, and returns the result as an Http Response. * * @param $templateName the complete template name, with extension - * @param array $args the template arguments + * @param array $args the template arguments + * @param int $status http code status * @return \Symfony\Component\HttpFoundation\Response */ - protected function render($templateName, $args = array()) + protected function render($templateName, $args = array(), $status = 200) { - $response = new Response(); - - return $response->setContent($this->renderRaw($templateName, $args)); + return Response::create($this->renderRaw($templateName, $args), $status); } /** @@ -430,7 +432,7 @@ class BaseAdminController extends BaseController Redirect::exec(URL::getInstance()->absoluteUrl($ex->getLoginTemplate())); } catch (AuthorizationException $ex) { // User is not allowed to perform the required action. Return the error page instead of the requested page. - return $this->errorPage($this->getTranslator()->trans("Sorry, you are not allowed to perform this action.")); + return $this->errorPage($this->getTranslator()->trans("Sorry, you are not allowed to perform this action."), 403); } } } diff --git a/core/lib/Thelia/Controller/Admin/CountryController.php b/core/lib/Thelia/Controller/Admin/CountryController.php index e5bcce67b..0ef2f1006 100644 --- a/core/lib/Thelia/Controller/Admin/CountryController.php +++ b/core/lib/Thelia/Controller/Admin/CountryController.php @@ -251,6 +251,6 @@ class CountryController extends AbstractCrudController } - return $this->nullResponse($content, 500); + return $this->nullResponse(500); } } diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index 7c6a08681..5dc950d6f 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -23,6 +23,7 @@ namespace Thelia\Controller\Admin; +use Thelia\Core\Event\Lang\LangToggleDefaultEvent; use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; @@ -109,6 +110,32 @@ class LangController extends BaseAdminController public function toggleDefaultAction($lang_id) { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response; + $this->checkXmlHttpRequest(); + $error = false; + try { + $event = new LangToggleDefaultEvent($lang_id); + + $this->dispatch(TheliaEvents::LANG_TOGGLEDEFAULT, $event); + + if (false === $event->hasLang()) { + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang'))); + } + + $changedObject = $event->getLang(); + $this->adminLogAppend(sprintf("%s %s (ID %s) modified", 'Lang', $changedObject->getTitle(), $changedObject->getId())); + + } catch (\Exception $e) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("Error on changing default languages with message : %s", $e->getMessage())); + $error = $e->getMessage(); + } + + if($error) { + return $this->nullResponse(500); + } else { + return $this->nullResponse(); + } } } \ No newline at end of file diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 548ef9681..959b7ff0e 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -57,8 +57,10 @@ class BaseController extends ContainerAware /** * Return an empty response (after an ajax request, for example) + * @param int $status + * @return \Symfony\Component\HttpFoundation\Response */ - protected function nullResponse($content = null, $status = 200) + protected function nullResponse($status = 200) { return new Response(null, $status); } diff --git a/templates/admin/default/languages.html b/templates/admin/default/languages.html index f18bfdcc3..957a587c1 100644 --- a/templates/admin/default/languages.html +++ b/templates/admin/default/languages.html @@ -54,7 +54,7 @@ {$DATE_FORMAT} {$TIME_FORMAT} -
+
@@ -287,8 +287,9 @@ }); }); - $(".lang-default-change").on("switch-change", function(data, value){ - var baseUrl = "{url path='/admin/configuration/languages/toggleDefault'}"; + $(".lang-default-change").on("switch-change", function(e, data){ + $('.lang-default-change').bootstrapSwitch('toggleRadioState'); + var baseUrl = "{url path='/admin/configuration/languages/toggleDefault/'}"; if(data.value) { $.ajax({ url : baseUrl+$(this).data('id') From f881edbaa6cc5015029fd79c38e0be3da9d1d64a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 09:19:53 +0200 Subject: [PATCH 07/12] create toggleDefault method in lang method and modify in Country model --- core/lib/Thelia/Action/Lang.php | 12 ++++++++++++ core/lib/Thelia/Model/Country.php | 27 +++++++++++++++++++++------ core/lib/Thelia/Model/Lang.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php index cbd4d074b..bf3df3f62 100644 --- a/core/lib/Thelia/Action/Lang.php +++ b/core/lib/Thelia/Action/Lang.php @@ -23,6 +23,7 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangToggleDefaultEvent; use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\LangQuery; @@ -52,6 +53,17 @@ class Lang extends BaseAction implements EventSubscriberInterface } } + public function toggleDefault(LangToggleDefaultEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) { + $lang->setDispatcher($this->getDispatcher()); + + $lang->toggleDefault(); + + $event->setLang($lang); + } + } + /** * Returns an array of event names this subscriber wants to listen to. * diff --git a/core/lib/Thelia/Model/Country.php b/core/lib/Thelia/Model/Country.php index e64bd4609..d30f2d281 100755 --- a/core/lib/Thelia/Model/Country.php +++ b/core/lib/Thelia/Model/Country.php @@ -3,9 +3,12 @@ namespace Thelia\Model; use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Exception\PropelException; +use Propel\Runtime\Propel; use Thelia\Core\Event\Country\CountryEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Country as BaseCountry; +use Thelia\Model\Map\CountryTableMap; class Country extends BaseCountry { @@ -16,13 +19,25 @@ class Country extends BaseCountry if($this->getId() === null) { throw new \RuntimeException("impossible to just uncheck default country, choose a new one"); } - CountryQuery::create() - ->filterByByDefault(1) - ->update(array('ByDefault' => 0)); - $this - ->setByDefault(1) - ->save(); + $con = Propel::getWriteConnection(CountryTableMap::DATABASE_NAME); + $con->beginTransaction(); + + try { + CountryQuery::create() + ->filterByByDefault(1) + ->update(array('ByDefault' => 0), $con); + + $this + ->setByDefault(1) + ->save($con); + + $con->commit(); + } catch(PropelException $e) { + $con->rollBack(); + throw $e; + } + } public function preInsert(ConnectionInterface $con = null) diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index 054a4e807..189b640ab 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -3,9 +3,13 @@ namespace Thelia\Model; use Propel\Runtime\Connection\ConnectionInterface; +use Propel\Runtime\Exception\PropelException; +use Propel\Runtime\Propel; use Thelia\Core\Event\Lang\LangEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\Lang as BaseLang; +use Thelia\Model\Base\LangQuery; +use Thelia\Model\Map\LangTableMap; class Lang extends BaseLang { @@ -25,6 +29,30 @@ class Lang extends BaseLang { return $default_lang; } + public function toggleDefault() + { + if($this->getId() === null) { + throw new \RuntimeException("impossible to just uncheck default language, choose a new one"); + } + $con = Propel::getWriteConnection(LangTableMap::DATABASE_NAME); + $con->beginTransaction(); + try { + LangQuery::create() + ->filterByByDefault(1) + ->update(array('ByDefault' => 0), $con); + + $this + ->setByDefault(1) + ->save($con); + + $con->commit(); + } catch(PropelException $e) { + $con->rollBack(); + throw $e; + } + + } + public function preInsert(ConnectionInterface $con = null) { $this->dispatchEvent(TheliaEvents::BEFORE_CREATELANG, new LangEvent($this)); From 24b504ad3be8440d7e8d10e70789ddb5230d6ce2 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 12:00:33 +0200 Subject: [PATCH 08/12] finish language creation process --- core/lib/Thelia/Action/Lang.php | 20 +++++- core/lib/Thelia/Config/Resources/config.xml | 3 +- .../Thelia/Config/Resources/routing/admin.xml | 4 ++ .../Controller/Admin/BaseAdminController.php | 2 +- .../Controller/Admin/LangController.php | 65 +++++++++++++++++-- core/lib/Thelia/Core/Event/TheliaEvents.php | 1 + core/lib/Thelia/Form/LanguageCreationForm.php | 58 ----------------- templates/admin/default/admin-layout.tpl | 11 ++++ .../includes/generic-create-dialog.html | 2 +- templates/admin/default/languages.html | 33 ++++++---- 10 files changed, 117 insertions(+), 82 deletions(-) delete mode 100644 core/lib/Thelia/Form/LanguageCreationForm.php diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php index bf3df3f62..a539cd486 100644 --- a/core/lib/Thelia/Action/Lang.php +++ b/core/lib/Thelia/Action/Lang.php @@ -23,10 +23,12 @@ namespace Thelia\Action; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangCreateEvent; use Thelia\Core\Event\Lang\LangToggleDefaultEvent; use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Model\LangQuery; +use Thelia\Model\Lang as LangModel; /** @@ -64,6 +66,21 @@ class Lang extends BaseAction implements EventSubscriberInterface } } + public function create(LangCreateEvent $event) + { + $lang = new LangModel(); + + $lang + ->setTitle($event->getTitle()) + ->setCode($event->getCode()) + ->setLocale($event->getLocale()) + ->setDateFormat($event->getDateFormat()) + ->setTimeFormat($event->getTimeFormat()) + ->save(); + + $event->setLang($lang); + } + /** * Returns an array of event names this subscriber wants to listen to. * @@ -88,7 +105,8 @@ class Lang extends BaseAction implements EventSubscriberInterface { return array( TheliaEvents::LANG_UPDATE => array('update', 128), - TheliaEvents::LANG_TOGGLEDEFAULT => array('toggleDefault', 128) + TheliaEvents::LANG_TOGGLEDEFAULT => array('toggleDefault', 128), + TheliaEvents::LANG_CREATE => array('create', 128) ); } } \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 67c856a05..875215015 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -138,8 +138,6 @@
- - @@ -155,6 +153,7 @@ + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 744d24ca8..d61e2ce72 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -941,6 +941,10 @@ \d+ + + Thelia\Controller\Admin\LangController::addAction + + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index b7d80383f..be6deca15 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -136,7 +136,7 @@ class BaseAdminController extends BaseController // Generate the proper response $response = new Response(); - return $this->errorPage($this->getTranslator()->trans("Sorry, you're not allowed to perform this action")); + return $this->errorPage($this->getTranslator()->trans("Sorry, you're not allowed to perform this action"), 403); } /* diff --git a/core/lib/Thelia/Controller/Admin/LangController.php b/core/lib/Thelia/Controller/Admin/LangController.php index 5dc950d6f..71e95988f 100644 --- a/core/lib/Thelia/Controller/Admin/LangController.php +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -23,11 +23,15 @@ namespace Thelia\Controller\Admin; +use Symfony\Component\Form\Form; +use Thelia\Core\Event\Lang\LangCreateEvent; use Thelia\Core\Event\Lang\LangToggleDefaultEvent; use Thelia\Core\Event\Lang\LangUpdateEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\Resource\AdminResources; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Form\Lang\LangCreateForm; use Thelia\Form\Lang\LangUpdateForm; use Thelia\Model\LangQuery; @@ -83,13 +87,7 @@ class LangController extends BaseAdminController $form = $this->validateForm($langForm); $event = new LangUpdateEvent($form->get('id')->getData()); - $event - ->setTitle($form->get('title')->getData()) - ->setCode($form->get('code')->getData()) - ->setLocale($form->get('locale')->getData()) - ->setDateFormat($form->get('date_format')->getData()) - ->setTimeFormat($form->get('time_format')->getData()) - ; + $event = $this->hydrateEvent($event, $form); $this->dispatch(TheliaEvents::LANG_UPDATE, $event); @@ -108,6 +106,17 @@ class LangController extends BaseAdminController return $this->render('languages'); } + protected function hydrateEvent($event,Form $form) + { + return $event + ->setTitle($form->get('title')->getData()) + ->setCode($form->get('code')->getData()) + ->setLocale($form->get('locale')->getData()) + ->setDateFormat($form->get('date_format')->getData()) + ->setTimeFormat($form->get('time_format')->getData()) + ; + } + public function toggleDefaultAction($lang_id) { if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response; @@ -138,4 +147,46 @@ class LangController extends BaseAdminController return $this->nullResponse(); } } + + public function addAction() + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::CREATE)) return $response; + + $createForm = new LangCreateForm($this->getRequest()); + + $error_msg = false; + + try { + $form = $this->validateForm($createForm); + + $createEvent = new LangCreateEvent(); + $createEvent = $this->hydrateEvent($createEvent, $form); + + $this->dispatch(TheliaEvents::LANG_CREATE, $createEvent); + + if (false === $createEvent->hasLang()) { + throw new \LogicException( + $this->getTranslator()->trans("No %obj was updated.", array('%obj', 'Lang'))); + } + + $createdObject = $createEvent->getLang(); + $this->adminLogAppend(sprintf("%s %s (ID %s) created", 'Lang', $createdObject->getTitle(), $createdObject->getId())); + + $this->redirectToRoute('admin.configuration.languages'); + + } catch (FormValidationException $ex) { + // Form cannot be validated + $error_msg = $this->createStandardFormValidationErrorMessage($ex); + } catch (\Exception $ex) { + // Any other error + $error_msg = $ex->getMessage(); + } + + $this->setupFormErrorContext( + $this->getTranslator()->trans("%obj creation", array('%obj' => 'Lang')), $error_msg, $createForm, $ex); + + // At this point, the form has error, and should be redisplayed. + return $this->render('languages'); + + } } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 9aede0ae2..5da2dc88c 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -692,6 +692,7 @@ final class TheliaEvents /************ LANG MANAGEMENT ****************************/ const LANG_UPDATE = 'action.lang.update'; + const LANG_CREATE = 'action.lang.create'; const LANG_TOGGLEDEFAULT = 'action.lang.toggleDefault'; diff --git a/core/lib/Thelia/Form/LanguageCreationForm.php b/core/lib/Thelia/Form/LanguageCreationForm.php deleted file mode 100644 index 713cafcd9..000000000 --- a/core/lib/Thelia/Form/LanguageCreationForm.php +++ /dev/null @@ -1,58 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ -namespace Thelia\Form; - -use Symfony\Component\Validator\Constraints\NotBlank; -use Thelia\Core\Translation\Translator; - -class LanguageCreationForm extends BaseForm -{ - protected function buildForm() - { - $this->formBuilder - ->add("title", "text", array( - "constraints" => array( - new NotBlank() - ), - "label" => Translator::getInstance()->trans("Language title *"), - "label_attr" => array( - "for" => "title" - ) - )) - ->add("isocode", "text", array( - "constraints" => array( - new NotBlank() - ), - "label" => Translator::getInstance()->trans("ISO Code *"), - "label_attr" => array( - "for" => "isocode" - ) - )) - ; - } - - public function getName() - { - return "thelia_language_creation"; - } -} diff --git a/templates/admin/default/admin-layout.tpl b/templates/admin/default/admin-layout.tpl index 3d671ecb2..dc667e6fc 100644 --- a/templates/admin/default/admin-layout.tpl +++ b/templates/admin/default/admin-layout.tpl @@ -250,6 +250,17 @@ {block name="javascript-initialization"}{/block} + + {* Modules scripts are included now *} {module_include location='footer_js'} diff --git a/templates/admin/default/includes/generic-create-dialog.html b/templates/admin/default/includes/generic-create-dialog.html index fe3488535..f9a79a98b 100755 --- a/templates/admin/default/includes/generic-create-dialog.html +++ b/templates/admin/default/includes/generic-create-dialog.html @@ -15,7 +15,7 @@ A generic modal creation dialog template. Parameters ok_button_id (optionnal) = the id of the OK button *} -