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