From eac2c274e78ab356a8b33e2efde3e5e8a691f4b9 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 23 Oct 2013 09:12:05 +0200 Subject: [PATCH] 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')