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 createDirectiveElement($sender, $param) {
185 $load_values = $this->getLoadValues();
186 for ($i = 0; $i < count($this->directive_types); $i++) {
187 $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
188 if (is_object($control)) {
189 $control->setHost($param->Item->DataItem['host']);
190 $control->setComponentType($param->Item->DataItem['component_type']);
191 $control->setComponentName($param->Item->DataItem['component_name']);
192 $control->setResourceType($param->Item->DataItem['resource_type']);
193 $control->setResourceName($param->Item->DataItem['resource_name']);
194 $control->setDirectiveName($param->Item->DataItem['directive_name']);
195 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
196 $control->setDefaultValue($param->Item->DataItem['default_value']);
197 $control->setRequired($param->Item->DataItem['required']);
198 $control->setData($param->Item->DataItem['data']);
199 $control->setResource($param->Item->DataItem['resource']);
200 $control->setLabel($param->Item->DataItem['label']);
201 $control->setInConfig($param->Item->DataItem['in_config']);
202 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
203 $control->setShow($show_all_directives);
204 $control->setResourceNames($this->resource_names);
208 for ($i = 0; $i < count($this->directive_list_types); $i++) {
209 $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
210 if (is_object($control)) {
211 $control->setHost($param->Item->DataItem['host']);
212 $control->setComponentType($param->Item->DataItem['component_type']);
213 $control->setComponentName($param->Item->DataItem['component_name']);
214 $control->setResourceType($param->Item->DataItem['resource_type']);
215 $control->setResourceName($param->Item->DataItem['resource_name']);
216 $control->setDirectiveName($param->Item->DataItem['directive_name']);
217 $control->setData($param->Item->DataItem['directive_value']);
218 $control->setLoadValues($this->getLoadValues());
219 $control->setResourceNames($this->resource_names);
220 $control->raiseEvent('OnDirectiveListLoad', $this, null);
226 public function saveResource($sender, $param) {
227 $directives = array();
228 $host = $this->getHost();
229 $component_type = $this->getComponentType();
230 $resource_type = $this->getResourceType();
231 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
232 for ($i = 0; $i < count($this->directive_types); $i++) {
233 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
234 for ($j = 0; $j < count($controls); $j++) {
235 $parent_name = $controls[$j]->getParentName();
236 if (!is_null($parent_name)) {
239 $directive_name = $controls[$j]->getDirectiveName();
240 $directive_value = $controls[$j]->getDirectiveValue();
241 $default_value = $resource_desc[$directive_name]->DefaultValue;
242 $in_config = $controls[$j]->getInConfig();
243 if (is_null($directive_value)) {
244 // skip not changed values that don't exist in config
247 if ($this->directive_types[$i] === 'DirectiveBoolean') {
248 settype($default_value, 'bool');
249 } elseif ($this->directive_types[$i] === 'DirectiveInteger') {
250 settype($directive_value, 'int');
252 if ($directive_value === $default_value && $in_config === false) {
253 // value the same as default value, skip it
256 $directives[$directive_name] = $directive_value;
259 for ($i = 0; $i < count($this->directive_list_types); $i++) {
260 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
261 for ($j = 0; $j < count($controls); $j++) {
262 $directive_name = $controls[$j]->getDirectiveName();
263 $directive_value = $controls[$j]->getDirectiveValue();
264 if (is_null($directive_value)) {
267 if (!array_key_exists($directive_name, $directives)) {
268 $directives[$directive_name] = array();
270 if (is_array($directive_value)) {
271 if ($this->directive_list_types[$i] === 'DirectiveMessages') {
272 $directives = array_merge($directives, $directive_value);
273 } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
274 if (!isset($directives[$directive_name])) {
275 $directives[$directive_name] = array();
277 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
278 } elseif (array_key_exists($directive_name, $directive_value)) {
279 $directives[$directive_name][] = $directive_value[$directive_name];
280 } elseif (count($directive_value) > 0) {
281 $directives[$directive_name][] = $directive_value;
284 $directives[$directive_name] = $directive_value;
288 $load_values = $this->getLoadValues();
289 if ($load_values === true) {
290 $resource_name = $this->getResourceName();
292 $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
301 $result = $this->Application->getModule('api')->set($params, array('config' => serialize($directives)), $host, false);
302 if ($result->error === 0) {
303 $this->SaveDirectiveOk->Display = 'Dynamic';
304 $this->SaveDirectiveError->Display = 'None';
305 $this->SaveDirectiveErrMsg->Text = '';
307 $this->SaveDirectiveOk->Display = 'None';
308 $this->SaveDirectiveError->Display = 'Dynamic';
309 $this->SaveDirectiveErrMsg->Display = 'Dynamic';
310 $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
314 public function setShowAllDirectives($show_all_directives) {
315 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
318 public function getShowAllDirectives() {
319 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);