DirectorObjectForm: reorganize, simplify, improve
This commit is contained in:
parent
f5bf209a8a
commit
591d933929
|
@ -41,22 +41,62 @@ abstract class DirectorObjectForm extends QuickForm
|
|||
return;
|
||||
}
|
||||
|
||||
if ($object->supportsCustomVars()) {
|
||||
$this->addElement('note', '_newvar_hint', array('label' => 'New custom variable'));
|
||||
$this->addElement('text', '_newvar_name', array(
|
||||
'label' => 'Name'
|
||||
));
|
||||
$this->addElement('text', '_newvar_value', array(
|
||||
'label' => 'Value'
|
||||
));
|
||||
$this->addElement('select', '_newvar_format', array(
|
||||
'label' => 'Type',
|
||||
'multiOptions' => array('string' => $this->translate('String'))
|
||||
));
|
||||
}
|
||||
|
||||
if (false && $object->supportsRanges()) {
|
||||
/* TODO implement when new logic is there
|
||||
protected function isTemplate()
|
||||
{
|
||||
return $this->objectType === 'template';
|
||||
}
|
||||
|
||||
protected function handleImports($object, & $values)
|
||||
{
|
||||
if (! $object->supportsImports()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (array_key_exists('imports', $values)) {
|
||||
$value = $values['imports'];
|
||||
unset($values['imports']);
|
||||
$object->clearImportedObjects();
|
||||
$object->imports()->set($value);
|
||||
}
|
||||
|
||||
$el = $this->getElement('imports');
|
||||
if ($el) {
|
||||
$el->setMultiOptions($this->enumAllowedTemplates());
|
||||
$el->setValue($object->imports()->listImportNames());
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleRanges($object, & $values)
|
||||
{
|
||||
if (! $object->supportsRanges()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$key = 'ranges';
|
||||
$object = $this->object();
|
||||
|
||||
/* Sample:
|
||||
|
||||
array(
|
||||
'monday' => 'eins',
|
||||
'tuesday' => '00:00-24:00',
|
||||
'sunday' => 'zwei',
|
||||
);
|
||||
|
||||
*/
|
||||
if (array_key_exists($key, $values)) {
|
||||
$object->ranges()->set($values[$key]);
|
||||
unset($values[$key]);
|
||||
}
|
||||
|
||||
foreach ($object->ranges()->getRanges() as $key => $value) {
|
||||
$this->addRange($key, $value);
|
||||
}
|
||||
|
||||
/*
|
||||
// TODO implement when new logic is there
|
||||
$this->addElement('note', '_newrange_hint', array('label' => 'New range'));
|
||||
$this->addElement('text', '_newrange_name', array(
|
||||
'label' => 'Name'
|
||||
|
@ -66,30 +106,59 @@ abstract class DirectorObjectForm extends QuickForm
|
|||
));
|
||||
*/
|
||||
}
|
||||
|
||||
protected function handleGroups($object, & $values)
|
||||
{
|
||||
if (! $object->supportsGroups()) {
|
||||
return;
|
||||
}
|
||||
|
||||
protected function isTemplate()
|
||||
{
|
||||
return $this->objectType === 'template';
|
||||
}
|
||||
|
||||
protected function handleIcingaObject(& $values)
|
||||
{
|
||||
$object = $this->object();
|
||||
$handled = array();
|
||||
|
||||
if ($object->supportsGroups()) {
|
||||
if (array_key_exists('groups', $values)) {
|
||||
$object->groups()->set(
|
||||
preg_split('/\s*,\s*/', $values['groups'], -1, PREG_SPLIT_NO_EMPTY)
|
||||
);
|
||||
$handled['groups'] = true;
|
||||
$value = $values['groups'];
|
||||
unset($values['groups']);
|
||||
|
||||
// TODO: Drop this once we have arrays everwhere
|
||||
if (is_string($value)) {
|
||||
$value = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
$object->groups()->set($value);
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->supportsCustomVars()) {
|
||||
protected function handleProperties($object, & $values)
|
||||
{
|
||||
if ($this->hasBeenSent()) {
|
||||
$object->setProperties($values);
|
||||
}
|
||||
|
||||
$props = $object->getProperties();
|
||||
if (! $object instanceof IcingaObject) {
|
||||
$this->setDefaults($props);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$inherited = $object->getInheritedProperties();
|
||||
$origins = $object->getOriginsProperties();
|
||||
|
||||
foreach ($props as $k => $v) {
|
||||
if (property_exists($inherited, $k)) {
|
||||
$this->setElementValue($k, $v, $inherited->$k, $origins->$k);
|
||||
} else {
|
||||
$this->setElementValue($k, $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function handleCustomVars($object, & $values)
|
||||
{
|
||||
if (! $object->supportsCustomVars()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->hasBeenSent()) {
|
||||
$vars = array();
|
||||
$handled = array();
|
||||
$newvar = array(
|
||||
'type' => 'string',
|
||||
'name' => null,
|
||||
|
@ -109,67 +178,93 @@ abstract class DirectorObjectForm extends QuickForm
|
|||
}
|
||||
|
||||
foreach ($vars as $k => $v) {
|
||||
$this->object->vars()->$k = $v;
|
||||
if ($v === '' || $v === null) {
|
||||
unset($object->vars()->$k);
|
||||
} else {
|
||||
$object->vars()->$k = $v;
|
||||
}
|
||||
}
|
||||
|
||||
if ($newvar['name'] && $newvar['value']) {
|
||||
$this->object->vars()->{$newvar['name']} = $newvar['value'];
|
||||
}
|
||||
$object->vars()->{$newvar['name']} = $newvar['value'];
|
||||
}
|
||||
|
||||
if ($object->supportsImports()) {
|
||||
if (array_key_exists('imports', $values)) {
|
||||
$value = $values['imports'];
|
||||
|
||||
// TODO: Compat for comma separated string, check if still needed
|
||||
if (! is_array($value)) {
|
||||
$value = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
|
||||
}
|
||||
|
||||
$object->imports()->set($value);
|
||||
$handled['imports'] = true;
|
||||
$object->clearImportedObjects();
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->supportsRanges()) {
|
||||
$object->ranges()->set(array(
|
||||
'monday' => 'eins',
|
||||
'tuesday' => '00:00-24:00',
|
||||
'sunday' => 'zwei',
|
||||
));
|
||||
}
|
||||
|
||||
foreach ($handled as $key => $value) {
|
||||
foreach ($handled as $key) {
|
||||
unset($values[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
$vars = $object->getVars();
|
||||
|
||||
$fields = $object->getResolvedFields();
|
||||
$inherits = $object->getInheritedVars();
|
||||
$origins = $object->getOriginsVars();
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$varname = $field->varname;
|
||||
|
||||
// Get value from the related varname if set:
|
||||
if (property_exists($vars, $varname)) {
|
||||
$value = $vars->$varname;
|
||||
} else {
|
||||
$value = null;
|
||||
}
|
||||
|
||||
if (property_exists($inherits, $varname)) {
|
||||
$inheritedValue = $inherits->$varname;
|
||||
$inheritFrom = $origins->$varname;
|
||||
if ($inheritFrom === $object->object_name) {
|
||||
$inherited = false;
|
||||
} else {
|
||||
$inherited = true;
|
||||
}
|
||||
} else {
|
||||
$inheritedValue = null;
|
||||
$inheritFrom = false;
|
||||
$inherited = false;
|
||||
}
|
||||
|
||||
$this->addField($field, $value, $inheritedValue, $inheritFrom);
|
||||
}
|
||||
|
||||
// Additional vars
|
||||
foreach ($vars as $key => $value) {
|
||||
// Did we already create a field for this var? Then skip it:
|
||||
if (array_key_exists($key, $fields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Show inheritance information in case we inherited this var:
|
||||
if (isset($inherited->$key)) {
|
||||
$this->addCustomVar($key, $value, $inherited->$key, $origins->$key);
|
||||
} else {
|
||||
$this->addCustomVar($key, $value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ($object->isTemplate()) {
|
||||
$this->addHtml('<h3>Add a custom variable</h3>');
|
||||
$this->addElement('text', '_newvar_name', array(
|
||||
'label' => 'Name'
|
||||
));
|
||||
$this->addElement('text', '_newvar_value', array(
|
||||
'label' => 'Value'
|
||||
));
|
||||
$this->addElement('select', '_newvar_format', array(
|
||||
'label' => 'Type',
|
||||
'multiOptions' => array('string' => $this->translate('String'))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function setObjectType($type)
|
||||
{
|
||||
$this->objectType = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFields()
|
||||
{
|
||||
$object = $this->object();
|
||||
$fields = $object->getResolvedFields();
|
||||
$vars = $object->vars();
|
||||
|
||||
foreach ($fields as $field) {
|
||||
$varname = $field->varname;
|
||||
if (isset($vars->$varname)) {
|
||||
$value = $vars->{$varname}->getValue();
|
||||
} else {
|
||||
$value = null;
|
||||
}
|
||||
$inherited = null; // Just testing
|
||||
$this->addField($field, $value, $inherited);
|
||||
}
|
||||
}
|
||||
|
||||
protected function addField($field, $value = null, $inherited = null)
|
||||
protected function addField($field, $value = null, $inherited = null, $inheritedFrom = null)
|
||||
{
|
||||
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
|
||||
$datatype = new $datafield->datatype;
|
||||
|
@ -181,15 +276,17 @@ $inherited = null; // Just testing
|
|||
$el->setLabel($datafield->caption);
|
||||
$el->setDescription($datafield->description);
|
||||
|
||||
if ($field->is_required === 'y') {
|
||||
if ($field->is_required === 'y' && ! $this->isTemplate()) {
|
||||
$el->setRequired(true);
|
||||
}
|
||||
|
||||
$this->addElement($el);
|
||||
$this->setElementValue($name, $value, $inherited);
|
||||
$this->setElementValue($name, $value, $inherited, $inheritedFrom);
|
||||
|
||||
return $el;
|
||||
}
|
||||
|
||||
protected function setElementValue($name, $value = null, $inherited = null)
|
||||
protected function setElementValue($name, $value = null, $inherited = null, $inheritedFrom = null)
|
||||
{
|
||||
$el = $this->getElement($name);
|
||||
if (! $el) {
|
||||
|
@ -200,44 +297,38 @@ $inherited = null; // Just testing
|
|||
$el->setValue($value);
|
||||
}
|
||||
|
||||
if ($inherited === null) {
|
||||
if ($inherited === null || empty($inherited)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$strInherited = $this->translate('(inherited)');
|
||||
if ($el instanceof Zf_Select) {
|
||||
$multi = $el->getMultiOptions();
|
||||
if (array_key_exists($inherited, $multi)) {
|
||||
$multi[null] = $multi[$inherited] . ' ' . $strInherited;
|
||||
$multi[null] = $multi[$inherited] . sprintf(' (%s)', $inheritedFrom);
|
||||
} else {
|
||||
$multi[null] = $strInherited;
|
||||
$multi[null] = $this->translate('- inherited -');
|
||||
}
|
||||
$el->setMultiOptions($multi);
|
||||
} else {
|
||||
$el->setAttrib('placeholder', $inherited . ' ' . $strInherited);
|
||||
$el->setAttrib('placeholder', $inherited . sprintf(' (%s)', $inheritedFrom));
|
||||
}
|
||||
}
|
||||
|
||||
public function onSuccess()
|
||||
{
|
||||
$object = $this->object;
|
||||
$values = $this->getValues();
|
||||
if ($object instanceof IcingaObject) {
|
||||
$this->handleIcingaObject($values);
|
||||
if (! array_key_exists('object_type', $values)) {
|
||||
$object->object_type = $this->objectType;
|
||||
}
|
||||
}
|
||||
$object->setProperties($values);
|
||||
$object = $this->object();
|
||||
if ($object->hasBeenModified()) {
|
||||
|
||||
$msg = sprintf(
|
||||
$object->hasBeenLoadedFromDb()
|
||||
? 'The Icinga %s has successfully been stored'
|
||||
: 'A new Icinga %s has successfully been created',
|
||||
? $this->translate('The Icinga %s has successfully been stored')
|
||||
: $this->translate('A new Icinga %s has successfully been created'),
|
||||
$this->translate($this->getObjectName())
|
||||
);
|
||||
|
||||
$object->store($this->db);
|
||||
} else {
|
||||
$msg = $this->translate('No action taken, object has not been modified');
|
||||
}
|
||||
$this->redirectOnSuccess($msg);
|
||||
}
|
||||
|
||||
|
@ -288,71 +379,54 @@ $inherited = null; // Just testing
|
|||
return $this->objectName;
|
||||
}
|
||||
|
||||
protected function onRequest()
|
||||
{
|
||||
$object = $this->object();
|
||||
$values = array();
|
||||
if ($this->hasBeenSent()) {
|
||||
$post = $this->getRequest()->getPost();
|
||||
foreach ($post as $key => $value) {
|
||||
$el = $this->getElement($key);
|
||||
if ($el && ! $el->getIgnore()) {
|
||||
$values[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($object instanceof IcingaObject) {
|
||||
if (! $object->hasBeenLoadedFromDb()) {
|
||||
$object->object_type = $this->objectType;
|
||||
}
|
||||
$this->handleImports($object, $values);
|
||||
$this->handleCustomVars($object, $values);
|
||||
$this->handleGroups($object, $values);
|
||||
$this->handleRanges($object, $values);
|
||||
}
|
||||
|
||||
$this->handleProperties($object, $values);
|
||||
|
||||
$this->moveSubmitToBottom();
|
||||
}
|
||||
|
||||
public function loadObject($id)
|
||||
{
|
||||
$class = $this->getObjectClassname();
|
||||
$object = $this->object = $class::load($id, $this->db);
|
||||
if ($object instanceof IcingaObject) {
|
||||
$this->objectType = $object->object_type;
|
||||
}
|
||||
$this->object = $class::load($id, $this->db);
|
||||
|
||||
// TODO: hmmmm...
|
||||
if (! is_array($id)) {
|
||||
$this->addHidden('id');
|
||||
}
|
||||
$this->prepareElements();
|
||||
|
||||
$props = $object->getProperties();
|
||||
if (! $object instanceof IcingaObject) {
|
||||
$this->setDefaults($props);
|
||||
return $this;
|
||||
}
|
||||
|
||||
$inherited = $this->object->getInheritedProperties();
|
||||
|
||||
foreach ($props as $k => $v) {
|
||||
$i = property_exists($inherited, $k) ? $inherited->$k : null;
|
||||
$this->setElementValue($k, $v, $i);
|
||||
}
|
||||
|
||||
if ($object->supportsGroups()) {
|
||||
$this->getElement('groups')->setValue(
|
||||
implode(', ', $object->groups()->listGroupNames())
|
||||
);
|
||||
}
|
||||
|
||||
if ($object->supportsImports()) {
|
||||
$el = $this->getElement('imports');
|
||||
if ($el) {
|
||||
$el->setMultiOptions($this->enumAllowedTemplates());
|
||||
$el->setValue($object->imports()->listImportNames());
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->supportsFields() && ! $object->isTemplate()) {
|
||||
foreach ($this->object->vars() as $key => $value) {
|
||||
$this->addCustomVar($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
if ($object->supportsRanges()) {
|
||||
/* TODO implement when new logic for customvars is there
|
||||
foreach ($this->object->ranges()->getRanges() as $key => $value) {
|
||||
$this->addRange($key, $value);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
$this->moveSubmitToBottom();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addCustomVar($key, $range)
|
||||
protected function addCustomVar($key, $value, $inherited = null, $inheritedFrom = null)
|
||||
{
|
||||
$this->addElement('text', 'var_' . $key, array(
|
||||
'label' => 'vars.' . $key,
|
||||
'value' => $range->getValue()
|
||||
));
|
||||
$label = 'vars.' . $key;
|
||||
$key = 'var_' . $key;
|
||||
$this->addElement('text', $key, array('label' => $label));
|
||||
$this->setElementValue($key, $value, $inherited, $inheritedFrom);
|
||||
}
|
||||
|
||||
protected function addRange($key, $range)
|
||||
|
|
Loading…
Reference in New Issue