summaryrefslogtreecommitdiffstats
path: root/application/forms/IcingaTemplateChoiceForm.php
blob: 27af5d222d93d390bde79e66384129cbbbf5d5d6 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaTemplateChoice;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;

class IcingaTemplateChoiceForm extends DirectorObjectForm
{
    private $choiceType;

    public static function create($type, Db $db)
    {
        return static::load()->setDb($db)->setChoiceType($type);
    }

    public function optionallyLoad($name)
    {
        if ($name !== null) {
            /** @var IcingaTemplateChoice $class - cheating IDE */
            $class = $this->getObjectClassname();
            $this->setObject($class::load($name, $this->getDb()));
        }

        return $this;
    }

    protected function getObjectClassname()
    {
        if ($this->className === null) {
            return 'Icinga\\Module\\Director\\Objects\\IcingaTemplateChoice'
                . ucfirst($this->choiceType);
        }

        return $this->className;
    }

    public function setChoiceType($type)
    {
        $this->choiceType = $type;
        return $this;
    }

    public function setup()
    {
        $this->addElement('text', 'object_name', array(
            'label'       => $this->translate('Choice name'),
            'required'    => true,
            'description' => $this->translate(
                'This will be shown as a label for the given choice'
            )
        ));

        $this->addElement('textarea', 'description', array(
            'label'       => $this->translate('Description'),
            'rows' => 4,
            'description' => $this->translate(
                'A detailled description explaining what this choice is all about'
            )
        ));

        $this->addElement('extensibleSet', 'members', array(
            'label'       => $this->translate('Available choices'),
            'required'    => true,
            'description' => $this->translate(
                'Your users will be allowed to choose among those templates'
            ),
            'multiOptions' => $this->fetchUnboundTemplates()
        ));

        $this->addElement('text', 'min_required', array(
            'label'       => $this->translate('Minimum required'),
            'description' => $this->translate(
                'Choosing this many options will be mandatory for this Choice.'
                . ' Setting this to zero will leave this Choice optional, setting'
                . ' it to one results in a "required" Choice. You can use higher'
                . ' numbers to enforce multiple options, this Choice will then turn'
                . ' into a multi-selection element.'
            ),
            'value' => 0,
        ));

        $this->addElement('text', 'max_allowed', array(
            'label'       => $this->translate('Allowed maximum'),
            'description' => $this->translate(
                'It will not be allowed to choose more than this many options.'
                . ' Setting it to one (1) will result in a drop-down box, a'
                . ' higher number will turn this into a multi-selection element.'
            ),
            'value' => 1,
        ));

        $this->addElement('select', 'required_template', [
            'label'        => $this->translate('Associated Template'),
            'description'  => $this->translate(
                'Choose Choice Associated Template'
            ),
            'required'     => true,
            'multiOptions' => $this->fetchUnboundTemplates(),
        ]);

        $this->setButtons();
    }

    protected function fetchUnboundTemplates()
    {
        /** @var IcingaTemplateChoice $object */
        $object = $this->object();
        $db = $this->getDb()->getDbAdapter();
        $table = $object->getObjectTableName();
        $query = $db->select()->from(
            ['o' => $table],
            [
                'k' => 'o.object_name',
                'v' => 'o.object_name',
            ]
        )->where("o.object_type = 'template'");
        if ($object->hasBeenLoadedFromDb()) {
            $query->where(
                'o.template_choice_id IS NULL OR o.template_choice_id = ?',
                $object->get('id')
            );
        } else {
            $query->where('o.template_choice_id IS NULL');
        }

        return $db->fetchPairs($query);
    }

    protected function setObjectSuccessUrl()
    {
        /** @var IcingaTemplateChoice $object */
        $object = $this->object();
        $this->setSuccessUrl(
            'director/templatechoice/' . $object->getObjectShortTableName(),
            $object->getUrlParams()
        );
    }
}