$contactName,
$others = []
) {
- self::initialize();
-
+ // Abort if this entity type is not supported
if (!self::isProviderEnabled($type)) {
return;
}
- $session = CRM_Core_Session::singleton();
-
- // make sure item is not already present in list
- for ($i = 0; $i < count(self::$_recent); $i++) {
- if (self::$_recent[$i]['type'] === $type && self::$_recent[$i]['id'] == $id) {
- // delete item from array
- array_splice(self::$_recent, $i, 1);
- break;
- }
- }
+ // Ensure item is not already present in list
+ self::removeItems(['id' => $id, 'type' => $type]);
if (!is_array($others)) {
$others = [];
]
);
- if (count(self::$_recent) > self::$_maxItems) {
+ // Keep the list trimmed to max length
+ while (count(self::$_recent) > self::$_maxItems) {
array_pop(self::$_recent);
}
CRM_Utils_Hook::recent(self::$_recent);
+ $session = CRM_Core_Session::singleton();
$session->set(self::STORE_NAME, self::$_recent);
}
/**
- * Delete an item from the recent stack.
- *
- * @param array $recentItem
- * Array of the recent Item to be removed.
+ * Callback for hook_civicrm_post().
+ * @param \Civi\Core\Event\PostEvent $event
*/
- public static function del($recentItem) {
- self::initialize();
- $tempRecent = self::$_recent;
+ public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
+ if ($event->action === 'delete' && $event->id && CRM_Core_Session::getLoggedInContactID()) {
+ // Is this an entity that might be in the recent items list?
+ $providersPermitted = Civi::settings()->get('recentItemsProviders') ?: array_keys(self::getProviders());
+ if (in_array($event->entity, $providersPermitted)) {
+ self::del(['id' => $event->id, 'type' => $event->entity]);
+ }
+ }
+ }
- self::$_recent = [];
+ /**
+ * Remove items from the array that match given props
+ * @param array $props
+ */
+ private static function removeItems(array $props) {
+ self::initialize();
- // make sure item is not already present in list
- for ($i = 0; $i < count($tempRecent); $i++) {
- if (!($tempRecent[$i]['id'] == $recentItem['id'] &&
- $tempRecent[$i]['type'] == $recentItem['type']
- )
- ) {
- self::$_recent[] = $tempRecent[$i];
+ self::$_recent = array_filter(self::$_recent, function($item) use ($props) {
+ foreach ($props as $key => $val) {
+ if (isset($item[$key]) && $item[$key] == $val) {
+ return FALSE;
+ }
}
- }
+ return TRUE;
+ });
+ }
+ /**
+ * Delete item(s) from the recently-viewed list.
+ *
+ * @param array $removeItem
+ * Item to be removed.
+ */
+ public static function del($removeItem) {
+ self::removeItems($removeItem);
CRM_Utils_Hook::recent(self::$_recent);
$session = CRM_Core_Session::singleton();
$session->set(self::STORE_NAME, self::$_recent);
* Delete an item from the recent stack.
*
* @param string $id
- * Contact id that had to be removed.
+ * @deprecated
*/
public static function delContact($id) {
- self::initialize();
-
- $tempRecent = self::$_recent;
-
- self::$_recent = [];
-
- // rebuild recent.
- for ($i = 0; $i < count($tempRecent); $i++) {
- // don't include deleted contact in recent.
- if (CRM_Utils_Array::value('contact_id', $tempRecent[$i]) == $id) {
- continue;
- }
- self::$_recent[] = $tempRecent[$i];
- }
-
- CRM_Utils_Hook::recent(self::$_recent);
- $session = CRM_Core_Session::singleton();
- $session->set(self::STORE_NAME, self::$_recent);
+ CRM_Core_Error::deprecatedFunctionWarning('del');
+ self::del(['contact_id' => $id]);
}
/**