mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-31 01:34:12 +02:00
DbObjectWithSettings: one more abstraction layer
Would be a candidate for traits, unfortunately we still support PHP 5.3
This commit is contained in:
parent
b05973f3a7
commit
4fd0054a59
135
library/Director/Data/Db/DbObjectWithSettings.php
Normal file
135
library/Director/Data/Db/DbObjectWithSettings.php
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Icinga\Module\Director\Data\Db;
|
||||||
|
|
||||||
|
abstract class DbObjectWithSettings extends DbObject
|
||||||
|
{
|
||||||
|
protected $settingsTable = 'your_table_name';
|
||||||
|
|
||||||
|
protected $settingsRemoteId = 'column_pointing_to_main_table_id';
|
||||||
|
|
||||||
|
protected $settings = array();
|
||||||
|
|
||||||
|
public function set($key, $value)
|
||||||
|
{
|
||||||
|
if ($this->hasProperty($key)) {
|
||||||
|
return parent::set($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! array_key_exists($key, $this->settings) || $value !== $this->settings[$key]) {
|
||||||
|
$this->hasBeenModified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->settings[$key] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key)
|
||||||
|
{
|
||||||
|
if ($this->hasProperty($key)) {
|
||||||
|
return parent::get($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($key, $this->settings)) {
|
||||||
|
return $this->settings[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::get($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSettings()
|
||||||
|
{
|
||||||
|
return $this->settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSetting($name, $default = null)
|
||||||
|
{
|
||||||
|
if (array_key_exists($name, $this->settings)) {
|
||||||
|
return $this->settings[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __unset($key)
|
||||||
|
{
|
||||||
|
if ($this->hasProperty($key)) {
|
||||||
|
return parent::__set($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($key, $this->settings)) {
|
||||||
|
unset($this->settings[$key]);
|
||||||
|
$this->hasBeenModified = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function onStore()
|
||||||
|
{
|
||||||
|
$old = $this->fetchSettingsFromDb();
|
||||||
|
$oldKeys = array_keys($old);
|
||||||
|
$newKeys = array_keys($this->settings);
|
||||||
|
$add = array();
|
||||||
|
$mod = array();
|
||||||
|
$del = array();
|
||||||
|
|
||||||
|
foreach ($this->settings as $key => $val) {
|
||||||
|
if (array_key_exists($key, $old)) {
|
||||||
|
if ($old[$key] !== $this->settings[$key]) {
|
||||||
|
$mod[$key] = $this->settings[$key];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$add[$key] = $this->settings[$key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (array_diff(array_keys($old), array_keys($this->settings)) as $key) {
|
||||||
|
$del[$key] = $key;
|
||||||
|
}
|
||||||
|
|
||||||
|
$where = sprintf($this->settingsRemoteId . ' = %d AND setting_name = ?', $this->id);
|
||||||
|
$db = $this->getDb();
|
||||||
|
foreach ($mod as $key => $val) {
|
||||||
|
$db->update(
|
||||||
|
$this->settingsTable,
|
||||||
|
array('setting_value' => $val),
|
||||||
|
$db->quoteInto($where, $key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($add as $key => $val) {
|
||||||
|
$db->insert(
|
||||||
|
$this->settingsTable,
|
||||||
|
array(
|
||||||
|
$this->settingsRemoteId => $this->id,
|
||||||
|
'setting_name' => $key,
|
||||||
|
'setting_value' => $val
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($del as $key) {
|
||||||
|
$db->delete(
|
||||||
|
$this->settingsTable,
|
||||||
|
$db->quoteInto($where, $key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fetchSettingsFromDb()
|
||||||
|
{
|
||||||
|
$db = $this->getDb();
|
||||||
|
return $db->fetchPairs(
|
||||||
|
$db->select()
|
||||||
|
->from($this->settingsTable, array('setting_name', 'setting_value'))
|
||||||
|
->where($this->settingsRemoteId . ' = ?', $this->id)
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function onLoadFromDb()
|
||||||
|
{
|
||||||
|
$this->settings = $this->fetchSettingsFromDb();
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Objects;
|
namespace Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
||||||
|
|
||||||
class DirectorDatafield extends DbObject
|
class DirectorDatafield extends DbObjectWithSettings
|
||||||
{
|
{
|
||||||
protected $table = 'director_datafield';
|
protected $table = 'director_datafield';
|
||||||
|
|
||||||
@ -21,6 +21,11 @@ class DirectorDatafield extends DbObject
|
|||||||
'format' => null,
|
'format' => null,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
protected $settingsTable = 'director_datafield_setting';
|
||||||
|
|
||||||
|
protected $settingsRemoteId = 'datafield_id';
|
||||||
|
|
||||||
|
/*
|
||||||
protected $settings = array();
|
protected $settings = array();
|
||||||
|
|
||||||
public function set($key, $value)
|
public function set($key, $value)
|
||||||
@ -136,4 +141,5 @@ class DirectorDatafield extends DbObject
|
|||||||
{
|
{
|
||||||
$this->settings = $this->fetchSettingsFromDb();
|
$this->settings = $this->fetchSettingsFromDb();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Director\Objects;
|
namespace Icinga\Module\Director\Objects;
|
||||||
|
|
||||||
use Icinga\Module\Director\Data\Db\DbObject;
|
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
|
||||||
|
|
||||||
class ImportSource extends DbObject
|
class ImportSource extends DbObjectWithSettings
|
||||||
{
|
{
|
||||||
protected $table = 'import_source';
|
protected $table = 'import_source';
|
||||||
|
|
||||||
@ -19,113 +19,7 @@ class ImportSource extends DbObject
|
|||||||
'key_column' => null
|
'key_column' => null
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $settings = array();
|
protected $settingsTable = 'import_source_setting';
|
||||||
|
|
||||||
public function set($key, $value)
|
protected $settingsRemoteId = 'source_id';
|
||||||
{
|
|
||||||
if ($this->hasProperty($key)) {
|
|
||||||
return parent::set($key, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (! array_key_exists($key, $this->settings) || $value !== $this->settings[$key]) {
|
|
||||||
$this->hasBeenModified = true;
|
|
||||||
}
|
|
||||||
$this->settings[$key] = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function get($key)
|
|
||||||
{
|
|
||||||
if ($this->hasProperty($key)) {
|
|
||||||
return parent::get($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (array_key_exists($key, $this->settings)) {
|
|
||||||
return $this->settings[$key];
|
|
||||||
}
|
|
||||||
|
|
||||||
return parent::get($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSettings()
|
|
||||||
{
|
|
||||||
return $this->settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSetting($name, $default = null)
|
|
||||||
{
|
|
||||||
if (array_key_exists($name, $this->settings)) {
|
|
||||||
return $this->settings[$name];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function onStore()
|
|
||||||
{
|
|
||||||
$old = $this->fetchSettingsFromDb();
|
|
||||||
$oldKeys = array_keys($old);
|
|
||||||
$newKeys = array_keys($this->settings);
|
|
||||||
$add = array();
|
|
||||||
$mod = array();
|
|
||||||
$del = array();
|
|
||||||
|
|
||||||
foreach ($this->settings as $key => $val) {
|
|
||||||
if (array_key_exists($key, $old)) {
|
|
||||||
if ($old[$key] !== $this->settings[$key]) {
|
|
||||||
$mod[$key] = $this->settings[$key];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$add[$key] = $this->settings[$key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (array_diff(array_keys($old), array_keys($this->settings)) as $key) {
|
|
||||||
$del[$key] = $key;
|
|
||||||
}
|
|
||||||
|
|
||||||
$where = sprintf('source_id = %d AND setting_name = ?', $this->id);
|
|
||||||
$db = $this->getDb();
|
|
||||||
foreach ($mod as $key => $val) {
|
|
||||||
$db->update(
|
|
||||||
'import_source_setting',
|
|
||||||
array('setting_value' => $val),
|
|
||||||
$db->quoteInto($where, $key)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($add as $key => $val) {
|
|
||||||
$db->insert(
|
|
||||||
'import_source_setting',
|
|
||||||
array(
|
|
||||||
'source_id' => $this->id,
|
|
||||||
'setting_name' => $key,
|
|
||||||
'setting_value' => $val
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($del as $key) {
|
|
||||||
$db->delete(
|
|
||||||
'import_source_setting',
|
|
||||||
$db->quoteInto($where, $key)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function fetchSettingsFromDb()
|
|
||||||
{
|
|
||||||
$db = $this->getDb();
|
|
||||||
return $db->fetchPairs(
|
|
||||||
$db->select()
|
|
||||||
->from('import_source_setting', array('setting_name', 'setting_value'))
|
|
||||||
->where('source_id = ?', $this->id)
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function onLoadFromDb()
|
|
||||||
{
|
|
||||||
$this->settings = $this->fetchSettingsFromDb();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user