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 MAX_ITEMS
= 30, STORE_NAME
= 'CRM_Utils_Recent';
30 * The list of recently viewed items.
34 static private $_recent = NULL;
40 static private $_maxItems = 10;
43 * Initialize this class and set the static variables.
45 public static function initialize() {
46 $maxItemsSetting = Civi
::settings()->get('recentItemsMaxCount');
47 if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self
::MAX_ITEMS
) {
48 self
::$_maxItems = $maxItemsSetting;
50 if (!self
::$_recent) {
51 $session = CRM_Core_Session
::singleton();
52 self
::$_recent = $session->get(self
::STORE_NAME
);
53 if (!self
::$_recent) {
60 * Return the recently viewed array.
63 * the recently viewed array
65 public static function &get() {
67 return self
::$_recent;
71 * Add an item to the recent stack.
73 * @param string $title
74 * The title to display.
76 * The link for the above title.
80 * @param int $contactId
81 * @param string $contactName
82 * @param array $others
84 public static function add(
95 if (!self
::isProviderEnabled($type)) {
99 $session = CRM_Core_Session
::singleton();
101 // make sure item is not already present in list
102 for ($i = 0; $i < count(self
::$_recent); $i++
) {
103 if (self
::$_recent[$i]['type'] === $type && self
::$_recent[$i]['id'] == $id) {
104 // delete item from array
105 array_splice(self
::$_recent, $i, 1);
110 if (!is_array($others)) {
114 array_unshift(self
::$_recent,
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),
130 if (count(self
::$_recent) > self
::$_maxItems) {
131 array_pop(self
::$_recent);
134 CRM_Utils_Hook
::recent(self
::$_recent);
136 $session->set(self
::STORE_NAME
, self
::$_recent);
140 * Delete an item from the recent stack.
142 * @param array $recentItem
143 * Array of the recent Item to be removed.
145 public static function del($recentItem) {
147 $tempRecent = self
::$_recent;
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'] &&
154 $tempRecent[$i]['type'] == $recentItem['type']
157 self
::$_recent[] = $tempRecent[$i];
161 CRM_Utils_Hook
::recent(self
::$_recent);
162 $session = CRM_Core_Session
::singleton();
163 $session->set(self
::STORE_NAME
, self
::$_recent);
167 * Delete an item from the recent stack.
170 * Contact id that had to be removed.
172 public static function delContact($id) {
175 $tempRecent = self
::$_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) {
185 self
::$_recent[] = $tempRecent[$i];
188 CRM_Utils_Hook
::recent(self
::$_recent);
189 $session = CRM_Core_Session
::singleton();
190 $session->set(self
::STORE_NAME
, self
::$_recent);
194 * Check if a provider is allowed to add stuff.
195 * If corresponding setting is empty, all are allowed
197 * @param string $providerName
200 public static function isProviderEnabled($providerName) {
202 // Join contact types to providerName 'Contact'
203 $contactTypes = CRM_Contact_BAO_ContactType
::contactTypes(TRUE);
204 if (in_array($providerName, $contactTypes)) {
205 $providerName = 'Contact';
209 // Use core setting recentItemsProviders if configured
210 $providersPermitted = Civi
::settings()->get('recentItemsProviders');
211 if ($providersPermitted) {
212 $allowed = in_array($providerName, $providersPermitted);
219 * Gets the list of available providers to civi's recent items stack
223 public static function getProviders() {
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'),