Commit c6bdc7c2 authored by Tino Goratsch's avatar Tino Goratsch

minor code optimizations + fixes

parent 2ac3e53d
......@@ -31,7 +31,7 @@ class Alerts
}
/**
* Displays a confirm box
* Displays a confirmation box
*
* @param string $text
* @param string|array $forward
......@@ -60,7 +60,7 @@ 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 array $data
......@@ -88,6 +88,30 @@ class Alerts
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
*/
......@@ -95,20 +119,14 @@ class Alerts
{
$hasNonIntegerKeys = false;
if (is_string($errors) && ($data = @unserialize($errors)) !== false) {
$errors = $data;
}
$errors = $this->prepareErrorBoxData($errors);
if (is_array($errors) === true) {
foreach (array_keys($errors) as $key) {
if (is_numeric($key) === false) {
$hasNonIntegerKeys = true;
break;
}
}
} else {
$errors = (array)$errors;
}
$this->view->assign(
'error_box',
......@@ -120,32 +138,19 @@ class Alerts
}
/**
* Gibt eine Box mit den aufgetretenen Fehlern aus
*
* @param string|array $errors
* @param bool $contentOnly
*
* @return string
* @return array
*/
public function errorBox($errors, $contentOnly = true)
protected function prepareErrorBoxData($errors)
{
if ($this->request->isAjax() === true) {
$contentOnly = true;
if (is_string($errors) && ($data = @unserialize($errors)) !== false) {
$errors = $data;
}
$this->view->assign('CONTENT_ONLY', $contentOnly);
return $this->view->fetchTemplate($this->errorBoxContent($errors));
if (is_array($errors) === false) {
$errors = (array)$errors;
}
/**
* @param string|array $errors
*
* @return string
*/
public function errorBoxContent($errors)
{
$this->setErrorBoxData($errors);
return 'system/alerts/error_box.tpl';
return $errors;
}
}
......@@ -126,7 +126,7 @@ class Action
$result = $callback($items);
if (is_string($result) === false) {
return $this->setRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl);
return $this->prepareRedirectMessageAfterPost($result, 'delete', $moduleIndexUrl);
}
},
$moduleConfirmUrl,
......@@ -151,7 +151,8 @@ class Action
$moduleConfirmUrl = null,
$moduleIndexUrl = null
) {
list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl, $moduleIndexUrl);
list($moduleConfirmUrl, $moduleIndexUrl) = $this->generateDefaultConfirmationBoxUris($moduleConfirmUrl,
$moduleIndexUrl);
$result = $this->deleteItem($action, $moduleConfirmUrl, $moduleIndexUrl);
if (is_string($result)) {
......@@ -174,7 +175,7 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
return $this->setRedirectMessageAfterPost($result, 'settings', $path);
return $this->prepareRedirectMessageAfterPost($result, 'settings', $path);
}, $path);
}
......@@ -189,7 +190,7 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
return $this->setRedirectMessageAfterPost($result, 'create', $path);
return $this->prepareRedirectMessageAfterPost($result, 'create', $path);
});
}
......@@ -204,7 +205,7 @@ class Action
return $this->handlePostAction(function () use ($callback, $path) {
$result = $callback();
return $this->setRedirectMessageAfterPost($result, 'edit', $path);
return $this->prepareRedirectMessageAfterPost($result, 'edit', $path);
});
}
......@@ -215,7 +216,7 @@ class Action
*
* @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(
$result,
......@@ -244,7 +245,7 @@ class Action
}
/**
* Little helper function for deleting an result set
* helper function for deleting a result set
*
* @param string $action
* @param string|null $moduleConfirmUrl
......@@ -254,11 +255,7 @@ class Action
*/
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');
}
$entries = $this->prepareRequestData();
if (empty($entries)) {
return $this->alerts->errorBoxContent($this->translator->t('system', 'no_entries_selected'));
......@@ -273,29 +270,43 @@ class Action
];
return $this->alerts->confirmBoxPost(
$this->fetchConfirmationBoxText($entries),
$this->prepareConfirmationBoxText($entries),
$data,
$this->router->route($moduleConfirmUrl),
$this->router->route($moduleIndexUrl)
);
} else {
}
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
*
* @return mixed|string
* @return string
*/
private function fetchConfirmationBoxText($entries)
private function prepareConfirmationBoxText(array $entries)
{
$entriesCount = count($entries);
if ($entriesCount === 1) {
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 ($) {
$elem.closest('div.form-group').addClass('has-error');
// 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) {
$elem.closest('div').append('<small class="help-block validation-failed"><i class="glyphicon glyphicon-remove"></i> ' + $this.html() + '</small>');
$this.remove();
......@@ -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) {
$errorBox.remove();
}
......
......@@ -12,7 +12,7 @@ class Alerts extends \ACP3\Core\Helpers\Alerts
/**
* @inheritdoc
*/
public function errorBox($errors, $contentOnly = true)
public function errorBox($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