Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sobreescribir opciones default con lo guardado en BBDD #1654

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Core/Controller/EditPageOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
17 changes: 4 additions & 13 deletions Core/Lib/ExtendedController/BaseView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions Core/Lib/Widget/VisualItemLoadEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}
}
}
}