summaryrefslogtreecommitdiffstats
path: root/modules/setup/application/forms/ModulePage.php
blob: d62b5a93ea7a7fd91e6d2ad7dae919d8bf4f325c (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Setup\Forms;

use Icinga\Application\Icinga;
use Icinga\Application\Modules\Module;
use Icinga\Web\Form;

class ModulePage extends Form
{
    protected $modules;

    protected $modulePaths;

    protected $foundIcingaDB = false;

    /**
     * Initialize this page
     */
    public function init()
    {
        $this->setName('setup_modules');
        $this->setViewScript('form/setup-modules.phtml');

        $this->modulePaths = array();
        if (($appModulePath = realpath(Icinga::app()->getApplicationDir() . '/../modules')) !== false) {
            $this->modulePaths[] = $appModulePath;
        }
    }

    public function createElements(array $formData)
    {
        foreach ($this->getModules() as $module) {
            $checked = false;
            if ($module->getName() === 'monitoring') {
                $checked = ! $this->foundIcingaDB;
            } elseif ($this->foundIcingaDB && $module->getName() === 'icingadb') {
                $checked = true;
            }

            $this->addElement(
                'checkbox',
                $module->getName(),
                array(
                    'description'   => $module->getDescription(),
                    'label'         => ucfirst($module->getName()),
                    'value'         => (int) $checked,
                    'decorators'    => array('ViewHelper')
                )
            );
        }
    }

    /**
     * @return Module[]
     */
    protected function getModules()
    {
        if ($this->modules !== null) {
            return $this->modules;
        } else {
            $this->modules = array();
        }

        $moduleManager = Icinga::app()->getModuleManager();
        $moduleManager->detectInstalledModules($this->modulePaths);
        foreach ($moduleManager->listInstalledModules() as $moduleName) {
            if ($moduleName !== 'setup') {
                $this->modules[$moduleName] = $moduleManager->loadModule($moduleName)->getModule($moduleName);
            }

            if ($moduleName === 'icingadb') {
                $this->foundIcingaDB = true;
            }
        }

        return $this->modules;
    }

    public function getCheckedModules()
    {
        $modules = $this->getModules();

        $checked = array();
        foreach ($this->getElements() as $name => $element) {
            if (array_key_exists($name, $modules) && $element->isChecked()) {
                $checked[$name] = $modules[$name];
            }
        }

        return $checked;
    }

    public function getModuleWizards()
    {
        $checked = $this->getCheckedModules();

        $wizards = array();
        foreach ($checked as $name => $module) {
            if ($module->providesSetupWizard()) {
                $wizards[$name] = $module->getSetupWizard();
            }
        }

        return $wizards;
    }
}