summaryrefslogtreecommitdiffstats
path: root/application/forms/BasketForm.php
blob: 8ff6cca6c683df4d84ea5ad88f6ef02a36092547 (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
141
142
143
144
145
146
147
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\DirectorObject\Automation\Basket;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Zend_Form_SubForm as ZfSubForm;

class BasketForm extends DirectorObjectForm
{
    protected $listUrl = 'director/baskets';

    protected function getAvailableTypes()
    {
        return [
            'Command'            => $this->translate('Command Definitions'),
            'ExternalCommand'    => $this->translate('External Command Definitions'),
            'CommandTemplate'    => $this->translate('Command Template'),
            'HostGroup'          => $this->translate('Host Group'),
            'IcingaTemplateChoiceHost' => $this->translate('Host Template Choice'),
            'HostTemplate'       => $this->translate('Host Templates'),
            'ServiceGroup'       => $this->translate('Service Groups'),
            'IcingaTemplateChoiceService' => $this->translate('Service Template Choice'),
            'ServiceTemplate'    => $this->translate('Service Templates'),
            'ServiceSet'         => $this->translate('Service Sets'),
            'UserGroup'          => $this->translate('User Groups'),
            'UserTemplate'       => $this->translate('User Templates'),
            'User'               => $this->translate('Users'),
            'NotificationTemplate' => $this->translate('Notification Templates'),
            'Notification'       => $this->translate('Notifications'),
            'TimePeriod'         => $this->translate('Time Periods'),
            'Dependency'         => $this->translate('Dependencies'),
            'DataList'           => $this->translate('Data Lists'),
            'ImportSource'       => $this->translate('Import Sources'),
            'SyncRule'           => $this->translate('Sync Rules'),
            'DirectorJob'        => $this->translate('Job Definitions'),
            'Basket'             => $this->translate('Basket Definitions'),
        ];
    }

    /**
     * @throws \Zend_Form_Exception
     */
    public function setup()
    {
        $this->addElement('text', 'basket_name', [
            'label'        => $this->translate('Basket Name'),
            'required'     => true,
        ]);

        $types = $this->getAvailableTypes();

        $options = [
            'IGNORE' => $this->translate('Ignore'),
            'ALL'    => $this->translate('All of them'),
            '[]'     => $this->translate('Custom Selection'),
        ];

        $this->addHtmlHint($this->translate(
            'What should we place into this Basket every time we create'
            . ' new snapshot?'
        ));

        $sub = new ZfSubForm();
        $sub->setDecorators([
            ['HtmlTag', ['tag' => 'dl']],
            'FormElements'
        ]);

        foreach ($types as $name => $label) {
            $sub->addElement('select', $name, [
                'label' => $label,
                'multiOptions' => $options,
            ]);
        }

        $this->addSubForm($sub, 'objects');
        $this->addDeleteButton();

        $this->addHtmlHint($this->translate(
            'Choose "All" to always add all of them,'
            . ' "Ignore" to not care about a specific Type at all and'
            . ' opt for "Custom Selection" in case you want to choose'
            . ' just some specific Objects.'
        ));
    }

    protected function setDefaultsFromObject(DbObject $object)
    {
        parent::setDefaultsFromObject($object);
        /** @var Basket $object */
        $values = [];
        foreach ($this->getAvailableTypes() as $type => $label) {
            $values[$type] = 'IGNORE';
        }
        foreach ($object->getChosenObjects() as $type => $selection) {
            if ($selection === true) {
                $values[$type] = 'ALL';
            } elseif (is_array($selection)) {
                $values[$type] = '[]';
            }
        }

        $this->populate([
            'objects' => $values
        ]);
    }

    protected function onRequest()
    {
        parent::onRequest(); // TODO: Change the autogenerated stub
    }

    protected function getObjectClassname()
    {
        return Basket::class;
    }

    public function onSuccess()
    {
        /** @var Basket $basket */
        $basket = $this->object();

        if ($basket->isEmpty()) {
            $this->addError($this->translate("It's not allowed to store an empty basket"));

            return;
        }
        if (! $basket->hasBeenLoadedFromDb()) {
            $basket->set('owner_type', 'user');
            $basket->set('owner_value', $this->getAuth()->getUser()->getUsername());
        }

        parent::onSuccess();
    }

    protected function setObjectSuccessUrl()
    {
        /** @var Basket $basket */
        $basket = $this->object();
        $this->setSuccessUrl(
            'director/basket',
            ['name' => $basket->get('basket_name')]
        );
    }
}