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(
43 private $directive_inc_exc_types = array(
47 public function loadConfig($sender, $param) {
48 $component_type = $this->getComponentType();
49 $component_name = $this->getComponentType();
50 $resource_type = $this->getResourceType();
51 $directive_name = $this->getDirectiveName();
52 $directives = $this->getData();
53 $data_source = array();
57 if (is_object($directives)) { // Include with options
58 foreach($directives as $name => $values) {
61 $this->setFile($include, $name, $values);
65 $this->setOption($options, $name, $values);
69 $this->setFile($exclude, $name, $values);
76 $this->RepeaterFileSetOptions->DataSource = $options;
77 $this->RepeaterFileSetOptions->dataBind();
78 $this->RepeaterFileSetInclude->DataSource = $include;
79 $this->RepeaterFileSetInclude->dataBind();
80 $this->RepeaterFileSetExclude->DataSource = $exclude;
81 $this->RepeaterFileSetExclude->dataBind();
82 $this->FileSetMenu->setComponentType($component_type);
83 $this->FileSetMenu->setComponentName($component_name);
84 $this->FileSetMenu->setResourceType($resource_type);
85 $this->FileSetMenu->setDirectiveName($directive_name);
88 private function setFile(&$files, $name, $config) {
89 $host = $this->getHost();
90 $component_type = $this->getComponentType();
91 $component_name = $this->getComponentName();
92 $resource_type = $this->getResourceType();
93 $resource_name = $this->getResourceName();
95 for ($i = 0; $i < count($config); $i++) {
98 'component_type' => $component_type,
99 'component_name' => $component_name,
100 'resource_type' => $resource_type,
101 'resource_name' => $resource_name,
102 'directive_name' => $name,
103 'directive_value' => $config[$i],
104 'parent_name' => $name
109 private function setOption(&$options, $name, $config) {
110 $load_values = $this->getLoadValues();
111 $host = $this->getHost();
112 $component_type = $this->getComponentType();
113 $component_name = $this->getComponentName();
114 $resource_type = $this->getResourceType();
115 $resource_name = $this->getResourceName();
117 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type, 'Include');
119 for ($i = 0; $i < count($config); $i++) {
120 foreach ($resource_desc->SubSections as $directive_name => $directive_desc) {
121 $in_config = property_exists($config[$i], $directive_name);
122 $directive_value = null;
123 if ($in_config === true) {
124 $directive_value = $config[$i]->{$directive_name};
127 $default_value = null;
129 $field_type = 'TextBox';
131 if (is_object($directive_desc)) {
132 if (property_exists($directive_desc, 'Required')) {
133 $required = $directive_desc->Required;
135 if (property_exists($directive_desc, 'DefaultValue')) {
136 $default_value = $directive_desc->DefaultValue;
138 if (property_exists($directive_desc, 'Data')) {
139 $data = $directive_desc->Data;
141 if (property_exists($directive_desc, 'FieldType')) {
142 $field_type = $directive_desc->FieldType;
145 if (!is_array($directive_value)) {
146 $directive_value = array($directive_value);
148 for ($j = 0; $j < count($directive_value); $j++) {
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' => $directive_value[$j],
157 'default_value' => $default_value,
158 'required' => $required,
160 'field_type' => $field_type,
161 'in_config' => $in_config,
162 'label' => $directive_name,
163 'show' => ($in_config || !$load_values || $this->SourceTemplateControl->getShowAllDirectives()),
164 'parent_name' => $name,
172 public function getDirectiveValue() {
173 $directive_values = array();
174 $component_type = $this->getComponentType();
175 $resource_type = $this->getResourceType();
176 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
178 for ($i = 0; $i < count($this->directive_types); $i++) {
179 $controls = $this->RepeaterFileSetOptions->findControlsByType($this->directive_types[$i]);
180 for ($j = 0; $j < count($controls); $j++) {
181 $directive_name = $controls[$j]->getDirectiveName();
182 $directive_value = $controls[$j]->getDirectiveValue();
183 $index = $controls[$j]->getGroupName();
184 $default_value = $resource_desc['Include']->SubSections->{$directive_name}->DefaultValue;
185 $in_config = $controls[$j]->getInConfig();
186 if (is_null($directive_value)) {
187 // option not set or removed
190 if ($this->directive_types[$i] === 'DirectiveBoolean') {
191 settype($default_value, 'bool');
193 if ($directive_value === $default_value) {
194 // value the same as default value, skip it
197 if (!array_key_exists('Include', $directive_values)) {
198 $directive_values['Include'] = array('Options' => array());
200 if (!isset($directive_values['Include']['Options'][$index])) {
201 $directive_values['Include']['Options'][$index] = array();
203 $directive_values['Include']['Options'][$index][$directive_name] = $directive_value;
205 $controls = $this->RepeaterFileSetInclude->findControlsByType($this->directive_types[$i]);
206 for ($j = 0; $j < count($controls); $j++) {
207 $directive_name = $controls[$j]->getDirectiveName();
208 $directive_value = $controls[$j]->getDirectiveValue();
209 if (is_null($directive_value)) {
210 // Include file directive removed
213 if (!array_key_exists('Include', $directive_values)) {
214 $directive_values['Include'] = array();
216 if (!array_key_exists($directive_name, $directive_values['Include'])) {
217 $directive_values['Include'][$directive_name] = array();
219 array_push($directive_values['Include'][$directive_name], $directive_value);
221 $controls = $this->RepeaterFileSetExclude->findControlsByType($this->directive_types[$i]);
222 for ($j = 0; $j < count($controls); $j++) {
223 $directive_name = $controls[$j]->getDirectiveName();
224 $directive_value = $controls[$j]->getDirectiveValue();
225 if (is_null($directive_value)) {
226 // Exclude file directive removed
229 if (!array_key_exists($directive_name, $directive_values)) {
230 $directive_values[$directive_name] = array('File' => array());
232 array_push($directive_values[$directive_name]['File'], $directive_value);
236 return $directive_values;
239 public function createFileSetOptions($sender, $param) {
240 $load_values = $this->getLoadValues();
241 $bconditionals = $this->RepeaterFileSetOptions->findControlsByType('BConditionalItem');
242 for ($i = 0; $i < count($bconditionals); $i++) {
243 $item = $bconditionals[$i]->getData();
244 for ($j = 0; $j < count($this->directive_types); $j++) {
245 $control = $this->getChildControl($item, $this->directive_types[$j]);
246 if (is_object($control)) {
247 $control->setHost($item->DataItem['host']);
248 $control->setComponentType($item->DataItem['component_type']);
249 $control->setComponentName($item->DataItem['component_name']);
250 $control->setResourceType($item->DataItem['resource_type']);
251 $control->setResourceName($item->DataItem['resource_name']);
252 $control->setDirectiveName($item->DataItem['directive_name']);
253 $control->setDirectiveValue($item->DataItem['directive_value']);
254 $control->setDefaultValue($item->DataItem['default_value']);
255 $control->setRequired($item->DataItem['required']);
256 $control->setData($item->DataItem['data']);
257 $control->setLabel($item->DataItem['label']);
258 $control->setInConfig($item->DataItem['in_config']);
259 $show_all_directives = ($item->DataItem['in_config'] || !$load_values || $this->SourceTemplateControl->getShowAllDirectives());
260 $control->setShow($show_all_directives);
261 $control->setParentName($item->DataItem['parent_name']);
262 $control->setGroupName($item->DataItem['group_name']);
268 public function createFileSetIncExcElement($sender, $param) {
269 for ($i = 0; $i < count($this->directive_inc_exc_types); $i++) {
270 $control = $this->getChildControl($param->Item, $this->directive_inc_exc_types[$i]);
271 if (is_object($control)) {
272 $control->setHost($param->Item->DataItem['host']);
273 $control->setComponentType($param->Item->DataItem['component_type']);
274 $control->setComponentName($param->Item->DataItem['component_name']);
275 $control->setResourceType($param->Item->DataItem['resource_type']);
276 $control->setResourceName($param->Item->DataItem['resource_name']);
277 $control->setDirectiveName($param->Item->DataItem['directive_name']);
278 $control->setDirectiveValue($param->Item->DataItem['directive_value']);
279 $control->setLabel($param->Item->DataItem['directive_name']);
280 $control->setData($param->Item->DataItem['directive_value']);
281 $control->setInConfig(true);
282 $control->setShow(true);
283 $control->setParentName($param->Item->DataItem['parent_name']);
288 private function getDirectiveData() {
289 $values = $this->getDirectiveValue();
291 if (array_key_exists('Include', $values) && array_key_exists('File', $values['Include'])) {
292 $data['File'] = $values['Include']['File'];
293 if (array_key_exists('Options', $values['Include']) && is_array($values['Include']['Options'])) {
294 $data['Options'] = array();
295 for ($i = 0; $i < count($values['Include']['Options']); $i++) {
296 $data['Options'][$i] = (object)$values['Include']['Options'][$i];
300 if (array_key_exists('Exclude', $values) && array_key_exists('File', $values['Exclude'])) {
301 $data['Exclude'] = $values['Exclude']['File'];
306 public function newIncludeFile($sender, $param) {
307 $data = $this->getDirectiveData();
308 if (array_key_exists('File', $data) && is_array($data['File'])) {
309 $data['File'][] = '';
311 $data['File'] = array('');
313 $data = (object)$data;
314 $this->setData($data);
315 $this->loadConfig(null, null);
318 public function newExcludeFile($sender, $param) {
319 $data = $this->getDirectiveData();
320 if (array_key_exists('Exclude', $data) && is_array($data['Exclude'])) {
321 $data['Exclude'][] = '';
323 $data['Exclude'] = array('');
325 $data = (object)$data;
326 $this->setData($data);
327 $this->loadConfig(null, null);
330 public function newIncludeOptions($sender, $param) {
331 $data = $this->getDirectiveData();
332 if (array_key_exists('Options', $data) && is_array($data['Options'])) {
333 $data['Options'][] = new stdClass;
335 $data['Options'] = array(new stdClass);
337 $data = (object)$data;
338 $this->SourceTemplateControl->setShowAllDirectives(true);
339 $this->setData($data);
340 $this->loadConfig(null, null);