INFRA-132 - Trailing commas for multiline arrays
[civicrm-core.git] / CRM / Contact / BAO / GroupContactCache.php
index acda3a755da8734156da24c0611193e78ac998a8..9c79144121e0ad78730d66a8b3ac101da666e47a 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -40,11 +40,12 @@ class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCach
    * Check to see if we have cache entries for this group
    * if not, regenerate, else return
    *
-   * @param $groupIDs of group that we are checking against
+   * @param $groupIDs
+   *   Of group that we are checking against.
    *
    * @return boolean true if we did not regenerate, false if we did
    */
-  static function check($groupIDs) {
+  public static function check($groupIDs) {
     if (empty($groupIDs)) {
       return TRUE;
     }
@@ -56,14 +57,15 @@ class CRM_Contact_BAO_GroupContactCache extends CRM_Contact_DAO_GroupContactCach
    * Common function that formulates the query to see which groups needs to be refreshed
    * based on their cache date and the smartGroupCacheTimeOut
    *
-   * @param string $groupIDClause the clause which limits which groups we need to evaluate
-   * @param boolean $includeHiddenGroups hidden groups are excluded by default
+   * @param string $groupIDClause
+   *   The clause which limits which groups we need to evaluate.
+   * @param bool $includeHiddenGroups
+   *   Hidden groups are excluded by default.
    *
    * @return string the sql query which lists the groups that need to be refreshed
    * @static
-   * @public
    */
-  static function groupRefreshedClause($groupIDClause = null, $includeHiddenGroups = FALSE) {
+  public static function groupRefreshedClause($groupIDClause = null, $includeHiddenGroups = FALSE) {
     $smartGroupCacheTimeout = self::smartGroupCacheTimeout();
     $now = CRM_Utils_Date::getUTCTime();
 
@@ -94,14 +96,15 @@ AND     ( g.cache_date IS NULL OR
    * in a locking scenario when some other process might have refreshed things underneath
    * this process
    *
-   * @param int $groupID the group ID
-   * @param boolean $includeHiddenGroups hidden groups are excluded by default
+   * @param int $groupID
+   *   The group ID.
+   * @param bool $includeHiddenGroups
+   *   Hidden groups are excluded by default.
    *
    * @return string the sql query which lists the groups that need to be refreshed
    * @static
-   * @public
    */
-  static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
+  public static function shouldGroupBeRefreshed($groupID, $includeHiddenGroups = FALSE) {
     $query = self::groupRefreshedClause("g.id = %1", $includeHiddenGroups);
     $params = array(1 => array($groupID, 'Integer'));
 
@@ -115,16 +118,17 @@ AND     ( g.cache_date IS NULL OR
    *
    * @param int/array $groupID groupID of group that we are checking against
    *                           if empty, all groups are checked
-   * @param int       $limit   limits the number of groups we evaluate
+   * @param int $limit
+   *   Limits the number of groups we evaluate.
    *
    * @return boolean true if we did not regenerate, false if we did
    */
-  static function loadAll($groupIDs = null, $limit = 0) {
+  public static function loadAll($groupIDs = null, $limit = 0) {
     // ensure that all the smart groups are loaded
     // this function is expensive and should be sparingly used if groupIDs is empty
     if (empty($groupIDs)) {
       $groupIDClause = null;
-      $groupIDs = array( );
+      $groupIDs = array();
     }
     else {
       if (!is_array($groupIDs)) {
@@ -191,7 +195,7 @@ AND    g.refresh_date IS NULL
   /**
    * FIXME: This function should not be needed, because the cache table should not be getting truncated
    */
-  static function fillIfEmpty() {
+  public static function fillIfEmpty() {
     if (!CRM_Core_DAO::singleValueQuery("SELECT COUNT(id) FROM civicrm_group_contact_cache")) {
       self::loadAll();
     }
@@ -200,7 +204,7 @@ AND    g.refresh_date IS NULL
   /**
    * @param int $groupID
    */
-  static function add($groupID) {
+  public static function add($groupID) {
     // first delete the current cache
     self::remove($groupID);
     if (!is_array($groupID)) {
@@ -219,7 +223,7 @@ AND    g.refresh_date IS NULL
    * @param int $groupID
    * @param $values
    */
-  static function store(&$groupID, &$values) {
+  public static function store(&$groupID, &$values) {
     $processed = FALSE;
 
     // sort the values so we put group IDs in front and hence optimize
@@ -240,10 +244,12 @@ AND    g.refresh_date IS NULL
   /**
    * Change the cache_date
    *
-   * @param $groupID array(int)
-   * @param $processed bool, whether the cache data was recently modified
+   * @param $groupID
+   *   Array(int).
+   * @param $processed
+   *   Bool, whether the cache data was recently modified.
    */
-  static function updateCacheTime($groupID, $processed) {
+  public static function updateCacheTime($groupID, $processed) {
     // only update cache entry if we had any values
     if ($processed) {
       // also update the group with cache date information
@@ -273,14 +279,15 @@ WHERE  id IN ( $groupIDs )
    * cache date, i.e. the removal is not done if the group was recently
    * loaded into the cache.
    *
-   * @param $groupID  int the groupID to delete cache entries, NULL for all groups
-   * @param $onceOnly boolean run the function exactly once for all groups.
+   * @param $groupID
+   *   Int the groupID to delete cache entries, NULL for all groups.
+   * @param $onceOnly
+   *   Boolean run the function exactly once for all groups.
    *
-   * @public
    * @return void
    * @static
    */
-  static function remove($groupID = NULL, $onceOnly = TRUE) {
+  public static function remove($groupID = NULL, $onceOnly = TRUE) {
     static $invoked = FALSE;
 
     // typically this needs to happy only once per instance
@@ -298,11 +305,13 @@ WHERE  id IN ( $groupIDs )
 
     if ($groupID == NULL) {
       $invoked = TRUE;
-    } else if (is_array($groupID)) {
+    }
+    elseif (is_array($groupID)) {
       foreach ($groupID as $gid) {
         unset(self::$_alreadyLoaded[$gid]);
       }
-    } else if ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
+    }
+    elseif ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) {
       unset(self::$_alreadyLoaded[$groupID]);
     }
 
@@ -384,14 +393,40 @@ WHERE  id = %1
     CRM_Core_DAO::executeQuery($update, $params);
   }
 
+  /**
+   * Removes one or more contacts from the smart group cache
+   * @param int|array $cid
+   * @param int $groupId
+   * @return bool - true if successful
+   */
+  public static function removeContact($cid, $groupId = NULL) {
+    $cids = array();
+    // sanitize input
+    foreach ((array) $cid as $c) {
+      $cids[] = CRM_Utils_Type::escape($c, 'Integer');
+    }
+    if ($cids) {
+      $condition = count($cids) == 1 ? "= {$cids[0]}" : "IN (" . implode(',', $cids) . ")";
+      if ($groupId) {
+        $condition .= " AND group_id = " . CRM_Utils_Type::escape($groupId, 'Integer');
+      }
+      $sql = "DELETE FROM civicrm_group_contact_cache WHERE contact_id $condition";
+      CRM_Core_DAO::executeQuery($sql);
+      return TRUE;
+    }
+    return FALSE;
+  }
+
   /**
    * Load the smart group cache for a saved search
    *
-   * @param object  $group - the smart group that needs to be loaded
-   * @param boolean $force - should we force a search through
+   * @param object $group
+   *   The smart group that needs to be loaded.
+   * @param bool $force
+   *   Should we force a search through.
    *
    */
-  static function load(&$group, $force = FALSE) {
+  public static function load(&$group, $force = FALSE) {
     $groupID = $group->id;
     $savedSearchID = $group->saved_search_id;
     if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
@@ -551,7 +586,7 @@ AND  civicrm_group_contact.group_id = $groupID ";
   /**
    * @return int
    */
-  static function smartGroupCacheTimeout() {
+  public static function smartGroupCacheTimeout() {
     $config = CRM_Core_Config::singleton();
 
     if (
@@ -570,12 +605,13 @@ AND  civicrm_group_contact.group_id = $groupID ";
    * Note that this could potentially be a super slow function since
    * it ensure that all contact groups are loaded in the cache
    *
-   * @param int     $contactID
-   * @param boolean $showHidden - hidden groups are shown only if this flag is set
+   * @param int $contactID
+   * @param bool $showHidden
+   *   Hidden groups are shown only if this flag is set.
    *
    * @return array an array of groups that this contact belongs to
    */
-  static function contactGroup($contactID, $showHidden = FALSE) {
+  public static function contactGroup($contactID, $showHidden = FALSE) {
     if (empty($contactID)) {
       return;
     }
@@ -617,7 +653,7 @@ ORDER BY   gc.contact_id, g.children
       $prevContactID = $dao->contact_id;
       if (!array_key_exists($dao->contact_id, $contactGroup)) {
         $contactGroup[$dao->contact_id] =
-          array( 'group' => array(), 'groupTitle' => array());
+          array('group' => array(), 'groupTitle' => array());
       }
 
       $contactGroup[$dao->contact_id]['group'][] =
@@ -625,7 +661,7 @@ ORDER BY   gc.contact_id, g.children
           'id' => $dao->group_id,
           'title' => $dao->title,
           'description' => $dao->description,
-          'children' => $dao->children
+          'children' => $dao->children,
         );
       $contactGroup[$dao->contact_id]['groupTitle'][] = $dao->title;
     }
@@ -643,4 +679,3 @@ ORDER BY   gc.contact_id, g.children
   }
 
 }
-