summaryrefslogtreecommitdiffstats
path: root/application/forms/DirectorDatalistEntryForm.php
blob: c6e309f16f5a34ff4c525a6836aca6b360199ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Application\Config;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\DirectorDatalist;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;

class DirectorDatalistEntryForm extends DirectorObjectForm
{
    /** @var  DirectorDatalist */
    protected $datalist;

    /**
     * @throws \Zend_Form_Exception
     */
    public function setup()
    {
        $this->addElement('text', 'entry_name', [
            'label'       => $this->translate('Key'),
            'required'    => true,
            'description' => $this->translate(
                'Will be stored as a custom variable value when this entry'
                . ' is chosen from the list'
            )
        ]);

        $this->addElement('text', 'entry_value', [
            'label'       => $this->translate('Label'),
            'required'    => true,
            'description' => $this->translate(
                'This will be the visible caption for this entry'
            )
        ]);

        $rolesConfig = Config::app('roles', true);
        $roles = [];
        foreach ($rolesConfig as $name => $role) {
            $roles[$name] = $name;
        }

        $this->addElement('extensibleSet', 'allowed_roles', [
            'label'        => $this->translate('Allowed roles'),
            'required'     => false,
            'multiOptions' => $roles,
            'description'  => $this->translate(
                'Allow to use this entry only to users with one of these Icinga Web 2 roles'
            )
        ]);

        $this->addHidden('list_id', $this->datalist->get('id'));
        $this->addHidden('format', 'string');
        if (!$this->isNew()) {
            $this->addHidden('entry_name', $this->object->get('entry_name'));
        }

        $this->addSimpleDisplayGroup(['entry_name', 'entry_value', 'allowed_roles'], 'entry', [
            'legend' => $this->isNew()
                ? $this->translate('Add data list entry')
                : $this->translate('Modify data list entry')
        ]);

        $this->setButtons();
    }

    /**
     * @param DirectorDatalist $list
     * @return $this
     */
    public function setList(DirectorDatalist $list)
    {
        $this->datalist = $list;
        /** @var Db $db */
        $db = $list->getConnection();
        $this->setDb($db);

        return $this;
    }
}