diff --git a/library/Director/Objects/IcingaObject.php b/library/Director/Objects/IcingaObject.php index e3908215..a8fc6221 100644 --- a/library/Director/Objects/IcingaObject.php +++ b/library/Director/Objects/IcingaObject.php @@ -66,6 +66,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer protected $loadedMultiRelations = array(); + /** + * Allows to set properties pointing to related objects by name without + * loading the related object. + * + * @var array + */ protected $unresolvedRelatedProperties = array(); protected $loadedRelatedSets = array(); @@ -117,6 +123,26 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return array_key_exists($property, $this->intervalProperties); } + 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) { + return $end; + } + + return false; + } + public function propertyIsRelatedSet($property) { return array_key_exists($property, $this->relatedSets); @@ -215,6 +241,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return __NAMESPACE__ . '\\' . $this->relations[$property]; } + protected function getRelationObjectClass($property) + { + return $this->relations[$property]; + } + protected function getRelatedObjectName($property, $id) { return $this->getRelatedObject($property, $id)->object_name; @@ -317,6 +348,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer return $this->isApplyRule(); } + /** + * 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 + */ public function resolveUnresolvedRelatedProperties() { foreach ($this->unresolvedRelatedProperties as $name => $p) { @@ -1291,6 +1331,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer } } + protected function renderLegacyRelationProperty($propertyName, $id, $renderKey = null) + { + return $this->renderLegacyObjectProperty( + $renderKey ?: $propertyName, + c::renderString($this->getRelatedObjectName($propertyName, $id)) + ); + } + // Disabled is a virtual property protected function renderDisabled() {