Commit 15ca0bd2 authored by Tino Goratsch's avatar Tino Goratsch

minor refactorings for the Date helpers class

parent 0481f9d3
......@@ -88,11 +88,11 @@ class Date
}
/**
* Gibts ein Array mit den mglichen Datumsformaten aus,
* um diese als Dropdownmen darstellen zu knnen
* Gibts ein Array mit den mglichen Datumsformaten aus,
* um diese als Dropdownmen� darstellen zu k�nnen
*
* @param string $format
* Optionaler Parameter fr das aktuelle Datumsformat
* Optionaler Parameter fr das aktuelle Datumsformat
*
* @return array
*/
......@@ -106,21 +106,14 @@ class Date
}
/**
* Zeigt Dropdown-Mens fr die Verffentlichungsdauer von Inhalten an
* Displays an input field with an associated datepicker
*
* @param mixed $name
* Name des jeweiligen Inputfeldes
* @param mixed $value
* Der Zeitstempel des jeweiligen Eintrages
* @param string $format
* Das anzuzeigende Format im Textfeld
* @param array $params
* Dient dem Festlegen von weiteren Parametern
* @param integer $isRange
* 1 = Start- und Enddatum anzeigen
* 2 = Einfaches Inputfeld mitsamt Datepicker anzeigen
* @param bool|int $withTime
* @param bool $inputFieldOnly
* @param string|array $name
* @param string|array $value
* @param string $format
* @param array $params
* @param bool $withTime
* @param bool $inputFieldOnly
*
* @return array
*/
......@@ -129,39 +122,31 @@ class Date
$value = '',
$format = 'Y-m-d H:i',
array $params = [],
$isRange = 1,
$withTime = true,
$inputFieldOnly = false
)
{
$isRange = (is_array($name) === true && $isRange === 1);
$datePicker = [
'range' => $isRange,
'range' => $this->isRange($name),
'with_time' => (bool)$withTime,
'length' => $withTime === true ? 16 : 10,
'input_only' => (bool)$inputFieldOnly,
'params' => [
'format' => 'YYYY-MM-DD',
'format' => $this->getPickerDateFormat($withTime),
'changeMonth' => 'true',
'changeYear' => 'true',
]
];
if ($withTime === true) {
$datePicker['params']['format'] .= ' HH:mm';
}
// Zustzliche Datepicker-Parameter hinzufgen
if (!empty($params) && is_array($params) === true) {
if (!empty($params)) {
$datePicker['params'] = array_merge($datePicker['params'], $params);
}
// Verffentlichungszeitraum
if ($isRange === true) {
if ($this->isRange($name) === true) {
$datePicker['name_start'] = $name[0];
$datePicker['name_end'] = $name[1];
$datePicker['id_start'] = 'date-' . str_replace('_', '-', $name[0]);
$datePicker['id_end'] = 'date-' . str_replace('_', '-', $name[1]);
$datePicker['id_start'] = $this->getInputId($name[0]);
$datePicker['id_end'] = $this->getInputId($name[1]);
$datePicker = array_merge($datePicker, $this->fetchRangeDatePickerValues($name, $value, $format));
......@@ -175,13 +160,23 @@ class Date
);
} else { // Einfaches Inputfeld mit Datepicker
$datePicker['name'] = $name;
$datePicker['id'] = 'date-' . str_replace('_', '-', $name);
$datePicker['id'] = $this->getInputId($name);
$datePicker['value'] = $this->fetchSimpleDatePickerValue($name, $value, $format);
}
return $datePicker;
}
/**
* @param string $fieldName
*
* @return string
*/
protected function getInputId($fieldName)
{
return 'date-' . str_replace('_', '-', $fieldName);
}
/**
* @param array $name
* @param array $value
......@@ -189,14 +184,14 @@ class Date
*
* @return array
*/
protected function fetchRangeDatePickerValues($name, $value, $format)
protected function fetchRangeDatePickerValues($name, array $value, $format)
{
if ($this->request->getPost()->has($name[0]) && $this->request->getPost()->has($name[1])) {
$valueStart = $this->request->getPost()->get($name[0]);
$valueEnd = $this->request->getPost()->get($name[1]);
$valueStartR = $this->date->format($valueStart, 'r', false);
$valueEndR = $this->date->format($valueEnd, 'r', false);
} elseif (is_array($value) === true && $this->dateValidator->date($value[0], $value[1]) === true) {
} elseif ($this->dateValidator->date($value[0], $value[1]) === true) {
$valueStart = $this->date->format($value[0], $format);
$valueEnd = $this->date->format($value[1], $format);
$valueStartR = $this->date->format($value[0], 'r');
......@@ -234,4 +229,24 @@ class Date
return $this->date->format('now', $format, false);
}
/**
* @param bool $withTime
*
* @return string
*/
protected function getPickerDateFormat($withTime)
{
return 'YYYY-MM-DD' . ($withTime === true ? ' HH:mm' : '');
}
/**
* @param string|array $name
*
* @return bool
*/
protected function isRange($name)
{
return (is_array($name) === true);
}
}
\ No newline at end of file
......@@ -112,13 +112,10 @@ class Index extends Core\Modules\AdminController
$this->_createPost($this->request->getPost()->getAll());
}
if ($this->acl->hasPermission('admin/menus/items/create') === true) {
if ($this->acl->hasPermission('admin/menus/items/create') === true && $this->menusHelpers) {
$lang_options = [$this->lang->t('articles', 'create_menu_item')];
$this->view->assign('options', $this->get('core.helpers.forms')->selectGenerator('create', [1], $lang_options, 0, 'checked'));
if ($this->menusHelpers) {
$this->view->assign($this->menusHelpers->createMenuItemFormFields());
}
$this->view->assign($this->menusHelpers->createMenuItemFormFields());
}
$this->view->assign('publication_period', $this->get('core.helpers.date')->datepicker(['start', 'end']));
......@@ -223,7 +220,7 @@ class Index extends Core\Modules\AdminController
$this->menusHelpers &&
$this->menusModel
) {
$menuItem = $this->menusModel->getOneMenuItemUri(sprintf(Articles\Helpers::URL_KEY_PATTERN, $id));
$menuItem = $this->menusModel->getOneMenuItemByUri(sprintf(Articles\Helpers::URL_KEY_PATTERN, $id));
$lang_options = [$this->lang->t('articles', 'create_menu_item')];
$this->view->assign('options', $this->get('core.helpers.forms')->selectGenerator('create', [1], $lang_options, !empty($menuItem) ? 1 : 0, 'checked'));
......
<?phpnamespace ACP3\Modules\ACP3\Menus; use ACP3\Core; /** * Class Helpers * @package ACP3\Modules\ACP3\Menus */class Helpers{ const ARTICLES_URL_KEY_REGEX = '/^(articles\/index\/details\/id_([0-9]+)\/)$/'; /** * @var array */ protected $menuItems = []; /** * @var array */ protected $navbar = []; /** * @var \ACP3\Core\Lang */ protected $lang; /** * @var \ACP3\Core\NestedSet */ protected $nestedSet; /** * @var \ACP3\Core\Helpers\Forms */ protected $formsHelper; /** * @var \ACP3\Modules\ACP3\Menus\Model */ protected $menusModel; /** * @var \ACP3\Modules\ACP3\Menus\Cache */ protected $menusCache; /** * @param \ACP3\Core\Lang $lang * @param \ACP3\Core\NestedSet $nestedSet * @param \ACP3\Core\Helpers\Forms $formsHelper * @param \ACP3\Modules\ACP3\Menus\Model $menusModel * @param \ACP3\Modules\ACP3\Menus\Cache $menusCache */ public function __construct( Core\Lang $lang, Core\NestedSet $nestedSet, Core\Helpers\Forms $formsHelper, Model $menusModel, Cache $menusCache ) { $this->lang = $lang; $this->nestedSet = $nestedSet; $this->formsHelper = $formsHelper; $this->menusModel = $menusModel; $this->menusCache = $menusCache; } /** * Auflistung der Seiten * * @param integer $parentId * ID des Elternknotens * @param integer $leftId * @param integer $rightId * * @return array */ public function menuItemsList($parentId = 0, $leftId = 0, $rightId = 0) { // Menüpunkte einbinden if (empty($this->menuItems)) { $this->menuItems = $this->menusCache->getMenusCache(); } $output = []; if (count($this->menuItems) > 0) { foreach ($this->menuItems as $row) { if (!($row['left_id'] >= $leftId && $row['right_id'] <= $rightId)) { $row['selected'] = $this->formsHelper->selectEntry('parent_id', $row['id'], $parentId); $row['spaces'] = str_repeat('&nbsp;&nbsp;', $row['level']); // Titel für den aktuellen Block setzen $output[$row['block_name']]['title'] = $row['block_title']; $output[$row['block_name']]['menu_id'] = $row['block_id']; $output[$row['block_name']]['items'][] = $row; } } } return $output; } /** * Gibt alle Menüleisten zur Benutzung in einem Dropdown-Menü aus * * @param integer $selected * * @return array */ public function menusDropdown($selected = 0) { $menus = $this->menusModel->getAllMenus(); $c_menus = count($menus); for ($i = 0; $i < $c_menus; ++$i) { $menus[$i]['selected'] = $this->formsHelper->selectEntry('block_id', (int)$menus[$i]['id'], (int)$selected); } return $menus; } /** * @param int $blockId * @param int $parentId * @param int $leftId * @param int $rightId * @param int $displayMenuItem * * @return array */ public function createMenuItemFormFields($blockId = 0, $parentId = 0, $leftId = 0, $rightId = 0, $displayMenuItem = 1) { $lang_display = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')]; return [ 'blocks' => $this->menusDropdown($blockId), 'display' => $this->formsHelper->selectGenerator('display', [1, 0], $lang_display, $displayMenuItem, 'checked'), 'menuItems' => $this->menuItemsList($parentId, $leftId, $rightId) ]; } /** * @param string $menuItemUri * @param bool $createOrUpdateMenuItem * @param array $data * * @return bool */ public function manageMenuItem($menuItemUri, $createOrUpdateMenuItem, array $data = []) { $menuItem = $this->menusModel->getOneMenuItemUri($menuItemUri); if ($createOrUpdateMenuItem === true) { // Create a new menu item if (empty($menuItem)) { $insertValues = [ 'id' => '', 'mode' => $data['mode'], 'block_id' => $data['block_id'], 'parent_id' => (int)$data['parent_id'], 'display' => $data['display'], 'title' => Core\Functions::strEncode($data['title']), 'uri' => $menuItemUri, 'target' => $data['target'], ]; return $this->nestedSet->insertNode( (int)$data['parent_id'], $insertValues, Model::TABLE_NAME_ITEMS, true ) !== false; } else { // Update an existing menu item $updateValues = [ 'block_id' => $data['block_id'], 'parent_id' => (int)$data['parent_id'], 'display' => $data['display'], 'title' => Core\Functions::strEncode($data['title']) ]; return $this->nestedSet->editNode( $menuItem['id'], (int)$data['parent_id'], (int)$data['block_id'], $updateValues, Model::TABLE_NAME_ITEMS, true ); } } elseif (!empty($menuItem)) { // Delete an existing menu item return $this->nestedSet->deleteNode( $menuItem['id'], Model::TABLE_NAME_ITEMS, true ); } return true; } }
\ No newline at end of file
<?phpnamespace ACP3\Modules\ACP3\Menus; use ACP3\Core; /** * Class Helpers * @package ACP3\Modules\ACP3\Menus */class Helpers{ const ARTICLES_URL_KEY_REGEX = '/^(articles\/index\/details\/id_([0-9]+)\/)$/'; /** * @var array */ protected $menuItems = []; /** * @var array */ protected $navbar = []; /** * @var \ACP3\Core\Lang */ protected $lang; /** * @var \ACP3\Core\NestedSet */ protected $nestedSet; /** * @var \ACP3\Core\Helpers\Forms */ protected $formsHelper; /** * @var \ACP3\Modules\ACP3\Menus\Model */ protected $menusModel; /** * @var \ACP3\Modules\ACP3\Menus\Cache */ protected $menusCache; /** * @param \ACP3\Core\Lang $lang * @param \ACP3\Core\NestedSet $nestedSet * @param \ACP3\Core\Helpers\Forms $formsHelper * @param \ACP3\Modules\ACP3\Menus\Model $menusModel * @param \ACP3\Modules\ACP3\Menus\Cache $menusCache */ public function __construct( Core\Lang $lang, Core\NestedSet $nestedSet, Core\Helpers\Forms $formsHelper, Model $menusModel, Cache $menusCache ) { $this->lang = $lang; $this->nestedSet = $nestedSet; $this->formsHelper = $formsHelper; $this->menusModel = $menusModel; $this->menusCache = $menusCache; } /** * Auflistung der Seiten * * @param integer $parentId * ID des Elternknotens * @param integer $leftId * @param integer $rightId * * @return array */ public function menuItemsList($parentId = 0, $leftId = 0, $rightId = 0) { // Menüpunkte einbinden if (empty($this->menuItems)) { $this->menuItems = $this->menusCache->getMenusCache(); } $output = []; if (count($this->menuItems) > 0) { foreach ($this->menuItems as $row) { if (!($row['left_id'] >= $leftId && $row['right_id'] <= $rightId)) { $row['selected'] = $this->formsHelper->selectEntry('parent_id', $row['id'], $parentId); $row['spaces'] = str_repeat('&nbsp;&nbsp;', $row['level']); // Titel für den aktuellen Block setzen $output[$row['block_name']]['title'] = $row['block_title']; $output[$row['block_name']]['menu_id'] = $row['block_id']; $output[$row['block_name']]['items'][] = $row; } } } return $output; } /** * Gibt alle Menüleisten zur Benutzung in einem Dropdown-Menü aus * * @param integer $selected * * @return array */ public function menusDropdown($selected = 0) { $menus = $this->menusModel->getAllMenus(); $c_menus = count($menus); for ($i = 0; $i < $c_menus; ++$i) { $menus[$i]['selected'] = $this->formsHelper->selectEntry('block_id', (int)$menus[$i]['id'], (int)$selected); } return $menus; } /** * @param int $blockId * @param int $parentId * @param int $leftId * @param int $rightId * @param int $displayMenuItem * * @return array */ public function createMenuItemFormFields($blockId = 0, $parentId = 0, $leftId = 0, $rightId = 0, $displayMenuItem = 1) { $lang_display = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')]; return [ 'blocks' => $this->menusDropdown($blockId), 'display' => $this->formsHelper->selectGenerator('display', [1, 0], $lang_display, $displayMenuItem, 'checked'), 'menuItems' => $this->menuItemsList($parentId, $leftId, $rightId) ]; } /** * @param string $menuItemUri * @param bool $createOrUpdateMenuItem * @param array $data * * @return bool */ public function manageMenuItem($menuItemUri, $createOrUpdateMenuItem, array $data = []) { $menuItem = $this->menusModel->getOneMenuItemByUri($menuItemUri); if ($createOrUpdateMenuItem === true) { // Create a new menu item if (empty($menuItem)) { $insertValues = [ 'id' => '', 'mode' => $data['mode'], 'block_id' => $data['block_id'], 'parent_id' => (int)$data['parent_id'], 'display' => $data['display'], 'title' => Core\Functions::strEncode($data['title']), 'uri' => $menuItemUri, 'target' => $data['target'], ]; return $this->nestedSet->insertNode( (int)$data['parent_id'], $insertValues, Model::TABLE_NAME_ITEMS, true ) !== false; } else { // Update an existing menu item $updateValues = [ 'block_id' => $data['block_id'], 'parent_id' => (int)$data['parent_id'], 'display' => $data['display'], 'title' => Core\Functions::strEncode($data['title']) ]; return $this->nestedSet->editNode( $menuItem['id'], (int)$data['parent_id'], (int)$data['block_id'], $updateValues, Model::TABLE_NAME_ITEMS, true ); } } elseif (!empty($menuItem)) { // Delete an existing menu item return $this->nestedSet->deleteNode( $menuItem['id'], Model::TABLE_NAME_ITEMS, true ); } return true; } }
\ No newline at end of file
......
......@@ -13,7 +13,7 @@ class Model extends Core\Model
const TABLE_NAME_ITEMS = 'menu_items';
/**
* @param $id
* @param int $id
*
* @return bool
*/
......@@ -23,8 +23,8 @@ class Model extends Core\Model
}
/**
* @param $indexName
* @param int $id
* @param string $indexName
* @param int $id
*
* @return bool
*/
......@@ -35,7 +35,7 @@ class Model extends Core\Model
}
/**
* @param $id
* @param int $id
*
* @return bool
*/
......@@ -45,7 +45,7 @@ class Model extends Core\Model
}
/**
* @param $id
* @param int $id
*
* @return array
*/
......@@ -58,7 +58,7 @@ class Model extends Core\Model
}
/**
* @param $id
* @param int $id
*
* @return array
*/
......@@ -71,11 +71,11 @@ class Model extends Core\Model
}
/**
* @param $uri
* @param string $uri
*
* @return mixed
*/
public function getOneMenuItemUri($uri)
public function getOneMenuItemByUri($uri)
{
return $this->db->fetchAssoc(
"SELECT * FROM {$this->getTableName(static::TABLE_NAME_ITEMS)} WHERE uri = ?",
......@@ -84,7 +84,7 @@ class Model extends Core\Model
}
/**
* @param $blockId
* @param int $blockId
*
* @return array
*/
......@@ -173,7 +173,7 @@ class Model extends Core\Model
}
/**
* @param $blockName
* @param string $blockName
*
* @return array
*/
......@@ -186,13 +186,13 @@ class Model extends Core\Model
}
/**
* @param $menu
* @param $uris
* @param string $menu
* @param array $uris
*
* @return mixed
* @throws \Doctrine\DBAL\DBALException
*/
public function getLeftIdByUris($menu, $uris)
public function getLeftIdByUris($menu, array $uris)
{
return $this->db->executeQuery(
"SELECT m.left_id FROM {$this->getTableName(static::TABLE_NAME_ITEMS)} AS m JOIN {$this->getTableName()} AS b ON(m.block_id = b.id) WHERE b.index_name = ? AND m.uri IN(?) ORDER BY LENGTH(m.uri) DESC",
......@@ -202,7 +202,7 @@ class Model extends Core\Model
}
/**
* @param $in
* @param array $in
*
* @return array
* @throws \Doctrine\DBAL\DBALException
......
......@@ -85,7 +85,7 @@ class Account extends Core\Modules\FrontendController
// Geburtstag
$datepickerParams = ['constrainInput' => 'true', 'changeMonth' => 'true', 'changeYear' => 'true', 'yearRange' => '\'-50:+0\''];
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', $user['birthday'], 'Y-m-d', $datepickerParams, 0, false, true));
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', $user['birthday'], 'Y-m-d', $datepickerParams, false, true));
// Kontaktangaben
$contact = [];
......
......@@ -117,7 +117,7 @@ class Index extends Core\Modules\AdminController
'changeYear' => 'true',
'yearRange' => '\'-50:+0\''
];
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', '', 'Y-m-d', $datepickerParams, 0, false, true));
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', '', 'Y-m-d', $datepickerParams, false, true));
// Kontaktangaben
$this->view->assign('contact', $this->fetchContactDetails());
......@@ -251,7 +251,7 @@ class Index extends Core\Modules\AdminController
// Geburtstag
$datepickerParams = ['constrainInput' => 'true', 'changeMonth' => 'true', 'changeYear' => 'true', 'yearRange' => '\'-50:+0\''];
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', $user['birthday'], 'Y-m-d', $datepickerParams, 0, false, true));
$this->view->assign('birthday_datepicker', $this->get('core.helpers.date')->datepicker('birthday', $user['birthday'], 'Y-m-d', $datepickerParams, false, true));
// Kontaktangaben
$this->view->assign('contact', $this->fetchContactDetails(
......
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