Forms: used templates should not be deleted

This commit is contained in:
Thomas Gelf 2016-03-02 19:29:35 +01:00
parent 6f35e3cd11
commit 4e677dbc56
3 changed files with 35 additions and 0 deletions

View File

@ -530,6 +530,19 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return $this;
}
public function countDirectDescendants()
{
$db = $this->getDb();
$table = $this->getTableName();
$type = strtolower($this->getType());
$query = $db->select()->from(
array('oi' => $table . '_inheritance'),
array('cnt' => 'COUNT(*)')
)->where('oi.parent_' . $type . '_id = ?', (int) $this->id);
return $db->fetchOne($query);
}
protected function resolve($what)
{
if ($this->hasResolveCached($what)) {

View File

@ -684,6 +684,8 @@ abstract class DirectorObjectForm extends QuickForm
protected function addDeleteButton($label = null)
{
$object = $this->object;
if ($label === null) {
$label = $this->translate('Delete');
}
@ -694,6 +696,20 @@ abstract class DirectorObjectForm extends QuickForm
//->removeDecorator('Label');
$this->deleteButtonName = $el->getName();
if ($object instanceof IcingaObject && $object->isTemplate()) {
if ($cnt = $object->countDirectDescendants()) {
$el->setAttrib('disabled', 'disabled');
$el->setAttrib(
'title',
sprintf(
$this->translate('This template is still in use by %d other objects'),
$cnt
)
);
}
}
$this->addElement($el);
return $this;

View File

@ -52,6 +52,12 @@ form input[type=submit] {
.button();
border-width: 1px;
margin-top: 0.5em;
&:disabled {
border-color: @gray-light;
background-color: @gray-light;
color: #fff;
}
}
form input[type=submit]:first-of-type {