3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Recent items utility class.
20 class CRM_Utils_Recent
{
27 const STORE_NAME
= 'CRM_Utils_Recent';
30 * Max number of recent items to store
37 * The list of recently viewed items.
41 static private $_recent = NULL;
47 static private $_maxItems = 10;
50 * Initialize this class and set the static variables.
52 public static function initialize() {
53 $maxItemsSetting = Civi
::settings()->get('recentItemsMaxCount');
54 if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self
::MAX_ITEMS
) {
55 self
::$_maxItems = $maxItemsSetting;
57 if (!self
::$_recent) {
58 $session = CRM_Core_Session
::singleton();
59 self
::$_recent = $session->get(self
::STORE_NAME
);
60 if (!self
::$_recent) {
67 * Return the recently viewed array.
70 * the recently viewed array
72 public static function &get() {
74 return self
::$_recent;
78 * Add an item to the recent stack.
80 * @param string $title
81 * The title to display.
83 * The link for the above title.
87 * @param int $contactId
88 * @param string $contactName
89 * @param array $others
91 public static function add(
100 // Abort if this entity type is not supported
101 if (!self
::isProviderEnabled($type)) {
105 // Ensure item is not already present in list
106 self
::removeItems(['id' => $id, 'type' => $type]);
108 if (!is_array($others)) {
112 array_unshift(self
::$_recent,
118 'contact_id' => $contactId,
119 'contactName' => $contactName,
120 'subtype' => $others['subtype'] ??
NULL,
121 'isDeleted' => $others['isDeleted'] ??
FALSE,
122 'image_url' => $others['imageUrl'] ??
NULL,
123 'edit_url' => $others['editUrl'] ??
NULL,
124 'delete_url' => $others['deleteUrl'] ??
NULL,
128 // Keep the list trimmed to max length
129 while (count(self
::$_recent) > self
::$_maxItems) {
130 array_pop(self
::$_recent);
133 CRM_Utils_Hook
::recent(self
::$_recent);
135 $session = CRM_Core_Session
::singleton();
136 $session->set(self
::STORE_NAME
, self
::$_recent);
140 * Callback for hook_civicrm_post().
141 * @param \Civi\Core\Event\PostEvent $event
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
]);
154 * Remove items from the array that match given props
155 * @param array $props
157 private static function removeItems(array $props) {
160 self
::$_recent = array_filter(self
::$_recent, function($item) use ($props) {
161 foreach ($props as $key => $val) {
162 if (isset($item[$key]) && $item[$key] != $val) {
171 * Delete item(s) from the recently-viewed list.
173 * @param array $removeItem
174 * Item to be removed.
176 public static function del($removeItem) {
177 self
::removeItems($removeItem);
178 CRM_Utils_Hook
::recent(self
::$_recent);
179 $session = CRM_Core_Session
::singleton();
180 $session->set(self
::STORE_NAME
, self
::$_recent);
184 * Delete an item from the recent stack.
189 public static function delContact($id) {
190 CRM_Core_Error
::deprecatedFunctionWarning('del');
191 self
::del(['contact_id' => $id]);
195 * Check if a provider is allowed to add stuff.
196 * If corresponding setting is empty, all are allowed
198 * @param string $providerName
201 public static function isProviderEnabled($providerName) {
203 // Join contact types to providerName 'Contact'
204 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypes(TRUE);
205 if (in_array($providerName, $contactTypes)) {
206 $providerName = 'Contact';
210 // Use core setting recentItemsProviders if configured
211 $providersPermitted = Civi
::settings()->get('recentItemsProviders');
212 if ($providersPermitted) {
213 $allowed = in_array($providerName, $providersPermitted);
220 * Gets the list of available providers to civi's recent items stack
224 public static function getProviders() {
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'),