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			'type' => PROFILE_TYPE_ID
95		];
96		if (!is_null($favobj)) {
97			$values['source'] = zbx_dbstr($favobj);
98		}
99
100		return DBexecute(
101			'INSERT INTO profiles ('.implode(', ', array_keys($values)).')'.
102			' VALUES ('.implode(', ', $values).')'
103		);
104	}
105
106	/**
107	 * Removes favorite from DB. Clears cache by $idx.
108	 *
109	 * @param string $idx    identifier of favorite value group
110	 * @param int    $favid  value id
111	 * @param string $favobj source object
112	 *
113	 * @return boolean did SQL DELETE succeeded
114	 */
115	public static function remove($idx, $favid = 0, $favobj = null) {
116		// empty cache, we know that all $idx values will be removed in DELETE
117		if ($favid == 0 && $favobj === null) {
118			self::$cache[$idx] = [];
119		}
120		// remove from cache, cache will be rebuilt upon get()
121		else {
122			self::$cache[$idx] = null;
123		}
124
125		return DBexecute(
126			'DELETE FROM profiles'.
127			' WHERE userid='.CWebUser::$data['userid'].
128				' AND idx='.zbx_dbstr($idx).
129				($favid > 0 ? ' AND value_id='.zbx_dbstr($favid) : '').
130				(is_null($favobj) ? '' : ' AND source='.zbx_dbstr($favobj))
131		);
132	}
133
134	/**
135	 * Checks whether favorite value exists.
136	 *
137	 * @param string $idx    identifier of favorite value group
138	 * @param int    $favid  value id
139	 * @param string $favobj source object
140	 *
141	 * @return boolean
142	 */
143	public static function exists($idx, $favid, $favobj = null) {
144		$favorites = self::get($idx);
145		foreach ($favorites as $favorite) {
146			if (idcmp($favid, $favorite['value']) && $favorite['source'] == $favobj) {
147				return true;
148			}
149		}
150
151		return false;
152	}
153}
154