X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FRecent.php;h=64636385199d5823f834027febf1c17d49d119fe;hb=27d10585ba373d48b8961100810fccb9e0927b5b;hp=59818c0b0161810545c107cf6937bfc61505a124;hpb=10781258d3d7c96bdae5a28ce0da6bed3c1acc33;p=civicrm-core.git diff --git a/CRM/Utils/Recent.php b/CRM/Utils/Recent.php index 59818c0b01..6463638519 100644 --- a/CRM/Utils/Recent.php +++ b/CRM/Utils/Recent.php @@ -90,22 +90,13 @@ class CRM_Utils_Recent { $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 = []; @@ -127,37 +118,56 @@ class CRM_Utils_Recent { ] ); - 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); @@ -167,27 +177,11 @@ class CRM_Utils_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]); } /**