]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveSchedule.php
baculum: Fix reading and writing schedule resource
[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, 'Required')) {
109                                         $required = $directive_desc->Required;
110                                 }
111                                 if (property_exists($directive_desc, 'DefaultValue')) {
112                                         $default_value = $directive_desc->DefaultValue;
113                                 }
114                                 if (property_exists($directive_desc, 'Data')) {
115                                         $data = $directive_desc->Data;
116                                 }
117                                 if (property_exists($directive_desc, 'Resource')) {
118                                         $resource = $directive_desc->Resource;
119                                 }
120                         }
121                         if (preg_match('/^(Full|Incremental|Differential)Pool$/', $this->overwrite_directives[$i]) === 1) {
122                                 $resource = 'Pool';
123                         }
124                         $in_config = false;
125                         if ($load_values === true) {
126                                 $in_config = property_exists($directives, $this->overwrite_directives[$i]);
127                         }
128
129                         $directive_value = $in_config ? $directives->{$this->overwrite_directives[$i]} : null;
130                         $this->{$this->overwrite_directives[$i]}->setHost($host);
131                         $this->{$this->overwrite_directives[$i]}->setComponentType($component_type);
132                         $this->{$this->overwrite_directives[$i]}->setComponentName($component_name);
133                         $this->{$this->overwrite_directives[$i]}->setResourceType($resource_type);
134                         $this->{$this->overwrite_directives[$i]}->setResourceName($resource_name);
135                         $this->{$this->overwrite_directives[$i]}->setDirectiveName($this->overwrite_directives[$i]);
136                         $this->{$this->overwrite_directives[$i]}->setDirectiveValue($directive_value);
137                         $this->{$this->overwrite_directives[$i]}->setDefaultValue($default_value);
138                         $this->{$this->overwrite_directives[$i]}->setRequired($required);
139                         $this->{$this->overwrite_directives[$i]}->setData($data);
140                         $this->{$this->overwrite_directives[$i]}->setResource($resource);
141                         $this->{$this->overwrite_directives[$i]}->setLabel($this->overwrite_directives[$i]);
142                         $this->{$this->overwrite_directives[$i]}->setInConfig($in_config);
143                         $this->{$this->overwrite_directives[$i]}->setShow($in_config || $this->SourceTemplateControl->getShowAllDirectives());
144                         $this->{$this->overwrite_directives[$i]}->setResourceNames($this->getResourceNames());
145                         $this->{$this->overwrite_directives[$i]}->setParentName(__CLASS__);
146
147
148                 }
149
150                 for ($i = 0; $i < count($this->time_directives); $i++) {
151                         $this->{$this->time_directives[$i]}->setHost($host);
152                         $this->{$this->time_directives[$i]}->setComponentType($component_type);
153                         $this->{$this->time_directives[$i]}->setComponentName($component_name);
154                         $this->{$this->time_directives[$i]}->setResourceType($resource_type);
155                         $this->{$this->time_directives[$i]}->setResourceName($resource_name);
156                         $this->{$this->time_directives[$i]}->setDirectiveName($this->time_directives[$i]);
157                         $this->{$this->time_directives[$i]}->setDefaultValue(0);
158                         $this->{$this->time_directives[$i]}->setParentName(__CLASS__);
159                 }
160
161                 $months_long = array_values(Params::$months);
162
163                 $this->directives['month'] = array(
164                         'data' => Params::$months,
165                 );
166
167                 $single_months = Params::$months;
168                 $single_months['monthly'] = 'Monthly';
169                 $this->Month->setData($single_months);
170                 $this->MonthRangeFrom->setData(Params::$months);
171                 $this->MonthRangeTo->setData(Params::$months);
172
173                 $month_single = null;
174                 $month_range_from = null;
175                 $month_range_to = null;
176                 $month_count = $load_values ? count($directives->Month) : 0;
177                 if ($month_count === 12) {
178                         $month_single = 'Monthly';
179                 } elseif ($month_count == 1) {
180                         $month_single = $months_long[$directives->Month[0]];
181                         $this->MonthSingle->Checked = true;
182                 } elseif ($month_count > 0 && $month_count < 12) {
183                         $month_start = $directives->Month[0];
184                         $month_end = $directives->Month[$month_count-1];
185                         $month_range_from = $months_long[$month_start];
186                         $month_range_to = $months_long[$month_end];
187                         $this->MonthRange->Checked = true;
188                 }
189                 $this->Month->setDirectiveValue($month_single);
190                 $this->MonthRangeFrom->setDirectiveValue($month_range_from);
191                 $this->MonthRangeTo->setDirectiveValue($month_range_to);
192
193                 $days = range(1, 31);
194                 $single_days = $days;
195                 $single_days['daily'] = 'Daily';
196                 $this->Day->setData($single_days);
197                 $this->DayRangeFrom->setData($days);
198                 $this->DayRangeTo->setData($days);
199
200                 $day_single = null;
201                 $day_range_from = null;
202                 $day_range_to = null;
203                 $day_count = $load_values ? count($directives->Day) : 0;
204                 if ($day_count === 31) {
205                         $day_single = 'Daily';
206                 } elseif ($day_count === 1) {
207                         $day_single = $days[$directives->Day[0]];
208                         $this->DaySingle->Checked = true;
209                 } elseif ($day_count > 0 && $day_count < 31) {
210                         $day_start = $directives->Day[0];
211                         $day_end = $directives->Day[$day_count-1];
212                         $day_range_from = $day_start;
213                         $day_range_to = $day_end;
214                         $this->DayRange->Checked = true;
215                 }
216                 $this->Day->setDirectiveValue($day_single);
217                 $this->DayRangeFrom->setDirectiveValue($day_range_from);
218                 $this->DayRangeTo->setDirectiveValue($day_range_to);
219
220                 $weeks_long = array_values(Params::$weeks);
221
222                 $single_weeks = Params::$weeks;
223                 $single_weeks['weekly'] = 'Weekly';
224                 $this->Week->setData($single_weeks);
225                 $this->WeekRangeFrom->setData(Params::$weeks);
226                 $this->WeekRangeTo->setData(Params::$weeks);
227                 $week_single = null;
228                 $week_range_from = null;
229                 $week_range_to = null;
230                 $week_count = $load_values ? count($directives->WeekOfMonth) : 0;
231                 if ($week_count === 5) {
232                         $week_single = 'Weekly';
233                 } elseif ($week_count == 1) {
234                         $week_single = $weeks_long[$directives->WeekOfMonth[0]];
235                         $this->WeekSingle->Checked = true;
236                 } elseif ($week_count > 0 && $week_count < 5) {
237                         $week_start = $directives->WeekOfMonth[0];
238                         $week_end = $directives->WeekOfMonth[$week_count-1];
239                         $week_range_from = $weeks_long[$week_start];
240                         $week_range_to = $weeks_long[$week_end];
241                         $this->WeekRange->Checked = true;
242                 }
243                 $this->Week->setDirectiveValue($week_single);
244                 $this->WeekRangeFrom->setDirectiveValue($week_range_from);
245                 $this->WeekRangeTo->setDirectiveValue($week_range_to);
246
247                 $wdays_long = array_values(Params::$wdays);
248                 $this->Wday->setData(Params::$wdays);
249                 $this->WdayRangeFrom->setData(Params::$wdays);
250                 $this->WdayRangeTo->setData(Params::$wdays);
251
252                 $wday_single = null;
253                 $wday_range_from = null;
254                 $wday_range_to = null;
255                 $wday_count = $load_values ? count($directives->DayOfWeek) : 0;
256                 if ($wday_count === 7) {
257                         $wday_single = '';
258                 } elseif ($wday_count === 1) {
259                         $wday_single = $wdays_long[$directives->DayOfWeek[0]];
260                         $this->WdaySingle->Checked = true;
261                 } elseif ($wday_count > 0 && $wday_count < 7) {
262                         $wday_start = $directives->DayOfWeek[0];
263                         $wday_end = $directives->DayOfWeek[$wday_count-1];
264                         $wday_range_from = $wdays_long[$wday_start];
265                         $wday_range_to = $wdays_long[$wday_end];
266                         $this->WdayRange->Checked = true;
267                 }
268                 $this->Wday->setDirectiveValue($wday_single);
269                 $this->WdayRangeFrom->setDirectiveValue($wday_range_from);
270                 $this->WdayRangeTo->setDirectiveValue($wday_range_to);
271
272                 $hour = null;
273                 $minute = null;
274                 if ($load_values) {
275                         $hour = $directives->Hour[0]; // @TODO: Check for many hour values;
276                         /**
277                          * Check if Minute property exists because of bug about missing Minute
278                          * @see http://bugs.bacula.org/view.php?id=2318
279                          */
280                         $minute = property_exists($directives, 'Minute') ? $directives->Minute : 0;
281                 }
282                 $this->directives['time'] = array(
283                         'hour' => $hour,
284                         'minute' => $minute
285                 );
286                 if ($load_values) {
287                         if (count($directives->Hour) == 24) {
288                                 $this->TimeHourly->Checked = true;
289                                 $this->TimeMinHourly->setDirectiveValue($minute);
290                         } elseif (count($directives->Hour) == 1) {
291                                 $this->TimeAt->Checked = true;
292                                 $this->TimeHourAt->setDirectiveValue($hour);
293                                 $this->TimeMinAt->setDirectiveValue($minute);
294                         } else {
295                                 $this->TimeDisable->Checked = true;
296                         }
297                 } else {
298                         $this->TimeDisable->Checked = true;
299                 }
300         }
301
302         public function getDirectiveValue() {
303                 $directive_values = array();
304                 $component_type = $this->getComponentType();
305                 $resource_type = $this->getResourceType();
306
307                 for ($i = 0; $i < count($this->directive_types); $i++) {
308                         $controls = $this->DirectiveContainer->findControlsByType($this->directive_types[$i]);
309                         for ($j = 0; $j < count($controls); $j++) {
310                                 $directive_name = $controls[$j]->getDirectiveName();
311                                 $directive_value = $controls[$j]->getDirectiveValue();
312                                 $default_value = $controls[$j]->getDefaultValue();
313                                 if (is_null($directive_value)) {
314                                         continue;
315                                 }
316                                 if ($this->directive_types[$i] === 'DirectiveBoolean') {
317                                         settype($default_value, 'bool');
318                                 }
319
320                                 if ($directive_value === $default_value) {
321                                         // value the same as default value, skip it
322                                         continue;
323                                 }
324                                 $directive_values[] = "{$directive_name}={$directive_value}";
325                         }
326                 }
327
328                 if ($this->MonthSingle->Checked === true) {
329                         $directive_values[] = $this->Month->getDirectiveValue();
330                 } elseif ($this->MonthRange->Checked === true) {
331                         $from = $this->MonthRangeFrom->getDirectiveValue();
332                         $to = $this->MonthRangeTo->getDirectiveValue();
333                         $directive_values[] = "{$from}-{$to}";
334                 }
335
336                 if ($this->WeekSingle->Checked === true) {
337                         $directive_values[] = $this->Week->getDirectiveValue();
338                 } elseif ($this->WeekRange->Checked === true) {
339                         $from = $this->WeekRangeFrom->getDirectiveValue();
340                         $to = $this->WeekRangeTo->getDirectiveValue();
341                         $directive_values[] = "{$from}-{$to}";
342                 }
343
344                 if ($this->DaySingle->Checked === true) {
345                         $directive_values[] = $this->Day->getDirectiveValue();
346                 } elseif ($this->DayRange->Checked === true) {
347                         $from = $this->DayRangeFrom->getDirectiveValue();
348                         $to = $this->DayRangeTo->getDirectiveValue();
349                         $directive_values[] = "{$from}-{$to}";
350                 }
351
352                 if ($this->WdaySingle->Checked === true) {
353                         $directive_values[] = $this->Wday->getDirectiveValue();
354                 } elseif ($this->WdayRange->Checked === true) {
355                         $from = $this->WdayRangeFrom->getDirectiveValue();
356                         $to = $this->WdayRangeTo->getDirectiveValue();
357                         $directive_values[] = "{$from}-{$to}";
358                 }
359
360                 if ($this->TimeAt->Checked === true) {
361                         $hour = $this->TimeHourAt->getDirectiveValue();
362                         $minute = sprintf('%02d', $this->TimeMinAt->getDirectiveValue());
363                         $directive_values[] = "at {$hour}:{$minute}";
364                 } elseif ($this->TimeHourly->Checked === true) {
365                         $hour = '00';
366                         $minute = sprintf('%02d', $this->TimeMinHourly->getDirectiveValue());
367                         $directive_values[] = "hourly at {$hour}:{$minute}";
368                 }
369
370                 $directive_value = array('Run' => implode(' ', $directive_values));
371                 return $directive_value;
372         }
373 }
374 ?>