]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveSchedule.php
baculum: Fix saving names with spaces inside schedule Run directive
[bacula/bacula] / gui / baculum / protected / Web / Portlets / DirectiveSchedule.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 Kern Sibbald
7  *
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.
11  *
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.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
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');
29
30 class DirectiveSchedule extends DirectiveListTemplate {
31
32         public $directives;
33
34         private $directive_types = array(
35                 'DirectiveBoolean',
36                 'DirectiveText',
37                 'DirectiveComboBox',
38                 'DirectiveTimePeriod'
39         );
40
41         private $overwrite_directives = array(
42                 'Level',
43                 'Pool',
44                 'Storage',
45                 'Messages',
46                 'FullPool',
47                 'DifferentialPool',
48                 'IncrementalPool',
49                 'Accurate',
50                 'Priority',
51                 'SpoolData',
52                 'writepartafterjob',
53                 'MaxRunSchedTime',
54                 'NextPool'
55         );
56
57         private $time_values = array(
58                 'Minute',
59                 'Hour',
60                 'Day',
61                 'Month',
62                 'DayOfWeek',
63                 'WeekOfMonth',
64                 'WeekOfYear'
65         );
66
67         private $time_directives = array(
68                 'Month',
69                 'MonthRangeFrom',
70                 'MonthRangeTo',
71                 'Week',
72                 'WeekRangeFrom',
73                 'WeekRangeTo',
74                 'Day',
75                 'DayRangeFrom',
76                 'DayRangeTo',
77                 'Wday',
78                 'WdayRangeFrom',
79                 'WdayRangeTo',
80                 'TimeHourAt',
81                 'TimeMinAt',
82                 'TimeMinHourly'
83         );
84
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;
100                         $data = null;
101                         $resource = null;
102                         $directive_desc = null;
103                         $required = false;
104                         if (array_key_exists($this->overwrite_directives[$i], $resource_desc)) {
105                                 $directive_desc = $resource_desc[$this->overwrite_directives[$i]];
106                         }
107                         if (is_object($directive_desc)) {
108                                 if (property_exists($directive_desc, 'DefaultValue')) {
109                                         $default_value = $directive_desc->DefaultValue;
110                                 }
111                                 if (property_exists($directive_desc, 'Data')) {
112                                         $data = $directive_desc->Data;
113                                 }
114                                 if (property_exists($directive_desc, 'Resource')) {
115                                         $resource = $directive_desc->Resource;
116                                 }
117                         }
118                         if (preg_match('/^(Full|Incremental|Differential)Pool$/', $this->overwrite_directives[$i]) === 1) {
119                                 $resource = 'Pool';
120                         }
121                         $in_config = false;
122                         if ($load_values === true) {
123                                 $in_config = property_exists($directives, $this->overwrite_directives[$i]);
124                         }
125
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__);
143
144
145                 }
146
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__);
156                 }
157
158                 $months_long = array_values(Params::$months);
159
160                 $this->directives['month'] = array(
161                         'data' => Params::$months,
162                 );
163
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);
169
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;
185                 }
186                 $this->Month->setDirectiveValue($month_single);
187                 $this->MonthRangeFrom->setDirectiveValue($month_range_from);
188                 $this->MonthRangeTo->setDirectiveValue($month_range_to);
189
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);
196
197                 $day_single = null;
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;
212                 }
213                 $this->Day->setDirectiveValue($day_single);
214                 $this->DayRangeFrom->setDirectiveValue($day_range_from);
215                 $this->DayRangeTo->setDirectiveValue($day_range_to);
216
217                 $weeks_long = array_values(Params::$weeks);
218
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);
224                 $week_single = null;
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;
239                 }
240                 $this->Week->setDirectiveValue($week_single);
241                 $this->WeekRangeFrom->setDirectiveValue($week_range_from);
242                 $this->WeekRangeTo->setDirectiveValue($week_range_to);
243
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);
248
249                 $wday_single = null;
250                 $wday_range_from = null;
251                 $wday_range_to = null;
252                 $wday_count = $load_values ? count($directives->DayOfWeek) : 0;
253                 if ($wday_count === 7) {
254                         $wday_single = '';
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;
264                 }
265                 $this->Wday->setDirectiveValue($wday_single);
266                 $this->WdayRangeFrom->setDirectiveValue($wday_range_from);
267                 $this->WdayRangeTo->setDirectiveValue($wday_range_to);
268
269                 $hour = null;
270                 $minute = null;
271                 if ($load_values) {
272                         $hour = $directives->Hour[0]; // @TODO: Check for many hour values;
273                         /**
274                          * Check if Minute property exists because of bug about missing Minute
275                          * @see http://bugs.bacula.org/view.php?id=2318
276                          */
277                         $minute = property_exists($directives, 'Minute') ? $directives->Minute : 0;
278                 }
279                 $this->directives['time'] = array(
280                         'hour' => $hour,
281                         'minute' => $minute
282                 );
283                 if ($load_values) {
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);
291                         } else {
292                                 $this->TimeDisable->Checked = true;
293                         }
294                 } else {
295                         $this->TimeDisable->Checked = true;
296                 }
297         }
298
299         public function getDirectiveValue() {
300                 $directive_values = array();
301                 $component_type = $this->getComponentType();
302                 $resource_type = $this->getResourceType();
303
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)) {
311                                         continue;
312                                 }
313                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
314                                         settype($default_value, 'bool');
315                                 }
316
317                                 if ($directive_value === $default_value) {
318                                         // value the same as default value, skip it
319                                         continue;
320                                 }
321                                 $directive_values[] = "{$directive_name}=\"{$directive_value}\"";
322                         }
323                 }
324
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}";
331                 }
332
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}";
339                 }
340
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}";
347                 }
348
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}";
355                 }
356
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) {
362                         $hour = '00';
363                         $minute = sprintf('%02d', $this->TimeMinHourly->getDirectiveValue());
364                         $directive_values[] = "hourly at {$hour}:{$minute}";
365                 }
366
367                 $directive_value = array('Run' => implode(' ', $directive_values));
368                 return $directive_value;
369         }
370 }
371 ?>