IcingaObjectField: new generic form and table
This commit is contained in:
parent
417b59f6d6
commit
69b394cda5
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Forms;
|
||||
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
||||
|
||||
class IcingaObjectFieldForm extends DirectorObjectForm
|
||||
{
|
||||
/**
|
||||
*
|
||||
* Please note that $object would conflict with logic in parent class
|
||||
*/
|
||||
protected $icingaObject;
|
||||
|
||||
public function setIcingaObject($object)
|
||||
{
|
||||
$this->icingaObject = $object;
|
||||
$this->className = get_class($object) . 'Field';
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setup()
|
||||
{
|
||||
$type = $this->icingaObject->getShortTableName();
|
||||
$this->addHidden($type . '_id', $this->icingaObject->id);
|
||||
|
||||
$this->addHtmlHint(
|
||||
'Custom data fields allow you to easily fill custom variables with'
|
||||
. " meaningful data. It's perfectly legal to override inherited fields."
|
||||
. ' You may for example want to allow "network devices" specifying any'
|
||||
. ' string for vars.snmp_community, but restrict "customer routers" to'
|
||||
. ' a specific set, shown as a dropdown.'
|
||||
);
|
||||
|
||||
$fields = $this->db->enumDatafields();
|
||||
$this->addElement('select', 'datafield_id', array(
|
||||
'label' => 'Field',
|
||||
'required' => true,
|
||||
'description' => 'Field to assign',
|
||||
'multiOptions' => $this->optionalEnum($fields)
|
||||
));
|
||||
|
||||
if (empty($fields)) {
|
||||
$msg = $this->translate(
|
||||
'There are no data fields available.'
|
||||
. ' Please ask an administrator to create such'
|
||||
);
|
||||
|
||||
$this->getElement('datafield_id')->setError($msg);
|
||||
}
|
||||
|
||||
$this->addElement('select', 'is_required', array(
|
||||
'label' => $this->translate('Mandatory'),
|
||||
'description' => $this->translate('Whether this field should be mandatory'),
|
||||
'required' => true,
|
||||
'multiOptions' => array(
|
||||
'n' => $this->translate('Optional'),
|
||||
'y' => $this->translate('Mandatory'),
|
||||
)
|
||||
));
|
||||
|
||||
$this->setSubmitLabel(
|
||||
$this->translate('Add new field')
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace Icinga\Module\Director\Tables;
|
||||
|
||||
use Icinga\Module\Director\Objects\IcingaObject;
|
||||
use Icinga\Module\Director\Web\Table\QuickTable;
|
||||
|
||||
class IcingaObjectDatafieldTable extends QuickTable
|
||||
{
|
||||
protected $object;
|
||||
|
||||
public function setObject(IcingaObject $object)
|
||||
{
|
||||
$this->object = $object;
|
||||
$this->setConnection($object->getConnection());
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected $searchColumns = array(
|
||||
'varname',
|
||||
);
|
||||
|
||||
public function getColumns()
|
||||
{
|
||||
return array(
|
||||
'id' => 'f.id',
|
||||
'varname' => 'f.varname',
|
||||
'caption' => 'f.caption',
|
||||
'description' => 'f.description',
|
||||
'datatype' => 'f.datatype',
|
||||
'required' => "CASE WHEN of.is_required = 'y' THEN 'mandatory' ELSE 'optional' END",
|
||||
);
|
||||
}
|
||||
|
||||
protected function getActionUrl($row)
|
||||
{
|
||||
return $this->url('director/objectdatafield', array('id' => $row->id));
|
||||
}
|
||||
|
||||
public function getTitles()
|
||||
{
|
||||
$view = $this->view();
|
||||
return array(
|
||||
'caption' => $view->translate('Label'),
|
||||
'varname' => $view->translate('Field name'),
|
||||
'required' => $view->translate('Required'),
|
||||
);
|
||||
}
|
||||
|
||||
public function getBaseQuery()
|
||||
{
|
||||
$db = $this->connection()->getConnection();
|
||||
$otable = $this->object->getTableName() . '_field';
|
||||
$oname = $this->object->getShortTableName();
|
||||
|
||||
$query = $db->select()->from(
|
||||
array('of' => $otable),
|
||||
array()
|
||||
)->join(
|
||||
array('f' => 'director_datafield'),
|
||||
'f.id = of.datafield_id',
|
||||
array()
|
||||
)->where('of.' . $oname . '_id = ?', $this->object->id)
|
||||
->order('caption ASC');
|
||||
|
||||
return $query;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<div class="controls">
|
||||
<?= $this->tabs ?>
|
||||
<h1><?= $this->escape($this->title) ?></h1>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<?= $this->form ?>
|
||||
<?= $this->table ?>
|
||||
</div>
|
Loading…
Reference in New Issue