CRM-17663 - Configurable cache time per dashlet & auto-refresh
[civicrm-core.git] / CRM / Core / DAO / AllCoreTables.php
CommitLineData
4ef04170
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2016 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2016
33 * $Id$
34 *
35 */
36class CRM_Core_DAO_AllCoreTables {
37
95b9a42e
TO
38 private static $tables = NULL;
39 private static $daoToClass = NULL;
40 private static $entityTypes = NULL;
4ef04170 41
95b9a42e 42 public static function init($fresh = FALSE) {
4ef04170 43 static $init = FALSE;
95b9a42e
TO
44 if ($init && !$fresh) {
45 return;
4ef04170 46 }
740dd877 47 Civi::$statics[__CLASS__] = array();
4ef04170
TO
48
49 $file = preg_replace('/\.php$/', '.data.php', __FILE__);
50 $entityTypes = require $file;
51 CRM_Utils_Hook::entityTypes($entityTypes);
52
53 self::$entityTypes = array();
54 self::$tables = array();
55 self::$daoToClass = array();
56 foreach ($entityTypes as $entityType) {
740dd877
TO
57 self::registerEntityType(
58 $entityType['name'],
59 $entityType['class'],
60 $entityType['table'],
61 isset($entityType['fields_callback']) ? $entityType['fields_callback'] : NULL,
62 isset($entityType['links_callback']) ? $entityType['links_callback'] : NULL
63 );
4ef04170
TO
64 }
65
66 $init = TRUE;
67 }
68
69 /**
70 * (Quasi-Private) Do not call externally (except for unit-testing)
71 */
740dd877 72 public static function registerEntityType($daoName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
4ef04170
TO
73 self::$daoToClass[$daoName] = $className;
74 self::$tables[$tableName] = $className;
75 self::$entityTypes[$className] = array(
76 'name' => $daoName,
77 'class' => $className,
78 'table' => $tableName,
740dd877
TO
79 'fields_callback' => $fields_callback,
80 'links_callback' => $links_callback,
4ef04170
TO
81 );
82 }
83
95b9a42e
TO
84 /**
85 * @return array
86 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
87 */
88 public static function get() {
4ef04170
TO
89 self::init();
90 return self::$entityTypes;
91 }
92
95b9a42e
TO
93 /**
94 * @return array
95 * List of SQL table names.
96 */
97 public static function tables() {
4ef04170
TO
98 self::init();
99 return self::$tables;
100 }
101
95b9a42e
TO
102 /**
103 * @return array
104 * Mapping from brief-names to class-names.
105 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
106 */
107 public static function daoToClass() {
4ef04170
TO
108 self::init();
109 return self::$daoToClass;
110 }
111
95b9a42e
TO
112 /**
113 * @return array
114 * Mapping from table-names to class-names.
115 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
116 */
117 public static function getCoreTables() {
4ef04170
TO
118 return self::tables();
119 }
120
95b9a42e
TO
121 /**
122 * Determine whether $tableName is a core table.
123 *
124 * @param string $tableName
125 * @return bool
126 */
127 public static function isCoreTable($tableName) {
4ef04170
TO
128 return FALSE !== array_search($tableName, self::tables());
129 }
130
95b9a42e 131 public static function getCanonicalClassName($className) {
4ef04170
TO
132 return str_replace('_BAO_', '_DAO_', $className);
133 }
134
95b9a42e
TO
135 /**
136 * @return array
137 * List of class names.
138 */
139 public static function getClasses() {
4ef04170
TO
140 return array_values(self::daoToClass());
141 }
142
95b9a42e 143 public static function getClassForTable($tableName) {
4ef04170
TO
144 return CRM_Utils_Array::value($tableName, self::tables());
145 }
146
95b9a42e
TO
147 /**
148 * Given a brief-name, determine the full class-name.
149 *
150 * @param string $daoName
151 * Ex: 'Contact'.
152 * @return string|NULL
153 * Ex: 'CRM_Contact_DAO_Contact'.
154 */
155 public static function getFullName($daoName) {
4ef04170
TO
156 return CRM_Utils_Array::value($daoName, self::daoToClass());
157 }
158
95b9a42e
TO
159 /**
160 * Given a full class-name, determine the brief-name.
161 *
162 * @param string $className
163 * Ex: 'CRM_Contact_DAO_Contact'.
164 * @return string|NULL
165 * Ex: 'Contact'.
166 */
167 public static function getBriefName($className) {
4ef04170
TO
168 return CRM_Utils_Array::value($className, array_flip(self::daoToClass()));
169 }
170
171 /**
172 * @param string $className DAO or BAO name
173 * @return string|FALSE SQL table name
174 */
95b9a42e
TO
175 public static function getTableForClass($className) {
176 return array_search(self::getCanonicalClassName($className),
177 self::tables());
4ef04170
TO
178 }
179
95b9a42e 180 public static function reinitializeCache($fresh = FALSE) {
4ef04170
TO
181 self::init($fresh);
182 }
183
84a0493c
TO
184 /**
185 * (Quasi-Private) Do not call externally. For use by DAOs.
186 *
187 * @param string $dao
188 * Ex: 'CRM_Core_DAO_Address'.
189 * @param string $labelName
190 * Ex: 'address'.
191 * @param bool $prefix
192 * @param array $foreignDAOs
193 * @return array
194 */
195 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
196 // Bug-level compatibility -- or sane behavior?
197 $cacheKey = $dao . ':export';
198 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
199
200 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
201 $exports = array();
202 $fields = $dao::fields();
203
204 foreach($fields as $name => $field) {
205 if (CRM_Utils_Array::value('export', $field)) {
206 if ($prefix) {
207 $exports[$labelName] = & $fields[$name];
208 } else {
209 $exports[$name] = & $fields[$name];
210 }
211 }
212 }
213
214 foreach ($foreignDAOs as $foreignDAO) {
215 $exports = array_merge($exports, $foreignDAO::export(TRUE));
216 }
217
218 Civi::$statics[__CLASS__][$cacheKey] = $exports;
219 }
220 return Civi::$statics[__CLASS__][$cacheKey];
221 }
222
223 /**
224 * (Quasi-Private) Do not call externally. For use by DAOs.
225 *
226 * @param string $dao
227 * Ex: 'CRM_Core_DAO_Address'.
228 * @param string $labelName
229 * Ex: 'address'.
230 * @param bool $prefix
231 * @param array $foreignDAOs
232 * @return array
233 */
234 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
235 // Bug-level compatibility -- or sane behavior?
236 $cacheKey = $dao . ':import';
237 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
238
239 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
240 $imports = array();
241 $fields = $dao::fields();
242
243 foreach($fields as $name => $field) {
244 if (CRM_Utils_Array::value('import', $field)) {
245 if ($prefix) {
246 $imports[$labelName] = & $fields[$name];
247 } else {
248 $imports[$name] = & $fields[$name];
249 }
250 }
251 }
252
253 foreach ($foreignDAOs as $foreignDAO) {
254 $imports = array_merge($imports, $foreignDAO::import(TRUE));
255 }
256
257 Civi::$statics[__CLASS__][$cacheKey] = $imports;
258 }
259 return Civi::$statics[__CLASS__][$cacheKey];
260 }
261
740dd877
TO
262 /**
263 * (Quasi-Private) Do not call externally. For use by DAOs.
264 *
265 * Apply any third-party alterations to the `fields()`.
266 *
267 * @param string $className
268 * @param string $event
269 * @param mixed $values
270 */
271 public static function invoke($className, $event, &$values) {
272 self::init();
273 if (isset(self::$entityTypes[$className][$event])) {
274 foreach (self::$entityTypes[$className][$event] as $filter) {
275 $args = array($className, &$values);
276 \Civi\Core\Resolver::singleton()->call($filter, $args);
277 }
278 }
279 }
280
4ef04170 281}