1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22class CClock extends CDiv {
23
24	private $width;
25	private $height;
26	private $time_zone_string;
27	private $time;
28	private $time_zone_offset;
29	private $error;
30	private $script_file;
31
32	public function __construct() {
33		parent::__construct();
34
35		$this->setId(uniqid());
36		$this->addClass(ZBX_STYLE_CLOCK);
37
38		$this->width = null;
39		$this->height = null;
40		$this->time_zone_string = null;
41		$this->time = null;
42		$this->time_zone_offset = null;
43		$this->error = null;
44		$this->script_file = 'js/class.cclock.js';
45	}
46
47	public function setWidth($value) {
48		$this->width = $value;
49
50		return $this;
51	}
52
53	public function setHeight($value) {
54		$this->height = $value;
55
56		return $this;
57	}
58
59	public function setTimeZoneString($value) {
60		$this->time_zone_string = $value;
61
62		return $this;
63	}
64
65	public function setTime($value) {
66		$this->time = $value;
67
68		return $this;
69	}
70
71	public function setTimeZoneOffset($value) {
72		$this->time_zone_offset = $value;
73
74		return $this;
75	}
76
77	public function setError($value) {
78		$this->error = $value;
79
80		return $this;
81	}
82
83	public function getTimeDiv() {
84		return (new CDiv($this->error))
85			->addClass(ZBX_STYLE_TIME_ZONE.'-'.$this->getId())
86			->addClass($this->error !== null ? ZBX_STYLE_RED : ZBX_STYLE_GREY);
87	}
88
89	public function getScriptFile() {
90		return $this->script_file;
91	}
92
93	public function getScriptRun() {
94		if ($this->error !== null) {
95			return '';
96		}
97
98		return 'jQuery(function($) {'.
99			'$("#'.$this->getId().'").zbx_clock('.
100				CJs::encodeJson([
101					'time' => $this->time,
102					'time_zone_string' => $this->time_zone_string,
103					'time_zone_offset' => $this->time_zone_offset,
104					'clock_id' => $this->getId()
105				]).
106			');'.
107		'});';
108	}
109
110	private function makeClockLine($width, $height, $x, $y, $deg) {
111		return (new CTag('rect', true))
112			->setAttribute('width', $width)
113			->setAttribute('height', $height)
114			->setAttribute('x', $x)
115			->setAttribute('y', $y)
116			->setAttribute('transform', 'rotate('.$deg.' 50 50)')
117			->addClass(ZBX_STYLE_CLOCK_LINES);
118	}
119
120	private function makeClockFace() {
121		return [
122			(new CTag('circle', true))
123				->setAttribute('cx', '50')
124				->setAttribute('cy', '50')
125				->setAttribute('r', '50')
126				->addClass(ZBX_STYLE_CLOCK_FACE),
127			$this->makeClockLine('1.5', '5', '49.25', '5', '330'),
128			$this->makeClockLine('1.5', '5', '49.25', '5', '300'),
129			$this->makeClockLine('2.5', '7', '48.75', '5', '270'),
130			$this->makeClockLine('1.5', '5', '49.25', '5', '240'),
131			$this->makeClockLine('1.5', '5', '49.25', '5', '210'),
132			$this->makeClockLine('2.5', '7', '48.75', '5', '180'),
133			$this->makeClockLine('1.5', '5', '49.25', '5', '150'),
134			$this->makeClockLine('1.5', '5', '49.25', '5', '120'),
135			$this->makeClockLine('2.5', '7', '48.75', '5', '90'),
136			$this->makeClockLine('1.5', '5', '49.25', '5', '60'),
137			$this->makeClockLine('1.5', '5', '49.25', '5', '30'),
138			$this->makeClockLine('2.5', '7', '48.75', '5', '0')
139		];
140	}
141
142	private function makeClockHands() {
143		return [
144			(new CTag('rect', true))
145				->setAttribute('width', '3.25')
146				->setAttribute('height', '24')
147				->setAttribute('x', '48.375')
148				->setAttribute('y', '26')
149				->setAttribute('rx', '1.5')
150				->setAttribute('ry', '1.5')
151				->addClass('clock-hand-h')
152				->addClass(ZBX_STYLE_CLOCK_HAND),
153			(new CTag('rect', true))
154				->setAttribute('width', '3.25')
155				->setAttribute('height', '35')
156				->setAttribute('x', '48.375')
157				->setAttribute('y', '15')
158				->setAttribute('rx', '1.5')
159				->setAttribute('ry', '1.5')
160				->addClass('clock-hand-m')
161				->addClass(ZBX_STYLE_CLOCK_HAND),
162			(new CTag('rect', true))
163				->setAttribute('width', '1.5')
164				->setAttribute('height', '55')
165				->setAttribute('x', '49.25')
166				->setAttribute('y', '5')
167				->addClass('clock-hand-s')
168				->addClass(ZBX_STYLE_CLOCK_HAND_SEC),
169			(new CTag('circle', true))
170				->setAttribute('cx', '50')
171				->setAttribute('cy', '50')
172				->setAttribute('r', '3.5')
173				->addClass(ZBX_STYLE_CLOCK_HAND_SEC)
174		];
175	}
176
177	private function build() {
178		$clock = (new CTag('svg', true))
179			->addItem($this->makeClockFace())
180			->addItem($this->makeClockHands())
181			->setAttribute('xmlns', 'http://www.w3.org/2000/svg')
182			->setAttribute('viewBox', '0 0 100 100')
183			->addClass(ZBX_STYLE_CLOCK_SVG);
184
185		if ($this->width !== null && $this->height !== null) {
186			$clock->setAttribute('style', 'width: '.$this->width.'px; height:'.$this->height.'px;');
187		}
188
189		if ($this->error !== null) {
190			$clock->addClass(ZBX_STYLE_DISABLED);
191		}
192
193		$this->addItem($clock);
194	}
195
196	public function toString($destroy = true) {
197		$this->build();
198
199		return parent::toString($destroy);
200	}
201}
202