Merge pull request #9658 from eileenmcnaughton/comments
[civicrm-core.git] / CRM / Core / DAO / AllCoreTables.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
33 */
34 class CRM_Core_DAO_AllCoreTables {
35
36 private static $tables = NULL;
37 private static $daoToClass = NULL;
38 private static $entityTypes = NULL;
39
40 /**
41 * Initialise.
42 *
43 * @param bool $fresh
44 */
45 public static function init($fresh = FALSE) {
46 static $init = FALSE;
47 if ($init && !$fresh) {
48 return;
49 }
50 Civi::$statics[__CLASS__] = array();
51
52 $file = preg_replace('/\.php$/', '.data.php', __FILE__);
53 $entityTypes = require $file;
54 CRM_Utils_Hook::entityTypes($entityTypes);
55
56 self::$entityTypes = array();
57 self::$tables = array();
58 self::$daoToClass = array();
59 foreach ($entityTypes as $entityType) {
60 self::registerEntityType(
61 $entityType['name'],
62 $entityType['class'],
63 $entityType['table'],
64 isset($entityType['fields_callback']) ? $entityType['fields_callback'] : NULL,
65 isset($entityType['links_callback']) ? $entityType['links_callback'] : NULL
66 );
67 }
68
69 $init = TRUE;
70 }
71
72 /**
73 * (Quasi-Private) Do not call externally (except for unit-testing)
74 *
75 * @param string $daoName
76 * @param string $className
77 * @param string $tableName
78 * @param string $fields_callback
79 * @param string $links_callback
80 */
81 public static function registerEntityType($daoName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
82 self::$daoToClass[$daoName] = $className;
83 self::$tables[$tableName] = $className;
84 self::$entityTypes[$className] = array(
85 'name' => $daoName,
86 'class' => $className,
87 'table' => $tableName,
88 'fields_callback' => $fields_callback,
89 'links_callback' => $links_callback,
90 );
91 }
92
93 /**
94 * @return array
95 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
96 */
97 public static function get() {
98 self::init();
99 return self::$entityTypes;
100 }
101
102 /**
103 * @return array
104 * List of SQL table names.
105 */
106 public static function tables() {
107 self::init();
108 return self::$tables;
109 }
110
111 /**
112 * @return array
113 * Mapping from brief-names to class-names.
114 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
115 */
116 public static function daoToClass() {
117 self::init();
118 return self::$daoToClass;
119 }
120
121 /**
122 * @return array
123 * Mapping from table-names to class-names.
124 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
125 */
126 public static function getCoreTables() {
127 return self::tables();
128 }
129
130 /**
131 * Determine whether $tableName is a core table.
132 *
133 * @param string $tableName
134 * @return bool
135 */
136 public static function isCoreTable($tableName) {
137 return FALSE !== array_search($tableName, self::tables());
138 }
139
140 /**
141 * Get the DAO for the class.
142 *
143 * @param string $className
144 *
145 * @return string
146 */
147 public static function getCanonicalClassName($className) {
148 return str_replace('_BAO_', '_DAO_', $className);
149 }
150
151 /**
152 * Get a list of all DAO classes.
153 *
154 * @return array
155 * List of class names.
156 */
157 public static function getClasses() {
158 return array_values(self::daoToClass());
159 }
160
161 /**
162 * Get the classname for the table.
163 *
164 * @param string $tableName
165 * @return string
166 */
167 public static function getClassForTable($tableName) {
168 //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
169 if (CRM_Core_I18n::isMultilingual()) {
170 global $dbLocale;
171 $tableName = str_replace($dbLocale, '', $tableName);
172 }
173 return CRM_Utils_Array::value($tableName, self::tables());
174 }
175
176 /**
177 * Given a brief-name, determine the full class-name.
178 *
179 * @param string $daoName
180 * Ex: 'Contact'.
181 * @return string|NULL
182 * Ex: 'CRM_Contact_DAO_Contact'.
183 */
184 public static function getFullName($daoName) {
185 return CRM_Utils_Array::value($daoName, self::daoToClass());
186 }
187
188 /**
189 * Given a full class-name, determine the brief-name.
190 *
191 * @param string $className
192 * Ex: 'CRM_Contact_DAO_Contact'.
193 * @return string|NULL
194 * Ex: 'Contact'.
195 */
196 public static function getBriefName($className) {
197 return CRM_Utils_Array::value($className, array_flip(self::daoToClass()));
198 }
199
200 /**
201 * @param string $className DAO or BAO name
202 * @return string|FALSE SQL table name
203 */
204 public static function getTableForClass($className) {
205 return array_search(self::getCanonicalClassName($className),
206 self::tables());
207 }
208
209 /**
210 * Reinitialise cache.
211 *
212 * @param bool $fresh
213 */
214 public static function reinitializeCache($fresh = FALSE) {
215 self::init($fresh);
216 }
217
218 /**
219 * (Quasi-Private) Do not call externally. For use by DAOs.
220 *
221 * @param string $dao
222 * Ex: 'CRM_Core_DAO_Address'.
223 * @param string $labelName
224 * Ex: 'address'.
225 * @param bool $prefix
226 * @param array $foreignDAOs
227 * @return array
228 */
229 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
230 // Bug-level compatibility -- or sane behavior?
231 $cacheKey = $dao . ':export';
232 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
233
234 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
235 $exports = array();
236 $fields = $dao::fields();
237
238 foreach ($fields as $name => $field) {
239 if (CRM_Utils_Array::value('export', $field)) {
240 if ($prefix) {
241 $exports[$labelName] = & $fields[$name];
242 }
243 else {
244 $exports[$name] = & $fields[$name];
245 }
246 }
247 }
248
249 foreach ($foreignDAOs as $foreignDAO) {
250 $exports = array_merge($exports, $foreignDAO::export(TRUE));
251 }
252
253 Civi::$statics[__CLASS__][$cacheKey] = $exports;
254 }
255 return Civi::$statics[__CLASS__][$cacheKey];
256 }
257
258 /**
259 * (Quasi-Private) Do not call externally. For use by DAOs.
260 *
261 * @param string $dao
262 * Ex: 'CRM_Core_DAO_Address'.
263 * @param string $labelName
264 * Ex: 'address'.
265 * @param bool $prefix
266 * @param array $foreignDAOs
267 * @return array
268 */
269 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
270 // Bug-level compatibility -- or sane behavior?
271 $cacheKey = $dao . ':import';
272 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
273
274 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
275 $imports = array();
276 $fields = $dao::fields();
277
278 foreach ($fields as $name => $field) {
279 if (CRM_Utils_Array::value('import', $field)) {
280 if ($prefix) {
281 $imports[$labelName] = & $fields[$name];
282 }
283 else {
284 $imports[$name] = & $fields[$name];
285 }
286 }
287 }
288
289 foreach ($foreignDAOs as $foreignDAO) {
290 $imports = array_merge($imports, $foreignDAO::import(TRUE));
291 }
292
293 Civi::$statics[__CLASS__][$cacheKey] = $imports;
294 }
295 return Civi::$statics[__CLASS__][$cacheKey];
296 }
297
298 /**
299 * (Quasi-Private) Do not call externally. For use by DAOs.
300 *
301 * Apply any third-party alterations to the `fields()`.
302 *
303 * @param string $className
304 * @param string $event
305 * @param mixed $values
306 */
307 public static function invoke($className, $event, &$values) {
308 self::init();
309 if (isset(self::$entityTypes[$className][$event])) {
310 foreach (self::$entityTypes[$className][$event] as $filter) {
311 $args = array($className, &$values);
312 \Civi\Core\Resolver::singleton()->call($filter, $args);
313 }
314 }
315 }
316
317 }