]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveFileSet.php
baculum: Add listbox control and use it for base and device directives
[bacula/bacula] / gui / baculum / protected / Web / Portlets / DirectiveFileSet.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.TActiveLinkButton');
25 Prado::using('Application.Web.Portlets.DirectiveListTemplate');
26 Prado::using('Application.Web.Portlets.DirectiveBoolean');
27 Prado::using('Application.Web.Portlets.DirectiveText');
28 Prado::using('Application.Web.Portlets.DirectiveComboBox');
29 Prado::using('Application.Web.Portlets.DirectiveInteger');
30
31 class DirectiveFileSet extends DirectiveListTemplate {
32
33         const MENU_CONTROL = 'NewFileSetMenu';
34
35         private $directive_types = array(
36                 'DirectiveBoolean',
37                 'DirectiveText',
38                 'DirectiveComboBox',
39                 'DirectiveListBox',
40                 'DirectiveInteger'
41         );
42
43         private $directive_inc_exc_types = array(
44                 'DirectiveText'
45         );
46
47         public function loadConfig($sender, $param) {
48                 $component_type = $this->getComponentType();
49                 $component_name = $this->getComponentType();
50                 $resource_type = $this->getResourceType();
51                 $directive_name = $this->getDirectiveName();
52                 $directives = $this->getData();
53                 $data_source = array();
54                 $include = array();
55                 $exclude = array();
56                 $options = array();
57                 if (is_object($directives)) { // Include with options
58                         foreach($directives as $name => $values) {
59                                 switch($name) {
60                                         case 'File': {
61                                                 $this->setFile($include, $name, $values);
62                                                 break;
63                                         }
64                                         case 'Options': {
65                                                 $this->setOption($options, $name, $values);
66                                                 break;
67                                         }
68                                         case 'Exclude': {
69                                                 $this->setFile($exclude, $name, $values);
70                                                 break;
71                                         }
72                                 }
73                         }
74                 }
75
76                 $this->RepeaterFileSetOptions->DataSource = $options;
77                 $this->RepeaterFileSetOptions->dataBind();
78                 $this->RepeaterFileSetInclude->DataSource = $include;
79                 $this->RepeaterFileSetInclude->dataBind();
80                 $this->RepeaterFileSetExclude->DataSource = $exclude;
81                 $this->RepeaterFileSetExclude->dataBind();
82                 $this->FileSetMenu->setComponentType($component_type);
83                 $this->FileSetMenu->setComponentName($component_name);
84                 $this->FileSetMenu->setResourceType($resource_type);
85                 $this->FileSetMenu->setDirectiveName($directive_name);
86         }
87
88         private function setFile(&$files, $name, $config) {
89                 $host = $this->getHost();
90                 $component_type = $this->getComponentType();
91                 $component_name = $this->getComponentName();
92                 $resource_type = $this->getResourceType();
93                 $resource_name = $this->getResourceName();
94
95                 for ($i = 0; $i < count($config); $i++) {
96                         $files[] = array(
97                                 'host' => $host,
98                                 'component_type' => $component_type,
99                                 'component_name' => $component_name,
100                                 'resource_type' => $resource_type,
101                                 'resource_name' => $resource_name,
102                                 'directive_name' => $name,
103                                 'directive_value' => $config[$i],
104                                 'parent_name' => $name
105                         );
106                 }
107         }
108
109         private function setOption(&$options, $name, $config) {
110                 $load_values = $this->getLoadValues();
111                 $host = $this->getHost();
112                 $component_type = $this->getComponentType();
113                 $component_name = $this->getComponentName();
114                 $resource_type = $this->getResourceType();
115                 $resource_name = $this->getResourceName();
116
117                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type, 'Include');
118
119                 for ($i = 0; $i < count($config); $i++) {
120                         foreach ($resource_desc->SubSections as $directive_name => $directive_desc) {
121                                 $in_config = property_exists($config[$i], $directive_name);
122                                 $directive_value = null;
123                                 if ($in_config === true) {
124                                         $directive_value = $config[$i]->{$directive_name};
125                                 }
126
127                                 $default_value = null;
128                                 $data = null;
129                                 $field_type = 'TextBox';
130                                 $required = false;
131                                 if (is_object($directive_desc)) {
132                                         if (property_exists($directive_desc, 'Required')) {
133                                                 $required = $directive_desc->Required;
134                                         }
135                                         if (property_exists($directive_desc, 'DefaultValue')) {
136                                                 $default_value = $directive_desc->DefaultValue;
137                                         }
138                                         if (property_exists($directive_desc, 'Data')) {
139                                                 $data = $directive_desc->Data;
140                                         }
141                                         if (property_exists($directive_desc, 'FieldType')) {
142                                                 $field_type = $directive_desc->FieldType;
143                                         }
144                                 }
145                                 if (!is_array($directive_value)) {
146                                         $directive_value = array($directive_value);
147                                 }
148                                 for ($j = 0; $j < count($directive_value); $j++) {
149                                         $options[] = 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' => $directive_value[$j],
157                                                 'default_value' => $default_value,
158                                                 'required' => $required,
159                                                 'data' => $data,
160                                                 'field_type' => $field_type,
161                                                 'in_config' => $in_config,
162                                                 'label' => $directive_name,
163                                                 'show' => ($in_config || !$load_values || $this->SourceTemplateControl->getShowAllDirectives()),
164                                                 'parent_name' => $name,
165                                                 'group_name' => $i
166                                         );
167                                 }
168                         }
169                 }
170         }
171
172         public function getDirectiveValue() {
173                 $directive_values = array();
174                 $component_type = $this->getComponentType();
175                 $resource_type = $this->getResourceType();
176                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
177
178                 for ($i = 0; $i < count($this->directive_types); $i++) {
179                         $controls = $this->RepeaterFileSetOptions->findControlsByType($this->directive_types[$i]);
180                         for ($j = 0; $j < count($controls); $j++) {
181                                 $directive_name = $controls[$j]->getDirectiveName();
182                                 $directive_value = $controls[$j]->getDirectiveValue();
183                                 $index = $controls[$j]->getGroupName();
184                                 $default_value = $resource_desc['Include']->SubSections->{$directive_name}->DefaultValue;
185                                 $in_config = $controls[$j]->getInConfig();
186                                 if (is_null($directive_value)) {
187                                         // option not set or removed
188                                         continue;
189                                 }
190                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
191                                         settype($default_value, 'bool');
192                                 }
193                                 if ($directive_value === $default_value) {
194                                         // value the same as default value, skip it
195                                         continue;
196                                 }
197                                 if (!array_key_exists('Include', $directive_values)) {
198                                         $directive_values['Include'] = array('Options' => array());
199                                 }
200                                 if (!isset($directive_values['Include']['Options'][$index])) {
201                                         $directive_values['Include']['Options'][$index] = array();
202                                 }
203                                 $directive_values['Include']['Options'][$index][$directive_name] = $directive_value;
204                         }
205                         $controls = $this->RepeaterFileSetInclude->findControlsByType($this->directive_types[$i]);
206                         for ($j = 0; $j < count($controls); $j++) {
207                                 $directive_name = $controls[$j]->getDirectiveName();
208                                 $directive_value = $controls[$j]->getDirectiveValue();
209                                 if (is_null($directive_value)) {
210                                         // Include file directive removed
211                                         continue;
212                                 }
213                                 if (!array_key_exists('Include', $directive_values)) {
214                                         $directive_values['Include'] = array();
215                                 }
216                                 if (!array_key_exists($directive_name, $directive_values['Include'])) {
217                                         $directive_values['Include'][$directive_name] = array();
218                                 }
219                                 array_push($directive_values['Include'][$directive_name], $directive_value);
220                         }
221                         $controls = $this->RepeaterFileSetExclude->findControlsByType($this->directive_types[$i]);
222                         for ($j = 0; $j < count($controls); $j++) {
223                                 $directive_name = $controls[$j]->getDirectiveName();
224                                 $directive_value = $controls[$j]->getDirectiveValue();
225                                 if (is_null($directive_value)) {
226                                         // Exclude file directive removed
227                                         continue;
228                                 }
229                                 if (!array_key_exists($directive_name, $directive_values)) {
230                                         $directive_values[$directive_name] = array('File' => array());
231                                 }
232                                 array_push($directive_values[$directive_name]['File'], $directive_value);
233                         }
234                 }
235
236                 return $directive_values;
237         }
238
239         public function createFileSetOptions($sender, $param) {
240                 $load_values = $this->getLoadValues();
241                 $bconditionals = $this->RepeaterFileSetOptions->findControlsByType('BConditionalItem');
242                 for ($i = 0; $i < count($bconditionals); $i++) {
243                         $item = $bconditionals[$i]->getData();
244                         for ($j = 0; $j < count($this->directive_types); $j++) {
245                                 $control = $this->getChildControl($item, $this->directive_types[$j]);
246                                 if (is_object($control)) {
247                                         $control->setHost($item->DataItem['host']);
248                                         $control->setComponentType($item->DataItem['component_type']);
249                                         $control->setComponentName($item->DataItem['component_name']);
250                                         $control->setResourceType($item->DataItem['resource_type']);
251                                         $control->setResourceName($item->DataItem['resource_name']);
252                                         $control->setDirectiveName($item->DataItem['directive_name']);
253                                         $control->setDirectiveValue($item->DataItem['directive_value']);
254                                         $control->setDefaultValue($item->DataItem['default_value']);
255                                         $control->setRequired($item->DataItem['required']);
256                                         $control->setData($item->DataItem['data']);
257                                         $control->setLabel($item->DataItem['label']);
258                                         $control->setInConfig($item->DataItem['in_config']);
259                                         $show_all_directives = ($item->DataItem['in_config'] || !$load_values || $this->SourceTemplateControl->getShowAllDirectives());
260                                         $control->setShow($show_all_directives);
261                                         $control->setParentName($item->DataItem['parent_name']);
262                                         $control->setGroupName($item->DataItem['group_name']);
263                                 }
264                         }
265                 }
266         }
267
268         public function createFileSetIncExcElement($sender, $param) {
269                 for ($i = 0; $i < count($this->directive_inc_exc_types); $i++) {
270                         $control = $this->getChildControl($param->Item, $this->directive_inc_exc_types[$i]);
271                         if (is_object($control)) {
272                                 $control->setHost($param->Item->DataItem['host']);
273                                 $control->setComponentType($param->Item->DataItem['component_type']);
274                                 $control->setComponentName($param->Item->DataItem['component_name']);
275                                 $control->setResourceType($param->Item->DataItem['resource_type']);
276                                 $control->setResourceName($param->Item->DataItem['resource_name']);
277                                 $control->setDirectiveName($param->Item->DataItem['directive_name']);
278                                 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
279                                 $control->setLabel($param->Item->DataItem['directive_name']);
280                                 $control->setData($param->Item->DataItem['directive_value']);
281                                 $control->setInConfig(true);
282                                 $control->setShow(true);
283                                 $control->setParentName($param->Item->DataItem['parent_name']);
284                         }
285                 }
286         }
287
288         private function getDirectiveData() {
289                 $values = $this->getDirectiveValue();
290                 $data = array();
291                 if (array_key_exists('Include', $values) && array_key_exists('File', $values['Include'])) {
292                         $data['File'] = $values['Include']['File'];
293                         if (array_key_exists('Options', $values['Include']) && is_array($values['Include']['Options'])) {
294                                 $data['Options'] = array();
295                                 for ($i = 0; $i < count($values['Include']['Options']); $i++) {
296                                         $data['Options'][$i] = (object)$values['Include']['Options'][$i];
297                                 }
298                         }
299                 }
300                 if (array_key_exists('Exclude', $values) && array_key_exists('File', $values['Exclude'])) {
301                         $data['Exclude'] = $values['Exclude']['File'];
302                 }
303                 return $data;
304         }
305
306         public function newIncludeFile($sender, $param) {
307                 $data = $this->getDirectiveData();
308                 if (array_key_exists('File', $data) && is_array($data['File'])) {
309                         $data['File'][] = '';
310                 } else {
311                         $data['File'] = array('');
312                 }
313                 $data = (object)$data;
314                 $this->setData($data);
315                 $this->loadConfig(null, null);
316         }
317
318         public function newExcludeFile($sender, $param) {
319                 $data = $this->getDirectiveData();
320                 if (array_key_exists('Exclude', $data) && is_array($data['Exclude'])) {
321                         $data['Exclude'][] = '';
322                 } else {
323                         $data['Exclude'] = array('');
324                 }
325                 $data = (object)$data;
326                 $this->setData($data);
327                 $this->loadConfig(null, null);
328         }
329
330         public function newIncludeOptions($sender, $param) {
331                 $data = $this->getDirectiveData();
332                 if (array_key_exists('Options', $data) && is_array($data['Options'])) {
333                         $data['Options'][] = new stdClass;
334                 } else {
335                         $data['Options'] = array(new stdClass);
336                 }
337                 $data = (object)$data;
338                 $this->SourceTemplateControl->setShowAllDirectives(true);
339                 $this->setData($data);
340                 $this->loadConfig(null, null);
341         }
342 }
343 ?>