Commit | Line | Data |
---|---|---|
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 | */ |
20 | class CRM_Utils_Recent { | |
21 | ||
22 | /** | |
ac5f7c7f | 23 | * Store name |
6a488035 | 24 | * |
ac5f7c7f | 25 | * @var string |
6a488035 | 26 | */ |
e144ca30 BT |
27 | const STORE_NAME = 'CRM_Utils_Recent'; |
28 | ||
29 | /** | |
30 | * Max number of recent items to store | |
31 | * | |
32 | * @var int | |
33 | */ | |
34 | const MAX_ITEMS = 30; | |
6a488035 TO |
35 | |
36 | /** | |
fe482240 | 37 | * The list of recently viewed items. |
6a488035 TO |
38 | * |
39 | * @var array | |
6a488035 TO |
40 | */ |
41 | static private $_recent = NULL; | |
42 | ||
ac5f7c7f NH |
43 | /** |
44 | * Maximum stack size | |
45 | * @var int | |
46 | */ | |
074585b6 | 47 | static private $_maxItems = 10; |
7943980b | 48 | |
6a488035 | 49 | /** |
fe482240 | 50 | * Initialize this class and set the static variables. |
6a488035 | 51 | */ |
00be9182 | 52 | public static function initialize() { |
43959a8a | 53 | $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount'); |
074585b6 | 54 | if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) { |
136b401b NH |
55 | self::$_maxItems = $maxItemsSetting; |
56 | } | |
6a488035 TO |
57 | if (!self::$_recent) { |
58 | $session = CRM_Core_Session::singleton(); | |
59 | self::$_recent = $session->get(self::STORE_NAME); | |
60 | if (!self::$_recent) { | |
be2fb01f | 61 | self::$_recent = []; |
6a488035 TO |
62 | } |
63 | } | |
64 | } | |
65 | ||
66 | /** | |
fe482240 | 67 | * Return the recently viewed array. |
6a488035 | 68 | * |
a6c01b45 CW |
69 | * @return array |
70 | * the recently viewed array | |
6a488035 | 71 | */ |
00be9182 | 72 | public static function &get() { |
6a488035 TO |
73 | self::initialize(); |
74 | return self::$_recent; | |
75 | } | |
76 | ||
77 | /** | |
fe482240 | 78 | * Add an item to the recent stack. |
6a488035 | 79 | * |
77855840 TO |
80 | * @param string $title |
81 | * The title to display. | |
82 | * @param string $url | |
83 | * The link for the above title. | |
84 | * @param string $id | |
85 | * Object id. | |
a2f24340 | 86 | * @param string $type |
100fef9d CW |
87 | * @param int $contactId |
88 | * @param string $contactName | |
f4aaa82a | 89 | * @param array $others |
6a488035 | 90 | */ |
608e6658 | 91 | public static function add( |
a3e55d9c | 92 | $title, |
6a488035 TO |
93 | $url, |
94 | $id, | |
95 | $type, | |
96 | $contactId, | |
97 | $contactName, | |
be2fb01f | 98 | $others = [] |
6a488035 | 99 | ) { |
b1fdd118 | 100 | // Abort if this entity type is not supported |
136b401b NH |
101 | if (!self::isProviderEnabled($type)) { |
102 | return; | |
103 | } | |
104 | ||
b1fdd118 CW |
105 | // Ensure item is not already present in list |
106 | self::removeItems(['id' => $id, 'type' => $type]); | |
6a488035 TO |
107 | |
108 | if (!is_array($others)) { | |
be2fb01f | 109 | $others = []; |
6a488035 TO |
110 | } |
111 | ||
112 | array_unshift(self::$_recent, | |
be2fb01f | 113 | [ |
6a488035 TO |
114 | 'title' => $title, |
115 | 'url' => $url, | |
116 | 'id' => $id, | |
117 | 'type' => $type, | |
118 | 'contact_id' => $contactId, | |
119 | 'contactName' => $contactName, | |
6b409353 | 120 | 'subtype' => $others['subtype'] ?? NULL, |
6187cca4 | 121 | 'isDeleted' => $others['isDeleted'] ?? FALSE, |
6b409353 CW |
122 | 'image_url' => $others['imageUrl'] ?? NULL, |
123 | 'edit_url' => $others['editUrl'] ?? NULL, | |
124 | 'delete_url' => $others['deleteUrl'] ?? NULL, | |
be2fb01f | 125 | ] |
6a488035 | 126 | ); |
136b401b | 127 | |
b1fdd118 CW |
128 | // Keep the list trimmed to max length |
129 | while (count(self::$_recent) > self::$_maxItems) { | |
6a488035 TO |
130 | array_pop(self::$_recent); |
131 | } | |
132 | ||
133 | CRM_Utils_Hook::recent(self::$_recent); | |
134 | ||
b1fdd118 | 135 | $session = CRM_Core_Session::singleton(); |
6a488035 TO |
136 | $session->set(self::STORE_NAME, self::$_recent); |
137 | } | |
138 | ||
54043949 CW |
139 | /** |
140 | * Callback for hook_civicrm_post(). | |
141 | * @param \Civi\Core\Event\PostEvent $event | |
142 | */ | |
143 | public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { | |
144 | if ($event->action === 'delete' && $event->id && CRM_Core_Session::getLoggedInContactID()) { | |
145 | // Is this an entity that might be in the recent items list? | |
146 | $providersPermitted = Civi::settings()->get('recentItemsProviders') ?: array_keys(self::getProviders()); | |
147 | if (in_array($event->entity, $providersPermitted)) { | |
148 | self::del(['id' => $event->id, 'type' => $event->entity]); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
6a488035 | 153 | /** |
b1fdd118 CW |
154 | * Remove items from the array that match given props |
155 | * @param array $props | |
6a488035 | 156 | */ |
b1fdd118 | 157 | private static function removeItems(array $props) { |
6a488035 | 158 | self::initialize(); |
6a488035 | 159 | |
b1fdd118 CW |
160 | self::$_recent = array_filter(self::$_recent, function($item) use ($props) { |
161 | foreach ($props as $key => $val) { | |
a51ed717 CW |
162 | if (isset($item[$key]) && $item[$key] != $val) { |
163 | return TRUE; | |
b1fdd118 | 164 | } |
6a488035 | 165 | } |
a51ed717 | 166 | return FALSE; |
b1fdd118 CW |
167 | }); |
168 | } | |
6a488035 | 169 | |
b1fdd118 CW |
170 | /** |
171 | * Delete item(s) from the recently-viewed list. | |
172 | * | |
173 | * @param array $removeItem | |
174 | * Item to be removed. | |
175 | */ | |
176 | public static function del($removeItem) { | |
177 | self::removeItems($removeItem); | |
ab217754 | 178 | CRM_Utils_Hook::recent(self::$_recent); |
6a488035 TO |
179 | $session = CRM_Core_Session::singleton(); |
180 | $session->set(self::STORE_NAME, self::$_recent); | |
181 | } | |
182 | ||
183 | /** | |
fe482240 | 184 | * Delete an item from the recent stack. |
6a488035 | 185 | * |
77855840 | 186 | * @param string $id |
b1fdd118 | 187 | * @deprecated |
6a488035 | 188 | */ |
00be9182 | 189 | public static function delContact($id) { |
b1fdd118 CW |
190 | CRM_Core_Error::deprecatedFunctionWarning('del'); |
191 | self::del(['contact_id' => $id]); | |
6a488035 | 192 | } |
96025800 | 193 | |
ac5f7c7f NH |
194 | /** |
195 | * Check if a provider is allowed to add stuff. | |
ab217754 | 196 | * If corresponding setting is empty, all are allowed |
136b401b | 197 | * |
ac5f7c7f | 198 | * @param string $providerName |
ab217754 | 199 | * @return bool |
ac5f7c7f NH |
200 | */ |
201 | public static function isProviderEnabled($providerName) { | |
136b401b NH |
202 | |
203 | // Join contact types to providerName 'Contact' | |
204 | $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(TRUE); | |
205 | if (in_array($providerName, $contactTypes)) { | |
206 | $providerName = 'Contact'; | |
207 | } | |
208 | $allowed = TRUE; | |
209 | ||
210 | // Use core setting recentItemsProviders if configured | |
43959a8a | 211 | $providersPermitted = Civi::settings()->get('recentItemsProviders'); |
136b401b NH |
212 | if ($providersPermitted) { |
213 | $allowed = in_array($providerName, $providersPermitted); | |
214 | } | |
215 | // Else allow | |
216 | return $allowed; | |
ac5f7c7f NH |
217 | } |
218 | ||
219 | /** | |
220 | * Gets the list of available providers to civi's recent items stack | |
ab217754 CW |
221 | * |
222 | * @return array | |
ac5f7c7f NH |
223 | */ |
224 | public static function getProviders() { | |
be2fb01f | 225 | $providers = [ |
136b401b NH |
226 | 'Contact' => ts('Contacts'), |
227 | 'Relationship' => ts('Relationships'), | |
228 | 'Activity' => ts('Activities'), | |
229 | 'Note' => ts('Notes'), | |
230 | 'Group' => ts('Groups'), | |
231 | 'Case' => ts('Cases'), | |
232 | 'Contribution' => ts('Contributions'), | |
233 | 'Participant' => ts('Participants'), | |
234 | 'Grant' => ts('Grants'), | |
235 | 'Membership' => ts('Memberships'), | |
236 | 'Pledge' => ts('Pledges'), | |
237 | 'Event' => ts('Events'), | |
238 | 'Campaign' => ts('Campaigns'), | |
be2fb01f | 239 | ]; |
ac5f7c7f | 240 | |
136b401b | 241 | return $providers; |
ac5f7c7f | 242 | } |
7943980b | 243 | |
6a488035 | 244 | } |