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.Common.Class.Params');
24 Prado::using('Application.Web.Portlets.DirectiveListTemplate');
25 Prado::using('Application.Web.Portlets.DirectiveBoolean');
26 Prado::using('Application.Web.Portlets.DirectiveComboBox');
27 Prado::using('Application.Web.Portlets.DirectiveText');
28 Prado::using('Application.Web.Portlets.DirectiveTimePeriod');
30 class DirectiveSchedule extends DirectiveListTemplate {
34 private $directive_types = array(
41 private $overwrite_directives = array(
57 private $time_values = array(
67 private $time_directives = array(
85 public function loadConfig($sender, $param) {
86 $load_values = $this->getLoadValues();
87 $directives = $this->getData();
88 $host = $this->getHost();
89 $component_type = $this->getComponentType();
90 $component_name = $this->getComponentName();
91 $resource_type = $this->getResourceType();
92 $resource_name = $this->getResourceName();
93 $data_desc = $this->Application->getModule('data_desc');
94 $resource_desc = $data_desc->getDescription($this->getComponentType(), 'Job');
95 for ($i = 0; $i < count($this->overwrite_directives); $i++) {
96 $key = strtolower($this->overwrite_directives[$i]);
97 $this->directives[$key] = array();
98 $this->directives[$key]['directive_name'] = $this->overwrite_directives[$i];
99 $default_value = null;
102 $directive_desc = null;
104 if (array_key_exists($this->overwrite_directives[$i], $resource_desc)) {
105 $directive_desc = $resource_desc[$this->overwrite_directives[$i]];
107 if (is_object($directive_desc)) {
108 if (property_exists($directive_desc, 'DefaultValue')) {
109 $default_value = $directive_desc->DefaultValue;
111 if (property_exists($directive_desc, 'Data')) {
112 $data = $directive_desc->Data;
114 if (property_exists($directive_desc, 'Resource')) {
115 $resource = $directive_desc->Resource;
118 if (preg_match('/^(Full|Incremental|Differential)Pool$/', $this->overwrite_directives[$i]) === 1) {
122 if ($load_values === true) {
123 $in_config = property_exists($directives, $this->overwrite_directives[$i]);
126 $directive_value = $in_config ? $directives->{$this->overwrite_directives[$i]} : null;
127 $this->{$this->overwrite_directives[$i]}->setHost($host);
128 $this->{$this->overwrite_directives[$i]}->setComponentType($component_type);
129 $this->{$this->overwrite_directives[$i]}->setComponentName($component_name);
130 $this->{$this->overwrite_directives[$i]}->setResourceType($resource_type);
131 $this->{$this->overwrite_directives[$i]}->setResourceName($resource_name);
132 $this->{$this->overwrite_directives[$i]}->setDirectiveName($this->overwrite_directives[$i]);
133 $this->{$this->overwrite_directives[$i]}->setDirectiveValue($directive_value);
134 $this->{$this->overwrite_directives[$i]}->setDefaultValue($default_value);
135 $this->{$this->overwrite_directives[$i]}->setRequired($required);
136 $this->{$this->overwrite_directives[$i]}->setData($data);
137 $this->{$this->overwrite_directives[$i]}->setResource($resource);
138 $this->{$this->overwrite_directives[$i]}->setLabel($this->overwrite_directives[$i]);
139 $this->{$this->overwrite_directives[$i]}->setInConfig($in_config);
140 $this->{$this->overwrite_directives[$i]}->setShow($in_config || $this->SourceTemplateControl->getShowAllDirectives());
141 $this->{$this->overwrite_directives[$i]}->setResourceNames($this->getResourceNames());
142 $this->{$this->overwrite_directives[$i]}->setParentName(__CLASS__);
147 for ($i = 0; $i < count($this->time_directives); $i++) {
148 $this->{$this->time_directives[$i]}->setHost($host);
149 $this->{$this->time_directives[$i]}->setComponentType($component_type);
150 $this->{$this->time_directives[$i]}->setComponentName($component_name);
151 $this->{$this->time_directives[$i]}->setResourceType($resource_type);
152 $this->{$this->time_directives[$i]}->setResourceName($resource_name);
153 $this->{$this->time_directives[$i]}->setDirectiveName($this->time_directives[$i]);
154 $this->{$this->time_directives[$i]}->setDefaultValue(0);
155 $this->{$this->time_directives[$i]}->setParentName(__CLASS__);
158 $months_long = array_values(Params::$months);
160 $this->directives['month'] = array(
161 'data' => Params::$months,
164 $single_months = Params::$months;
165 $single_months['monthly'] = 'Monthly';
166 $this->Month->setData($single_months);
167 $this->MonthRangeFrom->setData(Params::$months);
168 $this->MonthRangeTo->setData(Params::$months);
170 $month_single = null;
171 $month_range_from = null;
172 $month_range_to = null;
173 $month_count = $load_values ? count($directives->Month) : 0;
174 if ($month_count === 12) {
175 $month_single = 'Monthly';
176 } elseif ($month_count == 1) {
177 $month_single = $months_long[$directives->Month[0]];
178 $this->MonthSingle->Checked = true;
179 } elseif ($month_count > 0 && $month_count < 12) {
180 $month_start = $directives->Month[0];
181 $month_end = $directives->Month[$month_count-1];
182 $month_range_from = $months_long[$month_start];
183 $month_range_to = $months_long[$month_end];
184 $this->MonthRange->Checked = true;
186 $this->Month->setDirectiveValue($month_single);
187 $this->MonthRangeFrom->setDirectiveValue($month_range_from);
188 $this->MonthRangeTo->setDirectiveValue($month_range_to);
190 $days = range(1, 31);
191 $single_days = $days;
192 $single_days['daily'] = 'Daily';
193 $this->Day->setData($single_days);
194 $this->DayRangeFrom->setData($days);
195 $this->DayRangeTo->setData($days);
198 $day_range_from = null;
199 $day_range_to = null;
200 $day_count = $load_values ? count($directives->Day) : 0;
201 if ($day_count === 31) {
202 $day_single = 'Daily';
203 } elseif ($day_count === 1) {
204 $day_single = $days[$directives->Day[0]];
205 $this->DaySingle->Checked = true;
206 } elseif ($day_count > 0 && $day_count < 31) {
207 $day_start = $directives->Day[0];
208 $day_end = $directives->Day[$day_count-1];
209 $day_range_from = $day_start;
210 $day_range_to = $day_end;
211 $this->DayRange->Checked = true;
213 $this->Day->setDirectiveValue($day_single);
214 $this->DayRangeFrom->setDirectiveValue($day_range_from);
215 $this->DayRangeTo->setDirectiveValue($day_range_to);
217 $weeks_long = array_values(Params::$weeks);
219 $single_weeks = Params::$weeks;
220 $single_weeks['weekly'] = 'Weekly';
221 $this->Week->setData($single_weeks);
222 $this->WeekRangeFrom->setData(Params::$weeks);
223 $this->WeekRangeTo->setData(Params::$weeks);
225 $week_range_from = null;
226 $week_range_to = null;
227 $week_count = $load_values ? count($directives->WeekOfMonth) : 0;
228 if ($week_count === 5) {
229 $week_single = 'Weekly';
230 } elseif ($week_count == 1) {
231 $week_single = $weeks_long[$directives->WeekOfMonth[0]];
232 $this->WeekSingle->Checked = true;
233 } elseif ($week_count > 0 && $week_count < 5) {
234 $week_start = $directives->WeekOfMonth[0];
235 $week_end = $directives->WeekOfMonth[$week_count-1];
236 $week_range_from = $weeks_long[$week_start];
237 $week_range_to = $weeks_long[$week_end];
238 $this->WeekRange->Checked = true;
240 $this->Week->setDirectiveValue($week_single);
241 $this->WeekRangeFrom->setDirectiveValue($week_range_from);
242 $this->WeekRangeTo->setDirectiveValue($week_range_to);
244 $wdays_long = array_values(Params::$wdays);
245 $this->Wday->setData(Params::$wdays);
246 $this->WdayRangeFrom->setData(Params::$wdays);
247 $this->WdayRangeTo->setData(Params::$wdays);
250 $wday_range_from = null;
251 $wday_range_to = null;
252 $wday_count = $load_values ? count($directives->DayOfWeek) : 0;
253 if ($wday_count === 7) {
255 } elseif ($wday_count === 1) {
256 $wday_single = $wdays_long[$directives->DayOfWeek[0]];
257 $this->WdaySingle->Checked = true;
258 } elseif ($wday_count > 0 && $wday_count < 7) {
259 $wday_start = $directives->DayOfWeek[0];
260 $wday_end = $directives->DayOfWeek[$wday_count-1];
261 $wday_range_from = $wdays_long[$wday_start];
262 $wday_range_to = $wdays_long[$wday_end];
263 $this->WdayRange->Checked = true;
265 $this->Wday->setDirectiveValue($wday_single);
266 $this->WdayRangeFrom->setDirectiveValue($wday_range_from);
267 $this->WdayRangeTo->setDirectiveValue($wday_range_to);
272 $hour = $directives->Hour[0]; // @TODO: Check for many hour values;
274 * Check if Minute property exists because of bug about missing Minute
275 * @see http://bugs.bacula.org/view.php?id=2318
277 $minute = property_exists($directives, 'Minute') ? $directives->Minute : 0;
279 $this->directives['time'] = array(
284 if (count($directives->Hour) == 24) {
285 $this->TimeHourly->Checked = true;
286 $this->TimeMinHourly->setDirectiveValue($minute);
287 } elseif (count($directives->Hour) == 1) {
288 $this->TimeAt->Checked = true;
289 $this->TimeHourAt->setDirectiveValue($hour);
290 $this->TimeMinAt->setDirectiveValue($minute);
292 $this->TimeDisable->Checked = true;
295 $this->TimeDisable->Checked = true;
299 public function getDirectiveValue() {
300 $directive_values = array();
301 $component_type = $this->getComponentType();
302 $resource_type = $this->getResourceType();
304 for ($i = 0; $i < count($this->directive_types); $i++) {
305 $controls = $this->DirectiveContainer->findControlsByType($this->directive_types[$i]);
306 for ($j = 0; $j < count($controls); $j++) {
307 $directive_name = $controls[$j]->getDirectiveName();
308 $directive_value = $controls[$j]->getDirectiveValue();
309 $default_value = $controls[$j]->getDefaultValue();
310 if (is_null($directive_value)) {
313 if ($this->directive_types[$i] === 'DirectiveBoolean') {
314 settype($default_value, 'bool');
317 if ($directive_value === $default_value) {
318 // value the same as default value, skip it
321 $directive_values[] = "{$directive_name}=\"{$directive_value}\"";
325 if ($this->MonthSingle->Checked === true) {
326 $directive_values[] = $this->Month->getDirectiveValue();
327 } elseif ($this->MonthRange->Checked === true) {
328 $from = $this->MonthRangeFrom->getDirectiveValue();
329 $to = $this->MonthRangeTo->getDirectiveValue();
330 $directive_values[] = "{$from}-{$to}";
333 if ($this->WeekSingle->Checked === true) {
334 $directive_values[] = $this->Week->getDirectiveValue();
335 } elseif ($this->WeekRange->Checked === true) {
336 $from = $this->WeekRangeFrom->getDirectiveValue();
337 $to = $this->WeekRangeTo->getDirectiveValue();
338 $directive_values[] = "{$from}-{$to}";
341 if ($this->DaySingle->Checked === true) {
342 $directive_values[] = $this->Day->getDirectiveValue();
343 } elseif ($this->DayRange->Checked === true) {
344 $from = $this->DayRangeFrom->getDirectiveValue();
345 $to = $this->DayRangeTo->getDirectiveValue();
346 $directive_values[] = "{$from}-{$to}";
349 if ($this->WdaySingle->Checked === true) {
350 $directive_values[] = $this->Wday->getDirectiveValue();
351 } elseif ($this->WdayRange->Checked === true) {
352 $from = $this->WdayRangeFrom->getDirectiveValue();
353 $to = $this->WdayRangeTo->getDirectiveValue();
354 $directive_values[] = "{$from}-{$to}";
357 if ($this->TimeAt->Checked === true) {
358 $hour = $this->TimeHourAt->getDirectiveValue();
359 $minute = sprintf('%02d', $this->TimeMinAt->getDirectiveValue());
360 $directive_values[] = "at {$hour}:{$minute}";
361 } elseif ($this->TimeHourly->Checked === true) {
363 $minute = sprintf('%02d', $this->TimeMinHourly->getDirectiveValue());
364 $directive_values[] = "hourly at {$hour}:{$minute}";
367 $directive_value = array('Run' => implode(' ', $directive_values));
368 return $directive_value;