Commit c6bdc7c2 authored by Tino Goratsch's avatar Tino Goratsch

minor code optimizations + fixes

parent 2ac3e53d
...@@ -20,7 +20,7 @@ class Alerts ...@@ -20,7 +20,7 @@ class Alerts
/** /**
* @param \ACP3\Core\Http\RequestInterface $request * @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\View $view * @param \ACP3\Core\View $view
*/ */
public function __construct( public function __construct(
Core\Http\RequestInterface $request, Core\Http\RequestInterface $request,
...@@ -31,12 +31,12 @@ class Alerts ...@@ -31,12 +31,12 @@ class Alerts
} }
/** /**
* Displays a confirm box * Displays a confirmation box
* *
* @param string $text * @param string $text
* @param string|array $forward * @param string|array $forward
* @param string $backward * @param string $backward
* @param integer $overlay * @param integer $overlay
* *
* @return string * @return string
*/ */
...@@ -60,10 +60,10 @@ class Alerts ...@@ -60,10 +60,10 @@ class Alerts
} }
/** /**
* Displays a confirm box, where the forward button triggers a form submit using POST * Displays a confirmation box, where the forward button triggers a form submit using POST
* *
* @param string $text * @param string $text
* @param array $data * @param array $data
* @param string $forward * @param string $forward
* @param string $backward * @param string $backward
* *
...@@ -88,6 +88,30 @@ class Alerts ...@@ -88,6 +88,30 @@ class Alerts
return ''; return '';
} }
/**
* Returns the pretty printed form errors
*
* @param string|array $errors
* @return string
*/
public function errorBox($errors)
{
$this->view->assign('CONTENT_ONLY', $this->request->isAjax() === true);
return $this->view->fetchTemplate($this->errorBoxContent($errors));
}
/**
* @param string|array $errors
*
* @return string
*/
public function errorBoxContent($errors)
{
$this->setErrorBoxData($errors);
return 'system/alerts/error_box.tpl';
}
/** /**
* @param string|array $errors * @param string|array $errors
*/ */
...@@ -95,19 +119,13 @@ class Alerts ...@@ -95,19 +119,13 @@ class Alerts
{ {
$hasNonIntegerKeys = false; $hasNonIntegerKeys = false;
if (is_string($errors) && ($data = @unserialize($errors)) !== false) { $errors = $this->prepareErrorBoxData($errors);
$errors = $data;
}
if (is_array($errors) === true) { foreach (array_keys($errors) as $key) {
foreach (array_keys($errors) as $key) { if (is_numeric($key) === false) {
if (is_numeric($key) === false) { $hasNonIntegerKeys = true;
$hasNonIntegerKeys = true; break;
break;
}
} }
} else {
$errors = (array)$errors;
} }
$this->view->assign( $this->view->assign(
...@@ -120,32 +138,19 @@ class Alerts ...@@ -120,32 +138,19 @@ class Alerts
} }
/** /**
* Gibt eine Box mit den aufgetretenen Fehlern aus
*
* @param string|array $errors * @param string|array $errors
* @param bool $contentOnly * @return array
*
* @return string
*/ */
public function errorBox($errors, $contentOnly = true) protected function prepareErrorBoxData($errors)
{ {
if ($this->request->isAjax() === true) { if (is_string($errors) && ($data = @unserialize($errors)) !== false) {
$contentOnly = true; $errors = $data;
} }
$this->view->assign('CONTENT_ONLY', $contentOnly); if (is_array($errors) === false) {
return $this->view->fetchTemplate($this->errorBoxContent($errors)); $errors = (array)$errors;
} }
/** return $errors;
* @param string|array $errors
*
* @return string
*/
public function errorBoxContent($errors)
{
$this->setErrorBoxData($errors);
return 'system/alerts/error_box.tpl';
} }
} }
...@@ -44,11 +44,11 @@ class Action ...@@ -44,11 +44,11 @@ class Action
/** /**
* Action constructor. * Action constructor.
* *
* @param \ACP3\Core\I18n\Translator $translator * @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request * @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router * @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\View $view * @param \ACP3\Core\View $view
* @param \ACP3\Core\Helpers\Alerts $alerts * @param \ACP3\Core\Helpers\Alerts $alerts
* @param \ACP3\Core\Helpers\RedirectMessages $redirectMessages * @param \ACP3\Core\Helpers\RedirectMessages $redirectMessages
*/ */
public function __construct( public function __construct(
...@@ -68,7 +68,7 @@ class Action ...@@ -68,7 +68,7 @@ class Action
} }
/** /**
* @param callable $callback * @param callable $callback
* @param null|string $path * @param null|string $path
* *
* @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
...@@ -104,10 +104,10 @@ class Action ...@@ -104,10 +104,10 @@ class Action
/** /**
* @param \ACP3\Core\Controller\FrontendAction $context * @param \ACP3\Core\Controller\FrontendAction $context
* @param string $action * @param string $action
* @param callable $callback * @param callable $callback
* @param string|null $moduleConfirmUrl * @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl * @param string|null $moduleIndexUrl
* *
* @return array|string|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|void * @return array|string|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|void
* @throws \ACP3\Core\Exceptions\ResultNotExists * @throws \ACP3\Core\Exceptions\ResultNotExists
...@@ -126,7 +126,7 @@ class Action ...@@ -126,7 +126,7 @@ class Action
$result = $callback($items); $result = $callback($items);
if (is_string($result) === false) { if (is_string($result) === false) {
return $this->setRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl); return $this->prepareRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl);
} }
}, },
$moduleConfirmUrl, $moduleConfirmUrl,
...@@ -136,10 +136,10 @@ class Action ...@@ -136,10 +136,10 @@ class Action
/** /**
* @param \ACP3\Core\Controller\FrontendAction $context * @param \ACP3\Core\Controller\FrontendAction $context
* @param string $action * @param string $action
* @param callable $callback * @param callable $callback
* @param string|null $moduleConfirmUrl * @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl * @param string|null $moduleIndexUrl
* *
* @return void|string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return void|string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
* @throws \ACP3\Core\Exceptions\ResultNotExists * @throws \ACP3\Core\Exceptions\ResultNotExists
...@@ -151,7 +151,8 @@ class Action ...@@ -151,7 +151,8 @@ class Action
$moduleConfirmUrl = null, $moduleConfirmUrl = null,
$moduleIndexUrl = null $moduleIndexUrl = null
) { ) {
list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl); list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl,
$moduleIndexUrl);
$result = $this->deleteItem($action, $moduleConfirmUrl, $moduleIndexUrl); $result = $this->deleteItem($action, $moduleConfirmUrl, $moduleIndexUrl);
if (is_string($result)) { if (is_string($result)) {
...@@ -164,7 +165,7 @@ class Action ...@@ -164,7 +165,7 @@ class Action
} }
/** /**
* @param callable $callback * @param callable $callback
* @param null|string $path * @param null|string $path
* *
* @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
...@@ -174,12 +175,12 @@ class Action ...@@ -174,12 +175,12 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) { return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback(); $result = $callback();
return $this->setRedirectMessageAfterPost($result, 'settings', $path); return $this->prepareRedirectMessageAfterPost($result, 'settings', $path);
}, $path); }, $path);
} }
/** /**
* @param callable $callback * @param callable $callback
* @param null|string $path * @param null|string $path
* *
* @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
...@@ -189,12 +190,12 @@ class Action ...@@ -189,12 +190,12 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) { return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback(); $result = $callback();
return $this->setRedirectMessageAfterPost($result, 'create', $path); return $this->prepareRedirectMessageAfterPost($result, 'create', $path);
}); });
} }
/** /**
* @param callable $callback * @param callable $callback
* @param null|string $path * @param null|string $path
* *
* @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return string|array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
...@@ -204,18 +205,18 @@ class Action ...@@ -204,18 +205,18 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) { return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback(); $result = $callback();
return $this->setRedirectMessageAfterPost($result, 'edit', $path); return $this->prepareRedirectMessageAfterPost($result, 'edit', $path);
}); });
} }
/** /**
* @param bool|int $result * @param bool|int $result
* @param string $localization * @param string $localization
* @param null|string $path * @param null|string $path
* *
* @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
*/ */
private function setRedirectMessageAfterPost($result, $localization, $path = null) private function prepareRedirectMessageAfterPost($result, $localization, $path = null)
{ {
return $this->redirectMessages->setMessage( return $this->redirectMessages->setMessage(
$result, $result,
...@@ -244,9 +245,9 @@ class Action ...@@ -244,9 +245,9 @@ class Action
} }
/** /**
* Little helper function for deleting an result set * helper function for deleting a result set
* *
* @param string $action * @param string $action
* @param string|null $moduleConfirmUrl * @param string|null $moduleConfirmUrl
* @param string|null $moduleIndexUrl * @param string|null $moduleIndexUrl
* *
...@@ -254,11 +255,7 @@ class Action ...@@ -254,11 +255,7 @@ class Action
*/ */
private function deleteItem($action, $moduleConfirmUrl = null, $moduleIndexUrl = null) private function deleteItem($action, $moduleConfirmUrl = null, $moduleIndexUrl = null)
{ {
if (is_array($this->request->getPost()->get('entries')) === true) { $entries = $this->prepareRequestData();
$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)) { if (empty($entries)) {
return $this->alerts->errorBoxContent($this->translator->t('system', 'no_entries_selected')); return $this->alerts->errorBoxContent($this->translator->t('system', 'no_entries_selected'));
...@@ -273,29 +270,43 @@ class Action ...@@ -273,29 +270,43 @@ class Action
]; ];
return $this->alerts->confirmBoxPost( return $this->alerts->confirmBoxPost(
$this->fetchConfirmationBoxText($entries), $this->prepareConfirmationBoxText($entries),
$data, $data,
$this->router->route($moduleConfirmUrl), $this->router->route($moduleConfirmUrl),
$this->router->route($moduleIndexUrl) $this->router->route($moduleIndexUrl)
); );
} else {
return is_array($entries) ? $entries : explode('|', $entries);
} }
return is_array($entries) ? $entries : explode('|', $entries);
}
/**
* @return mixed
*/
private function prepareRequestData()
{
$entries = 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');
}
return $entries;
} }
/** /**
* @param array $entries * @param array $entries
* *
* @return mixed|string * @return string
*/ */
private function fetchConfirmationBoxText($entries) private function prepareConfirmationBoxText(array $entries)
{ {
$entriesCount = count($entries); $entriesCount = count($entries);
if ($entriesCount === 1) { if ($entriesCount === 1) {
return $this->translator->t('system', 'confirm_delete_single'); return $this->translator->t('system', 'confirm_delete_single');
} }
return str_replace('{items}', $entriesCount, $this->translator->t('system', 'confirm_delete_multiple')); return $this->translator->t('system', 'confirm_delete_multiple', ['{items}' => $entriesCount]);
} }
} }
...@@ -17,7 +17,7 @@ jQuery(document).ready(function ($) { ...@@ -17,7 +17,7 @@ jQuery(document).ready(function ($) {
$elem.closest('div.form-group').addClass('has-error'); $elem.closest('div.form-group').addClass('has-error');
// Move the error message to the responsible input field(s) // Move the error message to the responsible input field(s)
// and remove the list items for the error box container // and remove the list item from the error box container
if ($elem.length == 1) { if ($elem.length == 1) {
$elem.closest('div').append('<small class="help-block validation-failed"><i class="glyphicon glyphicon-remove"></i> ' + $this.html() + '</small>'); $elem.closest('div').append('<small class="help-block validation-failed"><i class="glyphicon glyphicon-remove"></i> ' + $this.html() + '</small>');
$this.remove(); $this.remove();
...@@ -26,7 +26,7 @@ jQuery(document).ready(function ($) { ...@@ -26,7 +26,7 @@ jQuery(document).ready(function ($) {
} }
}); });
// if all list items have been removes, remove the error box container too // if all list items have been removed, remove the error box container too
if ($errorBox.find('li').length == 0) { if ($errorBox.find('li').length == 0) {
$errorBox.remove(); $errorBox.remove();
} }
...@@ -36,4 +36,4 @@ jQuery(document).ready(function ($) { ...@@ -36,4 +36,4 @@ jQuery(document).ready(function ($) {
var tabId = $('.tabbable .form-group.has-error:first').closest('.tab-pane').prop('id'); var tabId = $('.tabbable .form-group.has-error:first').closest('.tab-pane').prop('id');
$('.tabbable .nav-tabs a[href="#' + tabId + '"]').tab('show'); $('.tabbable .nav-tabs a[href="#' + tabId + '"]').tab('show');
} }
}); });
\ No newline at end of file
...@@ -12,7 +12,7 @@ class Alerts extends \ACP3\Core\Helpers\Alerts ...@@ -12,7 +12,7 @@ class Alerts extends \ACP3\Core\Helpers\Alerts
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function errorBox($errors, $contentOnly = true) public function errorBox($errors)
{ {
$this->setErrorBoxData($errors); $this->setErrorBoxData($errors);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment