Commit 94f40cc7 authored by Tino Goratsch's avatar Tino Goratsch

moved the controller action helper into a separate class

parent 672958e0
......@@ -41,14 +41,14 @@ class Alerts
/**
* Displays a confirm box
*
* @param string $text
* @param int|string|array $forward
* @param int|string $backward
* @param integer $overlay
* @param string $text
* @param string|array $forward
* @param string $backward
* @param integer $overlay
*
* @return string
*/
public function confirmBox($text, $forward = 0, $backward = 0, $overlay = 0)
public function confirmBox($text, $forward = '', $backward = '', $overlay = 0)
{
if (!empty($text)) {
$confirm = [
......@@ -70,14 +70,14 @@ class Alerts
/**
* Displays a confirm box, where the forward button triggers a form submit using POST
*
* @param $text
* @param array $data
* @param $forward
* @param int $backward
* @param string $text
* @param array $data
* @param string $forward
* @param string $backward
*
* @return string
*/
public function confirmBoxPost($text, array $data, $forward, $backward = 0)
public function confirmBoxPost($text, array $data, $forward, $backward = '')
{
if (!empty($text) && !empty($data)) {
$confirm = [
......@@ -97,7 +97,7 @@ class Alerts
}
/**
* @param $errors
* @param string|array $errors
*/
protected function _setErrorBoxData($errors)
{
......
......@@ -3,7 +3,6 @@
namespace ACP3\Core\Modules;
use ACP3\Core;
use ACP3\Core\Modules\Controller\Context;
/**
* Class AdminController
......@@ -43,191 +42,4 @@ abstract class AdminController extends Core\Modules\FrontendController
return parent::preDispatch();
}
/**
* @param string $action
* @param callable $callback
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @throws \ACP3\Core\Exceptions\ResultNotExists
*/
protected function handleDeleteAction(
$action,
callable $callback,
$moduleConfirmUrl = null,
$moduleIndexUrl = null
)
{
$this->handleCustomDeleteAction(
$action,
function ($items) use ($callback, $moduleIndexUrl) {
$result = $callback($items);
if (is_string($result) === false) {
$this->setRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl);
}
},
$moduleConfirmUrl,
$moduleIndexUrl
);
}
/**
* @param string $action
* @param callable $callback
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @throws \ACP3\Core\Exceptions\ResultNotExists
*/
protected function handleCustomDeleteAction(
$action,
callable $callback,
$moduleConfirmUrl = null,
$moduleIndexUrl = null
)
{
list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl);
$result = $this->_deleteItem($action, $moduleConfirmUrl, $moduleIndexUrl);
if (is_string($result)) {
$this->setTemplate($result);
} elseif ($action === 'confirmed' && is_array($result)) {
return $callback($result);
} else {
throw new Core\Exceptions\ResultNotExists();
}
}
/**
* Little helper function for deleting an result set
*
* @param string $action
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @return string|array
*/
private function _deleteItem($action, $moduleConfirmUrl = null, $moduleIndexUrl = null)
{
if (is_array($this->request->getPost()->get('entries')) === true) {
$entries = $this->request->getPost()->get('entries');
} elseif ((bool)preg_match('/^((\d+)\|)*(\d+)$/', $this->request->getParameters()->get('entries')) === true) {
$entries = $this->request->getParameters()->get('entries');
}
/** @var \ACP3\Core\Helpers\Alerts $alerts */
$alerts = $this->get('core.helpers.alerts');
if (empty($entries)) {
return $alerts->errorBoxContent($this->lang->t('system', 'no_entries_selected'));
} elseif (empty($entries) === false && $action !== 'confirmed') {
if (is_array($entries) === false) {
$entries = [$entries];
}
$data = [
'action' => 'confirmed',
'entries' => $entries
];
return $alerts->confirmBoxPost(
$this->fetchConfirmationBoxText($entries),
$data,
$this->router->route($moduleConfirmUrl),
$this->router->route($moduleIndexUrl)
);
} else {
return is_array($entries) ? $entries : explode('|', $entries);
}
}
/**
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @return array
*/
protected function generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl)
{
if ($moduleConfirmUrl === null) {
$moduleConfirmUrl = $this->request->getFullPath();
}
if ($moduleIndexUrl === null) {
$moduleIndexUrl = $this->request->getModuleAndController();
}
return [$moduleConfirmUrl, $moduleIndexUrl];
}
/**
* @param callable $callback
* @param null|string $path
*/
protected function handleSettingsPostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'settings', $path);
}, $path);
}
/**
* @param callable $callback
* @param null|string $path
*/
protected function handleCreatePostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'create', $path);
});
}
/**
* @param callable $callback
* @param null|string $path
*/
protected function handleEditPostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'edit', $path);
});
}
/**
* @param bool|int $result
* @param string $localization
* @param null|string $path
*/
private function setRedirectMessageAfterPost($result, $localization, $path = null)
{
$this->redirectMessages()->setMessage(
$result,
$this->lang->t('system', $localization . ($result !== false ? '_success' : '_error')),
$path
);
}
/**
* @param array $entries
*
* @return mixed|string
*/
protected function fetchConfirmationBoxText($entries)
{
$entriesCount = count($entries);
if ($entriesCount === 1) {
return $this->lang->t('system', 'confirm_delete_single');
}
return str_replace('{items}', $entriesCount, $this->lang->t('system', 'confirm_delete_multiple'));
}
}
......@@ -33,7 +33,8 @@ class AdminContext extends FrontendContext
$frontendContext,
$frontendContext->getAssets(),
$frontendContext->getBreadcrumb(),
$frontendContext->getSeo()
$frontendContext->getSeo(),
$frontendContext->getActionHelper()
);
$this->validate = $validate;
......
......@@ -2,7 +2,6 @@
namespace ACP3\Core\Modules\Controller;
use ACP3\Core;
use ACP3\Core\Modules\Controller\Context;
/**
* Class FrontendContext
......@@ -22,18 +21,24 @@ class FrontendContext extends Core\Modules\Controller\Context
* @var \ACP3\Core\SEO
*/
protected $seo;
/**
* @var \ACP3\Core\Modules\Helper\Action
*/
protected $actionHelper;
/**
* @param \ACP3\Core\Modules\Controller\Context $context
* @param \ACP3\Core\Assets $assets
* @param \ACP3\Core\Breadcrumb $breadcrumb
* @param \ACP3\Core\SEO $seo
* @param \ACP3\Core\Modules\Helper\Action $actionHelper
*/
public function __construct(
Core\Modules\Controller\Context $context,
Core\Assets $assets,
Core\Breadcrumb $breadcrumb,
Core\SEO $seo
Core\SEO $seo,
Core\Modules\Helper\Action $actionHelper
)
{
parent::__construct(
......@@ -50,6 +55,7 @@ class FrontendContext extends Core\Modules\Controller\Context
$this->assets = $assets;
$this->breadcrumb = $breadcrumb;
$this->seo = $seo;
$this->actionHelper = $actionHelper;
}
/**
......@@ -75,4 +81,12 @@ class FrontendContext extends Core\Modules\Controller\Context
{
return $this->seo;
}
/**
* @return Core\Modules\Helper\Action
*/
public function getActionHelper()
{
return $this->actionHelper;
}
}
......@@ -27,6 +27,11 @@ abstract class FrontendController extends Core\Modules\Controller
* @var Core\Helpers\RedirectMessages
*/
protected $redirectMessages;
/**
* @var \ACP3\Core\Modules\Helper\Action
*/
protected $actionHelper;
/**
* Der auszugebende Content-Type der Seite
*
......@@ -44,6 +49,7 @@ abstract class FrontendController extends Core\Modules\Controller
$this->assets = $frontendContext->getAssets();
$this->breadcrumb = $frontendContext->getBreadcrumb();
$this->seo = $frontendContext->getSeo();
$this->actionHelper = $frontendContext->getActionHelper();
}
/**
......
<?php
namespace ACP3\Core\Modules\Helper;
use ACP3\Core;
use ACP3\Core\Modules\FrontendController;
/**
* Class Action
* @package ACP3\Core\Modules\Helper
*/
class Action
{
/**
* @var \ACP3\Core\Lang
*/
protected $lang;
/**
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
* @var \ACP3\Core\Router
*/
protected $router;
/**
* @var \ACP3\Core\View
*/
protected $view;
/**
* @var \ACP3\Core\Helpers\Alerts
*/
protected $alerts;
/**
* @var \ACP3\Core\Helpers\RedirectMessages
*/
protected $redirectMessages;
/**
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router $router
* @param \ACP3\Core\View $view
* @param \ACP3\Core\Helpers\Alerts $alerts
* @param \ACP3\Core\Helpers\RedirectMessages $redirectMessages
*/
public function __construct(
Core\Lang $lang,
Core\Http\RequestInterface $request,
Core\Router $router,
Core\View $view,
Core\Helpers\Alerts $alerts,
Core\Helpers\RedirectMessages $redirectMessages
)
{
$this->lang = $lang;
$this->request = $request;
$this->router = $router;
$this->view = $view;
$this->alerts = $alerts;
$this->redirectMessages = $redirectMessages;
}
/**
* @param callable $callback
* @param null|string $path
*/
public function handlePostAction(callable $callback, $path = null)
{
try {
$callback();
} catch (Core\Exceptions\InvalidFormToken $e) {
$this->redirectMessages->setMessage(false, $e->getMessage(), $path);
} catch (Core\Exceptions\ValidationFailed $e) {
$this->view->assign('error_msg', $this->alerts->errorBox($e->getMessage()));
}
}
/**
* @param \ACP3\Core\Modules\FrontendController $context
* @param string $action
* @param callable $callback
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @throws \ACP3\Core\Exceptions\ResultNotExists
*/
public function handleDeleteAction(
FrontendController $context,
$action,
callable $callback,
$moduleConfirmUrl = null,
$moduleIndexUrl = null
)
{
$this->handleCustomDeleteAction(
$context,
$action,
function ($items) use ($callback, $moduleIndexUrl) {
$result = $callback($items);
if (is_string($result) === false) {
$this->setRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl);
}
},
$moduleConfirmUrl,
$moduleIndexUrl
);
}
/**
* @param \ACP3\Core\Modules\FrontendController $context
* @param string $action
* @param callable $callback
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @return
* @throws \ACP3\Core\Exceptions\ResultNotExists
*/
public function handleCustomDeleteAction(
FrontendController $context,
$action,
callable $callback,
$moduleConfirmUrl = null,
$moduleIndexUrl = null
)
{
list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl);
$result = $this->deleteItem($action, $moduleConfirmUrl, $moduleIndexUrl);
if (is_string($result)) {
$context->setTemplate($result);
} elseif ($action === 'confirmed' && is_array($result)) {
return $callback($result);
} else {
throw new Core\Exceptions\ResultNotExists();
}
}
/**
* @param callable $callback
* @param null|string $path
*/
public function handleSettingsPostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'settings', $path);
}, $path);
}
/**
* @param callable $callback
* @param null|string $path
*/
public function handleCreatePostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'create', $path);
});
}
/**
* @param callable $callback
* @param null|string $path
*/
public function handleEditPostAction(callable $callback, $path = null)
{
$this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
$this->setRedirectMessageAfterPost($result, 'edit', $path);
});
}
/**
* @param bool|int $result
* @param string $localization
* @param null|string $path
*/
private function setRedirectMessageAfterPost($result, $localization, $path = null)
{
$this->redirectMessages->setMessage(
$result,
$this->lang->t('system', $localization . ($result !== false ? '_success' : '_error')),
$path
);
}
/**
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @return array
*/
private function generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl)
{
if ($moduleConfirmUrl === null) {
$moduleConfirmUrl = $this->request->getFullPath();
}
if ($moduleIndexUrl === null) {
$moduleIndexUrl = $this->request->getModuleAndController();
}
return [$moduleConfirmUrl, $moduleIndexUrl];
}
/**
* Little helper function for deleting an result set
*
* @param string $action
* @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl
*
* @return string|array
*/
private function deleteItem($action, $moduleConfirmUrl = null, $moduleIndexUrl = null)
{
if (is_array($this->request->getPost()->get('entries')) === true) {
$entries = $this->request->getPost()->get('entries');
} elseif ((bool)preg_match('/^((\d+)\|)*(\d+)$/', $this->request->getParameters()->get('entries')) === true) {
$entries = $this->request->getParameters()->get('entries');
}
if (empty($entries)) {
return $this->alerts->errorBoxContent($this->lang->t('system', 'no_entries_selected'));
} elseif (empty($entries) === false && $action !== 'confirmed') {
if (is_array($entries) === false) {
$entries = [$entries];
}
$data = [
'action' => 'confirmed',
'entries' => $entries
];
return $this->alerts->confirmBoxPost(
$this->fetchConfirmationBoxText($entries),
$data,
$this->router->route($moduleConfirmUrl),
$this->router->route($moduleIndexUrl)
);
} else {
return is_array($entries) ? $entries : explode('|', $entries);
}
}
/**
* @param array $entries
*
* @return mixed|string
*/
private function fetchConfirmationBoxText($entries)
{
$entriesCount = count($entries);
if ($entriesCount === 1) {
return $this->lang->t('system', 'confirm_delete_single');
}
return str_replace('{items}', $entriesCount, $this->lang->t('system', 'confirm_delete_multiple'));
}
}
\ No newline at end of file
......@@ -54,4 +54,4 @@ services:
core.helpers.formatter.rewriteInternalUri:
class: ACP3\Core\Helpers\Formatter\RewriteInternalUri
arguments: [@core.modules.controllerActionsExists, @core.request, '@core.router', '@core.validator.rules.router.aliases']
arguments: [@core.modules.helper.controllerActionExists, @core.request, '@core.router', '@core.validator.rules.router.aliases']
......@@ -11,12 +11,12 @@ services:
core.context.frontend:
class: ACP3\Core\Modules\Controller\FrontendContext
arguments: ['@core.context', '@core.assets', '@core.breadcrumb', '@core.seo']
arguments: ['@core.context', '@core.assets', '@core.breadcrumb', '@core.seo', @core.modules.helper.action]
public: false
core.modules:
class: ACP3\Core\Modules
arguments: ['@service_container', @core.modules.controllerActionsExists, @core.modules.moduleInfoCache, @core.modules.vendors]
arguments: ['@service_container', @core.modules.helper.controllerActionExists, @core.modules.moduleInfoCache, @core.modules.vendors]
core.modules.aclInstaller:
class: ACP3\Core\Modules\AclInstaller
......@@ -34,7 +34,11 @@ services:
abstract: true
arguments: ['@core.context.admin']
core.modules.controllerActionsExists:
core.modules.helper.action:
class: ACP3\Core\Modules\Helper\Action
arguments: [@core.lang, @core.request, @core.router, @core.view, @core.helpers.alerts, @core.helpers.redirect]
core.modules.helper.controllerActionExists:
class: ACP3\Core\Modules\Helper\ControllerActionExists
arguments: [@service_container]
......
......@@ -66,7 +66,7 @@ services:
core.request:
class: ACP3\Core\Http\Request
arguments: ['@core.modules.controllerActionsExists', '@core.config', '@seo.model']
arguments: ['@core.modules.helper.controllerActionExists', '@core.config', '@seo.model']
core.router:
class: ACP3\Core\Router
......
......@@ -137,7 +137,7 @@ class Index extends Core\Modules\AdminController
*/
protected function _createPost(array $formData)
{
$this->handleCreatePostAction(function () use ($formData) {
$this->actionHelper->handleCreatePostAction(function () use ($formData) {
$this->articlesValidator->validate($formData);
$insertValues = [
......@@ -173,7 +173,8 @@ class Index extends Core\Modules\AdminController
*/
public function actionDelete($action = '')
{
$this->handleDeleteAction(
$this->actionHelper->handleDeleteAction(
$this,
$action,
function ($items) {
$bool = false;
......@@ -255,7 +256,7 @@ class Index extends Core\Modules\AdminController
*/
protected function _editPost(array $formData, $id)
{
$this->handleEditPostAction(function () use ($formData, $id) {
$this->actionHelper->handleEditPostAction(function () use ($formData, $id) {
$this->articlesValidator->validate(
$formData,
sprintf(Articles\Helpers::URL_KEY_PATTERN, $id)
......
......@@ -76,7 +76,7 @@ class Index extends Core\Modules\AdminController
*/
protected function _createPost(array $formData)
{
$this->handleCreatePostAction(function() use ($formData) {
$this->actionHelper->handleCreatePostAction(function() use ($formData) {
$file = $this->request->getFiles()->get('picture');
$this->categoriesValidator->validate($formData, $file, $this->config->getSettings('categories'));
......@@ -111,7 +111,8 @@ class Index extends Core\Modules\AdminController
*/
public function actionDelete($action = '')
{
$this->handleCustomDeleteAction(
$this->actionHelper->handleCustomDeleteAction(
$this,
$action,
function ($items) {
$bool = false;
......@@ -181,7 +182,7 @@ class Index extends Core\Modules\AdminController
*/
protected function _editPost(array $formData, array $category, $id)
{
$this->handleEditPostAction(function() use ($formData, $category, $id) {
$this->actionHelper->handleEditPostAction(function() use ($formData, $category, $id) {
$file = $this->request->getFiles()->get('picture');
$this->categoriesValidator->validate($formData, $file, $this->config->getSettings('categories'), $id);
......@@ -249,7 +250,7 @@ class Index extends Core\Modules\AdminController
*/
protected function _settingsPost(array $formData)
{
$this->handleSettingsPostAction(function () use ($formData) {
$this->actionHelper->handleSettingsPostAction(function () use ($formData) {
$this->categoriesValidator->validateSettings($formData);
$data = [
......