2015-04-24 15:57:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Objects;
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
use Icinga\Module\Director\CustomVariable\CustomVariables;
|
2015-04-24 15:57:01 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObject;
|
2016-10-09 14:02:22 +02:00
|
|
|
use Icinga\Module\Director\Db\Cache\PrefetchCache;
|
2015-08-02 13:43:16 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2016-04-19 21:29:03 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
|
2015-06-16 17:58:47 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
|
2015-06-11 22:44:17 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
|
2016-07-28 08:57:27 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
|
2015-12-02 04:04:19 +01:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2015-12-10 13:18:35 +01:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2015-06-08 14:37:33 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
|
|
|
use Exception;
|
2015-04-24 15:57:01 +02:00
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
|
2015-04-24 15:57:01 +02:00
|
|
|
{
|
2016-10-06 18:29:50 +02:00
|
|
|
const RESOLVE_ERROR = '(unable to resolve)';
|
|
|
|
|
2015-07-02 14:31:35 +02:00
|
|
|
protected $keyName = 'object_name';
|
2015-04-24 15:57:01 +02:00
|
|
|
|
|
|
|
protected $autoincKeyName = 'id';
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether this Object supports custom variables */
|
2015-06-08 13:02:09 +02:00
|
|
|
protected $supportsCustomVars = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether there exist Groups for this object type */
|
2015-06-16 17:58:47 +02:00
|
|
|
protected $supportsGroups = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether this Object makes use of (time) ranges */
|
2015-07-01 15:15:49 +02:00
|
|
|
protected $supportsRanges = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether this object supports (command) Arguments */
|
2015-08-03 13:43:30 +02:00
|
|
|
protected $supportsArguments = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether inheritance via "imports" property is supported */
|
2015-06-26 10:39:30 +02:00
|
|
|
protected $supportsImports = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Allows controlled custom var access through Fields */
|
2015-07-30 09:08:27 +02:00
|
|
|
protected $supportsFields = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether this object can be rendered as 'apply Object' */
|
2016-02-24 21:37:48 +01:00
|
|
|
protected $supportsApplyRules = false;
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/** @var bool Whether Sets of object can be defined */
|
|
|
|
protected $supportsSets = false;
|
|
|
|
|
2016-08-31 18:23:23 +02:00
|
|
|
protected $rangeClass;
|
|
|
|
|
2015-08-28 18:21:04 +02:00
|
|
|
protected $type;
|
2015-06-08 14:37:33 +02:00
|
|
|
|
2015-12-02 04:04:19 +01:00
|
|
|
/* key/value!! */
|
2015-11-13 23:59:26 +01:00
|
|
|
protected $booleans = array();
|
|
|
|
|
2015-12-03 14:18:19 +01:00
|
|
|
// Property suffixed with _id must exist
|
|
|
|
protected $relations = array(
|
|
|
|
// property => PropertyClass
|
|
|
|
);
|
|
|
|
|
2016-02-29 18:19:01 +01:00
|
|
|
protected $relatedSets = array(
|
|
|
|
// property => ExtensibleSetClass
|
|
|
|
);
|
|
|
|
|
2016-03-16 13:01:55 +01:00
|
|
|
protected $multiRelations = array(
|
|
|
|
// property => IcingaObjectClass
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $loadedMultiRelations = array();
|
|
|
|
|
2016-10-05 19:58:48 +02:00
|
|
|
/**
|
|
|
|
* Allows to set properties pointing to related objects by name without
|
|
|
|
* loading the related object.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-03-07 02:06:04 +01:00
|
|
|
protected $unresolvedRelatedProperties = array();
|
|
|
|
|
2016-02-29 18:57:19 +01:00
|
|
|
protected $loadedRelatedSets = array();
|
|
|
|
|
2016-06-17 11:55:48 +02:00
|
|
|
// Will be rendered first, before imports
|
|
|
|
protected $prioritizedProperties = array();
|
|
|
|
|
2016-08-04 17:03:50 +02:00
|
|
|
protected $propertiesNotForRendering = array(
|
|
|
|
'id',
|
|
|
|
'object_name',
|
|
|
|
'object_type',
|
|
|
|
);
|
|
|
|
|
2016-02-28 14:21:00 +01:00
|
|
|
/**
|
|
|
|
* Array of interval property names
|
|
|
|
*
|
|
|
|
* Those will be automagically munged to integers (seconds) and rendered
|
|
|
|
* as durations (e.g. 2m 10s). Array expects (propertyName => renderedKey)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $intervalProperties = array();
|
|
|
|
|
2015-06-11 19:46:36 +02:00
|
|
|
private $vars;
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
private $groups;
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
private $imports;
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
private $ranges;
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
private $arguments;
|
|
|
|
|
2016-03-24 06:46:13 +01:00
|
|
|
private $assignments;
|
|
|
|
|
2015-12-04 10:24:54 +01:00
|
|
|
private $shouldBeRemoved = false;
|
|
|
|
|
2015-12-16 14:32:53 +01:00
|
|
|
private $resolveCache = array();
|
|
|
|
|
2016-03-20 12:00:03 +01:00
|
|
|
private $cachedPlainUnmodified;
|
|
|
|
|
2015-11-13 23:59:26 +01:00
|
|
|
public function propertyIsBoolean($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->booleans);
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:21:00 +01:00
|
|
|
public function propertyIsInterval($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->intervalProperties);
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
/**
|
|
|
|
* Whether a property ends with _id and might refer another object
|
|
|
|
*
|
|
|
|
* @param $property string Property name, like zone_id
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-05 19:58:48 +02:00
|
|
|
public function propertyIsRelation($property)
|
|
|
|
{
|
|
|
|
if ($key = $this->stripIdSuffix($property)) {
|
|
|
|
return $this->hasRelation($key);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function stripIdSuffix($key)
|
|
|
|
{
|
|
|
|
$end = substr($key, -3);
|
|
|
|
|
|
|
|
if ('_id' === $end) {
|
2016-10-09 14:48:13 +02:00
|
|
|
return substr($key, 0, -3);
|
2016-10-05 19:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:19:01 +01:00
|
|
|
public function propertyIsRelatedSet($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->relatedSets);
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:01:55 +01:00
|
|
|
public function propertyIsMultiRelation($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->multiRelations);
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:59:42 +02:00
|
|
|
public function listMultiRelations()
|
|
|
|
{
|
|
|
|
return array_keys($this->multiRelations);
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:01:55 +01:00
|
|
|
public function getMultiRelation($property)
|
|
|
|
{
|
|
|
|
if (! $this->hasLoadedMultiRelation($property)) {
|
|
|
|
$this->loadMultiRelation($property);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadedMultiRelations[$property];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setMultiRelation($property, $values)
|
|
|
|
{
|
|
|
|
$this->getMultiRelation($property)->set($values);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadMultiRelation($property)
|
|
|
|
{
|
2016-03-16 13:53:34 +01:00
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$rel = IcingaObjectMultiRelations::loadForStoredObject(
|
|
|
|
$this,
|
|
|
|
$property,
|
|
|
|
$this->multiRelations[$property]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$rel = new IcingaObjectMultiRelations(
|
|
|
|
$this,
|
|
|
|
$property,
|
|
|
|
$this->multiRelations[$property]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadedMultiRelations[$property] = $rel;
|
2016-03-16 13:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function hasLoadedMultiRelation($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->loadedMultiRelations);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadAllMultiRelations()
|
|
|
|
{
|
|
|
|
foreach (array_keys($this->multiRelations) as $key) {
|
|
|
|
if (! $this->hasLoadedMultiRelation($key)) {
|
|
|
|
$this->loadMultiRelation($key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-20 16:46:59 +02:00
|
|
|
ksort($this->loadedMultiRelations);
|
2016-03-16 13:01:55 +01:00
|
|
|
return $this->loadedMultiRelations;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:19:01 +01:00
|
|
|
protected function getRelatedSetClass($property)
|
|
|
|
{
|
|
|
|
$prefix = '\\Icinga\\Module\\Director\\IcingaConfig\\';
|
|
|
|
return $prefix . $this->relatedSets[$property];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRelatedSet($property)
|
|
|
|
{
|
2016-02-29 18:57:19 +01:00
|
|
|
if (! array_key_exists($property, $this->loadedRelatedSets)) {
|
|
|
|
$class = $this->getRelatedSetClass($property);
|
|
|
|
$this->loadedRelatedSets[$property]
|
|
|
|
= $class::forIcingaObject($this, $property);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->loadedRelatedSets[$property];
|
2016-02-29 18:19:01 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 19:32:55 +01:00
|
|
|
protected function relatedSets()
|
|
|
|
{
|
|
|
|
$sets = array();
|
|
|
|
foreach ($this->relatedSets as $key => $class) {
|
|
|
|
$sets[$key] = $this->getRelatedSet($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sets;
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
/**
|
|
|
|
* Whether the given property name is a short name for a relation
|
|
|
|
*
|
|
|
|
* This might be 'zone' for 'zone_id'
|
|
|
|
*
|
|
|
|
* @param string $property Property name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-12-03 14:18:19 +01:00
|
|
|
public function hasRelation($property)
|
|
|
|
{
|
|
|
|
return array_key_exists($property, $this->relations);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRelationClass($property)
|
|
|
|
{
|
|
|
|
return __NAMESPACE__ . '\\' . $this->relations[$property];
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:58:48 +02:00
|
|
|
protected function getRelationObjectClass($property)
|
|
|
|
{
|
|
|
|
return $this->relations[$property];
|
|
|
|
}
|
|
|
|
|
2015-12-03 17:09:51 +01:00
|
|
|
protected function getRelatedObjectName($property, $id)
|
2016-02-17 16:03:07 +01:00
|
|
|
{
|
|
|
|
return $this->getRelatedObject($property, $id)->object_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRelatedObject($property, $id)
|
2015-12-03 17:09:51 +01:00
|
|
|
{
|
|
|
|
$class = $this->getRelationClass($property);
|
2016-02-17 16:03:07 +01:00
|
|
|
return $class::loadWithAutoIncId($id, $this->connection);
|
2015-12-03 17:09:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 11:43:40 +01:00
|
|
|
public function getResolvedRelated($property)
|
|
|
|
{
|
|
|
|
$id = $this->getResolvedProperty($property . '_id');
|
|
|
|
|
|
|
|
if ($id) {
|
|
|
|
return $this->getRelatedObject($property, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether this Object supports custom variables
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-06-08 13:02:09 +02:00
|
|
|
public function supportsCustomVars()
|
|
|
|
{
|
|
|
|
return $this->supportsCustomVars;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether there exist Groups for this object type
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-06-16 17:58:47 +02:00
|
|
|
public function supportsGroups()
|
|
|
|
{
|
|
|
|
return $this->supportsGroups;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether this Object makes use of (time) ranges
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-01 15:15:49 +02:00
|
|
|
public function supportsRanges()
|
|
|
|
{
|
|
|
|
return $this->supportsRanges;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether this object supports (command) Arguments
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-08-03 13:43:30 +02:00
|
|
|
public function supportsArguments()
|
|
|
|
{
|
|
|
|
return $this->supportsArguments;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether this object supports inheritance through the "imports" property
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-06-26 10:39:30 +02:00
|
|
|
public function supportsImports()
|
|
|
|
{
|
|
|
|
return $this->supportsImports;
|
|
|
|
}
|
|
|
|
|
2016-09-15 20:45:08 +02:00
|
|
|
/**
|
|
|
|
* Whether this object allows controlled custom var access through fields
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-30 09:08:27 +02:00
|
|
|
public function supportsFields()
|
|
|
|
{
|
|
|
|
return $this->supportsFields;
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:06:31 +02:00
|
|
|
/**
|
|
|
|
* Whether this object can be rendered as 'apply Object'
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-24 21:37:48 +01:00
|
|
|
public function supportsApplyRules()
|
|
|
|
{
|
|
|
|
return $this->supportsApplyRules;
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:06:31 +02:00
|
|
|
/**
|
|
|
|
* Whether this object supports 'assign' properties
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function supportsAssignments()
|
|
|
|
{
|
|
|
|
return $this->isApplyRule();
|
|
|
|
}
|
|
|
|
|
2016-10-12 09:19:02 +02:00
|
|
|
/**
|
|
|
|
* Whether this object can be part of a 'set'
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function supportsSets()
|
|
|
|
{
|
|
|
|
return $this->supportsSets;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:58:48 +02:00
|
|
|
/**
|
|
|
|
* It sometimes makes sense to defer lookups for related properties. This
|
|
|
|
* kind of lazy-loading allows us to for example set host = 'localhost' and
|
|
|
|
* render an object even when no such host exists. Think of the activity log,
|
|
|
|
* one might want to visualize a history host or service template even when
|
|
|
|
* the related command has been deleted in the meantime.
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2016-03-07 17:11:23 +01:00
|
|
|
public function resolveUnresolvedRelatedProperties()
|
|
|
|
{
|
|
|
|
foreach ($this->unresolvedRelatedProperties as $name => $p) {
|
|
|
|
$this->resolveUnresolvedRelatedProperty($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function resolveUnresolvedRelatedProperty($name)
|
|
|
|
{
|
2016-03-07 18:10:09 +01:00
|
|
|
$short = substr($name, 0, -3);
|
|
|
|
$class = $this->getRelationClass($short);
|
2016-03-07 17:11:23 +01:00
|
|
|
$object = $class::load(
|
|
|
|
$this->unresolvedRelatedProperties[$name],
|
|
|
|
$this->connection
|
|
|
|
);
|
|
|
|
|
2016-03-11 02:41:22 +01:00
|
|
|
$this->reallySet($name, $object->id);
|
2016-03-07 17:11:23 +01:00
|
|
|
unset($this->unresolvedRelatedProperties[$name]);
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
public function hasBeenModified()
|
|
|
|
{
|
2016-03-07 17:11:23 +01:00
|
|
|
$this->resolveUnresolvedRelatedProperties();
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
if ($this->supportsCustomVars() && $this->vars !== null && $this->vars()->hasBeenModified()) {
|
2016-02-24 21:37:48 +01:00
|
|
|
//var_dump($this->vars()); exit;
|
2015-06-24 10:13:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:25:22 +02:00
|
|
|
if ($this->supportsGroups() && $this->groups !== null && $this->groups()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
if ($this->supportsImports() && $this->imports !== null && $this->imports()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
if ($this->supportsRanges() && $this->ranges !== null && $this->ranges()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:41:58 +01:00
|
|
|
if ($this->supportsArguments() && $this->arguments !== null && $this->arguments()->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:06:31 +02:00
|
|
|
if ($this->supportsAssignments() && $this->assignments !== null && $this->assignments()->hasBeenModified()) {
|
2016-03-24 06:46:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:57:19 +01:00
|
|
|
foreach ($this->loadedRelatedSets as $set) {
|
|
|
|
if ($set->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:01:55 +01:00
|
|
|
foreach ($this->loadedMultiRelations as $rel) {
|
|
|
|
if ($rel->hasBeenModified()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
return parent::hasBeenModified();
|
|
|
|
}
|
|
|
|
|
2016-03-08 09:21:59 +01:00
|
|
|
protected function hasUnresolvedRelatedProperty($name)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->unresolvedRelatedProperties);
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
protected function getRelationId($key)
|
2015-12-17 20:32:15 +01:00
|
|
|
{
|
2016-10-09 14:48:13 +02:00
|
|
|
if ($this->hasUnresolvedRelatedProperty($key)) {
|
|
|
|
$this->resolveUnresolvedRelatedProperty($key);
|
2016-03-08 09:21:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
return parent::get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRelatedProperty($key)
|
|
|
|
{
|
|
|
|
$idKey = $key . '_id';
|
|
|
|
if ($this->hasUnresolvedRelatedProperty($idKey)) {
|
|
|
|
return $this->unresolvedRelatedProperties[$idKey];
|
2016-03-07 02:06:04 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
if ($id = $this->get($idKey)) {
|
|
|
|
$class = $this->getRelationClass($key);
|
|
|
|
$object = $class::loadWithAutoIncId($id, $this->connection);
|
|
|
|
return $object->object_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get($key)
|
|
|
|
{
|
2016-07-14 17:06:48 +02:00
|
|
|
if (substr($key, 0, 5) === 'vars.') {
|
|
|
|
$var = $this->vars()->get(substr($key, 5));
|
|
|
|
if ($var === null) {
|
|
|
|
return $var;
|
|
|
|
} else {
|
|
|
|
return $var->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
// e.g. zone_id
|
|
|
|
if ($this->propertyIsRelation($key)) {
|
|
|
|
return $this->getRelationId($key);
|
|
|
|
}
|
2015-12-18 14:25:15 +01:00
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
// e.g. zone
|
|
|
|
if ($this->hasRelation($key)) {
|
|
|
|
return $this->getRelatedProperty($key);
|
2015-12-17 20:32:15 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 09:14:13 +01:00
|
|
|
if ($this->propertyIsRelatedSet($key)) {
|
|
|
|
return $this->getRelatedSet($key)->toPlainObject();
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:26:28 +01:00
|
|
|
if ($this->propertyIsMultiRelation($key)) {
|
|
|
|
return $this->getMultiRelation($key)->listRelatedNames();
|
|
|
|
}
|
|
|
|
|
2015-12-17 20:32:15 +01:00
|
|
|
return parent::get($key);
|
|
|
|
}
|
|
|
|
|
2016-04-11 11:06:54 +02:00
|
|
|
public function setProperties($props)
|
|
|
|
{
|
|
|
|
if (is_array($props)) {
|
|
|
|
if (array_key_exists('object_type', $props) && key($props) !== 'object_type') {
|
|
|
|
$type = $props['object_type'];
|
|
|
|
unset($props['object_type']);
|
|
|
|
$props = array('object_type' => $type) + $props;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parent::setProperties($props);
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:23:13 +02:00
|
|
|
public function set($key, $value)
|
|
|
|
{
|
2016-03-18 13:46:06 +01:00
|
|
|
if ($key === 'vars') {
|
2015-08-28 18:23:13 +02:00
|
|
|
$value = (array) $value;
|
|
|
|
$unset = array();
|
|
|
|
foreach ($this->vars() as $k => $f) {
|
|
|
|
if (! array_key_exists($k, $value)) {
|
|
|
|
$unset[] = $k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($unset as $k) {
|
|
|
|
unset($this->vars()->$k);
|
|
|
|
}
|
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$this->vars()->set($k, $v);
|
|
|
|
}
|
|
|
|
return $this;
|
2016-02-21 10:10:24 +01:00
|
|
|
} elseif (substr($key, 0, 5) === 'vars.') {
|
|
|
|
//TODO: allow for deep keys
|
2016-02-22 15:19:26 +01:00
|
|
|
$this->vars()->set(substr($key, 5), $value);
|
2016-02-21 10:10:24 +01:00
|
|
|
return $this;
|
2016-03-18 13:46:06 +01:00
|
|
|
} elseif (substr($key, 0, 10) === 'arguments.') {
|
|
|
|
$this->arguments()->set(substr($key, 10), $value);
|
|
|
|
return $this;
|
2015-08-28 18:23:13 +02:00
|
|
|
}
|
|
|
|
|
2016-03-21 10:35:11 +01:00
|
|
|
if ($this->propertyIsBoolean($key)) {
|
2016-02-16 11:46:01 +01:00
|
|
|
return parent::set($key, $this->normalizeBoolean($value));
|
2015-11-13 23:59:26 +01:00
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
// e.g. zone_id
|
|
|
|
if ($this->propertyIsRelation($key)) {
|
|
|
|
return $this->setRelation($key, $value);
|
|
|
|
}
|
2015-12-03 15:09:49 +01:00
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
// e.g. zone
|
|
|
|
if ($this->hasRelation($key)) {
|
|
|
|
return $this->setUnresolvedRelation($key, $value);
|
2015-12-03 14:18:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:01:55 +01:00
|
|
|
if ($this->propertyIsMultiRelation($key)) {
|
|
|
|
$this->setMultiRelation($key, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:57:19 +01:00
|
|
|
if ($this->propertyIsRelatedSet($key)) {
|
|
|
|
$this->getRelatedSet($key)->set($value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:21:00 +01:00
|
|
|
if ($this->propertyIsInterval($key)) {
|
|
|
|
return parent::set($key, c::parseInterval($value));
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:23:13 +02:00
|
|
|
return parent::set($key, $value);
|
|
|
|
}
|
|
|
|
|
2016-10-09 14:48:13 +02:00
|
|
|
private function setRelation($key, $value)
|
|
|
|
{
|
|
|
|
if ((int) $key !== (int) $this->$key) {
|
|
|
|
unset($this->unresolvedRelatedProperties[$key]);
|
|
|
|
}
|
|
|
|
return parent::set($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setUnresolvedRelation($key, $value)
|
|
|
|
{
|
|
|
|
if (strlen($value) === 0) {
|
|
|
|
unset($this->unresolvedRelatedProperties[$key . '_id']);
|
|
|
|
return parent::set($key . '_id', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->unresolvedRelatedProperties[$key . '_id'] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:08:57 +01:00
|
|
|
protected function setRanges($ranges)
|
|
|
|
{
|
|
|
|
$this->ranges()->set((array) $ranges);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-18 13:46:06 +01:00
|
|
|
protected function setArguments($value)
|
|
|
|
{
|
2016-03-21 10:35:11 +01:00
|
|
|
$this->arguments()->setArguments($value);
|
2016-03-18 13:46:06 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-04-03 21:27:49 +02:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
|
|
|
return $this->arguments()->toPlainObject();
|
|
|
|
}
|
|
|
|
|
2016-03-24 06:46:13 +01:00
|
|
|
protected function setAssignments($value)
|
|
|
|
{
|
|
|
|
$this->assignments()->setValues($value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-24 06:49:19 +01:00
|
|
|
public function assignments()
|
2016-03-24 06:46:13 +01:00
|
|
|
{
|
|
|
|
if ($this->assignments === null) {
|
|
|
|
$this->assignments = new IcingaObjectAssignments($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->assignments;
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:08:57 +01:00
|
|
|
protected function getRanges()
|
|
|
|
{
|
|
|
|
return $this->ranges()->getValues();
|
|
|
|
}
|
|
|
|
|
2016-02-16 11:46:01 +01:00
|
|
|
protected function normalizeBoolean($value)
|
|
|
|
{
|
|
|
|
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
|
|
|
|
return 'y';
|
|
|
|
} elseif ($value === 'n' || $value === '0' || $value === false || $value === 0) {
|
|
|
|
return 'n';
|
|
|
|
} elseif ($value === '' || $value === null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Got invalid boolean: %s',
|
|
|
|
var_export($value, 1)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 12:17:50 +01:00
|
|
|
protected function setDisabled($disabled)
|
|
|
|
{
|
2016-02-17 20:00:48 +01:00
|
|
|
return parent::reallySet('disabled', $this->normalizeBoolean($disabled));
|
2016-02-16 12:17:50 +01:00
|
|
|
}
|
|
|
|
|
2016-04-18 13:17:47 +02:00
|
|
|
public function isDisabled()
|
|
|
|
{
|
|
|
|
return $this->disabled === 'y';
|
|
|
|
}
|
|
|
|
|
2015-12-04 10:24:54 +01:00
|
|
|
public function markForRemoval($remove = true)
|
|
|
|
{
|
|
|
|
$this->shouldBeRemoved = $remove;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shouldBeRemoved()
|
|
|
|
{
|
|
|
|
return $this->shouldBeRemoved;
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
public function groups()
|
|
|
|
{
|
|
|
|
$this->assertGroupsSupport();
|
|
|
|
if ($this->groups === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->groups = IcingaObjectGroups::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->groups = new IcingaObjectGroups($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->groups;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
public function ranges()
|
|
|
|
{
|
|
|
|
$this->assertRangesSupport();
|
|
|
|
if ($this->ranges === null) {
|
2016-08-31 18:23:23 +02:00
|
|
|
$class = $this->getRangeClass();
|
2015-07-01 15:15:49 +02:00
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
2016-08-31 18:23:23 +02:00
|
|
|
$this->ranges = $class::loadForStoredObject($this);
|
2015-07-01 15:15:49 +02:00
|
|
|
} else {
|
2016-08-31 18:23:23 +02:00
|
|
|
$this->ranges = new $class($this);
|
2015-07-01 15:15:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->ranges;
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:23:23 +02:00
|
|
|
protected function getRangeClass()
|
|
|
|
{
|
|
|
|
if ($this->rangeClass === null) {
|
|
|
|
$this->rangeClass = get_class($this) . 'Ranges';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->rangeClass;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
public function arguments()
|
|
|
|
{
|
|
|
|
$this->assertArgumentsSupport();
|
|
|
|
if ($this->arguments === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->arguments = IcingaArguments::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->arguments = new IcingaArguments($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->arguments;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
public function imports()
|
|
|
|
{
|
|
|
|
$this->assertImportsSupport();
|
|
|
|
if ($this->imports === null) {
|
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
|
|
|
$this->imports = IcingaObjectImports::loadForStoredObject($this);
|
|
|
|
} else {
|
|
|
|
$this->imports = new IcingaObjectImports($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->imports;
|
|
|
|
}
|
|
|
|
|
2016-03-05 10:49:37 +01:00
|
|
|
public function setImports($imports)
|
|
|
|
{
|
2016-05-20 08:12:46 +02:00
|
|
|
if (! is_array($imports) && $imports !== null) {
|
2016-03-05 10:49:37 +01:00
|
|
|
$imports = array($imports);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->imports()->set($imports);
|
|
|
|
if ($this->imports()->hasBeenModified()) {
|
|
|
|
$this->invalidateResolveCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getImports()
|
|
|
|
{
|
|
|
|
return $this->imports()->listImportNames();
|
|
|
|
}
|
|
|
|
|
2016-08-23 16:18:54 +02:00
|
|
|
public function templateResolver()
|
|
|
|
{
|
|
|
|
// preserve object
|
|
|
|
return new IcingaTemplateResolver($this);
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:37:13 +01:00
|
|
|
public function getResolvedProperty($key, $default = null)
|
2015-11-06 09:49:05 +01:00
|
|
|
{
|
2016-05-26 01:35:12 +02:00
|
|
|
if (array_key_exists($key, $this->unresolvedRelatedProperties)) {
|
|
|
|
$this->resolveUnresolvedRelatedProperty($key);
|
|
|
|
$this->invalidateResolveCache();
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:49:05 +01:00
|
|
|
$properties = $this->getResolvedProperties();
|
|
|
|
if (property_exists($properties, $key)) {
|
|
|
|
return $properties->$key;
|
|
|
|
}
|
|
|
|
|
2016-02-28 14:37:13 +01:00
|
|
|
return $default;
|
2015-11-06 09:49:05 +01:00
|
|
|
}
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
public function getResolvedProperties()
|
|
|
|
{
|
2015-07-30 11:39:00 +02:00
|
|
|
return $this->getResolved('Properties');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInheritedProperties()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Properties');
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsProperties()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Properties');
|
|
|
|
}
|
|
|
|
|
2015-07-02 14:51:59 +02:00
|
|
|
public function resolveProperties()
|
|
|
|
{
|
2015-07-30 11:39:00 +02:00
|
|
|
return $this->resolve('Properties');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResolvedFields()
|
|
|
|
{
|
|
|
|
return $this->getResolved('Fields');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInheritedFields()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Fields');
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsFields()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Fields');
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function resolveFields()
|
|
|
|
{
|
|
|
|
return $this->resolve('Fields');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResolvedVars()
|
|
|
|
{
|
|
|
|
return $this->getResolved('Vars');
|
|
|
|
}
|
2015-07-02 14:51:59 +02:00
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function getInheritedVars()
|
|
|
|
{
|
|
|
|
return $this->getInherited('Vars');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function resolveVars()
|
|
|
|
{
|
|
|
|
return $this->resolve('Vars');
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
public function getOriginsVars()
|
|
|
|
{
|
|
|
|
return $this->getOrigins('Vars');
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
public function getVars()
|
|
|
|
{
|
|
|
|
$vars = (object) array();
|
|
|
|
foreach ($this->vars() as $key => $var) {
|
2016-02-22 15:43:59 +01:00
|
|
|
if ($var->hasBeenDeleted()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
$vars->$key = $var->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
2016-03-05 10:39:15 +01:00
|
|
|
public function getGroups()
|
|
|
|
{
|
|
|
|
return $this->groups()->listGroupNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setGroups($groups)
|
|
|
|
{
|
|
|
|
$this->groups()->set($groups);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
protected function getResolved($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_MERGED_'];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInherited($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_INHERITED_'];
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
protected function getOrigins($what)
|
|
|
|
{
|
|
|
|
$func = 'resolve' . $what;
|
|
|
|
$res = $this->$func();
|
|
|
|
return $res['_ORIGINS_'];
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:32:53 +01:00
|
|
|
protected function hasResolveCached($what)
|
|
|
|
{
|
|
|
|
return array_key_exists($what, $this->resolveCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function & getResolveCached($what)
|
|
|
|
{
|
|
|
|
return $this->resolveCache[$what];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function storeResolvedCache($what, $vals)
|
|
|
|
{
|
|
|
|
$this->resolveCache[$what] = $vals;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidateResolveCache()
|
|
|
|
{
|
|
|
|
$this->resolveCache = array();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:29:35 +01:00
|
|
|
public function countDirectDescendants()
|
|
|
|
{
|
|
|
|
$db = $this->getDb();
|
|
|
|
$table = $this->getTableName();
|
2016-10-12 11:09:28 +02:00
|
|
|
$type = $this->getShortTableName();
|
2016-03-16 21:46:00 +01:00
|
|
|
|
2016-03-02 19:29:35 +01:00
|
|
|
$query = $db->select()->from(
|
|
|
|
array('oi' => $table . '_inheritance'),
|
|
|
|
array('cnt' => 'COUNT(*)')
|
|
|
|
)->where('oi.parent_' . $type . '_id = ?', (int) $this->id);
|
|
|
|
|
|
|
|
return $db->fetchOne($query);
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
protected function resolve($what)
|
|
|
|
{
|
2015-12-16 14:32:53 +01:00
|
|
|
if ($this->hasResolveCached($what)) {
|
|
|
|
return $this->getResolveCached($what);
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
$vals = array();
|
2015-07-31 14:52:46 +02:00
|
|
|
$vals['_MERGED_'] = (object) array();
|
2015-07-30 11:39:00 +02:00
|
|
|
$vals['_INHERITED_'] = (object) array();
|
2015-07-31 14:52:46 +02:00
|
|
|
$vals['_ORIGINS_'] = (object) array();
|
2016-06-16 21:40:22 +02:00
|
|
|
$objects = $this->imports()->getObjects();
|
2015-07-30 11:39:00 +02:00
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
$get = 'get' . $what;
|
|
|
|
$getInherited = 'getInherited' . $what;
|
|
|
|
$getOrigins = 'getOrigins' . $what;
|
2015-07-30 11:39:00 +02:00
|
|
|
|
2016-02-28 14:28:27 +01:00
|
|
|
$blacklist = array('id', 'object_type', 'object_name', 'disabled');
|
2015-07-02 14:51:59 +02:00
|
|
|
foreach ($objects as $name => $object) {
|
2015-07-31 14:52:46 +02:00
|
|
|
$origins = $object->$getOrigins();
|
2015-07-30 11:39:00 +02:00
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
foreach ($object->$getInherited() as $key => $value) {
|
2016-02-26 11:58:37 +01:00
|
|
|
if (in_array($key, $blacklist)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-16 21:40:22 +02:00
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
// $vals[$name]->$key = $value;
|
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
$vals['_INHERITED_']->$key = $value;
|
|
|
|
$vals['_ORIGINS_']->$key = $origins->$key;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
2015-07-31 14:52:46 +02:00
|
|
|
|
|
|
|
foreach ($object->$get() as $key => $value) {
|
2016-02-28 14:28:27 +01:00
|
|
|
// TODO: skip if default value?
|
|
|
|
if ($value === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (in_array($key, $blacklist)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-31 14:52:46 +02:00
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
$vals['_INHERITED_']->$key = $value;
|
|
|
|
$vals['_ORIGINS_']->$key = $name;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 14:52:46 +02:00
|
|
|
foreach ($this->$get() as $key => $value) {
|
2016-02-26 11:58:37 +01:00
|
|
|
if ($value === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-31 14:52:46 +02:00
|
|
|
|
|
|
|
// $vals[$this->object_name]->$key = $value;
|
|
|
|
$vals['_MERGED_']->$key = $value;
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:32:53 +01:00
|
|
|
$this->storeResolvedCache($what, $vals);
|
|
|
|
|
2015-07-30 11:39:00 +02:00
|
|
|
return $vals;
|
2015-07-02 14:51:59 +02:00
|
|
|
}
|
|
|
|
|
2015-12-02 04:04:19 +01:00
|
|
|
public function matches(Filter $filter)
|
|
|
|
{
|
2016-03-20 14:51:18 +01:00
|
|
|
// TODO: speed up by passing only desired properties (filter columns) to
|
|
|
|
// toPlainObject method
|
|
|
|
return $filter->matches($this->toPlainObject());
|
2015-12-02 04:04:19 +01:00
|
|
|
}
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
protected function assertCustomVarsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsCustomVars()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no custom vars',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
protected function assertGroupsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsGroups()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no groups',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
protected function assertRangesSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsRanges()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no ranges',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function assertArgumentsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsArguments()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no arguments',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-26 10:39:30 +02:00
|
|
|
protected function assertImportsSupport()
|
|
|
|
{
|
|
|
|
if (! $this->supportsImports()) {
|
|
|
|
throw new ProgrammingError(
|
|
|
|
'Objects of type "%s" have no imports',
|
|
|
|
$this->getType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-11 19:18:47 +02:00
|
|
|
public function vars()
|
|
|
|
{
|
|
|
|
$this->assertCustomVarsSupport();
|
|
|
|
if ($this->vars === null) {
|
2015-06-15 16:35:18 +02:00
|
|
|
if ($this->hasBeenLoadedFromDb()) {
|
2016-10-09 14:02:22 +02:00
|
|
|
if (PrefetchCache::shouldBeUsed()) {
|
|
|
|
$this->vars = PrefetchCache::instance()->vars($this);
|
|
|
|
} else {
|
|
|
|
$this->vars = CustomVariables::loadForStoredObject($this);
|
|
|
|
}
|
2015-06-15 16:35:18 +02:00
|
|
|
} else {
|
|
|
|
$this->vars = new CustomVariables();
|
|
|
|
}
|
2015-06-11 19:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->vars;
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:35:18 +02:00
|
|
|
public function getVarsTableName()
|
|
|
|
{
|
|
|
|
return $this->getTableName() . '_var';
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
public function getShortTableName()
|
2015-06-15 16:35:18 +02:00
|
|
|
{
|
|
|
|
// strlen('icinga_') = 7
|
2015-06-16 17:58:47 +02:00
|
|
|
return substr($this->getTableName(), 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVarsIdColumn()
|
|
|
|
{
|
|
|
|
return $this->getShortTableName() . '_id';
|
2015-06-15 16:35:18 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 09:09:44 +02:00
|
|
|
public function getFields()
|
|
|
|
{
|
|
|
|
$fields = (object) array();
|
|
|
|
|
|
|
|
if (! $this->supportsFields()) {
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
$query = $db->select()->from(
|
|
|
|
array('df' => 'director_datafield'),
|
|
|
|
array(
|
|
|
|
'datafield_id' => 'f.datafield_id',
|
|
|
|
'is_required' => 'f.is_required',
|
2015-11-14 00:21:42 +01:00
|
|
|
'varname' => 'df.varname',
|
|
|
|
'description' => 'df.description',
|
|
|
|
'datatype' => 'df.datatype',
|
|
|
|
'format' => 'df.format',
|
2015-07-30 09:09:44 +02:00
|
|
|
)
|
|
|
|
)->join(
|
|
|
|
array('f' => $this->getTableName() . '_field'),
|
|
|
|
'df.id = f.datafield_id',
|
|
|
|
array()
|
2015-07-31 17:34:12 +02:00
|
|
|
)->where('f.' . $this->getShortTableName() . '_id = ?', (int) $this->id)
|
2015-07-30 09:09:44 +02:00
|
|
|
->order('df.caption ASC');
|
|
|
|
|
|
|
|
$res = $db->fetchAll($query);
|
|
|
|
|
|
|
|
foreach ($res as $r) {
|
|
|
|
$fields->{$r->varname} = $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2016-02-24 23:59:50 +01:00
|
|
|
protected function getAssignments()
|
|
|
|
{
|
2016-03-24 06:46:13 +01:00
|
|
|
return $this->assignments()->getValues();
|
2016-02-24 23:59:50 +01:00
|
|
|
}
|
|
|
|
|
2016-03-05 16:13:24 +01:00
|
|
|
public function hasProperty($key)
|
|
|
|
{
|
|
|
|
if ($this->propertyIsRelatedSet($key)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-24 23:59:50 +01:00
|
|
|
|
2016-03-17 08:27:11 +01:00
|
|
|
if ($this->propertyIsMultiRelation($key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-05 16:13:24 +01:00
|
|
|
return parent::hasProperty($key);
|
|
|
|
}
|
2016-02-24 23:59:50 +01:00
|
|
|
|
2016-03-20 12:00:03 +01:00
|
|
|
public function isObject()
|
|
|
|
{
|
|
|
|
return $this->hasProperty('object_type')
|
|
|
|
&& $this->object_type === 'object';
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
public function isTemplate()
|
|
|
|
{
|
2015-06-08 14:37:33 +02:00
|
|
|
return $this->hasProperty('object_type')
|
2015-06-08 13:02:09 +02:00
|
|
|
&& $this->object_type === 'template';
|
|
|
|
}
|
|
|
|
|
2015-12-01 14:47:10 +01:00
|
|
|
public function isExternal()
|
|
|
|
{
|
|
|
|
return $this->hasProperty('object_type')
|
|
|
|
&& $this->object_type === 'external_object';
|
|
|
|
}
|
|
|
|
|
2015-06-08 13:02:09 +02:00
|
|
|
public function isApplyRule()
|
|
|
|
{
|
2015-06-08 14:37:33 +02:00
|
|
|
return $this->hasProperty('object_type')
|
2015-06-08 13:02:09 +02:00
|
|
|
&& $this->object_type === 'apply';
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function storeRelatedObjects()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->storeCustomVars()
|
|
|
|
->storeGroups()
|
2016-03-16 13:53:34 +01:00
|
|
|
->storeMultiRelations()
|
2015-08-03 13:43:30 +02:00
|
|
|
->storeImports()
|
|
|
|
->storeRanges()
|
2016-02-29 18:57:19 +01:00
|
|
|
->storeRelatedSets()
|
2016-03-24 06:46:13 +01:00
|
|
|
->storeArguments()
|
|
|
|
->storeAssignments();
|
2015-08-03 13:43:30 +02:00
|
|
|
}
|
|
|
|
|
2016-03-08 09:21:59 +01:00
|
|
|
protected function beforeStore()
|
|
|
|
{
|
|
|
|
$this->resolveUnresolvedRelatedProperties();
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:57:01 +02:00
|
|
|
public function onInsert()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logCreation($this, $this->connection);
|
2015-08-29 01:03:34 +02:00
|
|
|
$this->storeRelatedObjects();
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function onUpdate()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logModification($this, $this->connection);
|
2015-08-29 01:03:34 +02:00
|
|
|
$this->storeRelatedObjects();
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
protected function storeCustomVars()
|
|
|
|
{
|
|
|
|
if ($this->supportsCustomVars()) {
|
2015-06-24 11:25:22 +02:00
|
|
|
$this->vars !== null && $this->vars()->storeToDb($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function storeGroups()
|
|
|
|
{
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
$this->groups !== null && $this->groups()->store();
|
2015-06-24 10:13:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 15:55:16 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:53:34 +01:00
|
|
|
protected function storeMultiRelations()
|
|
|
|
{
|
|
|
|
foreach ($this->loadedMultiRelations as $rel) {
|
|
|
|
$rel->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
protected function storeRanges()
|
|
|
|
{
|
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
$this->ranges !== null && $this->ranges()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
protected function storeArguments()
|
|
|
|
{
|
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
$this->arguments !== null && $this->arguments()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-02-29 18:57:19 +01:00
|
|
|
|
2016-03-24 06:46:13 +01:00
|
|
|
protected function storeAssignments()
|
|
|
|
{
|
2016-09-09 11:06:31 +02:00
|
|
|
if ($this->supportsAssignments()) {
|
2016-03-24 06:46:13 +01:00
|
|
|
$this->assignments !== null && $this->assignments()->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:57:19 +01:00
|
|
|
protected function storeRelatedSets()
|
|
|
|
{
|
|
|
|
foreach ($this->loadedRelatedSets as $set) {
|
|
|
|
if ($set->hasBeenModified()) {
|
|
|
|
$set->store();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2015-08-03 13:43:30 +02:00
|
|
|
|
2015-06-26 15:55:16 +02:00
|
|
|
protected function storeImports()
|
|
|
|
{
|
|
|
|
if ($this->supportsImports()) {
|
|
|
|
$this->imports !== null && $this->imports()->store();
|
|
|
|
}
|
|
|
|
|
2015-06-24 10:13:06 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:02:45 +01:00
|
|
|
public function beforeDelete()
|
|
|
|
{
|
|
|
|
$this->cachedPlainUnmodified = $this->getPlainUnmodifiedObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCachedUnmodifiedObject()
|
|
|
|
{
|
|
|
|
return $this->cachedPlainUnmodified;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:57:01 +02:00
|
|
|
public function onDelete()
|
|
|
|
{
|
|
|
|
DirectorActivityLog::logRemoval($this, $this->connection);
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
public function renderToLegacyConfig(IcingaConfig $config)
|
|
|
|
{
|
|
|
|
if ($this->isExternal()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 15:15:05 +02:00
|
|
|
$filename = $this->getRenderingFilename();
|
2016-07-28 08:57:27 +02:00
|
|
|
|
|
|
|
if ($config->isLegacy()) {
|
|
|
|
|
|
|
|
if ($this->getResolvedProperty('zone_id')) {
|
|
|
|
|
|
|
|
$a = clone($this);
|
|
|
|
$a->enable_active_checks = true;
|
|
|
|
|
|
|
|
$b = clone($this);
|
|
|
|
$a->enable_active_checks = false;
|
|
|
|
|
|
|
|
$config->configFile(
|
|
|
|
'director/master/' . $filename,
|
|
|
|
'.cfg'
|
|
|
|
)->addLegacyObject($a);
|
|
|
|
|
|
|
|
$config->configFile(
|
|
|
|
'director/' . $this->getRenderingZone($config) . '/' . $filename,
|
|
|
|
'.cfg'
|
|
|
|
)->addLegacyObject($b);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$config->configFile(
|
|
|
|
'director/' . $this->getRenderingZone($config) . '/' . $filename,
|
|
|
|
'.cfg'
|
|
|
|
)->addLegacyObject($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$config->configFile(
|
|
|
|
'director/' . $this->getRenderingZone($config) . '/' . $filename
|
|
|
|
)->addObject($this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 21:29:03 +02:00
|
|
|
public function renderToConfig(IcingaConfig $config)
|
|
|
|
{
|
2016-08-01 08:59:31 +02:00
|
|
|
if ($config->isLegacy()) {
|
|
|
|
return $this->renderToLegacyConfig($config);
|
|
|
|
}
|
|
|
|
|
2016-06-11 00:21:24 +02:00
|
|
|
if ($this->isExternal()) {
|
2016-04-19 21:29:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 15:15:05 +02:00
|
|
|
$config->configFile(
|
|
|
|
'zones.d/' . $this->getRenderingZone($config) . '/' . $this->getRenderingFilename()
|
|
|
|
)->addObject($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRenderingFilename()
|
|
|
|
{
|
2016-04-19 21:29:03 +02:00
|
|
|
$type = $this->getShortTableName();
|
|
|
|
|
|
|
|
if ($this->isTemplate()) {
|
|
|
|
$filename = strtolower($type) . '_templates';
|
|
|
|
} elseif ($this->isApplyRule()) {
|
|
|
|
$filename = strtolower($type) . '_apply';
|
|
|
|
} else {
|
|
|
|
$filename = strtolower($type) . 's';
|
|
|
|
}
|
|
|
|
|
2016-10-09 15:15:05 +02:00
|
|
|
return $filename;
|
2016-04-19 21:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRenderingZone(IcingaConfig $config = null)
|
|
|
|
{
|
2016-10-06 18:29:50 +02:00
|
|
|
if ($this->hasUnresolvedRelatedProperty('zone_id')) {
|
|
|
|
return $this->zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($zoneId = $this->getResolvedProperty('zone_id')) {
|
|
|
|
// Config has a lookup cache, is faster:
|
|
|
|
return $config->getZoneName($zoneId);
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return self::RESOLVE_ERROR;
|
2016-04-19 21:29:03 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 17:44:42 +02:00
|
|
|
if ($this->prefersGlobalZone()) {
|
2016-04-19 21:29:03 +02:00
|
|
|
return $this->connection->getDefaultGlobalZoneName();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->connection->getMasterZoneName();
|
|
|
|
}
|
|
|
|
|
2016-07-28 17:44:42 +02:00
|
|
|
protected function prefersGlobalZone()
|
|
|
|
{
|
|
|
|
return $this->isTemplate() || $this->isApplyRule();
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderImports()
|
|
|
|
{
|
|
|
|
// TODO: parent_host ORDERed by weigth...
|
2015-06-26 10:39:30 +02:00
|
|
|
if ($this->supportsImports()) {
|
|
|
|
return $this->imports()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacyImports()
|
|
|
|
{
|
|
|
|
if ($this->supportsImports()) {
|
|
|
|
return $this->imports()->toLegacyConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:58:48 +02:00
|
|
|
protected function renderLegacyRelationProperty($propertyName, $id, $renderKey = null)
|
|
|
|
{
|
|
|
|
return $this->renderLegacyObjectProperty(
|
|
|
|
$renderKey ?: $propertyName,
|
2016-10-12 10:31:22 +02:00
|
|
|
c1::renderString($this->getRelatedObjectName($propertyName, $id))
|
2016-10-05 19:58:48 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-16 12:17:50 +01:00
|
|
|
// Disabled is a virtual property
|
|
|
|
protected function renderDisabled()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
/**
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*/
|
2016-10-12 10:31:22 +02:00
|
|
|
protected function renderLegacyHost_id()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyRelationProperty('host', $this->host_id, 'host_name');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyTimeout()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacyEnable_active_checks()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'enable_active_checks',
|
|
|
|
'active_checks_enabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyEnable_passive_checks()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'enable_passive_checks',
|
|
|
|
'passive_checks_enabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyEnable_event_handler()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'enable_active_checks',
|
|
|
|
'event_handler_enabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyEnable_notifications()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'enable_notifications',
|
|
|
|
'notifications_enabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyEnable_perfdata()
|
|
|
|
{
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'enable_perfdata',
|
|
|
|
'process_perf_data'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyVolatile()
|
|
|
|
{
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
return $this->renderLegacyBooleanProperty(
|
|
|
|
'volatile',
|
|
|
|
'is_volatile'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function renderLegacyBooleanProperty($property, $legacyKey)
|
|
|
|
{
|
2016-10-05 20:05:59 +02:00
|
|
|
return c1::renderKeyValue(
|
|
|
|
$legacyKey,
|
|
|
|
c1::renderBoolean($this->$property)
|
|
|
|
);
|
2016-07-28 08:57:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderProperties()
|
|
|
|
{
|
|
|
|
$out = '';
|
2016-10-05 20:05:59 +02:00
|
|
|
$blacklist = array_merge(
|
|
|
|
$this->propertiesNotForRendering,
|
|
|
|
$this->prioritizedProperties
|
|
|
|
);
|
2015-06-08 14:37:33 +02:00
|
|
|
|
|
|
|
foreach ($this->properties as $key => $value) {
|
2016-06-17 11:44:38 +02:00
|
|
|
if (in_array($key, $blacklist)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
|
2016-06-17 11:44:38 +02:00
|
|
|
$out .= $this->renderObjectProperty($key, $value);
|
|
|
|
}
|
2016-03-08 09:21:59 +01:00
|
|
|
|
2016-06-17 11:44:38 +02:00
|
|
|
return $out;
|
|
|
|
}
|
2016-03-08 09:21:59 +01:00
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacyProperties()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
$blacklist = array_merge(array(
|
|
|
|
'id',
|
|
|
|
'object_name',
|
|
|
|
'object_type',
|
|
|
|
), array() /* $this->prioritizedProperties */);
|
|
|
|
|
|
|
|
foreach ($this->properties as $key => $value) {
|
|
|
|
if (in_array($key, $blacklist)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$out .= $this->renderLegacyObjectProperty($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2016-06-17 11:55:48 +02:00
|
|
|
protected function renderPrioritizedProperties()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
|
|
|
|
foreach ($this->prioritizedProperties as $key) {
|
|
|
|
$out .= $this->renderObjectProperty($key, $this->properties[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2016-06-17 11:44:38 +02:00
|
|
|
protected function renderObjectProperty($key, $value)
|
|
|
|
{
|
|
|
|
if (substr($key, -3) === '_id') {
|
|
|
|
$short = substr($key, 0, -3);
|
|
|
|
if ($this->hasUnresolvedRelatedProperty($key)) {
|
|
|
|
return c::renderKeyValue(
|
|
|
|
$short, // NOT
|
|
|
|
c::renderString($this->$short)
|
|
|
|
);
|
|
|
|
|
|
|
|
return '';
|
2016-02-26 11:58:37 +01:00
|
|
|
}
|
2016-06-17 11:44:38 +02:00
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
|
2016-06-17 11:44:38 +02:00
|
|
|
if ($value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$method = 'render' . ucfirst($key);
|
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
return $this->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->propertyIsBoolean($key)) {
|
|
|
|
if ($value === $this->defaultProperties[$key]) {
|
|
|
|
return '';
|
2015-06-08 14:37:33 +02:00
|
|
|
} else {
|
2016-06-17 11:44:38 +02:00
|
|
|
return c::renderKeyValue(
|
|
|
|
$this->booleans[$key],
|
|
|
|
c::renderBoolean($value)
|
|
|
|
);
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 11:44:38 +02:00
|
|
|
if ($this->propertyIsInterval($key)) {
|
|
|
|
return c::renderKeyValue(
|
|
|
|
$this->intervalProperties[$key],
|
|
|
|
c::renderInterval($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($key, -3) === '_id'
|
|
|
|
&& $this->hasRelation($relKey = substr($key, 0, -3))
|
|
|
|
) {
|
|
|
|
return $this->renderRelationProperty($relKey, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return c::renderKeyValue($key, c::renderString($value));
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacyObjectProperty($key, $value)
|
|
|
|
{
|
|
|
|
if (substr($key, -3) === '_id') {
|
|
|
|
$short = substr($key, 0, -3);
|
|
|
|
if ($this->hasUnresolvedRelatedProperty($key)) {
|
|
|
|
return c1::renderKeyValue(
|
|
|
|
$short, // NOT
|
|
|
|
c1::renderString($this->$short)
|
|
|
|
);
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$method = 'renderLegacy' . ucfirst($key);
|
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
return $this->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$method = 'render' . ucfirst($key);
|
|
|
|
if (method_exists($this, $method)) {
|
|
|
|
return $this->$method($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->propertyIsBoolean($key)) {
|
|
|
|
if ($value === $this->defaultProperties[$key]) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return c1::renderKeyValue(
|
|
|
|
$this->booleans[$key],
|
|
|
|
c1::renderBoolean($value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 10:31:22 +02:00
|
|
|
if (substr($key, -3) === '_id'
|
|
|
|
&& $this->hasRelation($relKey = substr($key, 0, -3))
|
|
|
|
) {
|
|
|
|
return $this->renderLegacyRelationProperty($relKey, $value);
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
return c1::renderKeyValue($key, c1::renderString($value));
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderBooleanProperty($key)
|
|
|
|
{
|
2015-06-11 23:02:43 +02:00
|
|
|
return c::renderKeyValue($key, c::renderBoolean($this->$key));
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:00:48 +01:00
|
|
|
protected function renderPropertyAsSeconds($key)
|
|
|
|
{
|
2016-02-28 12:40:11 +01:00
|
|
|
return c::renderKeyValue($key, c::renderInterval($this->$key));
|
2015-12-03 18:00:48 +01:00
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderSuffix()
|
|
|
|
{
|
2015-06-11 22:49:06 +02:00
|
|
|
return "}\n\n";
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacySuffix()
|
|
|
|
{
|
|
|
|
return "}\n\n";
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:40:45 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderCustomVars()
|
|
|
|
{
|
|
|
|
if ($this->supportsCustomVars()) {
|
2015-06-15 14:23:15 +02:00
|
|
|
return $this->vars()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:58:47 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderGroups()
|
|
|
|
{
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
return $this->groups()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:53:34 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderMultiRelations()
|
|
|
|
{
|
|
|
|
$out = '';
|
|
|
|
foreach ($this->loadAllMultiRelations() as $rel) {
|
|
|
|
$out .= $rel->toConfigString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2015-07-01 15:15:49 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderRanges()
|
|
|
|
{
|
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
return $this->ranges()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:43:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderArguments()
|
|
|
|
{
|
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
return $this->arguments()->toConfigString();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 18:41:07 +02:00
|
|
|
|
2016-02-29 18:19:01 +01:00
|
|
|
protected function renderRelatedSets()
|
|
|
|
{
|
|
|
|
$config = '';
|
|
|
|
foreach ($this->relatedSets as $property => $class) {
|
|
|
|
$config .= $this->getRelatedSet($property)->renderAs($property);
|
|
|
|
}
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
2015-12-09 15:05:31 +01:00
|
|
|
protected function renderRelationProperty($propertyName, $id, $renderKey = null)
|
2015-12-03 17:09:51 +01:00
|
|
|
{
|
|
|
|
return c::renderKeyValue(
|
2015-12-09 15:05:31 +01:00
|
|
|
$renderKey ?: $propertyName,
|
2015-12-03 17:09:51 +01:00
|
|
|
c::renderString($this->getRelatedObjectName($propertyName, $id))
|
|
|
|
);
|
|
|
|
}
|
2015-12-09 15:05:31 +01:00
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderCommandProperty($commandId, $propertyName = 'check_command')
|
|
|
|
{
|
2015-06-11 07:44:22 +02:00
|
|
|
return c::renderKeyValue(
|
2015-06-08 14:37:33 +02:00
|
|
|
$propertyName,
|
2015-06-11 23:02:43 +02:00
|
|
|
c::renderString($this->connection->getCommandName($commandId))
|
2015-06-08 14:37:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:58:37 +01:00
|
|
|
/**
|
|
|
|
* We do not render zone properties, objects are stored to zone dirs
|
|
|
|
*
|
|
|
|
* Avoid complaints for method names with underscore:
|
|
|
|
* @codingStandardsIgnoreStart
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderZone_id()
|
|
|
|
{
|
2016-02-26 11:58:37 +01:00
|
|
|
// @codingStandardsIgnoreEnd
|
2015-12-10 11:34:46 +01:00
|
|
|
return '';
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 11:27:28 +01:00
|
|
|
protected function renderCustomExtensions()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function renderObjectHeader()
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
"%s %s %s {\n",
|
|
|
|
$this->getObjectTypeName(),
|
|
|
|
$this->getType(),
|
2015-06-11 07:44:22 +02:00
|
|
|
c::renderString($this->getObjectName())
|
2015-06-08 14:37:33 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-24 23:59:50 +01:00
|
|
|
protected function renderAssignments()
|
|
|
|
{
|
2016-09-09 11:06:31 +02:00
|
|
|
if ($this->supportsAssignments()) {
|
2016-03-24 06:46:13 +01:00
|
|
|
return $this->assignments()->toConfigString();
|
2016-02-24 23:59:50 +01:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 08:57:27 +02:00
|
|
|
protected function renderLegacyObjectHeader()
|
|
|
|
{
|
|
|
|
$type = strtolower($this->getType());
|
|
|
|
|
|
|
|
if ($this->isTemplate()) {
|
2016-10-05 20:05:59 +02:00
|
|
|
$name = c1::renderKeyValue(
|
|
|
|
'name',
|
|
|
|
c1::renderString($this->getObjectName())
|
|
|
|
);
|
2016-07-28 08:57:27 +02:00
|
|
|
} else {
|
2016-10-05 20:05:59 +02:00
|
|
|
$name = c1::renderKeyValue(
|
|
|
|
$type . '_name',
|
|
|
|
c1::renderString($this->getObjectName())
|
|
|
|
);
|
2016-07-28 08:57:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$str = sprintf(
|
|
|
|
"define %s {\n$name",
|
|
|
|
$type,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->isTemplate()) {
|
|
|
|
$str .= c1::renderKeyValue('register', '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toLegacyConfigString()
|
|
|
|
{
|
|
|
|
$str = implode(array(
|
|
|
|
$this->renderLegacyObjectHeader(),
|
|
|
|
$this->renderLegacyImports(),
|
|
|
|
$this->renderLegacyProperties(),
|
|
|
|
//$this->renderRanges(),
|
|
|
|
//$this->renderArguments(),
|
|
|
|
//$this->renderRelatedSets(),
|
|
|
|
//$this->renderGroups(),
|
|
|
|
//$this->renderMultiRelations(),
|
|
|
|
//$this->renderCustomExtensions(),
|
|
|
|
//$this->renderCustomVars(),
|
|
|
|
//$this->renderAssignments(),
|
|
|
|
$this->renderLegacySuffix()
|
|
|
|
));
|
|
|
|
|
|
|
|
$str = $this->alignLegacyProperties($str);
|
|
|
|
|
|
|
|
if ($this->isDisabled()) {
|
|
|
|
return "/* --- This object has been disabled ---\n"
|
|
|
|
. $str . "*/\n";
|
|
|
|
} else {
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function alignLegacyProperties($configString)
|
|
|
|
{
|
|
|
|
$lines = preg_split('/\n/', $configString);
|
|
|
|
$len = 24;
|
|
|
|
|
|
|
|
foreach ($lines as &$line) {
|
|
|
|
if (preg_match('/^\s{4}([^\t]+)\t+(.+)$/', $line, $m)) {
|
|
|
|
if ($len - strlen($m[1]) < 0) {
|
|
|
|
var_dump($m);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line = ' ' . $m[1] . str_repeat(' ', $len - strlen($m[1])) . $m[2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode("\n", $lines);
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
public function toConfigString()
|
|
|
|
{
|
2016-06-11 00:21:24 +02:00
|
|
|
$str = implode(array(
|
2015-06-08 14:37:33 +02:00
|
|
|
$this->renderObjectHeader(),
|
2016-06-17 11:55:48 +02:00
|
|
|
$this->renderPrioritizedProperties(),
|
2015-06-08 14:37:33 +02:00
|
|
|
$this->renderImports(),
|
|
|
|
$this->renderProperties(),
|
2015-07-01 15:15:49 +02:00
|
|
|
$this->renderRanges(),
|
2015-08-03 13:43:30 +02:00
|
|
|
$this->renderArguments(),
|
2016-02-29 18:19:01 +01:00
|
|
|
$this->renderRelatedSets(),
|
2015-06-16 17:58:47 +02:00
|
|
|
$this->renderGroups(),
|
2016-03-16 13:53:34 +01:00
|
|
|
$this->renderMultiRelations(),
|
2015-12-10 11:27:28 +01:00
|
|
|
$this->renderCustomExtensions(),
|
2015-06-08 14:37:33 +02:00
|
|
|
$this->renderCustomVars(),
|
2016-02-24 23:59:50 +01:00
|
|
|
$this->renderAssignments(),
|
2015-06-08 14:37:33 +02:00
|
|
|
$this->renderSuffix()
|
|
|
|
));
|
2016-06-11 00:21:24 +02:00
|
|
|
|
|
|
|
if ($this->isDisabled()) {
|
2016-06-13 17:20:44 +02:00
|
|
|
return "/* --- This object has been disabled ---\n"
|
|
|
|
. $str . "*/\n";
|
2016-06-11 00:21:24 +02:00
|
|
|
} else {
|
|
|
|
return $str;
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function isGroup()
|
|
|
|
{
|
|
|
|
return substr($this->getType(), -5) === 'Group';
|
|
|
|
}
|
|
|
|
|
2015-11-06 09:49:05 +01:00
|
|
|
public function hasCheckCommand()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
protected function getType()
|
|
|
|
{
|
|
|
|
if ($this->type === null) {
|
|
|
|
$parts = explode('\\', get_class($this));
|
|
|
|
// 6 = strlen('Icinga');
|
|
|
|
$this->type = substr(end($parts), 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getObjectTypeName()
|
|
|
|
{
|
|
|
|
if ($this->isTemplate()) {
|
|
|
|
return 'template';
|
|
|
|
} elseif ($this->isApplyRule()) {
|
|
|
|
return 'apply';
|
|
|
|
} else {
|
|
|
|
return 'object';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getObjectName()
|
|
|
|
{
|
|
|
|
if ($this->hasProperty('object_name')) {
|
|
|
|
return $this->object_name;
|
|
|
|
} else {
|
|
|
|
// TODO: replace with an exception once finished
|
|
|
|
return 'ERROR: NO NAME';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
protected static function classByType($type)
|
2015-08-02 13:43:16 +02:00
|
|
|
{
|
2015-08-29 01:12:04 +02:00
|
|
|
// allow for icinga_host and host
|
2016-08-09 16:17:57 +02:00
|
|
|
$type = lcfirst(preg_replace('/^icinga_/', '', $type));
|
2015-08-29 01:12:04 +02:00
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
if (strpos($type, 'data') === false) {
|
|
|
|
$prefix = 'Icinga';
|
|
|
|
} else {
|
|
|
|
$prefix = 'Director';
|
|
|
|
}
|
2015-08-29 01:12:04 +02:00
|
|
|
|
2015-12-10 18:40:08 +01:00
|
|
|
// TODO: Provide a more sophisticated solution
|
2015-12-07 13:50:48 +01:00
|
|
|
if ($type === 'hostgroup') {
|
|
|
|
$type = 'hostGroup';
|
2015-12-10 18:40:08 +01:00
|
|
|
} elseif ($type === 'usergroup') {
|
|
|
|
$type = 'userGroup';
|
2016-03-16 21:46:00 +01:00
|
|
|
} elseif ($type === 'timeperiod') {
|
|
|
|
$type = 'timePeriod';
|
2015-12-10 18:40:08 +01:00
|
|
|
} elseif ($type === 'servicegroup') {
|
|
|
|
$type = 'serviceGroup';
|
2015-12-17 14:58:43 +01:00
|
|
|
} elseif ($type === 'apiuser') {
|
|
|
|
$type = 'apiUser';
|
2015-12-07 13:50:48 +01:00
|
|
|
}
|
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
return 'Icinga\\Module\\Director\\Objects\\' . $prefix . ucfirst($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createByType($type, $properties = array(), Db $db = null)
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::create($properties, $db);
|
|
|
|
}
|
2015-08-02 13:43:16 +02:00
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
public static function loadByType($type, $id, Db $db)
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::load($id, $db);
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:27:26 +01:00
|
|
|
public static function existsByType($type, $id, Db $db)
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
|
|
|
return $class::exists($id, $db);
|
|
|
|
}
|
|
|
|
|
2015-08-04 19:26:53 +02:00
|
|
|
public static function loadAllByType($type, Db $db, $query = null, $keyColumn = 'object_name')
|
|
|
|
{
|
|
|
|
$class = self::classByType($type);
|
2016-02-24 15:18:30 +01:00
|
|
|
|
|
|
|
if (is_array($class::create()->getKeyName())) {
|
|
|
|
return $class::loadAll($db, $query);
|
|
|
|
} else {
|
|
|
|
return $class::loadAll($db, $query, $keyColumn);
|
|
|
|
}
|
2015-08-02 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 13:04:34 +01:00
|
|
|
public static function fromJson($json, Db $connection = null)
|
|
|
|
{
|
|
|
|
return static::fromPlainObject(json_decode($json), $connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromPlainObject($plain, Db $connection = null)
|
|
|
|
{
|
|
|
|
return static::create((array) $plain, $connection);
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:06:48 +02:00
|
|
|
public function replaceWith(IcingaObject $object, $preserve = null)
|
2015-12-10 13:04:34 +01:00
|
|
|
{
|
2016-07-14 17:06:48 +02:00
|
|
|
if ($preserve === null) {
|
|
|
|
$this->setProperties((array) $object->toPlainObject());
|
|
|
|
} else {
|
|
|
|
$plain = (array) $object->toPlainObject();
|
|
|
|
foreach ($preserve as $k) {
|
|
|
|
$v = $this->$k;
|
|
|
|
if ($v !== null) {
|
|
|
|
$plain[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setProperties($plain);
|
|
|
|
}
|
2015-12-10 13:04:34 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:23:27 +01:00
|
|
|
// TODO: with rules? What if I want to override vars? Drop in favour of vars.x?
|
|
|
|
public function merge(IcingaObject $object)
|
|
|
|
{
|
|
|
|
$object = clone($object);
|
2016-03-11 13:56:34 +01:00
|
|
|
|
|
|
|
if ($object->supportsCustomVars()) {
|
|
|
|
$vars = $object->getVars();
|
|
|
|
$object->vars = array();
|
|
|
|
}
|
|
|
|
|
2016-02-25 19:38:52 +01:00
|
|
|
$this->setProperties((array) $object->toPlainObject(null, true));
|
2016-03-11 13:56:34 +01:00
|
|
|
|
|
|
|
if ($object->supportsCustomVars()) {
|
|
|
|
$myVars = $this->vars();
|
|
|
|
foreach ($vars as $key => $var) {
|
|
|
|
$myVars->set($key, $var);
|
|
|
|
}
|
2016-02-25 00:23:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:41:09 +01:00
|
|
|
public function toPlainObject(
|
|
|
|
$resolved = false,
|
2016-02-23 00:29:51 +01:00
|
|
|
$skipDefaults = false,
|
2016-03-06 10:27:37 +01:00
|
|
|
array $chosenProperties = null,
|
|
|
|
$resolveIds = true
|
2015-12-10 18:41:09 +01:00
|
|
|
) {
|
2015-12-02 03:08:57 +01:00
|
|
|
$props = array();
|
2015-12-10 13:04:34 +01:00
|
|
|
|
|
|
|
if ($resolved) {
|
|
|
|
$p = $this->getResolvedProperties();
|
|
|
|
} else {
|
2016-03-24 13:37:32 +01:00
|
|
|
$p = $this->properties;
|
2015-12-10 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($p as $k => $v) {
|
|
|
|
|
|
|
|
// Do not ship ids for IcingaObjects:
|
2016-03-06 10:27:37 +01:00
|
|
|
if ($resolveIds) {
|
|
|
|
if ($k === 'id' && $this->hasProperty('object_name')) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-10 13:04:34 +01:00
|
|
|
|
2016-03-06 10:27:37 +01:00
|
|
|
if ('_id' === substr($k, -3)) {
|
|
|
|
$relKey = substr($k, 0, -3);
|
2015-12-10 13:04:34 +01:00
|
|
|
|
2016-03-06 10:27:37 +01:00
|
|
|
if ($this->hasRelation($relKey)) {
|
2016-03-08 09:21:59 +01:00
|
|
|
|
|
|
|
if ($this->hasUnresolvedRelatedProperty($k)) {
|
|
|
|
$v = $this->$relKey;
|
|
|
|
} elseif ($v !== null) {
|
2016-03-06 10:27:37 +01:00
|
|
|
$v = $this->getRelatedObjectName($relKey, $v);
|
|
|
|
}
|
2015-12-10 13:04:34 +01:00
|
|
|
|
2016-03-06 10:27:37 +01:00
|
|
|
$k = $relKey;
|
2016-06-16 22:16:55 +02:00
|
|
|
} else {
|
|
|
|
throw new ProgrammingError('No such relation: %s', $relKey);
|
2016-03-06 10:27:37 +01:00
|
|
|
}
|
2015-12-10 13:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:41:09 +01:00
|
|
|
if ($chosenProperties !== null) {
|
|
|
|
if (! in_array($v, $chosenProperties)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:04:34 +01:00
|
|
|
// TODO: Do not ship null properties based on flag?
|
2016-02-23 00:29:51 +01:00
|
|
|
if (!$skipDefaults || $this->differsFromDefaultValue($k, $v)) {
|
2016-03-16 21:46:00 +01:00
|
|
|
if ($k === 'disabled' || $this->propertyIsBoolean($k)) {
|
2016-03-16 21:34:46 +01:00
|
|
|
if ($v === 'y') {
|
|
|
|
$props[$k] = true;
|
|
|
|
} elseif ($v === 'n') {
|
|
|
|
$props[$k] = false;
|
|
|
|
} else {
|
|
|
|
$props[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$props[$k] = $v;
|
|
|
|
}
|
2015-12-02 03:08:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
// TODO: resolve
|
|
|
|
$props['groups'] = $this->groups()->listGroupNames();
|
|
|
|
}
|
2016-03-16 13:01:55 +01:00
|
|
|
|
|
|
|
foreach ($this->loadAllMultiRelations() as $key => $rel) {
|
2016-03-16 15:17:58 +01:00
|
|
|
if (count($rel) || !$skipDefaults) {
|
2016-03-16 13:01:55 +01:00
|
|
|
$props[$key] = $rel->listRelatedNames();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 13:46:06 +01:00
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
// TODO: resolve
|
|
|
|
$props['arguments'] = $this->arguments()->toPlainObject(
|
|
|
|
$resolved,
|
|
|
|
$skipDefaults
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:06:31 +02:00
|
|
|
if ($this->supportsAssignments()) {
|
2016-03-24 06:46:13 +01:00
|
|
|
$props['assignments'] = $this->assignments()->getPlain();
|
|
|
|
}
|
|
|
|
|
2015-12-02 03:08:57 +01:00
|
|
|
if ($this->supportsCustomVars()) {
|
|
|
|
if ($resolved) {
|
|
|
|
$props['vars'] = $this->getResolvedVars();
|
2015-12-10 13:04:34 +01:00
|
|
|
} else {
|
|
|
|
$props['vars'] = $this->getVars();
|
2015-12-02 03:08:57 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-16 14:32:53 +01:00
|
|
|
|
2015-12-02 03:08:57 +01:00
|
|
|
if ($this->supportsImports()) {
|
2015-12-10 13:04:34 +01:00
|
|
|
if ($resolved) {
|
|
|
|
$props['imports'] = array();
|
|
|
|
} else {
|
|
|
|
$props['imports'] = $this->imports()->listImportNames();
|
|
|
|
}
|
2015-12-02 03:08:57 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 23:08:57 +01:00
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
// TODO: resolve
|
|
|
|
$props['ranges'] = $this->get('ranges');
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:29:51 +01:00
|
|
|
if ($skipDefaults) {
|
2016-03-17 23:08:57 +01:00
|
|
|
foreach (array('imports', 'ranges', 'arguments') as $key) {
|
|
|
|
if (empty($props[$key])) {
|
|
|
|
unset($props[$key]);
|
|
|
|
}
|
2016-02-26 11:58:37 +01:00
|
|
|
}
|
2016-03-17 23:08:57 +01:00
|
|
|
|
2016-02-23 00:29:51 +01:00
|
|
|
if (array_key_exists('vars', $props)) {
|
2016-02-26 11:58:37 +01:00
|
|
|
if (count((array) $props['vars']) === 0) {
|
|
|
|
unset($props['vars']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($props['groups'])) {
|
|
|
|
unset($props['groups']);
|
2016-02-23 00:29:51 +01:00
|
|
|
}
|
2016-02-03 00:58:13 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 19:32:55 +01:00
|
|
|
foreach ($this->relatedSets() as $property => $set) {
|
|
|
|
if ($resolved) {
|
2016-02-29 22:54:46 +01:00
|
|
|
if ($this->supportsImports()) {
|
|
|
|
$set = clone($set);
|
2016-06-16 21:40:22 +02:00
|
|
|
foreach ($this->imports()->getObjects() as $parent) {
|
2016-02-29 22:54:46 +01:00
|
|
|
$set->inheritFrom($parent->getRelatedSet($property));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 19:32:55 +01:00
|
|
|
$values = $set->getResolvedValues();
|
|
|
|
if (empty($values)) {
|
|
|
|
if (!$skipDefaults) {
|
|
|
|
$props[$property] = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$props[$property] = $values;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($set->isEmpty()) {
|
|
|
|
if (!$skipDefaults) {
|
|
|
|
$props[$property] = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$props[$property] = $set->toPlainObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:04:34 +01:00
|
|
|
ksort($props);
|
|
|
|
|
2015-12-03 14:20:29 +01:00
|
|
|
return (object) $props;
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:26:39 +01:00
|
|
|
protected function differsFromDefaultValue($key, $value)
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $this->defaultProperties)) {
|
|
|
|
return $value !== $this->defaultProperties[$key];
|
|
|
|
} else {
|
|
|
|
return $value !== null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 14:20:05 +01:00
|
|
|
public function getUrlParams()
|
|
|
|
{
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
if ($this->object_type === 'apply') {
|
|
|
|
$params['id'] = $this->id;
|
|
|
|
} else {
|
|
|
|
$params = array('name' => $this->object_name);
|
|
|
|
|
2016-03-16 21:46:00 +01:00
|
|
|
if ($this->hasProperty('host_id') && $this->host_id) {
|
2016-03-06 14:20:05 +01:00
|
|
|
$params['host'] = $this->host;
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:46:00 +01:00
|
|
|
if ($this->hasProperty('service_id') && $this->service_id) {
|
2016-03-06 14:20:05 +01:00
|
|
|
$params['service'] = $this->service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2016-03-20 12:02:45 +01:00
|
|
|
public function getOnDeleteUrl()
|
|
|
|
{
|
|
|
|
return 'director/' . strtolower($this->getShortTableName()) . 's';
|
|
|
|
}
|
|
|
|
|
2016-02-03 00:58:13 +01:00
|
|
|
public function toJson(
|
|
|
|
$resolved = false,
|
2016-02-23 00:29:51 +01:00
|
|
|
$skipDefaults = false,
|
2016-02-03 00:58:13 +01:00
|
|
|
array $chosenProperties = null
|
|
|
|
) {
|
2016-02-23 00:29:51 +01:00
|
|
|
|
|
|
|
return json_encode($this->toPlainObject($resolved, $skipDefaults, $chosenProperties));
|
2015-12-02 03:08:57 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 00:33:05 +01:00
|
|
|
public function getPlainUnmodifiedObject()
|
|
|
|
{
|
|
|
|
$props = array();
|
2016-03-08 21:30:12 +01:00
|
|
|
|
2016-02-23 00:33:05 +01:00
|
|
|
foreach ($this->getOriginalProperties() as $k => $v) {
|
|
|
|
// Do not ship ids for IcingaObjects:
|
|
|
|
if ($k === 'id' && $this->hasProperty('object_name')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('_id' === substr($k, -3)) {
|
|
|
|
$relKey = substr($k, 0, -3);
|
|
|
|
|
|
|
|
if ($this->hasRelation($relKey)) {
|
|
|
|
if ($v !== null) {
|
|
|
|
$v = $this->getRelatedObjectName($relKey, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
$k = $relKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->differsFromDefaultvalue($k, $v)) {
|
|
|
|
$props[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->supportsCustomVars()) {
|
|
|
|
$props['vars'] = (object) array();
|
|
|
|
foreach ($this->vars()->getOriginalVars() as $name => $var) {
|
|
|
|
$props['vars']->$name = $var->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->supportsGroups()) {
|
|
|
|
$groups = $this->groups()->listOriginalGroupNames();
|
|
|
|
if (! empty($groups)) {
|
|
|
|
$props['groups'] = $groups;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($this->supportsImports()) {
|
|
|
|
$imports = $this->imports()->listOriginalImportNames();
|
|
|
|
if (! empty($imports)) {
|
|
|
|
$props['imports'] = $imports;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 13:46:06 +01:00
|
|
|
if ($this->supportsArguments()) {
|
|
|
|
$args = $this->arguments()->toUnmodifiedPlainObject();
|
2016-04-03 21:27:49 +02:00
|
|
|
if (! empty($args)) {
|
|
|
|
$props['arguments'] = $args;
|
2016-03-18 13:46:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:08:57 +01:00
|
|
|
if ($this->supportsRanges()) {
|
|
|
|
$ranges = $this->ranges()->getOriginalValues();
|
|
|
|
if (!empty($ranges)) {
|
|
|
|
$props['ranges'] = $ranges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:06:31 +02:00
|
|
|
if ($this->supportsAssignments()) {
|
2016-03-24 11:40:32 +01:00
|
|
|
$props['assignments'] = $this->assignments()->getUnmodifiedPlain();
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:54:46 +01:00
|
|
|
foreach ($this->relatedSets() as $property => $set) {
|
2016-03-01 04:23:31 +01:00
|
|
|
if ($set->isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:54:46 +01:00
|
|
|
$props[$property] = $set->getPlainUnmodifiedObject();
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:46:00 +01:00
|
|
|
foreach ($this->loadAllMultiRelations() as $key => $rel) {
|
2016-03-17 08:27:11 +01:00
|
|
|
$old = $rel->listOriginalNames();
|
|
|
|
if (! empty($old)) {
|
|
|
|
$props[$key] = $old;
|
2016-03-16 21:46:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:30:12 +01:00
|
|
|
return (object) $props;
|
2016-02-23 00:33:05 +01:00
|
|
|
}
|
|
|
|
|
2015-06-08 14:37:33 +02:00
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return $this->toConfigString();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
trigger_error($e);
|
2016-02-26 11:58:37 +01:00
|
|
|
$previousHandler = set_exception_handler(
|
|
|
|
function () {
|
|
|
|
}
|
|
|
|
);
|
2015-06-08 14:37:33 +02:00
|
|
|
restore_error_handler();
|
2015-06-15 16:36:51 +02:00
|
|
|
if ($previousHandler !== null) {
|
|
|
|
call_user_func($previousHandler, $e);
|
|
|
|
die();
|
|
|
|
} else {
|
|
|
|
die($e->getMessage());
|
|
|
|
}
|
2015-06-08 14:37:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 16:58:32 +01:00
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
unset($this->resolveCache);
|
|
|
|
unset($this->vars);
|
|
|
|
unset($this->groups);
|
|
|
|
unset($this->imports);
|
|
|
|
unset($this->ranges);
|
|
|
|
unset($this->arguments);
|
|
|
|
|
|
|
|
|
|
|
|
parent::__destruct();
|
|
|
|
}
|
2015-04-24 15:57:01 +02:00
|
|
|
}
|