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