Merge pull request #15699 from mattwire/participant_cleanup_completeOrderPBRef
[civicrm-core.git] / CRM / Utils / Recent.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
6a488035 29 * @package CRM
f299f7db 30 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
31 */
32
33/**
b8c71ffa 34 * Recent items utility class.
6a488035
TO
35 */
36class CRM_Utils_Recent {
37
38 /**
ac5f7c7f 39 * Store name
6a488035 40 *
ac5f7c7f 41 * @var string
6a488035 42 */
074585b6 43 const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent';
6a488035
TO
44
45 /**
fe482240 46 * The list of recently viewed items.
6a488035
TO
47 *
48 * @var array
6a488035
TO
49 */
50 static private $_recent = NULL;
51
ac5f7c7f
NH
52 /**
53 * Maximum stack size
54 * @var int
55 */
074585b6 56 static private $_maxItems = 10;
7943980b 57
6a488035 58 /**
fe482240 59 * Initialize this class and set the static variables.
6a488035 60 */
00be9182 61 public static function initialize() {
43959a8a 62 $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount');
074585b6 63 if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) {
136b401b
NH
64 self::$_maxItems = $maxItemsSetting;
65 }
6a488035
TO
66 if (!self::$_recent) {
67 $session = CRM_Core_Session::singleton();
68 self::$_recent = $session->get(self::STORE_NAME);
69 if (!self::$_recent) {
be2fb01f 70 self::$_recent = [];
6a488035
TO
71 }
72 }
73 }
74
75 /**
fe482240 76 * Return the recently viewed array.
6a488035 77 *
a6c01b45
CW
78 * @return array
79 * the recently viewed array
6a488035 80 */
00be9182 81 public static function &get() {
6a488035
TO
82 self::initialize();
83 return self::$_recent;
84 }
85
86 /**
fe482240 87 * Add an item to the recent stack.
6a488035 88 *
77855840
TO
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.
f4aaa82a 95 * @param $type
100fef9d
CW
96 * @param int $contactId
97 * @param string $contactName
f4aaa82a 98 * @param array $others
6a488035 99 */
608e6658 100 public static function add(
a3e55d9c 101 $title,
6a488035
TO
102 $url,
103 $id,
104 $type,
105 $contactId,
106 $contactName,
be2fb01f 107 $others = []
6a488035
TO
108 ) {
109 self::initialize();
136b401b
NH
110
111 if (!self::isProviderEnabled($type)) {
112 return;
113 }
114
6a488035
TO
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++) {
7137241a 119 if (self::$_recent[$i]['type'] === $type && self::$_recent[$i]['id'] == $id) {
6a488035
TO
120 // delete item from array
121 array_splice(self::$_recent, $i, 1);
122 break;
123 }
124 }
125
126 if (!is_array($others)) {
be2fb01f 127 $others = [];
6a488035
TO
128 }
129
130 array_unshift(self::$_recent,
be2fb01f 131 [
6a488035
TO
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),
be2fb01f 143 ]
6a488035 144 );
136b401b 145
ac5f7c7f 146 if (count(self::$_recent) > self::$_maxItems) {
6a488035
TO
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 /**
fe482240 156 * Delete an item from the recent stack.
6a488035 157 *
77855840
TO
158 * @param array $recentItem
159 * Array of the recent Item to be removed.
6a488035 160 */
00be9182 161 public static function del($recentItem) {
6a488035
TO
162 self::initialize();
163 $tempRecent = self::$_recent;
164
be2fb01f 165 self::$_recent = [];
6a488035
TO
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'] &&
353ffa53
TO
170 $tempRecent[$i]['type'] == $recentItem['type']
171 )
172 ) {
6a488035
TO
173 self::$_recent[] = $tempRecent[$i];
174 }
175 }
176
ab217754 177 CRM_Utils_Hook::recent(self::$_recent);
6a488035
TO
178 $session = CRM_Core_Session::singleton();
179 $session->set(self::STORE_NAME, self::$_recent);
180 }
181
182 /**
fe482240 183 * Delete an item from the recent stack.
6a488035 184 *
77855840
TO
185 * @param string $id
186 * Contact id that had to be removed.
6a488035 187 */
00be9182 188 public static function delContact($id) {
6a488035
TO
189 self::initialize();
190
191 $tempRecent = self::$_recent;
192
be2fb01f 193 self::$_recent = [];
6a488035
TO
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
ab217754 204 CRM_Utils_Hook::recent(self::$_recent);
6a488035
TO
205 $session = CRM_Core_Session::singleton();
206 $session->set(self::STORE_NAME, self::$_recent);
207 }
96025800 208
ac5f7c7f
NH
209 /**
210 * Check if a provider is allowed to add stuff.
ab217754 211 * If corresponding setting is empty, all are allowed
136b401b 212 *
ac5f7c7f 213 * @param string $providerName
ab217754 214 * @return bool
ac5f7c7f
NH
215 */
216 public static function isProviderEnabled($providerName) {
136b401b
NH
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
43959a8a 226 $providersPermitted = Civi::settings()->get('recentItemsProviders');
136b401b
NH
227 if ($providersPermitted) {
228 $allowed = in_array($providerName, $providersPermitted);
229 }
230 // Else allow
231 return $allowed;
ac5f7c7f
NH
232 }
233
234 /**
235 * Gets the list of available providers to civi's recent items stack
ab217754
CW
236 *
237 * @return array
ac5f7c7f
NH
238 */
239 public static function getProviders() {
be2fb01f 240 $providers = [
136b401b
NH
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'),
be2fb01f 254 ];
ac5f7c7f 255
136b401b 256 return $providers;
ac5f7c7f 257 }
7943980b 258
6a488035 259}