Completed the backoffice variable management
This commit is contained in:
@@ -29,10 +29,4 @@ class AdminController extends BaseAdminController
|
||||
{
|
||||
return $this->render("home");
|
||||
}
|
||||
|
||||
public function processAction()
|
||||
{
|
||||
echo "not yet coded !";
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ use Thelia\Tools\URL;
|
||||
use Thelia\Tools\Redirect;
|
||||
use Thelia\Core\Security\SecurityContext;
|
||||
use Thelia\Model\AdminLog;
|
||||
use Thelia\Model\Lang;
|
||||
use Thelia\Model\LangQuery;
|
||||
|
||||
class BaseAdminController extends BaseController
|
||||
{
|
||||
@@ -46,6 +48,12 @@ class BaseAdminController extends BaseController
|
||||
AdminLog::append($message, $this->getRequest(), $this->getSecurityContext()->getAdminUser());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method process the rendering of view called from an admin page
|
||||
*
|
||||
* @param unknown $template
|
||||
* @return Response the reponse which contains the rendered view
|
||||
*/
|
||||
public function processTemplateAction($template)
|
||||
{
|
||||
try {
|
||||
@@ -89,19 +97,31 @@ class BaseAdminController extends BaseController
|
||||
/**
|
||||
* Check current admin user authorisations. An ADMIN role is assumed.
|
||||
*
|
||||
* @param unknown $permissions a single permission or an array of permissions.
|
||||
* @param mixed $permissions a single permission or an array of permissions.
|
||||
*
|
||||
* @return mixed null if authorization is granted, or a Response object which contains the error page otherwise
|
||||
*
|
||||
* @throws AuthenticationException if permissions are not granted ti the current user.
|
||||
*/
|
||||
protected function checkAuth($permissions)
|
||||
{
|
||||
if (! $this->getSecurityContext()->isGranted(array("ADMIN"), is_array($permissions) ? $permissions : array($permissions))) {
|
||||
throw new AuthorizationException("Sorry, you're not allowed to perform this action");
|
||||
}
|
||||
$permArr = is_array($permissions) ? $permissions : array($permissions);
|
||||
|
||||
if ($this->getSecurityContext()->isGranted(array("ADMIN"), $permArr)) {
|
||||
// Okay !
|
||||
return null;
|
||||
}
|
||||
|
||||
// Log the problem
|
||||
$this->adminLogAppend("User is not granted for permissions %s", implode(", ", $permArr));
|
||||
|
||||
// Generate the proper response
|
||||
$response = new Response();
|
||||
|
||||
return $response->setContent($this->errorPage("Sorry, you're not allowed to perform this action"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a ParserInterfac instance parser
|
||||
* @return a ParserInterface instance parser
|
||||
*/
|
||||
protected function getParser()
|
||||
{
|
||||
@@ -130,6 +150,23 @@ class BaseAdminController extends BaseController
|
||||
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current edition lang ID, checking if a change was requested in the current request
|
||||
*/
|
||||
protected function getCurrentEditionLangId() {
|
||||
return $this->getRequest()->get(
|
||||
'edition_language',
|
||||
$this->getSession()->getAdminEditionLangId()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple helper to get the current edition locale, from the session edition language ID
|
||||
*/
|
||||
protected function getCurrentEditionLocale() {
|
||||
return LangQuery::create()->findOneById($this->getCurrentEditionLangId())->getLocale();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the given template, and returns the result as an Http Response.
|
||||
*
|
||||
@@ -153,26 +190,40 @@ class BaseAdminController extends BaseController
|
||||
*/
|
||||
protected function renderRaw($templateName, $args = array())
|
||||
{
|
||||
|
||||
// Add the template standard extension
|
||||
$templateName .= '.html';
|
||||
|
||||
$session = $this->getSession();
|
||||
|
||||
// Find the current edit language ID
|
||||
$edition_language = $this->getCurrentEditionLangId();
|
||||
|
||||
// Prepare common template variables
|
||||
$args = array_merge($args, array(
|
||||
'locale' => $session->getLocale(),
|
||||
'lang' => $session->getLang()
|
||||
'locale' => $session->getLocale(),
|
||||
'lang' => $session->getLang(),
|
||||
'edition_language' => $edition_language,
|
||||
'current_url' => htmlspecialchars($this->getRequest()->getUri())
|
||||
));
|
||||
|
||||
// Update the current edition language in session
|
||||
$this->getSession()->setAdminEditionLangId($edition_language);
|
||||
|
||||
// Render the template.
|
||||
try {
|
||||
$data = $this->getParser()->render($templateName, $args);
|
||||
|
||||
return $data;
|
||||
} catch (AuthenticationException $ex) {
|
||||
|
||||
}
|
||||
catch (AuthenticationException $ex) {
|
||||
// User is not authenticated, and templates requires authentication -> redirect to login page
|
||||
// We user login_tpl as a path, not a template.
|
||||
|
||||
Redirect::exec(URL::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("Sorry, you are not allowed to perform this action.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ class CategoryController extends BaseAdminController
|
||||
}
|
||||
catch (FormValidationException $e) {
|
||||
$categoryCreationForm->setErrorMessage($e->getMessage());
|
||||
$this->getParserContext()->setErrorForm($categoryCreationForm);
|
||||
$this->getParserContext()->addForm($categoryCreationForm);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
Tlog::getInstance()->error(sprintf("Failed to create category: %s", $e->getMessage()));
|
||||
@@ -79,7 +79,7 @@ class CategoryController extends BaseAdminController
|
||||
|
||||
protected function editCategory($args)
|
||||
{
|
||||
$this->checkAuth("ADMIN", "admin.category.edit");
|
||||
if (null !== $response = $this->checkAuth("admin.category.edit")) return $response;
|
||||
|
||||
return $this->render('edit_category', $args);
|
||||
}
|
||||
@@ -107,7 +107,7 @@ class CategoryController extends BaseAdminController
|
||||
}
|
||||
catch (FormValidationException $e) {
|
||||
$categoryDeletionForm->setErrorMessage($e->getMessage());
|
||||
$this->getParserContext()->setErrorForm($categoryDeletionForm);
|
||||
$this->getParserContext()->addForm($categoryDeletionForm);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
Tlog::getInstance()->error(sprintf("Failed to delete category: %s", $e->getMessage()));
|
||||
@@ -120,7 +120,7 @@ class CategoryController extends BaseAdminController
|
||||
|
||||
protected function browseCategory($args)
|
||||
{
|
||||
$this->checkAuth("AMIN", "admin.catalog.view");
|
||||
if (null !== $response = $this->checkAuth("admin.catalog.view")) return $response;
|
||||
|
||||
return $this->render('categories', $args);
|
||||
}
|
||||
@@ -203,9 +203,6 @@ class CategoryController extends BaseAdminController
|
||||
'current_category_id' => $id,
|
||||
'category_order' => $category_order,
|
||||
'edition_language' => $edition_language,
|
||||
'date_format' => Lang::getDefaultLanguage()->getDateFormat(),
|
||||
'time_format' => Lang::getDefaultLanguage()->getTimeFormat(),
|
||||
'datetime_format' => Lang::getDefaultLanguage()->getDateTimeFormat(),
|
||||
);
|
||||
|
||||
// Store the current sort order in session
|
||||
|
||||
@@ -96,7 +96,7 @@ class SessionController extends BaseAdminController
|
||||
$adminLoginForm->setErrorMessage($message);
|
||||
|
||||
// Store the form name in session (see Form Smarty plugin to find usage of this parameter)
|
||||
$this->getParserContext()->setErrorForm($adminLoginForm);
|
||||
$this->getParserContext()->addForm($adminLoginForm);
|
||||
|
||||
// Display the login form again
|
||||
return $this->render("login");
|
||||
|
||||
@@ -26,23 +26,272 @@ namespace Thelia\Controller\Admin;
|
||||
use Thelia\Core\Event\ConfigDeleteEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Tools\URL;
|
||||
use Thelia\Core\Event\ConfigChangeEvent;
|
||||
use Thelia\Form\VariableCreationForm;
|
||||
use Thelia\Core\Event\ConfigCreateEvent;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Core\Security\Exception\AuthorizationException;
|
||||
use Thelia\Form\VariableModificationForm;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
|
||||
/**
|
||||
* Manages Thelmia system variables, aka Config objects.
|
||||
*
|
||||
* @author Franck Allimant <franck@cqfdev.fr>
|
||||
*/
|
||||
class VariablesController extends BaseAdminController
|
||||
{
|
||||
/**
|
||||
* The default action is displaying the variables list.
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function defaultAction() {
|
||||
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.view")) return $response;
|
||||
|
||||
return $this->render('variables');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new config object
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function createAction() {
|
||||
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.create")) return $response;
|
||||
|
||||
$message = false;
|
||||
|
||||
// Create the Creation Form
|
||||
$creationForm = new VariableCreationForm($this->getRequest());
|
||||
|
||||
try {
|
||||
|
||||
// Validate the form, create the ConfigCreation event and dispatch it.
|
||||
$form = $this->validateForm($creationForm, "POST");
|
||||
|
||||
$data = $form->getData();
|
||||
|
||||
$createEvent = new ConfigCreateEvent();
|
||||
|
||||
$createEvent
|
||||
->setEventName($data['name'])
|
||||
->setValue($data['value'])
|
||||
->setLocale($data["locale"])
|
||||
->setTitle($data['title'])
|
||||
;
|
||||
|
||||
$this->dispatch(TheliaEvents::CONFIG_CREATE, $createEvent);
|
||||
|
||||
$createdObject = $createEvent->getConfig();
|
||||
|
||||
// Log config creation
|
||||
$this->adminLogAppend(sprintf("Variable %s (ID %s) created", $createdObject->getName(), $createdObject->getId()));
|
||||
|
||||
// Substitute _ID_ in the URL with the ID of the created object
|
||||
$successUrl = str_replace('_ID_', $createdObject->getId(), $creationForm->getSuccessUrl());
|
||||
|
||||
// Redirect to the success URL
|
||||
$this->redirect($successUrl);
|
||||
}
|
||||
catch (FormValidationException $ex) {
|
||||
// Form cannot be validated
|
||||
$message = sprintf("Please check your input: %s", $ex->getMessage());
|
||||
}
|
||||
catch (\Exception $ex) {
|
||||
// Any other error
|
||||
$message = sprintf("Sorry, an error occured: %s", $ex->getMessage());
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
// An error has been detected: log it
|
||||
Tlog::getInstance()->error(sprintf("Error during variable creation process : %s. Exception was %s", $message, $ex->getMessage()));
|
||||
|
||||
// Mark the form as errored
|
||||
$creationForm->setErrorMessage($message);
|
||||
|
||||
// Pass it to the parser, along with the error message
|
||||
$this->getParserContext()
|
||||
->addForm($creationForm)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
}
|
||||
|
||||
// At this point, the form has error, and should be redisplayed.
|
||||
return $this->render('variables');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a config object for modification, and display the edit template.
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function changeAction() {
|
||||
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.change")) return $response;
|
||||
|
||||
// Load the config object
|
||||
$config = ConfigQuery::create()
|
||||
->joinWithI18n($this->getCurrentEditionLocale())
|
||||
->findOneById($this->getRequest()->get('variable_id'));
|
||||
|
||||
if ($config != null) {
|
||||
|
||||
// Prepare the data that will hydrate the form
|
||||
$data = array(
|
||||
'id' => $config->getId(),
|
||||
'name' => $config->getName(),
|
||||
'value' => $config->getValue(),
|
||||
'hidden' => $config->getHidden(),
|
||||
'secured' => $config->getSecured(),
|
||||
'locale' => $config->getLocale(),
|
||||
'title' => $config->getTitle(),
|
||||
'chapo' => $config->getChapo(),
|
||||
'description' => $config->getDescription(),
|
||||
'postscriptum' => $config->getPostscriptum()
|
||||
);
|
||||
|
||||
// Setup the object form
|
||||
$changeForm = new VariableModificationForm($this->getRequest(), "form", $data);
|
||||
|
||||
// Pass it to the parser
|
||||
$this->getParserContext()->addForm($changeForm);
|
||||
}
|
||||
|
||||
// Render the edition template.
|
||||
return $this->render('variable-edit', array('variable_id' => $this->getRequest()->get('variable_id')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save changes on a modified config object, and either go back to the variable list, or stay on the edition page.
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function saveChangeAction() {
|
||||
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.change")) return $response;
|
||||
|
||||
$message = false;
|
||||
|
||||
// Create the form from the request
|
||||
$changeForm = new VariableModificationForm($this->getRequest());
|
||||
|
||||
// Get the variable ID
|
||||
$variable_id = $this->getRequest()->get('variable_id');
|
||||
|
||||
try {
|
||||
|
||||
// Check the form against constraints violations
|
||||
$form = $this->validateForm($changeForm, "POST");
|
||||
|
||||
// Get the form field values
|
||||
$data = $form->getData();
|
||||
|
||||
$changeEvent = new ConfigChangeEvent($data['id']);
|
||||
|
||||
// Create and dispatch the change event
|
||||
$changeEvent
|
||||
->setEventName($data['name'])
|
||||
->setValue($data['value'])
|
||||
->setHidden($data['hidden'])
|
||||
->setSecured($data['secured'])
|
||||
->setLocale($data["locale"])
|
||||
->setTitle($data['title'])
|
||||
->setChapo($data['chapo'])
|
||||
->setDescription($data['description'])
|
||||
->setPostscriptum($data['postscriptum'])
|
||||
;
|
||||
|
||||
$this->dispatch(TheliaEvents::CONFIG_MODIFY, $changeEvent);
|
||||
|
||||
// Log config modification
|
||||
$changedObject = $changeEvent->getConfig();
|
||||
|
||||
$this->adminLogAppend(sprintf("Variable %s (ID %s) modified", $changedObject->getName(), $changedObject->getId()));
|
||||
|
||||
// If we have to stay on the same page, do not redirect to the succesUrl,
|
||||
// just redirect to the edit page again.
|
||||
if ($this->getRequest()->get('save_mode') == 'stay') {
|
||||
$this->redirect(URL::absoluteUrl(
|
||||
"admin/configuration/variables/change",
|
||||
array('variable_id' => $variable_id)
|
||||
));
|
||||
}
|
||||
|
||||
// Redirect to the success URL
|
||||
$this->redirect($changeForm->getSuccessUrl());
|
||||
}
|
||||
catch (FormValidationException $ex) {
|
||||
// Invalid data entered
|
||||
$message = sprintf("Please check your input: %s", $ex->getMessage());
|
||||
}
|
||||
catch (\Exception $ex) {
|
||||
// Any other error
|
||||
$message = sprintf("Sorry, an error occured: %s", $ex->getMessage());
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
// Log error message
|
||||
Tlog::getInstance()->error(sprintf("Error during variable creation process : %s. Exception was %s", $message, $ex->getMessage()));
|
||||
|
||||
// Mark the form as errored
|
||||
$changeForm->setErrorMessage($message);
|
||||
|
||||
// Pas the form and the error to the parser
|
||||
$this->getParserContext()
|
||||
->addForm($changeForm)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
}
|
||||
|
||||
// At this point, the form has errors, and should be redisplayed.
|
||||
return $this->render('variable-edit', array('variable_id' => $variable_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Change values modified directly from the variable list
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function changeValuesAction() {
|
||||
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.change")) return $response;
|
||||
|
||||
$variables = $this->getRequest()->get('variable', array());
|
||||
|
||||
// Process all changed variables
|
||||
foreach($variables as $id => $value) {
|
||||
$event = new ConfigChangeEvent($id);
|
||||
$event->setValue($value);
|
||||
|
||||
$this->dispatch(TheliaEvents::CONFIG_SETVALUE, $event);
|
||||
}
|
||||
|
||||
$this->redirect(URL::adminViewUrl('variables'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a config object
|
||||
*
|
||||
* @return Symfony\Component\HttpFoundation\Response the response
|
||||
*/
|
||||
public function deleteAction() {
|
||||
|
||||
// Check current user authorization
|
||||
if (null !== $response = $this->checkAuth("admin.configuration.variables.delete")) return $response;
|
||||
|
||||
// Get the config id, and dispatch the delet request
|
||||
$event = new ConfigDeleteEvent($this->getRequest()->get('id'));
|
||||
|
||||
$this->dispatch(TheliaEvents::CONFIG_DELETE, $event);
|
||||
|
||||
$this->redirect(URL::adminViewUrl('variables'));
|
||||
}
|
||||
|
||||
public function updateAction() {
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user