application/forms: more object form abstraction

This commit is contained in:
Thomas Gelf 2016-03-10 20:44:12 +01:00
parent e8a28a12da
commit d3957c276c
5 changed files with 171 additions and 126 deletions

View File

@ -11,7 +11,7 @@ class IcingaHostForm extends DirectorObjectForm
{ {
$this->addObjectTypeElement(); $this->addObjectTypeElement();
if (! $this->hasObjectType()) { if (! $this->hasObjectType()) {
return; return $this->groupMainProperties();
} }
$this->addElement('text', 'object_name', array( $this->addElement('text', 'object_name', array(
@ -25,53 +25,13 @@ class IcingaHostForm extends DirectorObjectForm
) )
)); ));
$this->addGroupsElement(); $this->addGroupsElement()
$this->addImportsElement(); ->addImportsElement()
->addDisplayNameElement()
$this->addElement('text', 'display_name', array( ->addAddressElements()
'label' => $this->translate('Display name'), ->addDisabledElement()
'description' => $this->translate( ->groupMainProperties()
'Alternative name for this host. Might be a host alias or and kind' ->addZoneElement();
. ' of string helping your users to identify this host'
)
));
$this->addElement('text', 'address', array(
'label' => $this->translate('Host address'),
'description' => $this->translate(
'Host address. Usually an IPv4 address, but may be any kind of address'
. ' your check plugin is able to deal with'
)
));
$this->addElement('text', 'address6', array(
'label' => $this->translate('IPv6 address'),
'description' => $this->translate('Usually your hosts main IPv6 address')
));
$this->addDisabledElement();
$elements = array(
'object_type',
'object_name',
'display_name',
'address',
'address6',
'imports',
'groups',
'disabled',
);
$this->addDisplayGroup($elements, 'object_definition', array(
'decorators' => array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset',
),
'order' => 20,
'legend' => $this->translate('Host properties')
));
$this->addZoneElement();
$this->addBoolean('has_agent', array( $this->addBoolean('has_agent', array(
'label' => $this->translate('Icinga2 Agent'), 'label' => $this->translate('Icinga2 Agent'),
@ -112,14 +72,9 @@ class IcingaHostForm extends DirectorObjectForm
'legend' => $this->translate('Icinga Agent and zone settings') 'legend' => $this->translate('Icinga Agent and zone settings')
)); ));
if ($this->isTemplate()) { $this->addCheckCommandElements()
$this->addCheckCommandElements(); ->addCheckExecutionElements()
$this->addCheckExecutionElements(); ->setButtons();
} else {
$this->getElement('imports')->setRequired();
}
$this->setButtons();
} }
protected function addGroupsElement() protected function addGroupsElement()
@ -145,6 +100,45 @@ class IcingaHostForm extends DirectorObjectForm
return $this; return $this;
} }
protected function addAddressElements()
{
if ($this->isTemplate()) {
return $this;
}
$this->addElement('text', 'address', array(
'label' => $this->translate('Host address'),
'description' => $this->translate(
'Host address. Usually an IPv4 address, but may be any kind of address'
. ' your check plugin is able to deal with'
)
));
$this->addElement('text', 'address6', array(
'label' => $this->translate('IPv6 address'),
'description' => $this->translate('Usually your hosts main IPv6 address')
));
return $this;
}
protected function addDisplayNameElement()
{
if ($this->isTemplate()) {
return $this;
}
$this->addElement('text', 'display_name', array(
'label' => $this->translate('Display name'),
'description' => $this->translate(
'Alternative name for this host. Might be a host alias or and kind'
. ' of string helping your users to identify this host'
)
));
return $this;
}
protected function enumHostgroups() protected function enumHostgroups()
{ {
$db = $this->db->getDbAdapter(); $db = $this->db->getDbAdapter();

View File

@ -10,6 +10,7 @@ class IcingaNotificationForm extends DirectorObjectForm
{ {
$this->addObjectTypeElement(); $this->addObjectTypeElement();
if (! $this->hasObjectType()) { if (! $this->hasObjectType()) {
$this->groupMainProperties();
return; return;
} }
@ -19,8 +20,11 @@ class IcingaNotificationForm extends DirectorObjectForm
'description' => $this->translate('Icinga object name for this notification') 'description' => $this->translate('Icinga object name for this notification')
)); ));
$this->addDisabledElement()
$this->addDisabledElement(); ->addImportsElement()
$this->setButtons(); ->addDisabledElement()
->addEventFilterElements()
->groupMainProperties()
->setButtons();
} }
} }

