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.DirectiveListBox');
31 Prado::using('Application.Web.Portlets.DirectiveText');
32 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
33 Prado::using('Application.Web.Portlets.DirectiveRunscript');
34 Prado::using('Application.Web.Portlets.DirectiveMessages');
35 Prado::using('Application.IO.TTextWriter');
37 class BaculaConfigDirectives extends DirectiveListTemplate {
39 const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
41 private $show_all_directives = false;
43 public $resource_names = array();
45 private $directive_types = array(
54 private $directive_list_types = array(
61 private $field_multple_values = array(
65 private function getConfigData($host, array $parameters) {
66 $default_params = array('config');
67 $params = array_merge($default_params, $parameters);
68 $result = $this->Application->getModule('api')->get($params, $host, false);
70 if (is_object($result) && $result->error === 0 && (is_object($result->output) || is_array($result->output))) {
71 $config = $result->output;
76 public function loadConfig() {
77 $load_values = $this->getLoadValues();
79 $host = $this->getHost();
80 $component_type = $this->getComponentType();
81 $component_name = $this->getComponentName();
82 $resource_type = $this->getResourceType();
83 $resource_name = $this->getResourceName();
84 $directives = array();
85 $parent_directives = array();
86 $config = new stdClass;
87 if ($load_values === true) {
88 $config = $this->getConfigData($host, array(
93 if ($resource_type === 'Job' && property_exists($config, 'JobDefs')) {
94 $parent_directives = $this->getConfigData($host, array(
101 $data_desc = $this->Application->getModule('data_desc');
102 $resource_desc = $data_desc->getDescription($component_type, $resource_type);
103 foreach ($resource_desc as $directive_name => $directive_desc) {
105 if ($load_values === true) {
106 $in_config = property_exists($config, $directive_name);
109 $directive_value = null;
110 if ($in_config === true && $load_values === true) {
111 $directive_value = $config->{$directive_name};
114 $default_value = null;
116 $field_type = 'TextBox';
119 // @TODO: Add support for all directive properties defined in description file
120 if (is_object($directive_desc)) {
121 if (property_exists($directive_desc, 'Required')) {
122 $required = $directive_desc->Required;
123 if ($load_values === true && array_key_exists($directive_name, $parent_directives)) {
124 // values can be taken from JobDefs
128 if (property_exists($directive_desc, 'DefaultValue')) {
129 $default_value = $directive_desc->DefaultValue;
131 if (property_exists($directive_desc, 'Data')) {
132 $data = $directive_desc->Data;
134 if (property_exists($directive_desc, 'FieldType')) {
135 $field_type = $directive_desc->FieldType;
137 if (property_exists($directive_desc, 'Resource')) {
138 $resource = $directive_desc->Resource;
142 if ((!is_array($directive_value) && !is_object($directive_value)) || in_array($field_type, $this->field_multple_values)) {
143 $directive_value = array($directive_value);
145 if (is_object($directive_value)) {
146 $directive_value = (array)$directive_value;
148 foreach ($directive_value as $key => $value) {
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' => $value,
157 'default_value' => $default_value,
158 'required' => $required,
160 'resource' => $resource,
161 'field_type' => $field_type,
162 'label' => $directive_name,
163 'in_config' => $in_config,
164 'show' => (($in_config || !$load_values) || $this->getShowAllDirectives())
166 array_push($directives, $directive);
169 $config = $this->getConfigData($host, array($component_type));
170 for ($i = 0; $i < count($config); $i++) {
171 $resource_type = $this->getConfigResourceType($config[$i]);
172 $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
173 if (!array_key_exists($resource_type, $this->resource_names)) {
174 $this->resource_names[$resource_type] = array();
176 array_push($this->resource_names[$resource_type], $resource_name);
178 $this->setResourceNames($this->resource_names);
180 $this->RepeaterDirectives->DataSource = $directives;
181 $this->RepeaterDirectives->dataBind();
184 public function loadDirectives($sender, $param) {
185 $show_all_directives = !$this->getShowAllDirectives();
186 $this->setShowAllDirectives($show_all_directives);
190 public function unloadDirectives() {
191 $this->RepeaterDirectives->DataSource = array();
192 $this->RepeaterDirectives->dataBind();
195 public function createDirectiveElement($sender, $param) {
196 $load_values = $this->getLoadValues();
197 for ($i = 0; $i < count($this->directive_types); $i++) {
198 $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
199 if (is_object($control)) {
200 $control->setHost($param->Item->DataItem['host']);
201 $control->setComponentType($param->Item->DataItem['component_type']);
202 $control->setComponentName($param->Item->DataItem['component_name']);
203 $control->setResourceType($param->Item->DataItem['resource_type']);
204 $control->setResourceName($param->Item->DataItem['resource_name']);
205 $control->setDirectiveName($param->Item->DataItem['directive_name']);
206 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
207 $control->setDefaultValue($param->Item->DataItem['default_value']);
208 $control->setRequired($param->Item->DataItem['required']);
209 $control->setData($param->Item->DataItem['data']);
210 $control->setResource($param->Item->DataItem['resource']);
211 $control->setLabel($param->Item->DataItem['label']);
212 $control->setInConfig($param->Item->DataItem['in_config']);
213 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
214 $control->setShow($show_all_directives);
215 $control->setResourceNames($this->resource_names);
219 for ($i = 0; $i < count($this->directive_list_types); $i++) {
220 $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
221 if (is_object($control)) {
222 $control->setHost($param->Item->DataItem['host']);
223 $control->setComponentType($param->Item->DataItem['component_type']);
224 $control->setComponentName($param->Item->DataItem['component_name']);
225 $control->setResourceType($param->Item->DataItem['resource_type']);
226 $control->setResourceName($param->Item->DataItem['resource_name']);
227 $control->setDirectiveName($param->Item->DataItem['directive_name']);
228 $control->setData($param->Item->DataItem['directive_value']);
229 $control->setLoadValues($this->getLoadValues());
230 $control->setResourceNames($this->resource_names);
231 $control->raiseEvent('OnDirectiveListLoad', $this, null);
237 public function saveResource($sender, $param) {
238 $directives = array();
239 $host = $this->getHost();
240 $component_type = $this->getComponentType();
241 $resource_type = $this->getResourceType();
242 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
243 for ($i = 0; $i < count($this->directive_types); $i++) {
244 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
245 for ($j = 0; $j < count($controls); $j++) {
246 $parent_name = $controls[$j]->getParentName();
247 if (!is_null($parent_name)) {
250 $directive_name = $controls[$j]->getDirectiveName();
251 $directive_value = $controls[$j]->getDirectiveValue();
252 $default_value = $resource_desc[$directive_name]->DefaultValue;
253 $in_config = $controls[$j]->getInConfig();
254 if (is_null($directive_value)) {
255 // skip not changed values that don't exist in config
258 if ($this->directive_types[$i] === 'DirectiveBoolean') {
259 settype($default_value, 'bool');
260 } elseif ($this->directive_types[$i] === 'DirectiveInteger') {
261 settype($directive_value, 'int');
263 if ($directive_value === $default_value && $in_config === false) {
264 // value the same as default value, skip it
267 $directives[$directive_name] = $directive_value;
270 for ($i = 0; $i < count($this->directive_list_types); $i++) {
271 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
272 for ($j = 0; $j < count($controls); $j++) {
273 $directive_name = $controls[$j]->getDirectiveName();
274 $directive_value = $controls[$j]->getDirectiveValue();
275 if (is_null($directive_value)) {
278 if (!array_key_exists($directive_name, $directives)) {
279 $directives[$directive_name] = array();
281 if (is_array($directive_value)) {
282 if ($this->directive_list_types[$i] === 'DirectiveMessages') {
283 $directives = array_merge($directives, $directive_value);
284 } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
285 if (!isset($directives[$directive_name])) {
286 $directives[$directive_name] = array();
288 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
289 } elseif (array_key_exists($directive_name, $directive_value)) {
290 $directives[$directive_name][] = $directive_value[$directive_name];
291 } elseif (count($directive_value) > 0) {
292 $directives[$directive_name][] = $directive_value;
295 $directives[$directive_name] = $directive_value;
299 $load_values = $this->getLoadValues();
300 if ($load_values === true) {
301 $resource_name = $this->getResourceName();
303 $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
312 $result = $this->Application->getModule('api')->set($params, array('config' => json_encode($directives)), $host, false);
313 if ($result->error === 0) {
314 $this->SaveDirectiveOk->Display = 'Dynamic';
315 $this->SaveDirectiveError->Display = 'None';
316 $this->SaveDirectiveErrMsg->Text = '';
318 $this->SaveDirectiveOk->Display = 'None';
319 $this->SaveDirectiveError->Display = 'Dynamic';
320 $this->SaveDirectiveErrMsg->Display = 'Dynamic';
321 $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
325 public function setShowAllDirectives($show_all_directives) {
326 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
329 public function getShowAllDirectives() {
330 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);