3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 Kern Sibbald
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.
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.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
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.DirectiveComboBox');
29 Prado::using('Application.Web.Portlets.DirectiveInteger');
30 Prado::using('Application.Web.Portlets.DirectiveText');
31 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
32 Prado::using('Application.Web.Portlets.DirectiveRunscript');
33 Prado::using('Application.Web.Portlets.DirectiveMessages');
34 Prado::using('Application.IO.TTextWriter');
36 class BaculaConfigDirectives extends DirectiveListTemplate {
38 const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
40 private $show_all_directives = false;
42 public $resource_names = array();
44 private $directive_types = array(
52 private $directive_list_types = array(
59 private function getConfigData($host, array $parameters) {
60 $default_params = array('config');
61 $params = array_merge($default_params, $parameters);
62 $result = $this->Application->getModule('api')->get($params, $host, false);
64 if (is_object($result) && $result->error === 0 && (is_object($result->output) || is_array($result->output))) {
65 $config = $result->output;
70 public function loadConfig() {
71 $load_values = $this->getLoadValues();
73 $host = $this->getHost();
74 $component_type = $this->getComponentType();
75 $component_name = $this->getComponentName();
76 $resource_type = $this->getResourceType();
77 $resource_name = $this->getResourceName();
78 $directives = array();
79 $parent_directives = array();
80 $config = new stdClass;
81 if ($load_values === true) {
82 $config = $this->getConfigData($host, array(
87 if ($resource_type === 'Job' && property_exists($config, 'JobDefs')) {
88 $parent_directives = $this->getConfigData($host, array(
95 $data_desc = $this->Application->getModule('data_desc');
96 $resource_desc = $data_desc->getDescription($component_type, $resource_type);
97 foreach ($resource_desc as $directive_name => $directive_desc) {
99 if ($load_values === true) {
100 $in_config = property_exists($config, $directive_name);
103 $directive_value = null;
104 if ($in_config === true && $load_values === true) {
105 $directive_value = $config->{$directive_name};
108 $default_value = null;
110 $field_type = 'TextBox';
113 // @TODO: Add support for all directive properties defined in description file
114 if (is_object($directive_desc)) {
115 if (property_exists($directive_desc, 'Required')) {
116 $required = $directive_desc->Required;
117 if ($load_values === true && array_key_exists($directive_name, $parent_directives)) {
118 // values can be taken from JobDefs
122 if (property_exists($directive_desc, 'DefaultValue')) {
123 $default_value = $directive_desc->DefaultValue;
125 if (property_exists($directive_desc, 'Data')) {
126 $data = $directive_desc->Data;
128 if (property_exists($directive_desc, 'FieldType')) {
129 $field_type = $directive_desc->FieldType;
131 if (property_exists($directive_desc, 'Resource')) {
132 $resource = $directive_desc->Resource;
136 if (!is_array($directive_value) && !is_object($directive_value)) {
137 $directive_value = array($directive_value);
139 if (is_object($directive_value)) {
140 $directive_value = (array)$directive_value;
142 foreach ($directive_value as $key => $value) {
145 'component_type' => $component_type,
146 'component_name' => $component_name,
147 'resource_type' => $resource_type,
148 'resource_name' => $resource_name,
149 'directive_name' => $directive_name,
150 'directive_value' => $value,
151 'default_value' => $default_value,
152 'required' => $required,
154 'resource' => $resource,
155 'field_type' => $field_type,
156 'label' => $directive_name,
157 'in_config' => $in_config,
158 'show' => (($in_config || !$load_values) || $this->getShowAllDirectives())
160 array_push($directives, $directive);
163 $config = $this->getConfigData($host, array($component_type));
164 for ($i = 0; $i < count($config); $i++) {
165 $resource_type = $this->getConfigResourceType($config[$i]);
166 $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
167 if (!array_key_exists($resource_type, $this->resource_names)) {
168 $this->resource_names[$resource_type] = array();
170 array_push($this->resource_names[$resource_type], $resource_name);
172 $this->setResourceNames($this->resource_names);
174 $this->RepeaterDirectives->DataSource = $directives;
175 $this->RepeaterDirectives->dataBind();
178 public function loadDirectives($sender, $param) {
179 $show_all_directives = !$this->getShowAllDirectives();
180 $this->setShowAllDirectives($show_all_directives);
184 public function unloadDirectives() {
185 $this->RepeaterDirectives->DataSource = array();
186 $this->RepeaterDirectives->dataBind();
189 public function createDirectiveElement($sender, $param) {
190 $load_values = $this->getLoadValues();
191 for ($i = 0; $i < count($this->directive_types); $i++) {
192 $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
193 if (is_object($control)) {
194 $control->setHost($param->Item->DataItem['host']);
195 $control->setComponentType($param->Item->DataItem['component_type']);
196 $control->setComponentName($param->Item->DataItem['component_name']);
197 $control->setResourceType($param->Item->DataItem['resource_type']);
198 $control->setResourceName($param->Item->DataItem['resource_name']);
199 $control->setDirectiveName($param->Item->DataItem['directive_name']);
200 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
201 $control->setDefaultValue($param->Item->DataItem['default_value']);
202 $control->setRequired($param->Item->DataItem['required']);
203 $control->setData($param->Item->DataItem['data']);
204 $control->setResource($param->Item->DataItem['resource']);
205 $control->setLabel($param->Item->DataItem['label']);
206 $control->setInConfig($param->Item->DataItem['in_config']);
207 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
208 $control->setShow($show_all_directives);
209 $control->setResourceNames($this->resource_names);
213 for ($i = 0; $i < count($this->directive_list_types); $i++) {
214 $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
215 if (is_object($control)) {
216 $control->setHost($param->Item->DataItem['host']);
217 $control->setComponentType($param->Item->DataItem['component_type']);
218 $control->setComponentName($param->Item->DataItem['component_name']);
219 $control->setResourceType($param->Item->DataItem['resource_type']);
220 $control->setResourceName($param->Item->DataItem['resource_name']);
221 $control->setDirectiveName($param->Item->DataItem['directive_name']);
222 $control->setData($param->Item->DataItem['directive_value']);
223 $control->setLoadValues($this->getLoadValues());
224 $control->setResourceNames($this->resource_names);
225 $control->raiseEvent('OnDirectiveListLoad', $this, null);
231 public function saveResource($sender, $param) {
232 $directives = array();
233 $host = $this->getHost();
234 $component_type = $this->getComponentType();
235 $resource_type = $this->getResourceType();
236 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
237 for ($i = 0; $i < count($this->directive_types); $i++) {
238 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
239 for ($j = 0; $j < count($controls); $j++) {
240 $parent_name = $controls[$j]->getParentName();
241 if (!is_null($parent_name)) {
244 $directive_name = $controls[$j]->getDirectiveName();
245 $directive_value = $controls[$j]->getDirectiveValue();
246 $default_value = $resource_desc[$directive_name]->DefaultValue;
247 $in_config = $controls[$j]->getInConfig();
248 if (is_null($directive_value)) {
249 // skip not changed values that don't exist in config
252 if ($this->directive_types[$i] === 'DirectiveBoolean') {
253 settype($default_value, 'bool');
254 } elseif ($this->directive_types[$i] === 'DirectiveInteger') {
255 settype($directive_value, 'int');
257 if ($directive_value === $default_value && $in_config === false) {
258 // value the same as default value, skip it
261 $directives[$directive_name] = $directive_value;
264 for ($i = 0; $i < count($this->directive_list_types); $i++) {
265 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
266 for ($j = 0; $j < count($controls); $j++) {
267 $directive_name = $controls[$j]->getDirectiveName();
268 $directive_value = $controls[$j]->getDirectiveValue();
269 if (is_null($directive_value)) {
272 if (!array_key_exists($directive_name, $directives)) {
273 $directives[$directive_name] = array();
275 if (is_array($directive_value)) {
276 if ($this->directive_list_types[$i] === 'DirectiveMessages') {
277 $directives = array_merge($directives, $directive_value);
278 } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
279 if (!isset($directives[$directive_name])) {
280 $directives[$directive_name] = array();
282 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
283 } elseif (array_key_exists($directive_name, $directive_value)) {
284 $directives[$directive_name][] = $directive_value[$directive_name];
285 } elseif (count($directive_value) > 0) {
286 $directives[$directive_name][] = $directive_value;
289 $directives[$directive_name] = $directive_value;
293 $load_values = $this->getLoadValues();
294 if ($load_values === true) {
295 $resource_name = $this->getResourceName();
297 $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
306 $result = $this->Application->getModule('api')->set($params, array('config' => json_encode($directives)), $host, false);
307 if ($result->error === 0) {
308 $this->SaveDirectiveOk->Display = 'Dynamic';
309 $this->SaveDirectiveError->Display = 'None';
310 $this->SaveDirectiveErrMsg->Text = '';
312 $this->SaveDirectiveOk->Display = 'None';
313 $this->SaveDirectiveError->Display = 'Dynamic';
314 $this->SaveDirectiveErrMsg->Display = 'Dynamic';
315 $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
319 public function setShowAllDirectives($show_all_directives) {
320 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
323 public function getShowAllDirectives() {
324 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);