View File

@ -10,99 +10,98 @@ class IcingaServiceForm extends DirectorObjectForm
{ {
$this->addObjectTypeElement(); $this->addObjectTypeElement();
if (! $this->hasObjectType()) { if (! $this->hasObjectType()) {
$this->groupObjectDefinition(); return $this->groupMainProperties();
return;
} }
$this->addElement('text', 'object_name', array( $this->addElement('text', 'object_name', array(
'label' => $this->translate('Name'), 'label' => $this->translate('Name'),
'required' => true, 'required' => true,
'description' => $this->translate('Name for the Icinga object you are going to create') 'description' => $this->translate(
'Name for the Icinga object you are going to create'
)
)); ));
// TODO: Should not be 'object' on new empty form: $this->addHostObjectElement()
->addImportsElement()
->addGroupsElement()
->addDisabledElement()
->groupMainProperties()
->addCheckCommandElements()
->addCheckExecutionElements()
->addAgentAndZoneElements()
->setButtons();
}
protected function addHostObjectElement()
{
if ($this->isObject()) { if ($this->isObject()) {
$this->addElement('select', 'host_id', array( $this->addElement('select', 'host_id', array(
'label' => $this->translate('Host'), 'label' => $this->translate('Host'),
'required' => true, 'required' => true,
'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()), 'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()),
'description' => $this->translate('Choose the host this single service should be assigned to') 'description' => $this->translate(
'Choose the host this single service should be assigned to'
)
)); ));
} }
return $this;
}
protected function addGroupsElement()
{
$groups = $this->enumServicegroups(); $groups = $this->enumServicegroups();
if (! empty($groups)) { if (! empty($groups)) {
$this->addElement('extensibleSet', 'groups', array( $this->addElement('extensibleSet', 'groups', array(
'label' => $this->translate('Groups'), 'label' => $this->translate('Groups'),
'multiOptions' => $this->optionallyAddFromEnum($groups), 'multiOptions' => $this->optionallyAddFromEnum($groups),
'positional' => false, 'positional' => false,
'description' => $this->translate( 'description' => $this->translate(
'Service groups that should be directly assigned to this service. Servicegroups can be useful' 'Service groups that should be directly assigned to this service.'
. ' for various reasons. They are helpful to provided service-type specific view in Icinga Web 2,' . ' Servicegroups can be useful for various reasons. They are'
. ' either for custom dashboards or as an instrument to enforce restrictior. Service groups' . ' helpful to provided service-type specific view in Icinga Web 2,'
. ' can be directly assigned to single services or to service templates.' . ' either for custom dashboards or as an instrument to enforce'
. ' restrictions. Service groups can be directly assigned to'
. ' single services or to service templates.'
) )
)); ));
} }
$this->addImportsElement(); return $this;
$this->addDisabledElement();
$this->groupObjectDefinition();
$this->addCheckCommandElements();
if ($this->isTemplate()) {
$this->addCheckExecutionElements();
}
if ($this->isTemplate()) {
$this->optionalBoolean(
'use_agent',
$this->translate('Run on agent'),
$this->translate('Whether the check commmand for this service should be executed on the Icinga agent')
);
$this->addZoneElement();
$elements = array(
'use_agent',
'zone_id',
);
$this->addDisplayGroup($elements, 'clustering', array(
'decorators' => array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset',
),
'order' => 40,
'legend' => $this->translate('Icinga Agent and zone settings')
));
}
$this->setButtons();
} }
protected function addAgentAndZoneElements()
protected function groupObjectDefinition()
{ {
$elements = array( if ($this->isTemplate()) {
'object_type', return $this;
'object_name', }
'display_name',
'imports', $this->optionalBoolean(
'host_id', 'use_agent',
'groups', $this->translate('Run on agent'),
'disabled', $this->translate(
'Whether the check commmand for this service should be executed'
. ' on the Icinga agent'
)
); );
$this->addDisplayGroup($elements, 'object_definition', array( $this->addZoneElement();
$elements = array(
'use_agent',
'zone_id',
);
$this->addDisplayGroup($elements, 'clustering', array(
'decorators' => array( 'decorators' => array(
'FormElements', 'FormElements',
array('HtmlTag', array('tag' => 'dl')), array('HtmlTag', array('tag' => 'dl')),
'Fieldset', 'Fieldset',
), ),
'order' => 20, 'order' => 40,
'legend' => $this->translate('Service properties') 'legend' => $this->translate('Icinga Agent and zone settings')
)); ));
return $this;
} }
protected function enumHostsAndTemplates() protected function enumHostsAndTemplates()

