Remove instances of fatal
[civicrm-core.git] / CRM / Utils / Recent.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * Recent items utility class.
19 */
20 class CRM_Utils_Recent {
21
22 /**
23 * Store name
24 *
25 * @var string
26 */
27 const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent';
28
29 /**
30 * The list of recently viewed items.
31 *
32 * @var array
33 */
34 static private $_recent = NULL;
35
36 /**
37 * Maximum stack size
38 * @var int
39 */
40 static private $_maxItems = 10;
41
42 /**
43 * Initialize this class and set the static variables.
44 */
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;
49 }
50 if (!self::$_recent) {
51 $session = CRM_Core_Session::singleton();
52 self::$_recent = $session->get(self::STORE_NAME);
53 if (!self::$_recent) {
54 self::$_recent = [];
55 }
56 }
57 }
58
59 /**
60 * Return the recently viewed array.
61 *
62 * @return array
63 * the recently viewed array
64 */
65 public static function &get() {
66 self::initialize();
67 return self::$_recent;
68 }
69
70 /**
71 * Add an item to the recent stack.
72 *
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.
79 * @param $type
80 * @param int $contactId
81 * @param string $contactName
82 * @param array $others
83 */
84 public static function add(
85 $title,
86 $url,
87 $id,
88 $type,
89 $contactId,
90 $contactName,
91 $others = []
92 ) {
93 self::initialize();
94
95 if (!self::isProviderEnabled($type)) {
96 return;
97 }
98
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++) {
103 if (self::$_recent[$i]['type'] === $type && self::$_recent[$i]['id'] == $id) {
104 // delete item from array
105 array_splice(self::$_recent, $i, 1);
106 break;
107 }
108 }
109
110 if (!is_array($others)) {
111 $others = [];
112 }
113
114 array_unshift(self::$_recent,
115 [
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),
127 ]
128 );
129
130 if (count(self::$_recent) > self::$_maxItems) {
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 /**
140 * Delete an item from the recent stack.
141 *
142 * @param array $recentItem
143 * Array of the recent Item to be removed.
144 */
145 public static function del($recentItem) {
146 self::initialize();
147 $tempRecent = self::$_recent;
148
149 self::$_recent = [];
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'] &&
154 $tempRecent[$i]['type'] == $recentItem['type']
155 )
156 ) {
157 self::$_recent[] = $tempRecent[$i];
158 }
159 }
160
161 CRM_Utils_Hook::recent(self::$_recent);
162 $session = CRM_Core_Session::singleton();
163 $session->set(self::STORE_NAME, self::$_recent);
164 }
165
166 /**
167 * Delete an item from the recent stack.
168 *
169 * @param string $id
170 * Contact id that had to be removed.
171 */
172 public static function delContact($id) {
173 self::initialize();
174
175 $tempRecent = self::$_recent;
176
177 self::$_recent = [];
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
188 CRM_Utils_Hook::recent(self::$_recent);
189 $session = CRM_Core_Session::singleton();
190 $session->set(self::STORE_NAME, self::$_recent);
191 }
192
193 /**
194 * Check if a provider is allowed to add stuff.
195 * If corresponding setting is empty, all are allowed
196 *
197 * @param string $providerName
198 * @return bool
199 */
200 public static function isProviderEnabled($providerName) {
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
210 $providersPermitted = Civi::settings()->get('recentItemsProviders');
211 if ($providersPermitted) {
212 $allowed = in_array($providerName, $providersPermitted);
213 }
214 // Else allow
215 return $allowed;
216 }
217
218 /**
219 * Gets the list of available providers to civi's recent items stack
220 *
221 * @return array
222 */
223 public static function getProviders() {
224 $providers = [
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'),
238 ];
239
240 return $providers;
241 }
242
243 }