Merge branch 'master' into tax

This commit is contained in:
Etienne Roudeix
2013-10-23 16:32:10 +02:00
25 changed files with 1469 additions and 128 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
);
}
@@ -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);
}
}
}

View File

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

View File

@@ -0,0 +1,261 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
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 <mraynaud@openstudio.fr>
*/
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();
}
}

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