Merge pull request #10162 from yashodha/CRM-20429
[civicrm-core.git] / CRM / Utils / Recent.php
index 25de25889d585078dbe3db9ec90d6abde0768797..7f8e5362727d008b2cdab081a45c71422c6a7178 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -27,7 +27,7 @@
 
 /**
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 
 /**
 class CRM_Utils_Recent {
 
   /**
-   * Max number of items in queue.
+   * Store name
    *
-   * @var int
+   * @var string
    */
-  const MAX_ITEMS = 10, STORE_NAME = 'CRM_Utils_Recent';
+  const MAX_ITEMS = 30, STORE_NAME = 'CRM_Utils_Recent';
 
   /**
    * The list of recently viewed items.
@@ -49,10 +49,20 @@ class CRM_Utils_Recent {
    */
   static private $_recent = NULL;
 
+  /**
+   * Maximum stack size
+   * @var int
+   */
+  static private $_maxItems = 10;
+
   /**
    * Initialize this class and set the static variables.
    */
   public static function initialize() {
+    $maxItemsSetting = Civi::settings()->get('recentItemsMaxCount');
+    if (isset($maxItemsSetting) && $maxItemsSetting > 0 && $maxItemsSetting < self::MAX_ITEMS) {
+      self::$_maxItems = $maxItemsSetting;
+    }
     if (!self::$_recent) {
       $session = CRM_Core_Session::singleton();
       self::$_recent = $session->get(self::STORE_NAME);
@@ -97,6 +107,11 @@ class CRM_Utils_Recent {
     $others = array()
   ) {
     self::initialize();
+
+    if (!self::isProviderEnabled($type)) {
+      return;
+    }
+
     $session = CRM_Core_Session::singleton();
 
     // make sure item is not already present in list
@@ -127,7 +142,8 @@ class CRM_Utils_Recent {
         'delete_url' => CRM_Utils_Array::value('deleteUrl', $others),
       )
     );
-    if (count(self::$_recent) > self::MAX_ITEMS) {
+
+    if (count(self::$_recent) > self::$_maxItems) {
       array_pop(self::$_recent);
     }
 
@@ -188,4 +204,51 @@ class CRM_Utils_Recent {
     $session->set(self::STORE_NAME, self::$_recent);
   }
 
+  /**
+   * Check if a provider is allowed to add stuff.
+   * If correspondig setting is empty, all are allowed
+   *
+   * @param string $providerName
+   */
+  public static function isProviderEnabled($providerName) {
+
+    // Join contact types to providerName 'Contact'
+    $contactTypes = CRM_Contact_BAO_ContactType::contactTypes(TRUE);
+    if (in_array($providerName, $contactTypes)) {
+      $providerName = 'Contact';
+    }
+    $allowed = TRUE;
+
+    // Use core setting recentItemsProviders if configured
+    $providersPermitted = Civi::settings()->get('recentItemsProviders');
+    if ($providersPermitted) {
+      $allowed = in_array($providerName, $providersPermitted);
+    }
+    // Else allow
+    return $allowed;
+  }
+
+  /**
+   * Gets the list of available providers to civi's recent items stack
+   */
+  public static function getProviders() {
+    $providers = array(
+      'Contact' => ts('Contacts'),
+      'Relationship' => ts('Relationships'),
+      'Activity' => ts('Activities'),
+      'Note' => ts('Notes'),
+      'Group' => ts('Groups'),
+      'Case' => ts('Cases'),
+      'Contribution' => ts('Contributions'),
+      'Participant' => ts('Participants'),
+      'Grant' => ts('Grants'),
+      'Membership' => ts('Memberships'),
+      'Pledge' => ts('Pledges'),
+      'Event' => ts('Events'),
+      'Campaign' => ts('Campaigns'),
+    );
+
+    return $providers;
+  }
+
 }