change http code status on different context

This commit is contained in:
Manuel Raynaud
2013-10-23 09:12:05 +02:00
parent d356974ef7
commit eac2c274e7
5 changed files with 46 additions and 14 deletions

View File

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

View File

@@ -251,6 +251,6 @@ class CountryController extends AbstractCrudController
}
return $this->nullResponse($content, 500);
return $this->nullResponse(500);
}
}

View File

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

View File

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

View File

@@ -54,7 +54,7 @@
<td>{$DATE_FORMAT}</td>
<td>{$TIME_FORMAT}</td>
<td>
<div class="make-switch switch-small switch-radio lang-default-change" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
<div class="make-switch switch-small switch-radio lang-default-change" data-id="{$ID}" data-on="success" data-off="danger" data-on-label="<i class='glyphicon glyphicon-ok'></i>" data-off-label="<i class='glyphicon glyphicon-remove'></i>">
<input type="radio" name="is_default" {if $IS_DEFAULT}checked{/if}>
</div>
</td>
@@ -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')