View File

@ -10,8 +10,7 @@ class IcingaUserForm extends DirectorObjectForm
{ {
$this->addObjectTypeElement(); $this->addObjectTypeElement();
if (! $this->hasObjectType()) { if (! $this->hasObjectType()) {
$this->groupObjectDefinition(); return $this->groupMainProperties();
return;
} }
if ($this->isTemplate()) { if ($this->isTemplate()) {
@ -38,20 +37,24 @@ class IcingaUserForm extends DirectorObjectForm
'description' => $this->translate('The pager address of the user.') 'description' => $this->translate('The pager address of the user.')
)); ));
$this->addGroupsElement(); $this->addGroupsElement()
$this->addImportsElement(); ->addImportsElement()
->addEnableNotificationsElement()
->addDisabledElement()
->addEventFilterElements()
->groupMainProperties()
->setButtons();
}
protected function addEnableNotificationsElement()
{
$this->optionalBoolean( $this->optionalBoolean(
'enable_notifications', 'enable_notifications',
$this->translate('Send notifications'), $this->translate('Send notifications'),
$this->translate('Whether to send notifications for this user') $this->translate('Whether to send notifications for this user')
); );
$this->addDisabledElement();
$this->groupObjectDefinition();
return $this;
$this->addEventFilterElements();
$this->setButtons();
} }
protected function addGroupsElement() protected function addGroupsElement()

View File

@ -404,6 +404,36 @@ abstract class DirectorObjectForm extends QuickForm
} }
} }
protected function groupMainProperties()
{
$elements = array(
'object_type',
'object_name',
'display_name',
'imports',
'host_id',
'address',
'address6',
'groups',
'email',
'pager',
'enable_notifications',
'disabled',
);
$this->addDisplayGroup($elements, 'object_definition', array(
'decorators' => array(
'FormElements',
array('HtmlTag', array('tag' => 'dl')),
'Fieldset',
),
'order' => 20,
'legend' => $this->translate('Main properties')
));
return $this;
}
protected function addField($field, $value = null, $inherited = null, $inheritedFrom = null) protected function addField($field, $value = null, $inherited = null, $inheritedFrom = null)
{ {
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb()); $datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
@ -972,6 +1002,10 @@ print_r($object);
protected function addDisabledElement() protected function addDisabledElement()
{ {
if ($this->isTemplate()) {
return $this;
}
$this->addBoolean( $this->addBoolean(
'disabled', 'disabled',
array( array(
@ -999,6 +1033,10 @@ print_r($object);
protected function addCheckCommandElements() protected function addCheckCommandElements()
{ {
if ($this->isTemplate()) {
return $this;
}
$this->addElement('select', 'check_command_id', array( $this->addElement('select', 'check_command_id', array(
'label' => $this->translate('Check command'), 'label' => $this->translate('Check command'),
'description' => $this->translate('Check command definition'), 'description' => $this->translate('Check command definition'),
@ -1006,10 +1044,15 @@ print_r($object);
'class' => 'autosubmit', // This influences fields 'class' => 'autosubmit', // This influences fields
)); ));
$this->addToCheckExecutionDisplayGroup('check_command_id'); $this->addToCheckExecutionDisplayGroup('check_command_id');
return $this;
} }
protected function addCheckExecutionElements() protected function addCheckExecutionElements()
{ {
if ($this->isTemplate()) {
return $this;
}
$this->addElement( $this->addElement(
'text', 'text',
@ -1130,6 +1173,8 @@ print_r($object);
'order' =>70, 'order' =>70,
'legend' => $this->translate('State and transition type filters') 'legend' => $this->translate('State and transition type filters')
)); ));
return $this;
} }
protected function enumStates() protected function enumStates()