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 CSvgGraphAnnotation extends CSvgTag {
23
24	const TYPE_SIMPLE = 0x1;
25	const TYPE_RANGE = 0x2;
26
27	const DASH_LINE_START = 0x4;
28	const DASH_LINE_END = 0x8;
29	const DASH_LINE_BOTH = 0x12;
30
31	/**
32	 * Annotation type. One of self::TYPE_* constants.
33	 *
34	 * @var int
35	 */
36	private $type;
37
38	/**
39	 * Problem information as JSON string. Is used by frontend for rendering tooltip with list of problems.
40	 *
41	 * @var string
42	 */
43	private $data_info;
44
45	/**
46	 * Color value.
47	 *
48	 * @var string
49	 */
50	private $color;
51
52	public function __construct($type) {
53		$this->data_info = null;
54		$this->color = '';
55		$this->type = $type;
56	}
57
58	public function makeStyles() {
59		return [
60			'.'.CSvgTag::ZBX_STYLE_GRAPH_DASHED => [
61				'stroke-dasharray' => '2,2'
62			],
63			'.'.CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_HANDLE => [
64				'fill' => $this->color,
65				'stroke' => $this->color
66			],
67			'.'.CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_BOX => [
68				'fill' => $this->color,
69				'opacity' => '0.1'
70			],
71			'.'.CSvgTag::ZBX_STYLE_GRAPH_PROBLEMS.' line' => [
72				'stroke' => $this->color
73			],
74			'.'.CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_ARROW => [
75				'stroke' => $this->color,
76				'fill' => $this->color,
77				'stroke-width' => 3
78			]
79		];
80	}
81
82	/**
83	 * Set array of problem information
84	 *
85	 * @param string $info    Single problem information.
86	 */
87	public function setInformation($info) {
88		$this->data_info = $info;
89
90		return $this;
91	}
92
93	/**
94	 * Set color.
95	 *
96	 * @param string $color   Color value.
97	 */
98	public function setColor($color) {
99		$this->color = $color;
100
101		return $this;
102	}
103
104	/**
105	 * Return markup for problem of type simple as array.
106	 *
107	 * @return array
108	 */
109	private function drawTypeSimple() {
110		$y = $this->y + $this->height;
111		$arrow_width = 6;
112		$offset = (int) $arrow_width / 2;
113
114		return [
115			(new CSvgLine($this->x, $this->y, $this->x, $this->y + $this->height))
116				->addClass(CSvgTag::ZBX_STYLE_GRAPH_DASHED),
117			(new CSvgPolygon([
118				[$this->x, $y + 1],
119				[$this->x - $offset, $y + 5],
120				[$this->x + $offset, $y + 5]
121			]))
122				->addClass(CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_ARROW)
123				->setAttribute('x', $this->x - $offset)
124				->setAttribute('width', $arrow_width)
125				->setAttribute('data-info', $this->data_info)
126		];
127	}
128
129	/**
130	 * Return markup for problem of type range as array.
131	 *
132	 * @return array
133	 */
134	private function drawTypeRange() {
135		$start_line = new CSvgLine($this->x, $this->y, $this->x, $this->y + $this->height);
136		$end_line = new CSvgLine($this->x + $this->width, $this->y, $this->x + $this->width, $this->y + $this->height);
137
138		if ($this->type & self::DASH_LINE_START) {
139			$start_line->addClass(CSvgTag::ZBX_STYLE_GRAPH_DASHED);
140		}
141
142		if ($this->type & self::DASH_LINE_END) {
143			$end_line->addClass(CSvgTag::ZBX_STYLE_GRAPH_DASHED);
144		}
145
146		return [
147			$start_line,
148			(new CSvgRect($this->x, $this->y, $this->width, $this->height))
149				->addClass(CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_BOX),
150			$end_line,
151			(new CSvgRect($this->x, $this->y + $this->height, $this->width, 4))
152				->addClass(CSvgTag::ZBX_STYLE_GRAPH_PROBLEM_HANDLE)
153				->setAttribute('data-info', $this->data_info)
154		];
155	}
156
157	public function toString($destroy = true) {
158		$problem = $this->type & self::TYPE_SIMPLE ? $this->drawTypeSimple() : $this->drawTypeRange();
159
160		return implode('', $problem);
161	}
162}
163