Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / Recent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2020
31 */
32
33 /**
34 * Recent items utility class.
35 */
36 class CRM_Utils_Recent {
37
38 /**
39 * Store name
40 *
41 * @var string
42 */
43 const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent';
44
45 /**
46 * The list of recently viewed items.
47 *
48 * @var array
49 */
50 static private $_recent = NULL;
51
52 /**
53 * Maximum stack size
54 * @var int
55 */
56 static private $_maxItems = 10;
57
58 /**
59 * Initialize this class and set the static variables.
60 */
61 public static function initialize() {
62 $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount');
63 if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) {
64 self::$_maxItems = $maxItemsSetting;
65 }
66 if (!self::$_recent) {
67 $session = CRM_Core_Session::singleton();
68 self::$_recent = $session->get(self::STORE_NAME);
69 if (!self::$_recent) {
70 self::$_recent = [];
71 }
72 }
73 }
74
75 /**
76 * Return the recently viewed array.
77 *
78 * @return array
79 * the recently viewed array
80 */
81 public static function &get() {
82 self::initialize();
83 return self::$_recent;
84 }
85
86 /**
87 * Add an item to the recent stack.
88 *
89 * @param string $title
90 * The title to display.
91 * @param string $url
92 * The link for the above title.
93 * @param string $id
94 * Object id.
95 * @param $type
96 * @param int $contactId
97 * @param string $contactName
98 * @param array $others
99 */
100 public static function add(
101 $title,
102 $url,
103 $id,
104 $type,
105 $contactId,
106 $contactName,
107 $others = []
108 ) {
109 self::initialize();
110
111 if (!self::isProviderEnabled($type)) {
112 return;
113 }
114
115 $session = CRM_Core_Session::singleton();
116
117 // make sure item is not already present in list
118 for ($i = 0; $i < count(self::$_recent); $i++) {
119 if (self::$_recent[$i]['type'] === $type && self::$_recent[$i]['id'] == $id) {
120 // delete item from array
121 array_splice(self::$_recent, $i, 1);
122 break;
123 }
124 }
125
126 if (!is_array($others)) {
127 $others = [];
128 }
129
130 array_unshift(self::$_recent,
131 [
132 'title' => $title,
133 'url' => $url,
134 'id' => $id,
135 'type' => $type,
136 'contact_id' => $contactId,
137 'contactName' => $contactName,
138 'subtype' => CRM_Utils_Array::value('subtype', $others),
139 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE),
140 'image_url' => CRM_Utils_Array::value('imageUrl', $others),
141 'edit_url' => CRM_Utils_Array::value('editUrl', $others),
142 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others),
143 ]
144 );
145
146 if (count(self::$_recent) > self::$_maxItems) {
147 array_pop(self::$_recent);
148 }
149
150 CRM_Utils_Hook::recent(self::$_recent);
151
152 $session->set(self::STORE_NAME, self::$_recent);
153 }
154
155 /**
156 * Delete an item from the recent stack.
157 *
158 * @param array $recentItem
159 * Array of the recent Item to be removed.
160 */
161 public static function del($recentItem) {
162 self::initialize();
163 $tempRecent = self::$_recent;
164
165 self::$_recent = [];
166
167 // make sure item is not already present in list
168 for ($i = 0; $i < count($tempRecent); $i++) {
169 if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
170 $tempRecent[$i]['type'] == $recentItem['type']
171 )
172 ) {
173 self::$_recent[] = $tempRecent[$i];
174 }
175 }
176
177 CRM_Utils_Hook::recent(self::$_recent);
178 $session = CRM_Core_Session::singleton();
179 $session->set(self::STORE_NAME, self::$_recent);
180 }
181
182 /**
183 * Delete an item from the recent stack.
184 *
185 * @param string $id
186 * Contact id that had to be removed.
187 */
188 public static function delContact($id) {
189 self::initialize();
190
191 $tempRecent = self::$_recent;
192
193 self::$_recent = [];
194
195 // rebuild recent.
196 for ($i = 0; $i < count($tempRecent); $i++) {
197 // don't include deleted contact in recent.
198 if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
199 continue;
200 }
201 self::$_recent[] = $tempRecent[$i];
202 }
203
204 CRM_Utils_Hook::recent(self::$_recent);
205 $session = CRM_Core_Session::singleton();
206 $session->set(self::STORE_NAME, self::$_recent);
207 }
208
209 /**
210 * Check if a provider is allowed to add stuff.
211 * If corresponding setting is empty, all are allowed
212 *
213 * @param string $providerName
214 * @return bool
215 */
216 public static function isProviderEnabled($providerName) {
217
218 // Join contact types to providerName 'Contact'
219 $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(TRUE);
220 if (in_array($providerName, $contactTypes)) {
221 $providerName = 'Contact';
222 }
223 $allowed = TRUE;
224
225 // Use core setting recentItemsProviders if configured
226 $providersPermitted = Civi::settings()->get('recentItemsProviders');
227 if ($providersPermitted) {
228 $allowed = in_array($providerName, $providersPermitted);
229 }
230 // Else allow
231 return $allowed;
232 }
233
234 /**
235 * Gets the list of available providers to civi's recent items stack
236 *
237 * @return array
238 */
239 public static function getProviders() {
240 $providers = [
241 'Contact' => ts('Contacts'),
242 'Relationship' => ts('Relationships'),
243 'Activity' => ts('Activities'),
244 'Note' => ts('Notes'),
245 'Group' => ts('Groups'),
246 'Case' => ts('Cases'),
247 'Contribution' => ts('Contributions'),
248 'Participant' => ts('Participants'),
249 'Grant' => ts('Grants'),
250 'Membership' => ts('Memberships'),
251 'Pledge' => ts('Pledges'),
252 'Event' => ts('Events'),
253 'Campaign' => ts('Campaigns'),
254 ];
255
256 return $providers;
257 }
258
259 }