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