]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveRunscript.php
2e6b227f835d578d8f25af7ca7efb3d6e449ca97
[bacula/bacula] / gui / baculum / protected / Web / Portlets / DirectiveRunscript.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 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('Application.Web.Portlets.DirectiveListTemplate');
24 Prado::using('Application.Web.Portlets.DirectiveBoolean');
25 Prado::using('Application.Web.Portlets.DirectiveText');
26 Prado::using('Application.Web.Portlets.DirectiveComboBox');
27
28 class DirectiveRunscript extends DirectiveListTemplate {
29
30         private $directive_types = array(
31                 'DirectiveBoolean',
32                 'DirectiveText',
33                 'DirectiveComboBox'
34         );
35
36         public function loadConfig($sender, $param) {
37                 $load_values = $this->getLoadValues();
38                 $host = $this->getHost();
39                 $component_type = $this->getComponentType();
40                 $component_name = $this->getComponentName();
41                 $resource_type = $this->getResourceType();
42                 $resource_name = $this->getResourceName();
43
44                 $config = $this->getData();
45                 if (is_null($config)) {
46                         return null;
47                 } elseif (is_object($config)) {
48                         $config = array($config);
49                 }
50                 $options = array();
51                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type, 'Runscript');
52                 for ($i = 0; $i < count($config); $i++) {
53                         foreach ($resource_desc->SubSections as $directive_name => $directive_desc) {
54                                 $in_config = property_exists($config[$i], $directive_name);
55
56                                 $directive_value = null;
57                                 if ($in_config === true) {
58                                         $directive_value = $config[$i]->{$directive_name};
59                                 }
60
61                                 $default_value = null;
62                                 $data = null;
63                                 $field_type = 'TextBox';
64                                 $required = false;
65                                 if (is_object($directive_desc)) {
66                                         if (property_exists($directive_desc, 'Required')) {
67                                                 $required = $directive_desc->Required;
68                                         }
69                                         if (property_exists($directive_desc, 'DefaultValue')) {
70                                                 $default_value = $directive_desc->DefaultValue;
71                                         }
72                                         if (property_exists($directive_desc, 'Data')) {
73                                                 $data = $directive_desc->Data;
74                                         }
75                                         if (property_exists($directive_desc, 'FieldType')) {
76                                                 $field_type = $directive_desc->FieldType;
77                                         }
78                                 }
79                                 if (!is_array($directive_value)) {
80                                         $directive_value = array($directive_value);
81                                 }
82                                 for ($j = 0; $j < count($directive_value); $j++) {
83                                         $options[] = array(
84                                                 'host' => $host,
85                                                 'component_type' => $component_type,
86                                                 'component_name' => $component_name,
87                                                 'resource_type' => $resource_type,
88                                                 'resource_name' => $resource_name,
89                                                 'directive_name' => $directive_name,
90                                                 'directive_value' => $directive_value[$j],
91                                                 'default_value' => $default_value,
92                                                 'required' => $required,
93                                                 'data' => $data,
94                                                 'label' => $directive_name,
95                                                 'field_type' => $field_type,
96                                                 'in_config' => $in_config,
97                                                 'show' => ($in_config || !$load_values || $this->SourceTemplateControl->getShowAllDirectives()),
98                                                 'parent_name' => __CLASS__,
99                                                 'group_name' => $i
100                                         );
101                                 }
102                         }
103                 }
104                 $this->RepeaterRunscriptOptions->dataSource = $options;
105                 $this->RepeaterRunscriptOptions->dataBind();
106         }
107
108         public function createRunscriptOptions($sender, $param) {
109                 $load_values = $this->getLoadValues();
110                 $bconditionals = $this->RepeaterRunscriptOptions->findControlsByType('BConditionalItem');
111                 for ($i = 0; $i < count($bconditionals); $i++) {
112                         $item = $bconditionals[$i]->getData();
113                         for ($j = 0; $j < count($this->directive_types); $j++) {
114                                 $control = $this->getChildControl($item, $this->directive_types[$j]);
115                                 if (is_object($control)) {
116                                         $control->setHost($item->DataItem['host']);
117                                         $control->setComponentType($item->DataItem['component_type']);
118                                         $control->setComponentName($item->DataItem['component_name']);
119                                         $control->setResourceType($item->DataItem['resource_type']);
120                                         $control->setResourceName($item->DataItem['resource_name']);
121                                         $control->setDirectiveName($item->DataItem['directive_name']);
122                                         $control->setDirectiveValue($item->DataItem['directive_value']);
123                                         $control->setDefaultValue($item->DataItem['default_value']);
124                                         $control->setRequired($item->DataItem['required']);
125                                         $control->setData($item->DataItem['data']);
126                                         $control->setLabel($item->DataItem['label']);
127                                         $control->setInConfig($item->DataItem['in_config']);
128                                         $show_all_directives = ($item->DataItem['in_config'] || !$load_values || $this->SourceTemplateControl->getShowAllDirectives());
129                                         $control->setShow($show_all_directives);
130                                         $control->setParentName($item->DataItem['parent_name']);
131                                         $control->setGroupName($item->DataItem['group_name']);
132                                         break;
133                                 }
134                         }
135                 }
136         }
137
138         public function getDirectiveValue() {
139                 $directive_values = null;
140                 $component_type = $this->getComponentType();
141                 $resource_type = $this->getResourceType();
142                 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
143
144                 for ($i = 0; $i < count($this->directive_types); $i++) {
145                         $controls = $this->RepeaterRunscriptOptions->findControlsByType($this->directive_types[$i]);
146                         for ($j = 0; $j < count($controls); $j++) {
147                                 $directive_name = $controls[$j]->getDirectiveName();
148                                 $directive_value = $controls[$j]->getDirectiveValue();
149                                 $default_value = $resource_desc['Runscript']->SubSections->{$directive_name}->DefaultValue;
150                                 $in_config = $controls[$j]->getInConfig();
151                                 $index = $controls[$j]->getGroupName();
152                                 if (is_null($directive_value)) {
153                                         // skip not changed values that don't exist in config
154                                         continue;
155                                 }
156                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
157                                         settype($default_value, 'bool');
158                                 }
159                                 if ($directive_value === $default_value) {
160                                         // value the same as default value, skip it
161                                         continue;
162                                 }
163                                 if (!isset($directive_values['Runscript'])) {
164                                         $directive_values['Runscript'] = array();
165                                 }
166                                 if (!isset($directive_values['Runscript'][$index])) {
167                                         $directive_values['Runscript'][$index] = array();
168                                 }
169                                 $directive_values['Runscript'][$index][$directive_name] = $directive_value;
170                         }
171                 }
172                 return $directive_values;
173         }
174
175         public function getDirectiveData() {
176                 $data = array();
177                 $values = $this->getDirectiveValue();
178                 if (is_array($values) && array_key_exists('Runscript', $values)) {
179                         for ($i = 0; $i < count($values['Runscript']); $i++) {
180                                 $data[$i] = (object)$values['Runscript'][$i];
181                         }
182                 }
183                 return $data;
184         }
185
186         public function newRunscriptDirective() {
187                 $data = $this->getDirectiveData();
188                 array_push($data, new stdClass);
189                 $this->setData($data);
190                 $this->SourceTemplateControl->setShowAllDirectives(true);
191                 $this->loadConfig(null, null);
192         }
193 }