diff --git a/core/lib/Thelia/Action/Lang.php b/core/lib/Thelia/Action/Lang.php new file mode 100644 index 000000000..da5a07197 --- /dev/null +++ b/core/lib/Thelia/Action/Lang.php @@ -0,0 +1,135 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\Lang\LangCreateEvent; +use Thelia\Core\Event\Lang\LangDefaultBehaviorEvent; +use Thelia\Core\Event\Lang\LangDeleteEvent; +use Thelia\Core\Event\Lang\LangToggleDefaultEvent; +use Thelia\Core\Event\Lang\LangUpdateEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\ConfigQuery; +use Thelia\Model\LangQuery; +use Thelia\Model\Lang as LangModel; + + +/** + * 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); + } + } + + public function toggleDefault(LangToggleDefaultEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) { + $lang->setDispatcher($this->getDispatcher()); + + $lang->toggleDefault(); + + $event->setLang($lang); + } + } + + public function create(LangCreateEvent $event) + { + $lang = new LangModel(); + + $lang + ->setDispatcher($this->getDispatcher()) + ->setTitle($event->getTitle()) + ->setCode($event->getCode()) + ->setLocale($event->getLocale()) + ->setDateFormat($event->getDateFormat()) + ->setTimeFormat($event->getTimeFormat()) + ->save(); + + $event->setLang($lang); + } + + public function delete(LangDeleteEvent $event) + { + if (null !== $lang = LangQuery::create()->findPk($event->getLangId())) { + $lang->setDispatcher($this->getDispatcher()) + ->delete(); + + $event->setLang($lang); + } + } + + public function defaultBehavior(LangDefaultBehaviorEvent $event) + { + ConfigQuery::create() + ->filterByName('default_lang_without_translation') + ->update(array('Value' => $event->getDefaultBehavior())); + } + + /** + * 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), + TheliaEvents::LANG_TOGGLEDEFAULT => array('toggleDefault', 128), + TheliaEvents::LANG_CREATE => array('create', 128), + TheliaEvents::LANG_DELETE => array('delete', 128), + TheliaEvents::LANG_DEFAULTBEHAVIOR => array('defaultBehavior', 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 15737d79b..0aa44a7cc 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -165,6 +165,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index c744b4820..69874762a 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -141,8 +141,6 @@
- - @@ -154,6 +152,10 @@ + + + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index cb6db3f9b..d6c46af78 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -933,6 +933,39 @@ + + + + Thelia\Controller\Admin\LangController::defaultAction + + + + Thelia\Controller\Admin\LangController::updateAction + \d+ + + + + Thelia\Controller\Admin\LangController::processUpdateAction + \d+ + + + + Thelia\Controller\Admin\LangController::toggleDefaultAction + \d+ + + + + Thelia\Controller\Admin\LangController::addAction + + + + Thelia\Controller\Admin\LangController::deleteAction + + + + Thelia\Controller\Admin\LangController::defaultBehaviorAction + + diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index fdbb6c1c4..be6deca15 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 ); } @@ -133,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); } /* @@ -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 new file mode 100644 index 000000000..0b882579a --- /dev/null +++ b/core/lib/Thelia/Controller/Admin/LangController.php @@ -0,0 +1,261 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Controller\Admin; + +use Symfony\Component\Form\Form; +use Thelia\Core\Event\Lang\LangCreateEvent; +use Thelia\Core\Event\Lang\LangDefaultBehaviorEvent; +use Thelia\Core\Event\Lang\LangDeleteEvent; +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\LangDefaultBehaviorForm; +use Thelia\Form\Lang\LangUpdateForm; +use Thelia\Model\ConfigQuery; +use Thelia\Model\LangQuery; + + +/** + * 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->renderDefault(); + } + + public function renderDefault(array $param = array()) + { + return $this->render('languages', array_merge($param, array( + 'lang_without_translation' => ConfigQuery::getDefaultLangWhenNoTranslationAvailable(), + 'one_domain_per_lang' => ConfigQuery::read("one_domain_foreach_lang", false) + ))); + } + + 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 + )); + } + + 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); + + $event = new LangUpdateEvent($form->get('id')->getData()); + $event = $this->hydrateEvent($event, $form); + + $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->renderDefault(); + } + + 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; + + $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(); + } + } + + 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->renderDefault(); + + } + + public function deleteAction() + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::DELETE)) return $response; + + $error_msg = false; + + try { + + $deleteEvent = new LangDeleteEvent($this->getRequest()->get('language_id', 0)); + + $this->dispatch(TheliaEvents::LANG_DELETE, $deleteEvent); + + $this->redirectToRoute('admin.configuration.languages'); + } catch (\Exception $ex) { + \Thelia\Log\Tlog::getInstance()->error(sprintf("error during language removal with message : %s", $ex->getMessage())); + $error_msg = $ex->getMessage(); + } + + return $this->renderDefault(array( + 'error_delete_message' => $error_msg + )); + + } + + public function defaultBehaviorAction() + { + if (null !== $response = $this->checkAuth(AdminResources::LANGUAGE, AccessManager::UPDATE)) return $response; + + $error_msg = false; + $exception = false; + + $behaviorForm = new LangDefaultBehaviorForm($this->getRequest()); + + try { + $form = $this->validateForm($behaviorForm); + + $event = new LangDefaultBehaviorEvent($form->get('behavior')->getData()); + + $this->dispatch(TheliaEvents::LANG_DEFAULTBEHAVIOR, $event); + + $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, $behaviorForm, $ex); + + // At this point, the form has error, and should be redisplayed. + return $this->renderDefault(); + } +} \ 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/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/LangDefaultBehaviorEvent.php b/core/lib/Thelia/Core/Event/Lang/LangDefaultBehaviorEvent.php new file mode 100644 index 000000000..4c3e857e3 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangDefaultBehaviorEvent.php @@ -0,0 +1,64 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; +use Thelia\Core\Event\ActionEvent; + + +/** + * Class LangDefaultBehaviorEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangDefaultBehaviorEvent extends ActionEvent +{ + /** + * @var int default behavior status + */ + protected $defaultBehavior; + + function __construct($defaultBehavior) + { + $this->defaultBehavior = $defaultBehavior; + } + + /** + * @param int $defaultBehavior + */ + public function setDefaultBehavior($defaultBehavior) + { + $this->defaultBehavior = $defaultBehavior; + } + + /** + * @return int + */ + public function getDefaultBehavior() + { + return $this->defaultBehavior; + } + + + + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/Lang/LangDeleteEvent.php b/core/lib/Thelia/Core/Event/Lang/LangDeleteEvent.php new file mode 100644 index 000000000..eebbe27e4 --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangDeleteEvent.php @@ -0,0 +1,67 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; + + +/** + * Class LangDeleteEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +class LangDeleteEvent 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/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/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/Lang/LangUpdateEvent.php b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php new file mode 100644 index 000000000..f0d4b928d --- /dev/null +++ b/core/lib/Thelia/Core/Event/Lang/LangUpdateEvent.php @@ -0,0 +1,69 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\Lang; + + +/** + * Class LangUpdateEvent + * @package Thelia\Core\Event\Lang + * @author Manuel Raynaud + */ +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 db5ea6aa6..22e5a1841 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -694,4 +694,23 @@ final class TheliaEvents * sent for subscribing to the newsletter */ const NEWSLETTER_SUBSCRIBE = 'thelia.newsletter.subscribe'; + + /************ LANG MANAGEMENT ****************************/ + + const LANG_UPDATE = 'action.lang.update'; + const LANG_CREATE = 'action.lang.create'; + const LANG_DELETE = 'action.lang.delete'; + + const LANG_DEFAULTBEHAVIOR = 'action.lang.defaultBehavior'; + + const LANG_TOGGLEDEFAULT = 'action.lang.toggleDefault'; + + 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/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/LangDefaultBehaviorForm.php b/core/lib/Thelia/Form/Lang/LangDefaultBehaviorForm.php new file mode 100644 index 000000000..23418a392 --- /dev/null +++ b/core/lib/Thelia/Form/Lang/LangDefaultBehaviorForm.php @@ -0,0 +1,85 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Form\Lang; + +use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints\Range; +use Thelia\Form\BaseForm; +use Thelia\Core\Translation\Translator; + + +/** + * Class LangDefaultBehaviorForm + * @package Thelia\Form\Lang + * @author Manuel Raynaud + */ +class LangDefaultBehaviorForm 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('behavior', 'choice', array( + 'choices' => array( + 0 => Translator::getInstance()->trans("Strictly use the requested language"), + 1 => Translator::getInstance()->trans("Replace by the default language"), + ), + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans("If a translation is missing or incomplete :"), + 'label_attr' => array( + 'for' => 'defaultBehavior-form' + ) + )); + } + + /** + * @return string the name of you form. This name must be unique + */ + public function getName() + { + return 'thelia_lang_defaultBehavior'; + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Form/LanguageCreationForm.php b/core/lib/Thelia/Form/Lang/LangUpdateForm.php similarity index 70% rename from core/lib/Thelia/Form/LanguageCreationForm.php rename to core/lib/Thelia/Form/Lang/LangUpdateForm.php index 713cafcd9..0463335f7 100644 --- a/core/lib/Thelia/Form/LanguageCreationForm.php +++ b/core/lib/Thelia/Form/Lang/LangUpdateForm.php @@ -20,39 +20,35 @@ /* along with this program. If not, see . */ /* */ /*************************************************************************************/ -namespace Thelia\Form; +namespace Thelia\Form\Lang; +use Symfony\Component\Validator\Constraints\GreaterThan; use Symfony\Component\Validator\Constraints\NotBlank; -use Thelia\Core\Translation\Translator; -class LanguageCreationForm extends BaseForm + +/** + * Class LangUpdateForm + * @package Thelia\Form\Lang + * @author Manuel Raynaud + */ +class LangUpdateForm extends LangCreateForm { - protected function buildForm() + + public function buildForm() { + parent::buildForm(); + $this->formBuilder - ->add("title", "text", array( - "constraints" => array( - new NotBlank() - ), - "label" => Translator::getInstance()->trans("Language title *"), - "label_attr" => array( - "for" => "title" + ->add('id', 'hidden', array( + 'constraints' => array( + new NotBlank(), + new GreaterThan(array('value' => 0)) ) - )) - ->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"; + return 'thelia_lang_update'; } -} +} \ No newline at end of file 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 3c45ade33..189b640ab 100755 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -2,10 +2,18 @@ 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 { + use \Thelia\Model\Tools\ModelEventDispatcherTrait; /** * Return the default language object, using a local variable to cache it. * @@ -20,4 +28,65 @@ 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)); + + 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)); + } + } 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/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/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 *} -