Hook for custom field in Director form
This commit is contained in:
parent
6889c63a69
commit
08e38f2c75
|
@ -0,0 +1,184 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Icinga\Module\Director\Field;
|
||||
|
||||
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
|
||||
class FieldSpec
|
||||
{
|
||||
/** @var string */
|
||||
protected $varName;
|
||||
|
||||
/** @var string */
|
||||
protected $caption;
|
||||
|
||||
/** @var boolean */
|
||||
protected $isRequired = false;
|
||||
|
||||
/** @var string */
|
||||
protected $description;
|
||||
|
||||
/** @var string */
|
||||
protected $dataType;
|
||||
|
||||
/** @var string */
|
||||
protected $varFilter;
|
||||
|
||||
/** @var string */
|
||||
protected $format = "string";
|
||||
|
||||
/**
|
||||
* FieldSpec constructor.
|
||||
* @param $dataType
|
||||
* @param $varName
|
||||
* @param $caption
|
||||
*/
|
||||
public function __construct($dataType, $varName, $caption)
|
||||
{
|
||||
$this->dataType = $dataType;
|
||||
$this->varName = $varName;
|
||||
$this->caption = $caption;
|
||||
}
|
||||
|
||||
public function toDataField(IcingaObject $object)
|
||||
{
|
||||
return DirectorDatafield::create([
|
||||
'varname' => $this->getVarName(),
|
||||
'caption' => $this->getCaption(),
|
||||
'description' => $this->getDescription(),
|
||||
'datatype' => $this->getDataType(),
|
||||
'format' => $this->getFormat(),
|
||||
'var_filter' => $this->getVarFilter(),
|
||||
'icinga_type' => $object->getShortTableName(),
|
||||
'object_id' => $object->get('id')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getVarName()
|
||||
{
|
||||
return $this->varName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $varName
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setVarName($varName)
|
||||
{
|
||||
$this->varName = $varName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCaption()
|
||||
{
|
||||
return $this->caption;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $caption
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setCaption($caption)
|
||||
{
|
||||
$this->caption = $caption;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->isRequired;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isRequired
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setIsRequired($isRequired)
|
||||
{
|
||||
$this->isRequired = $isRequired;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDataType()
|
||||
{
|
||||
return $this->dataType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dataType
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setDataType($dataType)
|
||||
{
|
||||
$this->dataType = $dataType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getVarFilter()
|
||||
{
|
||||
return $this->varFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $varFilter
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setVarFilter($varFilter)
|
||||
{
|
||||
$this->varFilter = $varFilter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFormat()
|
||||
{
|
||||
return $this->format;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $format
|
||||
* @return FieldSpec
|
||||
*/
|
||||
public function setFormat($format)
|
||||
{
|
||||
$this->format = $format;
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Hook;
|
||||
|
||||
use Icinga\Module\Director\Field\FieldSpec;
|
||||
use Icinga\Module\Director\Objects\IcingaHost;
|
||||
|
||||
abstract class HostFieldHook
|
||||
{
|
||||
public function wants(IcingaHost $host)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FieldSpec
|
||||
*/
|
||||
abstract public function getFieldSpec(IcingaHost $host);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Hook;
|
||||
|
||||
use Icinga\Module\Director\Field\FieldSpec;
|
||||
use Icinga\Module\Director\Objects\IcingaService;
|
||||
|
||||
abstract class ServiceFieldHook
|
||||
{
|
||||
public function wants(IcingaService $service)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FieldSpec
|
||||
*/
|
||||
abstract public function getFieldSpec(IcingaService $service);
|
||||
}
|
|
@ -7,12 +7,15 @@ use Icinga\Data\Filter\Filter;
|
|||
use Icinga\Data\Filter\FilterChain;
|
||||
use Icinga\Data\Filter\FilterExpression;
|
||||
use Icinga\Exception\IcingaException;
|
||||
use Icinga\Module\Director\Hook\HostFieldHook;
|
||||
use Icinga\Module\Director\Hook\ServiceFieldHook;
|
||||
use Icinga\Module\Director\Objects\IcingaCommand;
|
||||
use Icinga\Module\Director\Objects\IcingaHost;
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Icinga\Module\Director\Objects\DirectorDatafield;
|
||||
use Icinga\Module\Director\Objects\IcingaService;
|
||||
use Icinga\Module\Director\Objects\ObjectApplyMatches;
|
||||
use Icinga\Web\Hook;
|
||||
use stdClass;
|
||||
use Zend_Db_Select as ZfSelect;
|
||||
use Zend_Form_Element as ZfElement;
|
||||
|
@ -519,7 +522,7 @@ class IcingaObjectFieldLoader
|
|||
{
|
||||
$res = $this->fetchFieldDetailsForObject($object);
|
||||
|
||||
$result = array();
|
||||
$result = [];
|
||||
foreach ($res as $r) {
|
||||
$id = $r->object_id;
|
||||
unset($r->object_id);
|
||||
|
@ -533,6 +536,52 @@ class IcingaObjectFieldLoader
|
|||
);
|
||||
}
|
||||
|
||||
foreach ($this->loadHookedDataFieldForObject($object) as $id => $fields) {
|
||||
if (array_key_exists($id, $fields)) {
|
||||
foreach ($fields as $varName => $field) {
|
||||
$result[$id]->$varName = $field;
|
||||
}
|
||||
} else {
|
||||
$result[$id] = $fields;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IcingaObject $object
|
||||
* @return array
|
||||
*/
|
||||
protected function loadHookedDataFieldForObject(IcingaObject $object)
|
||||
{
|
||||
$fields = [];
|
||||
if ($object instanceof IcingaHost || $object instanceof IcingaService) {
|
||||
$fields = $this->addHookedFields($object);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param IcingaObject $object
|
||||
* @return mixed
|
||||
*/
|
||||
protected function addHookedFields(IcingaObject $object)
|
||||
{
|
||||
$fields = [];
|
||||
/** @var HostFieldHook|ServiceFieldHook $hook */
|
||||
$type = ucfirst($object->getShortTableName());
|
||||
foreach (Hook::all("Director\\${type}Field") as $hook) {
|
||||
if ($hook->wants($object)) {
|
||||
$id = $object->get('id');
|
||||
$spec = $hook->getFieldSpec($object);
|
||||
if (!array_key_exists($id, $fields)) {
|
||||
$fields[$id] = new stdClass();
|
||||
}
|
||||
$fields[$id]->{$spec->getVarName()} = $spec->toDataField($object);
|
||||
}
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue