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