Cache.php 4.95 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37 38 39 40 41 42
 * $Id$
 *
 */

/**
 *  Access Control Cache
 */
class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_Cache {

  static $_cache = NULL;

43
  /**
colemanw's avatar
colemanw committed
44
   * @param int $id
45 46 47
   *
   * @return mixed
   */
48
  public static function &build($id) {
totten's avatar
totten committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    if (!self::$_cache) {
      self::$_cache = array();
    }

    if (array_key_exists($id, self::$_cache)) {
      return self::$_cache[$id];
    }

    // check if this entry exists in db
    // if so retrieve and return
    self::$_cache[$id] = self::retrieve($id);
    if (self::$_cache[$id]) {
      return self::$_cache[$id];
    }

    self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id);
    self::store($id, self::$_cache[$id]);
    return self::$_cache[$id];
  }

69
  /**
colemanw's avatar
colemanw committed
70
   * @param int $id
71 72 73
   *
   * @return array
   */
74
  public static function retrieve($id) {
totten's avatar
totten committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    $query = "
SELECT acl_id
  FROM civicrm_acl_cache
 WHERE contact_id = %1
";
    $params = array(1 => array($id, 'Integer'));

    if ($id == 0) {
      $query .= " OR contact_id IS NULL";
    }

    $dao = CRM_Core_DAO::executeQuery($query, $params);

    $cache = array();
    while ($dao->fetch()) {
      $cache[$dao->acl_id] = 1;
    }
    return $cache;
  }

95
  /**
colemanw's avatar
colemanw committed
96 97
   * @param int $id
   * @param array $cache
98
   */
99
  public static function store($id, &$cache) {
totten's avatar
totten committed
100 101 102 103 104 105 106 107 108 109 110 111 112
    foreach ($cache as $aclID => $data) {
      $dao = new CRM_ACL_DAO_Cache();
      if ($id) {
        $dao->contact_id = $id;
      }
      $dao->acl_id = $aclID;

      $cache[$aclID] = 1;

      $dao->save();
    }
  }

113
  /**
colemanw's avatar
colemanw committed
114
   * @param int $id
115
   */
116
  public static function deleteEntry($id) {
totten's avatar
totten committed
117 118 119 120 121 122 123 124 125 126 127
    if (self::$_cache &&
      array_key_exists($id, self::$_cache)
    ) {
      unset(self::$_cache[$id]);
    }

    $query = "
DELETE FROM civicrm_acl_cache
WHERE contact_id = %1
";
    $params = array(1 => array($id, 'Integer'));
128
    CRM_Core_DAO::executeQuery($query, $params);
totten's avatar
totten committed
129 130
  }

131
  /**
colemanw's avatar
colemanw committed
132
   * @param int $id
133
   */
134
  public static function updateEntry($id) {
totten's avatar
totten committed
135 136 137 138 139 140 141 142
    // rebuilds civicrm_acl_cache
    self::deleteEntry($id);
    self::build($id);

    // rebuilds civicrm_acl_contact_cache
    CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE);
  }

143 144 145
  /**
   * Deletes all the cache entries.
   */
146
  public static function resetCache() {
totten's avatar
totten committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    // reset any static caching
    self::$_cache = NULL;

    // reset any db caching
    $config = CRM_Core_Config::singleton();
    $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout();

    //make sure to give original timezone settings again.
    $now = CRM_Utils_Date::getUTCTime();

    $query = "
DELETE
FROM   civicrm_acl_cache
WHERE  modified_date IS NULL
   OR  (TIMESTAMPDIFF(MINUTE, modified_date, $now) >= $smartGroupCacheTimeout)
";
    CRM_Core_DAO::singleValueQuery($query);

165 166 167
    // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
    // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
    if (CRM_Core_Transaction::isActive()) {
totten's avatar
totten committed
168
      CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () {
169 170
        CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
      });
171 172
    }
    else {
173 174
      CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
    }
totten's avatar
totten committed
175
  }
176

totten's avatar
totten committed
177
}