INFRA-132 - Fix misc oddball syntax
[civicrm-core.git] / CRM / ACL / BAO / Cache.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Access Control Cache
38 */
39class CRM_ACL_BAO_Cache extends CRM_ACL_DAO_Cache {
40
41 static $_cache = NULL;
42
28518c90 43 /**
100fef9d 44 * @param int $id
28518c90
EM
45 *
46 * @return mixed
47 */
00be9182 48 public static function &build($id) {
6a488035
TO
49 if (!self::$_cache) {
50 self::$_cache = array();
51 }
52
53 if (array_key_exists($id, self::$_cache)) {
54 return self::$_cache[$id];
55 }
56
57 // check if this entry exists in db
58 // if so retrieve and return
59 self::$_cache[$id] = self::retrieve($id);
60 if (self::$_cache[$id]) {
61 return self::$_cache[$id];
62 }
63
64 self::$_cache[$id] = CRM_ACL_BAO_ACL::getAllByContact($id);
65 self::store($id, self::$_cache[$id]);
66 return self::$_cache[$id];
67 }
68
28518c90 69 /**
100fef9d 70 * @param int $id
28518c90
EM
71 *
72 * @return array
73 */
00be9182 74 public static function retrieve($id) {
6a488035
TO
75 $query = "
76SELECT acl_id
77 FROM civicrm_acl_cache
78 WHERE contact_id = %1
79";
80 $params = array(1 => array($id, 'Integer'));
81
82 if ($id == 0) {
83 $query .= " OR contact_id IS NULL";
84 }
85
86 $dao = CRM_Core_DAO::executeQuery($query, $params);
87
88 $cache = array();
89 while ($dao->fetch()) {
90 $cache[$dao->acl_id] = 1;
91 }
92 return $cache;
93 }
94
28518c90 95 /**
100fef9d
CW
96 * @param int $id
97 * @param array $cache
28518c90 98 */
00be9182 99 public static function store($id, &$cache) {
6a488035
TO
100 foreach ($cache as $aclID => $data) {
101 $dao = new CRM_ACL_DAO_Cache();
102 if ($id) {
103 $dao->contact_id = $id;
104 }
105 $dao->acl_id = $aclID;
106
107 $cache[$aclID] = 1;
108
109 $dao->save();
110 }
111 }
112
28518c90 113 /**
100fef9d 114 * @param int $id
28518c90 115 */
00be9182 116 public static function deleteEntry($id) {
6a488035
TO
117 if (self::$_cache &&
118 array_key_exists($id, self::$_cache)
119 ) {
120 unset(self::$_cache[$id]);
121 }
122
123 $query = "
124DELETE FROM civicrm_acl_cache
125WHERE contact_id = %1
126";
127 $params = array(1 => array($id, 'Integer'));
7e1f3142 128 CRM_Core_DAO::executeQuery($query, $params);
6a488035
TO
129 }
130
28518c90 131 /**
100fef9d 132 * @param int $id
28518c90 133 */
00be9182 134 public static function updateEntry($id) {
6a488035
TO
135 // rebuilds civicrm_acl_cache
136 self::deleteEntry($id);
137 self::build($id);
138
139 // rebuilds civicrm_acl_contact_cache
140 CRM_Contact_BAO_Contact_Permission::cache($id, CRM_Core_Permission::VIEW, TRUE);
141 }
142
143 // deletes all the cache entries
00be9182 144 public static function resetCache() {
6a488035
TO
145 // reset any static caching
146 self::$_cache = NULL;
147
148 // reset any db caching
149 $config = CRM_Core_Config::singleton();
150 $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout();
151
152 //make sure to give original timezone settings again.
153 $now = CRM_Utils_Date::getUTCTime();
154
155 $query = "
156DELETE
157FROM civicrm_acl_cache
158WHERE modified_date IS NULL
159 OR (TIMESTAMPDIFF(MINUTE, modified_date, $now) >= $smartGroupCacheTimeout)
160";
161 CRM_Core_DAO::singleValueQuery($query);
162
cded6e48
TO
163 // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction
164 // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe
165 if (CRM_Core_Transaction::isActive()) {
9b873358 166 CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function() {
cded6e48
TO
167 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
168 });
0db6c3e1
TO
169 }
170 else {
cded6e48
TO
171 CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache");
172 }
6a488035
TO
173 }
174}