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