3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2016 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('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');
28 class DirectiveTimePeriod extends DirectiveTemplate {
30 const TIME_FORMAT = 'TimeFormat';
31 const DEFAULT_TIME_FORMAT = 'day';
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')
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);
52 public function getTimeFormat() {
53 return $this->getViewState(self::TIME_FORMAT, self::DEFAULT_TIME_FORMAT);
56 public function setTimeFormat($format) {
57 $this->setViewState(self::TIME_FORMAT, $format);
60 public function getTimeFormats() {
61 $time_formats = array();
62 for ($i = 0; $i < count($this->time_formats); $i++) {
64 'label' => Prado::localize($this->time_formats[$i]['label']),
65 'format' => $this->time_formats[$i]['format']
67 array_push($time_formats, $format);
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;
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);
94 * Convert original time period in seconds into given time format.
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.
99 * time_seconds: 5184060
101 * returned value: 86401
102 * returned format: minute
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'];
118 $result = array('value' => $value, 'format' => $format);
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) {