diff --git a/Core/Controller/EditPageOption.php b/Core/Controller/EditPageOption.php index c5780f08c..12c05b568 100644 --- a/Core/Controller/EditPageOption.php +++ b/Core/Controller/EditPageOption.php @@ -166,15 +166,12 @@ protected function deleteAction() */ protected function loadPageOptions() { - if ($this->selectedUser && false === $this->loadPageOptionsForUser()) { - VisualItemLoadEngine::installXML($this->selectedViewName, $this->model); + $user = new User(); + if(false === $user->loadFromCode($this->selectedUser)){ + $user = false; } - if (empty($this->selectedUser) && false === $this->loadPageOptionsForAll()) { - VisualItemLoadEngine::installXML($this->selectedViewName, $this->model); - } - - VisualItemLoadEngine::loadArray($this->columns, $this->modals, $this->rows, $this->model); + VisualItemLoadEngine::loadPageOptions($this->selectedViewName, $this->model, $this->columns, $this->modals, $this->rows, $user); } protected function loadSelectedViewName() diff --git a/Core/Lib/ExtendedController/BaseView.php b/Core/Lib/ExtendedController/BaseView.php index f62ecce9e..1f8212131 100644 --- a/Core/Lib/ExtendedController/BaseView.php +++ b/Core/Lib/ExtendedController/BaseView.php @@ -353,21 +353,12 @@ public function loadFromData(array &$data) */ public function loadPageOptions($user = false) { - if (false === is_bool($user)) { - // sets user security level for use in render - VisualItem::setLevel($user->level); - } + $viewName = explode('-', $this->name)[0]; - $orderBy = ['nick' => 'ASC']; - $where = $this->getPageWhere($user); - if ($this->pageOption->loadFromCode('', $where, $orderBy)) { - $this->settings['customized'] = true; - } else { - $viewName = explode('-', $this->name)[0]; - VisualItemLoadEngine::installXML($viewName, $this->pageOption); - } + $isCustomized = false; + VisualItemLoadEngine::loadPageOptions($viewName, $this->pageOption, $this->columns, $this->modals, $this->rows, $user, $isCustomized); - VisualItemLoadEngine::loadArray($this->columns, $this->modals, $this->rows, $this->pageOption); + $this->settings['customized'] = $isCustomized; } public function setSettings(string $key, $value): BaseView diff --git a/Core/Lib/Widget/VisualItemLoadEngine.php b/Core/Lib/Widget/VisualItemLoadEngine.php index 3e09cce25..8393c914e 100644 --- a/Core/Lib/Widget/VisualItemLoadEngine.php +++ b/Core/Lib/Widget/VisualItemLoadEngine.php @@ -19,9 +19,12 @@ namespace FacturaScripts\Core\Lib\Widget; +use FacturaScripts\Core\Base\DataBase\DataBaseWhere; use FacturaScripts\Core\Base\MiniLog; use FacturaScripts\Core\Model; use FacturaScripts\Core\Translator; +use FacturaScripts\Dinamic\Model\PageOption; +use FacturaScripts\Dinamic\Model\User; use SimpleXMLElement; /** @@ -229,4 +232,73 @@ private static function xmlToArrayAux($tag, $attributes): string return ''; } + + /** + * @param string $viewName + * @param PageOption $model + * @param array $columns + * @param array $modals + * @param array $rows + * @param User|false $user + * @param bool $isCustomized + * + * @return void + */ + public static function loadPageOptions(string $viewName, PageOption &$model, array &$columns, array &$modals, array &$rows, $user = false, bool &$isCustomized = false) + { + if (false === is_bool($user)) { + // sets user security level for use in render + VisualItem::setLevel($user->level); + $model->nick = $user->nick; + } + + // Cargamos las opciones desde los xml + self::installXML($viewName, $model); + + // Cargamos los opciones personalizadas desde la base de datos + $customPageOptions = new PageOption(); + $orderBy = ['nick' => 'ASC']; + $where = self::getPageWhere($viewName, $user); + + // Si existen opciones personalizadas guardadas, sobreescribimos las opciones del XML + if ($customPageOptions->loadFromCode('', $where, $orderBy)) { + $model->id = $customPageOptions->id; + $model->nick = $customPageOptions->nick; + self::updateOptions($model->columns, $customPageOptions->columns); + self::updateOptions($model->rows, $customPageOptions->rows); + self::updateOptions($model->modals, $customPageOptions->modals); + + $isCustomized = true; + } + + self::loadArray($columns, $modals, $rows, $model); + } + + /** + * Returns DataBaseWhere[] for locate a pageOption model. + * + * @param User|false $user + */ + protected static function getPageWhere($viewName, $user = false) + { + return is_bool($user) ? [new DataBaseWhere('name', $viewName)] : [ + new DataBaseWhere('name', $viewName), + new DataBaseWhere('nick', $user->nick) + ]; + } + + /** + * Actualiza las opciones de columnas o filas con los valores personalizados. + * + * @param array $target Las opciones de destino (columnas o filas). + * @param array $source Las opciones personalizadas. + */ + private static function updateOptions(array &$target, array $source) + { + foreach ($source as $key => $value) { + if (!empty($value)) { + $target[$key] = $value; + } + } + } }