]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Web/Portlets/DirectiveTimePeriod.php
baculum: Fix redundant loading users portlet
[bacula/bacula] / gui / baculum / protected / Web / Portlets / DirectiveTimePeriod.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 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('System.Web.UI.ActiveControls.TActiveLabel');
24 Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
25 Prado::using('System.Web.UI.ActiveControls.TActiveDropDownList');
26 Prado::using('Application.Web.Portlets.DirectiveTemplate');
27
28 class DirectiveTimePeriod extends DirectiveTemplate {
29
30         const TIME_FORMAT = 'TimeFormat';
31         const DEFAULT_TIME_FORMAT = 'day';
32
33         private $time_formats = array(
34                 array('format' => 'second', 'value' => 1, 'label' => 'Seconds'),
35                 array('format' => 'minute', 'value' => 60, 'label' => 'Minutes'),
36                 array('format' => 'hour', 'value' => 60, 'label' => 'Hours'),
37                 array('format' => 'day', 'value' => 24, 'label' => 'Days')
38         );
39
40         public function getValue() {
41                 $value = $this->Directive->Text;
42                 if (is_numeric($value)) {
43                         settype($value, 'integer');
44                         $time_format = $this->TimeFormat->SelectedValue;
45                         $value = $this->getValueSeconds($value, $time_format);
46                 } else {
47                         $value = null;
48                 }
49                 return $value;
50         }
51
52         public function getTimeFormat() {
53                 return $this->getViewState(self::TIME_FORMAT, self::DEFAULT_TIME_FORMAT);
54         }
55
56         public function setTimeFormat($format) {
57                 $this->setViewState(self::TIME_FORMAT, $format);
58         }
59
60         public function getTimeFormats() {
61                 $time_formats = array();
62                 for ($i = 0; $i < count($this->time_formats); $i++) {
63                         $format = array(
64                                 'label' => Prado::localize($this->time_formats[$i]['label']),
65                                 'format' => $this->time_formats[$i]['format']
66                         );
67                         array_push($time_formats, $format);
68                 }
69                 return $time_formats;
70         }
71
72         public function createDirective() {
73                 $time_format = $this->getTimeFormat();
74                 $directive_value = $this->getDirectiveValue();
75                 $default_value = $this->getDefaultValue();
76                 if ($this->getInConfig() === false) {
77                         if ($default_value !== 0) {
78                                 $directive_value = $default_value;
79                         } else {
80                                 $directive_value = 0;
81                         }
82                 }
83                 $formatted_value = $this->formatTimePeriod($directive_value, $time_format);
84                 $this->Directive->Text = $formatted_value['value'];
85                 $this->TimeFormat->DataSource = $this->getTimeFormats();
86                 $this->TimeFormat->SelectedValue = $formatted_value['format'];
87                 $this->TimeFormat->dataBind();
88                 $this->Label->Text = $this->getLabel();
89                 $validate = $this->getRequired();
90                 $this->DirectiveValidator->setVisible($validate);
91         }
92
93         /**
94          * Convert original time period in seconds into given time format.
95          *
96          * Note, if there is not possible to convert time period into given format
97          * then there will be returned value converted by using as close format as possible.
98          * Example:
99          *  time_seconds: 5184060
100          *  given format: day
101          *  returned value: 86401
102          *  returned format: minute
103          */
104         private function formatTimePeriod($time_seconds, $format) {
105                 $value = $time_seconds;
106                 for ($i = 0; $i < count($this->time_formats); $i++) {
107                         if ($this->time_formats[$i]['format'] != $format) {
108                                 $remainder = $value % $this->time_formats[$i]['value'];
109                                 if ($remainder === 0) {
110                                         $value /= $this->time_formats[$i]['value'];
111                                         $format = $this->time_formats[$i]['format'];
112                                         continue;
113                                 }
114                                 break;
115                         }
116                         break;
117                 }
118                 $result = array('value' => $value, 'format' => $format);
119                 return $result;
120         }
121
122         private function getValueSeconds($value, $time_format) {
123                 for ($i = 0; $i < count($this->time_formats); $i++) {
124                         $value *= $this->time_formats[$i]['value'];
125                         if ($this->time_formats[$i]['format'] === $time_format) {
126                                 break;
127                         }
128                 }
129                 return $value;
130         }
131 }
132 ?>