]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/BaculaConfigDirectives.php
baculum: Add listbox control and use it for base and device directives
[bacula/bacula] / gui / baculum / protected / Web / Portlets / BaculaConfigDirectives.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('System.Web.UI.ActiveControls.TActiveRepeater');
24 Prado::using('System.Web.UI.ActiveControls.TActiveLabel');
25 Prado::using('Application.Web.Portlets.BConditional');
26 Prado::using('Application.Web.Portlets.DirectiveListTemplate');
27 Prado::using('Application.Web.Portlets.DirectiveBoolean');
28 Prado::using('Application.Web.Portlets.DirectiveComboBox');
29 Prado::using('Application.Web.Portlets.DirectiveInteger');
30 Prado::using('Application.Web.Portlets.DirectiveListBox');
31 Prado::using('Application.Web.Portlets.DirectiveText');
32 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
33 Prado::using('Application.Web.Portlets.DirectiveRunscript');
34 Prado::using('Application.Web.Portlets.DirectiveMessages');
35 Prado::using('Application.IO.TTextWriter');
36
37 class BaculaConfigDirectives extends DirectiveListTemplate {
38
39         const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
40
41         private $show_all_directives = false;
42
43         public $resource_names = array();
44
45         private $directive_types = array(
46                 'DirectiveBoolean',
47                 'DirectiveComboBox',
48                 'DirectiveInteger',
49                 'DirectiveListBox',
50                 'DirectiveText',
51                 'DirectiveTimePeriod'
52         );
53
54         private $directive_list_types = array(
55                 'DirectiveFileSet',
56                 'DirectiveSchedule',
57                 'DirectiveMessages',
58                 'DirectiveRunscript'
59         );
60
61         private $field_multple_values = array(
62                 'ListBox'
63         );
64
65         private function getConfigData($host, array $parameters) {
66                 $default_params = array('config');
67                 $params = array_merge($default_params, $parameters);
68                 $result = $this->Application->getModule('api')->get($params, $host, false);
69                 $config = array();
70                 if (is_object($result) && $result->error === 0 && (is_object($result->output) || is_array($result->output))) {
71                         $config = $result->output;
72                 }
73                 return $config;
74         }
75
76         public function loadConfig() {
77                 $load_values = $this->getLoadValues();
78
79                 $host = $this->getHost();
80                 $component_type = $this->getComponentType();
81                 $component_name = $this->getComponentName();
82                 $resource_type = $this->getResourceType();
83                 $resource_name = $this->getResourceName();
84                 $directives = array();
85                 $parent_directives = array();
86                 $config = new stdClass;
87                 if ($load_values === true) {
88                         $config = $this->getConfigData($host, array(
89                                 $component_type,
90                                 $resource_type,
91                                 $resource_name
92                         ));
93                         if ($resource_type === 'Job' && property_exists($config, 'JobDefs')) {
94                                 $parent_directives = $this->getConfigData($host, array(
95                                         $component_type,
96                                         'JobDefs',
97                                         $config->JobDefs
98                                 ));
99                         }
100                 }
101                 $data_desc = $this->Application->getModule('data_desc');
102                 $resource_desc = $data_desc->getDescription($component_type, $resource_type);
103                 foreach ($resource_desc as $directive_name => $directive_desc) {
104                         $in_config = false;
105                         if ($load_values === true) {
106                                 $in_config = property_exists($config, $directive_name);
107                         }
108
109                         $directive_value = null;
110                         if ($in_config === true && $load_values === true) {
111                                 $directive_value = $config->{$directive_name};
112                         }
113
114                         $default_value = null;
115                         $data = null;
116                         $field_type = 'TextBox';
117                         $resource = null;
118                         $required = false;
119                         // @TODO: Add support for all directive properties defined in description file
120                         if (is_object($directive_desc)) {
121                                 if (property_exists($directive_desc, 'Required')) {
122                                         $required = $directive_desc->Required;
123                                         if ($load_values === true && array_key_exists($directive_name, $parent_directives)) {
124                                                 // values can be taken from JobDefs
125                                                 $required = false;
126                                         }
127                                 }
128                                 if (property_exists($directive_desc, 'DefaultValue')) {
129                                         $default_value = $directive_desc->DefaultValue;
130                                 }
131                                 if (property_exists($directive_desc, 'Data')) {
132                                         $data = $directive_desc->Data;
133                                 }
134                                 if (property_exists($directive_desc, 'FieldType')) {
135                                         $field_type = $directive_desc->FieldType;
136                                 }
137                                 if (property_exists($directive_desc, 'Resource')) {
138                                         $resource = $directive_desc->Resource;
139                                 }
140                         }
141
142                         if ((!is_array($directive_value) && !is_object($directive_value)) || in_array($field_type, $this->field_multple_values)) {
143                                 $directive_value = array($directive_value);
144                         }
145                         if (is_object($directive_value)) {
146                                 $directive_value = (array)$directive_value;
147                         }
148                         foreach ($directive_value as $key => $value) {
149                                 $directive = array(
150                                         'host' => $host,
151                                         'component_type' => $component_type,
152                                         'component_name' => $component_name,
153                                         'resource_type' => $resource_type,
154                                         'resource_name' => $resource_name,
155                                         'directive_name' => $directive_name,
156                                         'directive_value' => $value,
157                                         'default_value' => $default_value,
158                                         'required' => $required,
159                                         'data' => $data,
160                                         'resource' => $resource,
161                                         'field_type' => $field_type,
162                                         'label' => $directive_name,
163                                         'in_config' => $in_config,
164                                         'show' => (($in_config || !$load_values) || $this->getShowAllDirectives())
165                                 );
166                                 array_push($directives, $directive);
167                         }
168                 }
169                 $config = $this->getConfigData($host, array($component_type));
170                 for ($i = 0; $i < count($config); $i++) {
171                         $resource_type = $this->getConfigResourceType($config[$i]);
172                         $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
173                         if (!array_key_exists($resource_type, $this->resource_names)) {
174                                 $this->resource_names[$resource_type] = array();
175                         }
176                         array_push($this->resource_names[$resource_type], $resource_name);
177                 }
178                 $this->setResourceNames($this->resource_names);
179
180                 $this->RepeaterDirectives->DataSource = $directives;
181                 $this->RepeaterDirectives->dataBind();
182         }
183
184         public function loadDirectives($sender, $param) {
185                 $show_all_directives = !$this->getShowAllDirectives();
186                 $this->setShowAllDirectives($show_all_directives);
187                 $this->loadConfig();
188         }
189
190         public function unloadDirectives() {
191                 $this->RepeaterDirectives->DataSource = array();
192                 $this->RepeaterDirectives->dataBind();
193         }
194
195         public function createDirectiveElement($sender, $param) {
196                 $load_values = $this->getLoadValues();
197                 for ($i = 0; $i < count($this->directive_types); $i++) {
198                         $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
199                         if (is_object($control)) {
200                                 $control->setHost($param->Item->DataItem['host']);
201                                 $control->setComponentType($param->Item->DataItem['component_type']);
202                                 $control->setComponentName($param->Item->DataItem['component_name']);
203                                 $control->setResourceType($param->Item->DataItem['resource_type']);
204                                 $control->setResourceName($param->Item->DataItem['resource_name']);
205                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
206                                 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
207                                 $control->setDefaultValue($param->Item->DataItem['default_value']);
208                                 $control->setRequired($param->Item->DataItem['required']);
209                                 $control->setData($param->Item->DataItem['data']);
210                                 $control->setResource($param->Item->DataItem['resource']);
211                                 $control->setLabel($param->Item->DataItem['label']);
212                                 $control->setInConfig($param->Item->DataItem['in_config']);
213                                 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
214                                 $control->setShow($show_all_directives);
215                                 $control->setResourceNames($this->resource_names);
216                                 break;
217                         }
218                 }
219                 for ($i = 0; $i < count($this->directive_list_types); $i++) {
220                         $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
221                         if (is_object($control)) {
222                                 $control->setHost($param->Item->DataItem['host']);
223                                 $control->setComponentType($param->Item->DataItem['component_type']);
224                                 $control->setComponentName($param->Item->DataItem['component_name']);
225                                 $control->setResourceType($param->Item->DataItem['resource_type']);
226                                 $control->setResourceName($param->Item->DataItem['resource_name']);
227                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
228                                 $control->setData($param->Item->DataItem['directive_value']);
229                                 $control->setLoadValues($this->getLoadValues());
230                                 $control->setResourceNames($this->resource_names);
231                                 $control->raiseEvent('OnDirectiveListLoad', $this, null);
232                         }
233                 }
234         }
235
236
237         public function saveResource($sender, $param) {
238                 $directives = array();
239                 $host = $this->getHost();
240                 $component_type = $this->getComponentType();
241                 $resource_type = $this->getResourceType();
242                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
243                 for ($i = 0; $i < count($this->directive_types); $i++) {
244                         $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
245                         for ($j = 0; $j < count($controls); $j++) {
246                                 $parent_name = $controls[$j]->getParentName();
247                                 if (!is_null($parent_name)) {
248                                         continue;
249                                 }
250                                 $directive_name = $controls[$j]->getDirectiveName();
251                                 $directive_value = $controls[$j]->getDirectiveValue();
252                                 $default_value = $resource_desc[$directive_name]->DefaultValue;
253                                 $in_config = $controls[$j]->getInConfig();
254                                 if (is_null($directive_value)) {
255                                         // skip not changed values that don't exist in config
256                                         continue;
257                                 }
258                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
259                                         settype($default_value, 'bool');
260                                 } elseif ($this->directive_types[$i] === 'DirectiveInteger') {
261                                         settype($directive_value, 'int');
262                                 }
263                                 if ($directive_value === $default_value && $in_config === false) {
264                                         // value the same as default value, skip it
265                                         continue;
266                                 }
267                                 $directives[$directive_name] = $directive_value;
268                         }
269                 }
270                 for ($i = 0; $i < count($this->directive_list_types); $i++) {
271                         $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
272                         for ($j = 0; $j < count($controls); $j++) {
273                                 $directive_name = $controls[$j]->getDirectiveName();
274                                 $directive_value = $controls[$j]->getDirectiveValue();
275                                 if (is_null($directive_value)) {
276                                         continue;
277                                 }
278                                 if (!array_key_exists($directive_name, $directives)) {
279                                         $directives[$directive_name] = array();
280                                 }
281                                 if (is_array($directive_value)) {
282                                         if ($this->directive_list_types[$i] === 'DirectiveMessages') {
283                                                 $directives = array_merge($directives, $directive_value);
284                                         } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
285                                                 if (!isset($directives[$directive_name])) {
286                                                         $directives[$directive_name] = array();
287                                                 }
288                                                 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
289                                         } elseif (array_key_exists($directive_name, $directive_value)) {
290                                                 $directives[$directive_name][] = $directive_value[$directive_name];
291                                         } elseif (count($directive_value) > 0) {
292                                                 $directives[$directive_name][] = $directive_value;
293                                         }
294                                 } else {
295                                         $directives[$directive_name] = $directive_value;
296                                 }
297                         }
298                 }
299                 $load_values = $this->getLoadValues();
300                 if ($load_values === true) {
301                         $resource_name = $this->getResourceName();
302                 } else {
303                         $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
304                 }
305
306                 $params = array(
307                         'config',
308                         $component_type,
309                         $resource_type,
310                         $resource_name
311                 );
312                 $result = $this->Application->getModule('api')->set($params, array('config' => json_encode($directives)), $host, false);
313                 if ($result->error === 0) {
314                         $this->SaveDirectiveOk->Display = 'Dynamic';
315                         $this->SaveDirectiveError->Display = 'None';
316                         $this->SaveDirectiveErrMsg->Text = '';
317                 } else {
318                         $this->SaveDirectiveOk->Display = 'None';
319                         $this->SaveDirectiveError->Display = 'Dynamic';
320                         $this->SaveDirectiveErrMsg->Display = 'Dynamic';
321                         $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
322                 }
323         }
324
325         public function setShowAllDirectives($show_all_directives) {
326                 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
327         }
328
329         public function getShowAllDirectives() {
330                 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);
331         }
332 }
333 ?>