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
22/**
23 * Container class for favorite value management.
24 * Uses caching.
25 */
26class CFavorite {
27
28	/**
29	 * Cache for favorite values.
30	 *
31	 * $cache[idx][]['value']
32	 * $cache[idx][]['source']
33	 */
34	private static $cache = null;
35
36	/**
37	 * Returns favorite values from db. Uses caching for performance.
38	 *
39	 * @param string $idx identifier of favorite value group
40	 *
41	 * @return array list of favorite values corresponding to $idx
42	 */
43	public static function get($idx) {
44		// return values if cached
45		if (isset(self::$cache[$idx])) {
46			return self::$cache[$idx];
47		}
48
49		$result = [];
50		$db_profiles = DBselect(
51			'SELECT p.value_id,p.source'.
52			' FROM profiles p'.
53			' WHERE p.userid='.CWebUser::$data['userid'].
54				' AND p.idx='.zbx_dbstr($idx).
55			' ORDER BY p.profileid'
56		);
57		while ($profile = DBfetch($db_profiles)) {
58			$result[] = ['value' => $profile['value_id'], 'source' => $profile['source']];
59		}
60
61		// store db values in cache
62		self::$cache[$idx] = $result;
63
64		return $result;
65	}
66
67	/**
68	 * Adds favorite value to DB.
69	 *
70	 * @param string $idx    identifier of favorite value group
71	 * @param int    $favid  value id
72	 * @param string $favobj source object
73	 *
74	 * @return bool did SQL INSERT succeeded
75	 */
76	public static function add($idx, $favid, $favobj = null) {
77		if (self::exists($idx, $favid, $favobj)) {
78			return true;
79		}
80
81		// add to cache only if cache is created
82		if (isset(self::$cache[$idx])) {
83			self::$cache[$idx][] = [
84				'value' => $favid,
85				'source' => $favobj
86			];
87		}
88
89		$values = [
90			'profileid' => get_dbid('profiles', 'profileid'),
91			'userid' => CWebUser::$data['userid'],
92			'idx' => zbx_dbstr($idx),
93			'value_id' => zbx_dbstr($favid),
94			'value_str' =>zbx_dbstr(''),
95			'type' => PROFILE_TYPE_ID
96		];
97		if (!is_null($favobj)) {
98			$values['source'] = zbx_dbstr($favobj);
99		}
100
101		return DBexecute(
102			'INSERT INTO profiles ('.implode(', ', array_keys($values)).')'.
103			' VALUES ('.implode(', ', $values).')'
104		);
105	}
106
107	/**
108	 * Removes favorite from DB. Clears cache by $idx.
109	 *
110	 * @param string $idx    identifier of favorite value group
111	 * @param int    $favid  value id
112	 * @param string $favobj source object
113	 *
114	 * @return boolean did SQL DELETE succeeded
115	 */
116	public static function remove($idx, $favid = 0, $favobj = null) {
117		// empty cache, we know that all $idx values will be removed in DELETE
118		if ($favid == 0 && $favobj === null) {
119			self::$cache[$idx] = [];
120		}
121		// remove from cache, cache will be rebuilt upon get()
122		else {
123			self::$cache[$idx] = null;
124		}
125
126		return DBexecute(
127			'DELETE FROM profiles'.
128			' WHERE userid='.CWebUser::$data['userid'].
129				' AND idx='.zbx_dbstr($idx).
130				($favid > 0 ? ' AND value_id='.zbx_dbstr($favid) : '').
131				(is_null($favobj) ? '' : ' AND source='.zbx_dbstr($favobj))
132		);
133	}
134
135	/**
136	 * Checks whether favorite value exists.
137	 *
138	 * @param string $idx    identifier of favorite value group
139	 * @param int    $favid  value id
140	 * @param string $favobj source object
141	 *
142	 * @return boolean
143	 */
144	public static function exists($idx, $favid, $favobj = null) {
145		$favorites = self::get($idx);
146		foreach ($favorites as $favorite) {
147			if (idcmp($favid, $favorite['value']) && $favorite['source'] == $favobj) {
148				return true;
149			}
150		}
151
152		return false;
153	}
154}
155