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.DirectiveText');
29 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
30 Prado::using('Application.Web.Portlets.DirectiveRunscript');
31 Prado::using('Application.Web.Portlets.DirectiveMessages');
32 Prado::using('Application.IO.TTextWriter');
34 class BaculaConfigDirectives extends DirectiveListTemplate {
36 const SHOW_ALL_DIRECTIVES = 'ShowAllDirectives';
38 private $show_all_directives = false;
40 public $resource_names = array();
42 private $directive_types = array(
49 private $directive_list_types = array(
56 private function getConfigData($host, array $parameters) {
57 $default_params = array('config');
58 $params = array_merge($default_params, $parameters);
59 $result = $this->Application->getModule('api')->get($params, $host, false);
61 if (is_object($result) && $result->error === 0 && (is_object($result->output) || is_array($result->output))) {
62 $config = $result->output;
67 public function loadConfig() {
68 $load_values = $this->getLoadValues();
70 $host = $this->getHost();
71 $component_type = $this->getComponentType();
72 $component_name = $this->getComponentName();
73 $resource_type = $this->getResourceType();
74 $resource_name = $this->getResourceName();
75 $directives = array();
76 $parent_directives = array();
77 $config = new stdClass;
78 if ($load_values === true) {
79 $config = $this->getConfigData($host, array(
84 if ($resource_type === 'Job' && property_exists($config, 'JobDefs')) {
85 $parent_directives = $this->getConfigData($host, array(
92 $data_desc = $this->Application->getModule('data_desc');
93 $resource_desc = $data_desc->getDescription($component_type, $resource_type);
94 foreach ($resource_desc as $directive_name => $directive_desc) {
96 if ($load_values === true) {
97 $in_config = property_exists($config, $directive_name);
100 $directive_value = null;
101 if ($in_config === true && $load_values === true) {
102 $directive_value = $config->{$directive_name};
105 $default_value = null;
107 $field_type = 'TextBox';
110 // @TODO: Add support for all directive properties defined in description file
111 if (is_object($directive_desc)) {
112 if (property_exists($directive_desc, 'Required')) {
113 $required = $directive_desc->Required;
114 if ($load_values === true && array_key_exists($directive_name, $parent_directives)) {
115 // values can be taken from JobDefs
119 if (property_exists($directive_desc, 'DefaultValue')) {
120 $default_value = $directive_desc->DefaultValue;
122 if (property_exists($directive_desc, 'Data')) {
123 $data = $directive_desc->Data;
125 if (property_exists($directive_desc, 'FieldType')) {
126 $field_type = $directive_desc->FieldType;
128 if (property_exists($directive_desc, 'Resource')) {
129 $resource = $directive_desc->Resource;
133 if (!is_array($directive_value) && !is_object($directive_value)) {
134 $directive_value = array($directive_value);
136 if (is_object($directive_value)) {
137 $directive_value = (array)$directive_value;
139 foreach ($directive_value as $key => $value) {
142 'component_type' => $component_type,
143 'component_name' => $component_name,
144 'resource_type' => $resource_type,
145 'resource_name' => $resource_name,
146 'directive_name' => $directive_name,
147 'directive_value' => $value,
148 'default_value' => $default_value,
149 'required' => $required,
151 'resource' => $resource,
152 'field_type' => $field_type,
153 'label' => $directive_name,
154 'in_config' => $in_config,
155 'show' => (($in_config || !$load_values) || $this->getShowAllDirectives())
157 array_push($directives, $directive);
160 $config = $this->getConfigData($host, array($component_type));
161 for ($i = 0; $i < count($config); $i++) {
162 $resource_type = $this->getConfigResourceType($config[$i]);
163 $resource_name = property_exists($config[$i]->{$resource_type}, 'Name') ? $config[$i]->{$resource_type}->Name : '';
164 if (!array_key_exists($resource_type, $this->resource_names)) {
165 $this->resource_names[$resource_type] = array();
167 array_push($this->resource_names[$resource_type], $resource_name);
169 $this->setResourceNames($this->resource_names);
171 $this->RepeaterDirectives->DataSource = $directives;
172 $this->RepeaterDirectives->dataBind();
175 public function loadDirectives($sender, $param) {
176 $show_all_directives = !$this->getShowAllDirectives();
177 $this->setShowAllDirectives($show_all_directives);
181 public function createDirectiveElement($sender, $param) {
182 $load_values = $this->getLoadValues();
183 for ($i = 0; $i < count($this->directive_types); $i++) {
184 $control = $this->getChildControl($param->Item, $this->directive_types[$i]);
185 if (is_object($control)) {
186 $control->setHost($param->Item->DataItem['host']);
187 $control->setComponentType($param->Item->DataItem['component_type']);
188 $control->setComponentName($param->Item->DataItem['component_name']);
189 $control->setResourceType($param->Item->DataItem['resource_type']);
190 $control->setResourceName($param->Item->DataItem['resource_name']);
191 $control->setDirectiveName($param->Item->DataItem['directive_name']);
192 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
193 $control->setDefaultValue($param->Item->DataItem['default_value']);
194 $control->setRequired($param->Item->DataItem['required']);
195 $control->setData($param->Item->DataItem['data']);
196 $control->setResource($param->Item->DataItem['resource']);
197 $control->setLabel($param->Item->DataItem['label']);
198 $control->setInConfig($param->Item->DataItem['in_config']);
199 $show_all_directives = ($param->Item->DataItem['in_config'] || !$load_values || $this->getShowAllDirectives());
200 $control->setShow($show_all_directives);
201 $control->setResourceNames($this->resource_names);
205 for ($i = 0; $i < count($this->directive_list_types); $i++) {
206 $control = $this->getChildControl($param->Item, $this->directive_list_types[$i]);
207 if (is_object($control)) {
208 $control->setHost($param->Item->DataItem['host']);
209 $control->setComponentType($param->Item->DataItem['component_type']);
210 $control->setComponentName($param->Item->DataItem['component_name']);
211 $control->setResourceType($param->Item->DataItem['resource_type']);
212 $control->setResourceName($param->Item->DataItem['resource_name']);
213 $control->setDirectiveName($param->Item->DataItem['directive_name']);
214 $control->setData($param->Item->DataItem['directive_value']);
215 $control->setLoadValues($this->getLoadValues());
216 $control->setResourceNames($this->resource_names);
217 $control->raiseEvent('OnDirectiveListLoad', $this, null);
223 public function saveResource($sender, $param) {
224 $directives = array();
225 $host = $this->getHost();
226 $component_type = $this->getComponentType();
227 $resource_type = $this->getResourceType();
228 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
229 for ($i = 0; $i < count($this->directive_types); $i++) {
230 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_types[$i]);
231 for ($j = 0; $j < count($controls); $j++) {
232 $parent_name = $controls[$j]->getParentName();
233 if (!is_null($parent_name)) {
236 $directive_name = $controls[$j]->getDirectiveName();
237 $directive_value = $controls[$j]->getDirectiveValue();
238 $default_value = $resource_desc[$directive_name]->DefaultValue;
239 $in_config = $controls[$j]->getInConfig();
240 if (is_null($directive_value)) {
241 // skip not changed values that don't exist in config
244 if ($this->directive_types[$i] === 'DirectiveBoolean') {
245 settype($default_value, 'bool');
247 if ($directive_value === $default_value && $in_config === false) {
248 // value the same as default value, skip it
251 $directives[$directive_name] = $directive_value;
254 for ($i = 0; $i < count($this->directive_list_types); $i++) {
255 $controls = $this->RepeaterDirectives->findControlsByType($this->directive_list_types[$i]);
256 for ($j = 0; $j < count($controls); $j++) {
257 $directive_name = $controls[$j]->getDirectiveName();
258 $directive_value = $controls[$j]->getDirectiveValue();
259 if (is_null($directive_value)) {
262 if (!array_key_exists($directive_name, $directives)) {
263 $directives[$directive_name] = array();
265 if (is_array($directive_value)) {
266 if ($this->directive_list_types[$i] === 'DirectiveMessages') {
267 $directives = array_merge($directives, $directive_value);
268 } elseif ($this->directive_list_types[$i] === 'DirectiveRunscript') {
269 if (!isset($directives[$directive_name])) {
270 $directives[$directive_name] = array();
272 $directives[$directive_name] = array_merge($directives[$directive_name], $directive_value[$directive_name]);
273 } elseif (array_key_exists($directive_name, $directive_value)) {
274 $directives[$directive_name][] = $directive_value[$directive_name];
275 } elseif (count($directive_value) > 0) {
276 $directives[$directive_name][] = $directive_value;
279 $directives[$directive_name] = $directive_value;
283 $load_values = $this->getLoadValues();
284 if ($load_values === true) {
285 $resource_name = $this->getResourceName();
287 $resource_name = array_key_exists('Name', $directives) ? $directives['Name'] : '';
296 $result = $this->Application->getModule('api')->set($params, array('config' => serialize($directives)), $host, false);
297 if ($result->error === 0) {
298 $this->SaveDirectiveOk->Display = 'Dynamic';
299 $this->SaveDirectiveError->Display = 'None';
300 $this->SaveDirectiveErrMsg->Text = '';
302 $this->SaveDirectiveOk->Display = 'None';
303 $this->SaveDirectiveError->Display = 'Dynamic';
304 $this->SaveDirectiveErrMsg->Display = 'Dynamic';
305 $this->SaveDirectiveErrMsg->Text = "Error {$result->error}: {$result->output}";
309 public function setShowAllDirectives($show_all_directives) {
310 $this->setViewState(self::SHOW_ALL_DIRECTIVES, $show_all_directives);
313 public function getShowAllDirectives() {
314 return $this->getViewState(self::SHOW_ALL_DIRECTIVES, false);