Skip to content
Snippets Groups Projects

journal observer

Merged Patrick Barroca requested to merge sandbox-journal-observer into WIP
Files
11
+ 75
0
<?php
/**
* Copyright (c) 2012-2020, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_AdminVar_JournalStormModels {
use Trait_Translator;
const
MODEL_KEY = 'model',
IDS_KEY = 'ids',
IDS_SEPARATOR = ';';
public function modelsMultioptions() {
return ['' => $this->_(''),
Class_AdminVar::class => $this->_('Variables'),
Class_Profil::class => $this->_('Profils')];
}
public function defaultValue() {
return json_encode([static::MODEL_KEY => [Class_AdminVar::class],
static::IDS_KEY => ['']]);
}
public function isEnabled() {
return ($map = $this->_loadMapping())
? array_filter(array_keys($map))
: false;
}
public function modelMatch($model) {
if (!$map = $this->_loadMapping())
return false;
if (!array_key_exists(get_class($model), $map))
return false;
$ids = array_filter(explode(static::IDS_SEPARATOR, $map[get_class($model)]));
return !$ids || in_array($model->getId(), $ids);
}
protected function _loadMapping() {
if (!$conf = json_decode(Class_AdminVar::getValueOrDefault('JOURNAL_STORM_MODELS'), true))
return [];
if (!isset($conf[static::MODEL_KEY]) || !isset($conf[static::IDS_KEY]))
return [];
return ($map = array_combine($conf[static::MODEL_KEY], $conf[static::IDS_KEY]))
? $map
: [];
}
}