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.TActiveLinkButton');
25 Prado::using('Application.Web.Portlets.DirectiveListTemplate');
26 Prado::using('Application.Web.Portlets.DirectiveBoolean');
27 Prado::using('Application.Web.Portlets.DirectiveText');
28 Prado::using('Application.Web.Portlets.DirectiveComboBox');
29 Prado::using('Application.Web.Portlets.DirectiveInteger');
31 class DirectiveFileSet extends DirectiveListTemplate {
33 const MENU_CONTROL = 'NewFileSetMenu';
35 private $directive_types = array(
42 private $directive_inc_exc_types = array(
46 public function loadConfig($sender, $param) {
47 $component_type = $this->getComponentType();
48 $component_name = $this->getComponentType();
49 $resource_type = $this->getResourceType();
50 $directive_name = $this->getDirectiveName();
51 $directives = $this->getData();
52 $data_source = array();
56 if (is_object($directives)) { // Include with options
57 foreach($directives as $name => $values) {
60 $this->setFile($include, $name, $values);
64 $this->setOption($options, $name, $values);
68 $this->setFile($exclude, $name, $values);
75 $this->RepeaterFileSetOptions->DataSource = $options;
76 $this->RepeaterFileSetOptions->dataBind();
77 $this->RepeaterFileSetInclude->DataSource = $include;
78 $this->RepeaterFileSetInclude->dataBind();
79 $this->RepeaterFileSetExclude->DataSource = $exclude;
80 $this->RepeaterFileSetExclude->dataBind();
81 $this->FileSetMenu->setComponentType($component_type);
82 $this->FileSetMenu->setComponentName($component_name);
83 $this->FileSetMenu->setResourceType($resource_type);
84 $this->FileSetMenu->setDirectiveName($directive_name);
87 private function setFile(&$files, $name, $config) {
88 $host = $this->getHost();
89 $component_type = $this->getComponentType();
90 $component_name = $this->getComponentName();
91 $resource_type = $this->getResourceType();
92 $resource_name = $this->getResourceName();
94 for ($i = 0; $i < count($config); $i++) {
97 'component_type' => $component_type,
98 'component_name' => $component_name,
99 'resource_type' => $resource_type,
100 'resource_name' => $resource_name,
101 'directive_name' => $name,
102 'directive_value' => $config[$i],
103 'parent_name' => $name
108 private function setOption(&$options, $name, $config) {
109 $load_values = $this->getLoadValues();
110 $host = $this->getHost();
111 $component_type = $this->getComponentType();
112 $component_name = $this->getComponentName();
113 $resource_type = $this->getResourceType();
114 $resource_name = $this->getResourceName();
116 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type, 'Include');
118 for ($i = 0; $i < count($config); $i++) {
119 foreach ($resource_desc->SubSections as $directive_name => $directive_desc) {
120 $in_config = property_exists($config[$i], $directive_name);
121 $directive_value = null;
122 if ($in_config === true) {
123 $directive_value = $config[$i]->{$directive_name};
126 $default_value = null;
128 $field_type = 'TextBox';
130 if (is_object($directive_desc)) {
131 if (property_exists($directive_desc, 'Required')) {
132 $required = $directive_desc->Required;
134 if (property_exists($directive_desc, 'DefaultValue')) {
135 $default_value = $directive_desc->DefaultValue;
137 if (property_exists($directive_desc, 'Data')) {
138 $data = $directive_desc->Data;
140 if (property_exists($directive_desc, 'FieldType')) {
141 $field_type = $directive_desc->FieldType;
144 if (!is_array($directive_value)) {
145 $directive_value = array($directive_value);
147 for ($j = 0; $j < count($directive_value); $j++) {
150 'component_type' => $component_type,
151 'component_name' => $component_name,
152 'resource_type' => $resource_type,
153 'resource_name' => $resource_name,
154 'directive_name' => $directive_name,
155 'directive_value' => $directive_value[$j],
156 'default_value' => $default_value,
157 'required' => $required,
159 'field_type' => $field_type,
160 'in_config' => $in_config,
161 'label' => $directive_name,
162 'show' => ($in_config || !$load_values || $this->SourceTemplateControl->getShowAllDirectives()),
163 'parent_name' => $name,
171 public function getDirectiveValue() {
172 $directive_values = array();
173 $component_type = $this->getComponentType();
174 $resource_type = $this->getResourceType();
175 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
177 for ($i = 0; $i < count($this->directive_types); $i++) {
178 $controls = $this->RepeaterFileSetOptions->findControlsByType($this->directive_types[$i]);
179 for ($j = 0; $j < count($controls); $j++) {
180 $directive_name = $controls[$j]->getDirectiveName();
181 $directive_value = $controls[$j]->getDirectiveValue();
182 $index = $controls[$j]->getGroupName();
183 $default_value = $resource_desc['Include']->SubSections->{$directive_name}->DefaultValue;
184 $in_config = $controls[$j]->getInConfig();
185 if (is_null($directive_value)) {
186 // option not set or removed
189 if ($this->directive_types[$i] === 'DirectiveBoolean') {
190 settype($default_value, 'bool');
192 if ($directive_value === $default_value) {
193 // value the same as default value, skip it
196 if (!array_key_exists('Include', $directive_values)) {
197 $directive_values['Include'] = array('Options' => array());
199 if (!isset($directive_values['Include']['Options'][$index])) {
200 $directive_values['Include']['Options'][$index] = array();
202 $directive_values['Include']['Options'][$index][$directive_name] = $directive_value;
204 $controls = $this->RepeaterFileSetInclude->findControlsByType($this->directive_types[$i]);
205 for ($j = 0; $j < count($controls); $j++) {
206 $directive_name = $controls[$j]->getDirectiveName();
207 $directive_value = $controls[$j]->getDirectiveValue();
208 if (is_null($directive_value)) {
209 // Include file directive removed
212 if (!array_key_exists('Include', $directive_values)) {
213 $directive_values['Include'] = array();
215 if (!array_key_exists($directive_name, $directive_values['Include'])) {
216 $directive_values['Include'][$directive_name] = array();
218 array_push($directive_values['Include'][$directive_name], $directive_value);
220 $controls = $this->RepeaterFileSetExclude->findControlsByType($this->directive_types[$i]);
221 for ($j = 0; $j < count($controls); $j++) {
222 $directive_name = $controls[$j]->getDirectiveName();
223 $directive_value = $controls[$j]->getDirectiveValue();
224 if (is_null($directive_value)) {
225 // Exclude file directive removed
228 if (!array_key_exists($directive_name, $directive_values)) {
229 $directive_values[$directive_name] = array('File' => array());
231 array_push($directive_values[$directive_name]['File'], $directive_value);
235 return $directive_values;
238 public function createFileSetOptions($sender, $param) {
239 $load_values = $this->getLoadValues();
240 $bconditionals = $this->RepeaterFileSetOptions->findControlsByType('BConditionalItem');
241 for ($i = 0; $i < count($bconditionals); $i++) {
242 $item = $bconditionals[$i]->getData();
243 for ($j = 0; $j < count($this->directive_types); $j++) {
244 $control = $this->getChildControl($item, $this->directive_types[$j]);
245 if (is_object($control)) {
246 $control->setHost($item->DataItem['host']);
247 $control->setComponentType($item->DataItem['component_type']);
248 $control->setComponentName($item->DataItem['component_name']);
249 $control->setResourceType($item->DataItem['resource_type']);
250 $control->setResourceName($item->DataItem['resource_name']);
251 $control->setDirectiveName($item->DataItem['directive_name']);
252 $control->setDirectiveValue($item->DataItem['directive_value']);
253 $control->setDefaultValue($item->DataItem['default_value']);
254 $control->setRequired($item->DataItem['required']);
255 $control->setData($item->DataItem['data']);
256 $control->setLabel($item->DataItem['label']);
257 $control->setInConfig($item->DataItem['in_config']);
258 $show_all_directives = ($item->DataItem['in_config'] || !$load_values || $this->SourceTemplateControl->getShowAllDirectives());
259 $control->setShow($show_all_directives);
260 $control->setParentName($item->DataItem['parent_name']);
261 $control->setGroupName($item->DataItem['group_name']);
267 public function createFileSetIncExcElement($sender, $param) {
268 for ($i = 0; $i < count($this->directive_inc_exc_types); $i++) {
269 $control = $this->getChildControl($param->Item, $this->directive_inc_exc_types[$i]);
270 if (is_object($control)) {
271 $control->setHost($param->Item->DataItem['host']);
272 $control->setComponentType($param->Item->DataItem['component_type']);
273 $control->setComponentName($param->Item->DataItem['component_name']);
274 $control->setResourceType($param->Item->DataItem['resource_type']);
275 $control->setResourceName($param->Item->DataItem['resource_name']);
276 $control->setDirectiveName($param->Item->DataItem['directive_name']);
277 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
278 $control->setLabel($param->Item->DataItem['directive_name']);
279 $control->setData($param->Item->DataItem['directive_value']);
280 $control->setInConfig(true);
281 $control->setShow(true);
282 $control->setParentName($param->Item->DataItem['parent_name']);
287 private function getDirectiveData() {
288 $values = $this->getDirectiveValue();
290 if (array_key_exists('Include', $values) && array_key_exists('File', $values['Include'])) {
291 $data['File'] = $values['Include']['File'];
292 if (array_key_exists('Options', $values['Include']) && is_array($values['Include']['Options'])) {
293 $data['Options'] = array();
294 for ($i = 0; $i < count($values['Include']['Options']); $i++) {
295 $data['Options'][$i] = (object)$values['Include']['Options'][$i];
299 if (array_key_exists('Exclude', $values) && array_key_exists('File', $values['Exclude'])) {
300 $data['Exclude'] = $values['Exclude']['File'];
305 public function newIncludeFile($sender, $param) {
306 $data = $this->getDirectiveData();
307 if (array_key_exists('File', $data) && is_array($data['File'])) {
308 $data['File'][] = '';
310 $data['File'] = array('');
312 $data = (object)$data;
313 $this->setData($data);
314 $this->loadConfig(null, null);
317 public function newExcludeFile($sender, $param) {
318 $data = $this->getDirectiveData();
319 if (array_key_exists('Exclude', $data) && is_array($data['Exclude'])) {
320 $data['Exclude'][] = '';
322 $data['Exclude'] = array('');
324 $data = (object)$data;
325 $this->setData($data);
326 $this->loadConfig(null, null);
329 public function newIncludeOptions($sender, $param) {
330 $data = $this->getDirectiveData();
331 if (array_key_exists('Options', $data) && is_array($data['Options'])) {
332 $data['Options'][] = new stdClass;
334 $data['Options'] = array(new stdClass);
336 $data = (object)$data;
337 $this->SourceTemplateControl->setShowAllDirectives(true);
338 $this->setData($data);
339 $this->loadConfig(null, null);