3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2017 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('Application.Web.Portlets.DirectiveListTemplate');
24 Prado::using('Application.Web.Portlets.DirectiveBoolean');
25 Prado::using('Application.Web.Portlets.DirectiveText');
26 Prado::using('Application.Web.Portlets.DirectiveComboBox');
28 class DirectiveRunscript extends DirectiveListTemplate {
30 private $directive_types = array(
36 public function loadConfig($sender, $param) {
37 $load_values = $this->getLoadValues();
38 $host = $this->getHost();
39 $component_type = $this->getComponentType();
40 $component_name = $this->getComponentName();
41 $resource_type = $this->getResourceType();
42 $resource_name = $this->getResourceName();
44 $config = $this->getData();
45 if (is_null($config)) {
47 } elseif (is_object($config)) {
48 $config = array($config);
51 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type, 'Runscript');
52 for ($i = 0; $i < count($config); $i++) {
53 foreach ($resource_desc->SubSections as $directive_name => $directive_desc) {
54 $in_config = property_exists($config[$i], $directive_name);
56 $directive_value = null;
57 if ($in_config === true) {
58 $directive_value = $config[$i]->{$directive_name};
61 $default_value = null;
63 $field_type = 'TextBox';
65 if (is_object($directive_desc)) {
66 if (property_exists($directive_desc, 'Required')) {
67 $required = $directive_desc->Required;
69 if (property_exists($directive_desc, 'DefaultValue')) {
70 $default_value = $directive_desc->DefaultValue;
72 if (property_exists($directive_desc, 'Data')) {
73 $data = $directive_desc->Data;
75 if (property_exists($directive_desc, 'FieldType')) {
76 $field_type = $directive_desc->FieldType;
79 if (!is_array($directive_value)) {
80 $directive_value = array($directive_value);
82 for ($j = 0; $j < count($directive_value); $j++) {
85 'component_type' => $component_type,
86 'component_name' => $component_name,
87 'resource_type' => $resource_type,
88 'resource_name' => $resource_name,
89 'directive_name' => $directive_name,
90 'directive_value' => $directive_value[$j],
91 'default_value' => $default_value,
92 'required' => $required,
94 'label' => $directive_name,
95 'field_type' => $field_type,
96 'in_config' => $in_config,
97 'show' => ($in_config || !$load_values || $this->SourceTemplateControl->getShowAllDirectives()),
98 'parent_name' => __CLASS__,
104 $this->RepeaterRunscriptOptions->dataSource = $options;
105 $this->RepeaterRunscriptOptions->dataBind();
108 public function createRunscriptOptions($sender, $param) {
109 $load_values = $this->getLoadValues();
110 $bconditionals = $this->RepeaterRunscriptOptions->findControlsByType('BConditionalItem');
111 for ($i = 0; $i < count($bconditionals); $i++) {
112 $item = $bconditionals[$i]->getData();
113 for ($j = 0; $j < count($this->directive_types); $j++) {
114 $control = $this->getChildControl($item, $this->directive_types[$j]);
115 if (is_object($control)) {
116 $control->setHost($item->DataItem['host']);
117 $control->setComponentType($item->DataItem['component_type']);
118 $control->setComponentName($item->DataItem['component_name']);
119 $control->setResourceType($item->DataItem['resource_type']);
120 $control->setResourceName($item->DataItem['resource_name']);
121 $control->setDirectiveName($item->DataItem['directive_name']);
122 $control->setDirectiveValue($item->DataItem['directive_value']);
123 $control->setDefaultValue($item->DataItem['default_value']);
124 $control->setRequired($item->DataItem['required']);
125 $control->setData($item->DataItem['data']);
126 $control->setLabel($item->DataItem['label']);
127 $control->setInConfig($item->DataItem['in_config']);
128 $show_all_directives = ($item->DataItem['in_config'] || !$load_values || $this->SourceTemplateControl->getShowAllDirectives());
129 $control->setShow($show_all_directives);
130 $control->setParentName($item->DataItem['parent_name']);
131 $control->setGroupName($item->DataItem['group_name']);
138 public function getDirectiveValue() {
139 $directive_values = null;
140 $component_type = $this->getComponentType();
141 $resource_type = $this->getResourceType();
142 $resource_desc = $this->Application->getModule('data_desc')->getDescription($component_type, $resource_type);
144 for ($i = 0; $i < count($this->directive_types); $i++) {
145 $controls = $this->RepeaterRunscriptOptions->findControlsByType($this->directive_types[$i]);
146 for ($j = 0; $j < count($controls); $j++) {
147 $directive_name = $controls[$j]->getDirectiveName();
148 $directive_value = $controls[$j]->getDirectiveValue();
149 $default_value = $resource_desc['Runscript']->SubSections->{$directive_name}->DefaultValue;
150 $in_config = $controls[$j]->getInConfig();
151 $index = $controls[$j]->getGroupName();
152 if (is_null($directive_value)) {
153 // skip not changed values that don't exist in config
156 if ($this->directive_types[$i] === 'DirectiveBoolean') {
157 settype($default_value, 'bool');
159 if ($directive_value === $default_value) {
160 // value the same as default value, skip it
163 if (!isset($directive_values['Runscript'])) {
164 $directive_values['Runscript'] = array();
166 if (!isset($directive_values['Runscript'][$index])) {
167 $directive_values['Runscript'][$index] = array();
169 $directive_values['Runscript'][$index][$directive_name] = $directive_value;
172 return $directive_values;
175 public function getDirectiveData() {
177 $values = $this->getDirectiveValue();
178 if (is_array($values) && array_key_exists('Runscript', $values)) {
179 for ($i = 0; $i < count($values['Runscript']); $i++) {
180 $data[$i] = (object)$values['Runscript'][$i];
186 public function newRunscriptDirective() {
187 $data = $this->getDirectiveData();
188 array_push($data, new stdClass);
189 $this->setData($data);
190 $this->SourceTemplateControl->setShowAllDirectives(true);
191 $this->loadConfig(null, null);