Merge pull request #15955 from JMAConsulting/core-1420
[civicrm-core.git] / CRM / Utils / Recent.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
6a488035 13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
15 */
16
17/**
b8c71ffa 18 * Recent items utility class.
6a488035
TO
19 */
20class CRM_Utils_Recent {
21
22 /**
ac5f7c7f 23 * Store name
6a488035 24 *
ac5f7c7f 25 * @var string
6a488035 26 */
074585b6 27 const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent';
6a488035
TO
28
29 /**
fe482240 30 * The list of recently viewed items.
6a488035
TO
31 *
32 * @var array
6a488035
TO
33 */
34 static private $_recent = NULL;
35
ac5f7c7f
NH
36 /**
37 * Maximum stack size
38 * @var int
39 */
074585b6 40 static private $_maxItems = 10;
7943980b 41
6a488035 42 /**
fe482240 43 * Initialize this class and set the static variables.
6a488035 44 */
00be9182 45 public static function initialize() {
43959a8a 46 $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount');
074585b6 47 if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) {
136b401b
NH
48 self::$_maxItems = $maxItemsSetting;
49 }
6a488035
TO
50 if (!self::$_recent) {
51 $session = CRM_Core_Session::singleton();
52 self::$_recent = $session->get(self::STORE_NAME);
53 if (!self::$_recent) {
be2fb01f 54 self::$_recent = [];
6a488035
TO
55 }
56 }
57 }
58
59 /**
fe482240 60 * Return the recently viewed array.
6a488035 61 *
a6c01b45
CW
62 * @return array
63 * the recently viewed array
6a488035 64 */
00be9182 65 public static function &get() {
6a488035
TO
66 self::initialize();
67 return self::$_recent;
68 }
69
70 /**
fe482240 71 * Add an item to the recent stack.
6a488035 72 *
77855840
TO
73 * @param string $title
74 * The title to display.
75 * @param string $url
76 * The link for the above title.
77 * @param string $id
78 * Object id.
f4aaa82a 79 * @param $type
100fef9d
CW
80 * @param int $contactId
81 * @param string $contactName
f4aaa82a 82 * @param array $others
6a488035 83 */
608e6658 84 public static function add(
a3e55d9c 85 $title,
6a488035
TO
86 $url,
87 $id,
88 $type,
89 $contactId,
90 $contactName,
be2fb01f 91 $others = []
6a488035
TO
92 ) {
93 self::initialize();
136b401b
NH
94
95 if (!self::isProviderEnabled($type)) {
96 return;
97 }
98
6a488035
TO
99 $session = CRM_Core_Session::singleton();
100
101 // make sure item is not already present in list
102 for ($i = 0; $i < count(self::$_recent); $i++) {
7137241a 103 if (self::$_recent[$i]['type'] === $type && self::$_recent[$i]['id'] == $id) {
6a488035
TO
104 // delete item from array
105 array_splice(self::$_recent, $i, 1);
106 break;
107 }
108 }
109
110 if (!is_array($others)) {
be2fb01f 111 $others = [];
6a488035
TO
112 }
113
114 array_unshift(self::$_recent,
be2fb01f 115 [
6a488035
TO
116 'title' => $title,
117 'url' => $url,
118 'id' => $id,
119 'type' => $type,
120 'contact_id' => $contactId,
121 'contactName' => $contactName,
122 'subtype' => CRM_Utils_Array::value('subtype', $others),
123 'isDeleted' => CRM_Utils_Array::value('isDeleted', $others, FALSE),
124 'image_url' => CRM_Utils_Array::value('imageUrl', $others),
125 'edit_url' => CRM_Utils_Array::value('editUrl', $others),
126 'delete_url' => CRM_Utils_Array::value('deleteUrl', $others),
be2fb01f 127 ]
6a488035 128 );
136b401b 129
ac5f7c7f 130 if (count(self::$_recent) > self::$_maxItems) {
6a488035
TO
131 array_pop(self::$_recent);
132 }
133
134 CRM_Utils_Hook::recent(self::$_recent);
135
136 $session->set(self::STORE_NAME, self::$_recent);
137 }
138
139 /**
fe482240 140 * Delete an item from the recent stack.
6a488035 141 *
77855840
TO
142 * @param array $recentItem
143 * Array of the recent Item to be removed.
6a488035 144 */
00be9182 145 public static function del($recentItem) {
6a488035
TO
146 self::initialize();
147 $tempRecent = self::$_recent;
148
be2fb01f 149 self::$_recent = [];
6a488035
TO
150
151 // make sure item is not already present in list
152 for ($i = 0; $i < count($tempRecent); $i++) {
153 if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
353ffa53
TO
154 $tempRecent[$i]['type'] == $recentItem['type']
155 )
156 ) {
6a488035
TO
157 self::$_recent[] = $tempRecent[$i];
158 }
159 }
160
ab217754 161 CRM_Utils_Hook::recent(self::$_recent);
6a488035
TO
162 $session = CRM_Core_Session::singleton();
163 $session->set(self::STORE_NAME, self::$_recent);
164 }
165
166 /**
fe482240 167 * Delete an item from the recent stack.
6a488035 168 *
77855840
TO
169 * @param string $id
170 * Contact id that had to be removed.
6a488035 171 */
00be9182 172 public static function delContact($id) {
6a488035
TO
173 self::initialize();
174
175 $tempRecent = self::$_recent;
176
be2fb01f 177 self::$_recent = [];
6a488035
TO
178
179 // rebuild recent.
180 for ($i = 0; $i < count($tempRecent); $i++) {
181 // don't include deleted contact in recent.
182 if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
183 continue;
184 }
185 self::$_recent[] = $tempRecent[$i];
186 }
187
ab217754 188 CRM_Utils_Hook::recent(self::$_recent);
6a488035
TO
189 $session = CRM_Core_Session::singleton();
190 $session->set(self::STORE_NAME, self::$_recent);
191 }
96025800 192
ac5f7c7f
NH
193 /**
194 * Check if a provider is allowed to add stuff.
ab217754 195 * If corresponding setting is empty, all are allowed
136b401b 196 *
ac5f7c7f 197 * @param string $providerName
ab217754 198 * @return bool
ac5f7c7f
NH
199 */
200 public static function isProviderEnabled($providerName) {
136b401b
NH
201
202 // Join contact types to providerName 'Contact'
203 $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(TRUE);
204 if (in_array($providerName, $contactTypes)) {
205 $providerName = 'Contact';
206 }
207 $allowed = TRUE;
208
209 // Use core setting recentItemsProviders if configured
43959a8a 210 $providersPermitted = Civi::settings()->get('recentItemsProviders');
136b401b
NH
211 if ($providersPermitted) {
212 $allowed = in_array($providerName, $providersPermitted);
213 }
214 // Else allow
215 return $allowed;
ac5f7c7f
NH
216 }
217
218 /**
219 * Gets the list of available providers to civi's recent items stack
ab217754
CW
220 *
221 * @return array
ac5f7c7f
NH
222 */
223 public static function getProviders() {
be2fb01f 224 $providers = [
136b401b
NH
225 'Contact' => ts('Contacts'),
226 'Relationship' => ts('Relationships'),
227 'Activity' => ts('Activities'),
228 'Note' => ts('Notes'),
229 'Group' => ts('Groups'),
230 'Case' => ts('Cases'),
231 'Contribution' => ts('Contributions'),
232 'Participant' => ts('Participants'),
233 'Grant' => ts('Grants'),
234 'Membership' => ts('Memberships'),
235 'Pledge' => ts('Pledges'),
236 'Event' => ts('Events'),
237 'Campaign' => ts('Campaigns'),
be2fb01f 238 ];
ac5f7c7f 239
136b401b 240 return $providers;
ac5f7c7f 241 }
7943980b 242
6a488035 243}