1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Component/classes/class.ilPluginConfigGUI.php");
5include_once("class.ilCloudPluginConfig.php");
6
7/**
8 * Class ilCloudPluginConfigGUI
9 *
10 * GUI class for the administration settings. Plugin classes can extend this method and override getFields to declare
11 * the fields needed for the input of the settings.
12 *
13 * public function getFields()
14 * {
15 *  return array(
16 *   "app_name"               => array("type" => "ilTextInputGUI", "info" => "config_info_app_name", "subelements" => null),
17 *  );
18 * }
19 *
20 * @author  Timon Amstutz <timon.amstutz@ilub.unibe.ch>
21 * @version $Id$
22 * @extends ilPluginConfigGUI
23 * @ingroup ModulesCloud
24 */
25abstract class ilCloudPluginConfigGUI extends ilPluginConfigGUI
26{
27
28    /**
29     * @var ilCloudPluginConfig
30     */
31    protected $object;
32    /**
33     * @var array
34     */
35    protected $fields = array();
36
37
38    /**
39     * @return array
40     */
41    public function getFields()
42    {
43        return null;
44    }
45
46
47    /**
48     * @return string
49     */
50    public function getTableName()
51    {
52        return $this->getPluginObject()->getPrefix() . "_config";
53    }
54
55
56    /**
57     * @return ilCloudPluginConfig
58     */
59    public function getObject()
60    {
61        return $this->object;
62    }
63
64
65    /**
66     * Handles all commmands, default is "configure"
67     */
68    public function performCommand($cmd)
69    {
70        include_once("class.ilCloudPluginConfig.php");
71        $this->object = new ilCloudPluginConfig($this->getTableName());
72        $this->fields = $this->getFields();
73        switch ($cmd) {
74            case "configure":
75            case "save":
76                $this->$cmd();
77                break;
78        }
79    }
80
81
82    /**
83     * Configure screen
84     */
85    public function configure()
86    {
87        global $DIC;
88        $tpl = $DIC['tpl'];
89
90        $this->initConfigurationForm();
91        $this->getValues();
92        $tpl->setContent($this->form->getHTML());
93    }
94
95
96    public function getValues()
97    {
98        foreach ($this->fields as $key => $item) {
99            $values[$key] = $this->object->getValue($key);
100            if (is_array($item["subelements"])) {
101                foreach ($item["subelements"] as $subkey => $subitem) {
102                    $values[$key . "_" . $subkey] = $this->object->getValue($key . "_" . $subkey);
103                }
104            }
105        }
106
107        $this->form->setValuesByArray($values);
108    }
109
110
111    /**
112     * @return ilPropertyFormGUI
113     */
114    public function initConfigurationForm()
115    {
116        global $DIC;
117        $lng = $DIC['lng'];
118        $ilCtrl = $DIC['ilCtrl'];
119
120        include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
121        $this->form = new ilPropertyFormGUI();
122
123        foreach ($this->fields as $key => $item) {
124            $field = new $item["type"]($this->plugin_object->txt($key), $key);
125            $field->setInfo($this->plugin_object->txt($item["info"]));
126            if (is_array($item["subelements"])) {
127                foreach ($item["subelements"] as $subkey => $subitem) {
128                    $subfield = new $subitem["type"]($this->plugin_object->txt($key . "_" . $subkey), $key . "_" . $subkey);
129                    $subfield->setInfo($this->plugin_object->txt($subitem["info"]));
130                    $field->addSubItem($subfield);
131                }
132            }
133
134            $this->form->addItem($field);
135        }
136
137        $this->form->addCommandButton("save", $lng->txt("save"));
138
139        $this->form->setTitle($this->plugin_object->txt("configuration"));
140        $this->form->setFormAction($ilCtrl->getFormAction($this));
141
142        return $this->form;
143    }
144
145
146    public function save()
147    {
148        global $DIC;
149        $tpl = $DIC['tpl'];
150        $ilCtrl = $DIC['ilCtrl'];
151
152        $this->initConfigurationForm();
153        if ($this->form->checkInput()) {
154
155            // Save Checkbox Values
156            foreach ($this->fields as $key => $item) {
157                $this->object->setValue($key, $this->form->getInput($key));
158                if (is_array($item["subelements"])) {
159                    foreach ($item["subelements"] as $subkey => $subitem) {
160                        $this->object->setValue($key . "_" . $subkey, $this->form->getInput($key . "_" . $subkey));
161                    }
162                }
163            }
164
165            $ilCtrl->redirect($this, "configure");
166        } else {
167            $this->form->setValuesByPost();
168            $tpl->setContent($this->form->getHtml());
169        }
170    }
171}
172