Commit 839a17e1 authored by Tino Goratsch's avatar Tino Goratsch

Merge branch 'feature/controller-actions' into develop

parents 3c34cae2 0ff0953f
......@@ -9,6 +9,7 @@ namespace ACP3\Core\Application;
use ACP3\Core\Application\Event\ControllerActionAfterDispatchEvent;
use ACP3\Core\Application\Event\ControllerActionBeforeDispatchEvent;
use ACP3\Core\Controller\ActionInterface;
use ACP3\Core\Controller\Exception\ControllerActionNotFoundException;
use ACP3\Core\Http\RequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -61,7 +62,7 @@ class ControllerActionDispatcher
* @param array $arguments
* @return Response|string
*
* @throws \ACP3\Core\Controller\Exception\ControllerActionNotFoundException
* @throws ControllerActionNotFoundException
* @throws \ACP3\Core\Controller\Exception\ResultNotExistsException
*/
public function dispatch($serviceId = '', array $arguments = [])
......@@ -89,11 +90,23 @@ class ControllerActionDispatcher
return $response;
}
throw new \ACP3\Core\Controller\Exception\ControllerActionNotFoundException(
throw new ControllerActionNotFoundException(
'Service-Id ' . $serviceId . ' was not found!'
);
}
/**
* @return string
*/
protected function buildControllerServiceId()
{
return $this->request->getModule()
. '.controller.'
. $this->request->getArea()
. '.' . $this->request->getController()
. '.' . $this->request->getAction();
}
/**
* @param \ACP3\Core\Controller\ActionInterface $controller
* @param array $arguments
......@@ -103,7 +116,7 @@ class ControllerActionDispatcher
*/
private function executeControllerAction(ActionInterface $controller, array $arguments)
{
$callable = [$controller, 'execute'];
$callable = $this->getCallable($controller);
if (empty($arguments)) {
$arguments = $this->argumentResolver->getArguments($this->request->getSymfonyRequest(), $callable);
......@@ -113,14 +126,20 @@ class ControllerActionDispatcher
}
/**
* @return string
* @param ActionInterface $controller
* @return array
*/
protected function buildControllerServiceId()
private function getCallable(ActionInterface $controller)
{
return $this->request->getModule()
. '.controller.'
. $this->request->getArea()
. '.' . $this->request->getController()
. '.' . $this->request->getAction();
$callable = [$controller, 'execute'];
if ($this->request->getPost()->has('submit') && method_exists($controller, 'executePost')) {
$reflection = new \ReflectionMethod($controller, 'executePost');
if ($reflection->isPublic()) {
$callable = [$controller, 'executePost'];
}
}
return $callable;
}
}
......@@ -28,6 +28,7 @@ class NotEmptyValidationRuleTest extends AbstractValidationRuleTest
'invalid-data-string-whitespaces-newlines' => [" \r\n", '', [], false],
'invalid-data-empty-array' => [['foo' => []], 'foo', [], false],
'invalid-data-empty-array-2' => [[], '', [], false],
'invalid-data-missing-key' => [['foo' => ['bar-baz']], 'baz', [], false],
'invalid-data-array' => [['foo' => ''], 'foo', [], false],
'invalid-data-array-whitespace' => [['foo' => ' '], 'foo', [], false],
'invalid-data-array-whitespaces-newlines' => [['foo' => " \r\n"], 'foo', [], false],
......
......@@ -12,10 +12,20 @@ class NotEmptyValidationRule extends AbstractValidationRule
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && array_key_exists($field, $data)) {
return $this->isValid($data[$field], $field, $extra);
if (is_scalar($data)) {
return !empty(trim($data));
}
return !empty(is_array($data) ? $data : trim($data));
if (is_array($data)) {
if (empty($field)) {
return count($data) > 0;
}
if (array_key_exists($field, $data)) {
return !empty(is_scalar($data[$field]) ? trim($data[$field]) : $data[$field]);
}
}
return false;
}
}
......@@ -54,10 +54,6 @@ class Create extends AbstractFormAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$defaults = [
'title' => '',
'text' => '',
......@@ -74,13 +70,12 @@ class Create extends AbstractFormAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSaveAction(function () use ($formData) {
return $this->actionHelper->handleSaveAction(function () {
$formData = $this->request->getPost()->all();
$this->adminFormValidation->validate($formData);
$formData['user_id'] = $this->user->getUserId();
......
......@@ -62,10 +62,6 @@ class Edit extends AbstractFormAction
if (empty($article) === false) {
$this->title->setPageTitlePostfix($article['title']);
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all(), $id);
}
return [
'form' => array_merge($article, $this->request->getPost()->all()),
'form_token' => $this->formTokenHelper->renderFormToken(),
......@@ -78,22 +74,22 @@ class Edit extends AbstractFormAction
}
/**
* @param array $formData
* @param int $articleId
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData, $articleId)
public function executePost($id)
{
return $this->actionHelper->handleSaveAction(function () use ($formData, $articleId) {
return $this->actionHelper->handleSaveAction(function () use ($id) {
$formData = $this->request->getPost()->all();
$this->adminFormValidation
->setUriAlias(sprintf(Articles\Helpers::URL_KEY_PATTERN, $articleId))
->setUriAlias(sprintf(Articles\Helpers::URL_KEY_PATTERN, $id))
->validate($formData);
$formData['user_id'] = $this->user->getUserId();
$result = $this->articlesModel->save($formData, $articleId);
$result = $this->articlesModel->save($formData, $id);
$this->createOrUpdateMenuItem($formData, $articleId);
$this->createOrUpdateMenuItem($formData, $id);
return $result;
});
......
......@@ -59,10 +59,6 @@ class Create extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
return [
'form' => array_merge(['title' => '', 'description' => ''], $this->request->getPost()->all()),
'mod_list' => $this->fetchModules(),
......@@ -71,13 +67,12 @@ class Create extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSaveAction(function () use ($formData) {
return $this->actionHelper->handleSaveAction(function () {
$formData = $this->request->getPost()->all();
$file = $this->request->getFiles()->get('picture');
$this->adminFormValidation
......
......@@ -60,10 +60,6 @@ class Edit extends Core\Controller\AbstractAdminAction
if (empty($category) === false) {
$this->title->setPageTitlePostfix($category['title']);
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all(), $category, $id);
}
return [
'form' => array_merge($category, $this->request->getPost()->all()),
'form_token' => $this->formTokenHelper->renderFormToken()
......@@ -74,31 +70,30 @@ class Edit extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
* @param array $category
* @param int $categoryId
*
* @param int $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData, array $category, $categoryId)
public function executePost($id)
{
return $this->actionHelper->handleSaveAction(function () use ($formData, $category, $categoryId) {
return $this->actionHelper->handleSaveAction(function () use ($id) {
$formData = $this->request->getPost()->all();
$file = $this->request->getFiles()->get('picture');
$this->adminFormValidation
->setFile($file)
->setSettings($this->config->getSettings(Categories\Installer\Schema::MODULE_NAME))
->setCategoryId($categoryId)
->setCategoryId($id)
->validate($formData);
if (empty($file) === false) {
$category = $this->categoriesModel->getOneById($id);
$upload = new Core\Helpers\Upload($this->appPath, Categories\Installer\Schema::MODULE_NAME);
$upload->removeUploadedFile($category['picture']);
$result = $upload->moveFile($file->getPathname(), $file->getClientOriginalName());
$formData['picture'] = $result['name'];
}
return $this->categoriesModel->save($formData, $categoryId);
return $this->categoriesModel->save($formData, $id);
});
}
}
......@@ -47,10 +47,6 @@ class Settings extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$settings = $this->config->getSettings(Categories\Installer\Schema::MODULE_NAME);
return [
......@@ -60,13 +56,12 @@ class Settings extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () use ($formData) {
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->adminSettingsFormValidation->validate($formData);
$data = [
......
......@@ -71,15 +71,6 @@ class Edit extends Core\Controller\AbstractAdminAction
$this->title->setPageTitlePostfix($comment['name']);
if ($this->request->getPost()->count() !== 0) {
return $this->executePost(
$this->request->getPost()->all(),
$comment,
$id,
$comment['module_id']
);
}
return [
'form' => array_merge($comment, $this->request->getPost()->all()),
'module_id' => (int)$comment['module_id'],
......@@ -91,17 +82,17 @@ class Edit extends Core\Controller\AbstractAdminAction
throw new Core\Controller\Exception\ResultNotExistsException();
}
/**
* @param array $formData
* @param array $comment
* @param int $commentId
* @param int $moduleId
* @param int $id
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData, array $comment, $commentId, $moduleId)
public function executePost($id)
{
$comment = $this->commentsModel->getOneById($id);
return $this->actionHelper->handleSaveAction(
function () use ($formData, $comment, $commentId) {
function () use ($id) {
$formData = $this->request->getPost()->all();
$this->adminFormValidation->validate($formData);
$updateValues = [
......@@ -113,9 +104,9 @@ class Edit extends Core\Controller\AbstractAdminAction
$updateValues['name'] = $formData['name'];
}
return $this->commentsModel->save($updateValues, $commentId);
return $this->commentsModel->save($updateValues, $id);
},
'acp/comments/details/index/id_' . $moduleId
'acp/comments/details/index/id_' . $comment['module_id']
);
}
}
......@@ -54,15 +54,13 @@ class Settings extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$settings = $this->config->getSettings(Comments\Installer\Schema::MODULE_NAME);
// Emoticons erlauben
if ($this->modules->isActive('emoticons') === true) {
$this->view->assign('allow_emoticons', $this->formsHelper->yesNoCheckboxGenerator('emoticons', $settings['emoticons']));
$this->view->assign(
'allow_emoticons',
$this->formsHelper->yesNoCheckboxGenerator('emoticons', $settings['emoticons'])
);
}
return [
......@@ -72,13 +70,12 @@ class Settings extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () use ($formData) {
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->adminSettingsFormValidation->validate($formData);
$data = [
......
......@@ -54,28 +54,27 @@ class Create extends AbstractFrontendAction
*/
public function execute($module, $entryId, $redirectUrl)
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all(), $module, $entryId, $redirectUrl);
}
return [
'form' => array_merge($this->fetchFormDefaults($redirectUrl), $this->request->getPost()->all()),
'form' => array_merge($this->fetchFormDefaults(), $this->request->getPost()->all()),
'module' => $module,
'entry_id' => $entryId,
'redirect_url' => $redirectUrl,
'form_token' => $this->formTokenHelper->renderFormToken(),
'can_use_emoticons' => $this->emoticonsActive === true
];
}
/**
* @param array $formData
* @param string $module
* @param int $entryId
* @param string $redirectUrl
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData, $module, $entryId, $redirectUrl)
public function executePost($module, $entryId, $redirectUrl)
{
return $this->actionHelper->handlePostAction(
function () use ($formData, $module, $entryId, $redirectUrl) {
function () use ($module, $entryId, $redirectUrl) {
$formData = $this->request->getPost()->all();
$ipAddress = $this->request->getSymfonyRequest()->getClientIp();
$this->formValidation
......
<br/>
{if isset($error_msg)}
{if !empty($error_msg)}
{$error_msg}
{/if}
<form action="{$REQUEST_URI}#comments" method="post" accept-charset="UTF-8" class="form-horizontal" data-ajax-form="true" data-ajax-form-loading-text="{lang t="system|loading_please_wait"}">
{include file="asset:System/Partials/form_group.input_text.tpl" name="name" value=$form.name required=true maxlength=20 readonly=$form.name_disabled label={lang t="system|name"}}
{if $can_use_emoticons}
{$before_textarea={event name="emoticons.render_emoticons_list"}}
{else}
{$before_textarea=''}
{/if}
{include file="asset:System/Partials/form_group.textarea.tpl" name="message" value=$form.message required=true label={lang t="system|message"} before_textarea=$before_textarea}
{event name="captcha.event.display_captcha"}
{include file="asset:System/Partials/form_group.submit.tpl" form_token=$form_token}
</form>
{javascripts}
{else}
<form action="{$REQUEST_URI}#comments"
method="post"
accept-charset="UTF-8"
class="form-horizontal"
data-ajax-form="true"
data-ajax-form-loading-text="{lang t="system|loading_please_wait"}">
{include file="asset:System/Partials/form_group.input_text.tpl" name="name" value=$form.name required=true maxlength=20 readonly=$form.name_disabled label={lang t="system|name"}}
{if $can_use_emoticons}
{$before_textarea={event name="emoticons.render_emoticons_list"}}
{else}
{$before_textarea=''}
{/if}
{include file="asset:System/Partials/form_group.textarea.tpl" name="message" value=$form.message required=true label={lang t="system|message"} before_textarea=$before_textarea}
{event name="captcha.event.display_captcha"}
{include file="asset:System/Partials/form_group.submit.tpl" form_token=$form_token}
</form>
{javascripts}
{include_js module="system" file="ajax-form"}
{/javascripts}
{/javascripts}
{/if}
<section>
<section id="comments">
<h3 class="text-center">{lang t="comments|comments"}</h3>
{redirect_message}
{if !empty($comments)}
......
......@@ -54,10 +54,6 @@ class Settings extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$settings = $this->config->getSettings(Contact\Installer\Schema::MODULE_NAME);
return [
......@@ -67,13 +63,12 @@ class Settings extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () use ($formData) {
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->adminSettingsFormValidation->validate($formData);
$data = [
......
......@@ -59,10 +59,6 @@ class Index extends Core\Controller\AbstractFrontendAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$copy = [
1 => $this->translator->t('contact', 'send_copy_to_sender')
];
......@@ -76,14 +72,13 @@ class Index extends Core\Controller\AbstractFrontendAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handlePostAction(
function () use ($formData) {
function () {
$formData = $this->request->getPost()->all();
$this->formValidation->validate($formData);
$bool = $this->contactFormModel->sendContactFormEmail($formData);
......
......@@ -54,10 +54,6 @@ class Create extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
return [
'form' => array_merge(['code' => '', 'description' => ''], $this->request->getPost()->all()),
'form_token' => $this->formTokenHelper->renderFormToken()
......@@ -65,13 +61,12 @@ class Create extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSaveAction(function () use ($formData) {
return $this->actionHelper->handleSaveAction(function () {
$formData = $this->request->getPost()->all();
$file = $this->request->getFiles()->get('picture');
$this->adminFormValidation
......
......@@ -60,10 +60,6 @@ class Edit extends Core\Controller\AbstractAdminAction
$emoticon = $this->emoticonsModel->getOneById($id);
if (empty($emoticon) === false) {
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all(), $emoticon, $id);
}
return [
'form' => array_merge($emoticon, $this->request->getPost()->all()),
'form_token' => $this->formTokenHelper->renderFormToken()
......@@ -74,15 +70,15 @@ class Edit extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
* @param array $emoticon
* @param int $emoticonId
* @param int $id
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData, array $emoticon, $emoticonId)
public function executePost($id)
{
return $this->actionHelper->handleSaveAction(function () use ($formData, $emoticon, $emoticonId) {
return $this->actionHelper->handleSaveAction(function () use ($id) {
$formData = $this->request->getPost()->all();
$emoticon = $this->emoticonsModel->getOneById($id);
$file = $this->request->getFiles()->get('picture');
$this->adminFormValidation
......@@ -97,7 +93,7 @@ class Edit extends Core\Controller\AbstractAdminAction
$formData['img'] = $result['name'];
}
return $this->emoticonsModel->save($formData, $emoticonId);
return $this->emoticonsModel->save($formData, $id);
});
}
}
......@@ -47,10 +47,6 @@ class Settings extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
return [
'form' => array_merge(
$this->config->getSettings(Emoticons\Installer\Schema::MODULE_NAME),
......@@ -61,13 +57,12 @@ class Settings extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () use ($formData) {
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->adminSettingsFormValidation->validate($formData);
$data = [
......
......@@ -59,10 +59,6 @@ class Settings extends Core\Controller\AbstractAdminAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$settings = $this->config->getSettings(Feeds\Installer\Schema::MODULE_NAME);
$feedTypes = [
......@@ -79,13 +75,12 @@ class Settings extends Core\Controller\AbstractAdminAction
}
/**
* @param array $formData
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function executePost(array $formData)
public function executePost()
{
return $this->actionHelper->handleSettingsPostAction(function () use ($formData) {
return $this->actionHelper->handleSettingsPostAction(function () {
$formData = $this->request->getPost()->all();
$this->adminFormValidation->validate($formData);
$data = [
......
......@@ -70,10 +70,6 @@ class Create extends AbstractFormAction
*/
public function execute()
{
if ($this->request->getPost()->count() !== 0) {
return $this->executePost($this->request->getPost()->all());
}
$defaults = [
'title' => '',