4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Core_DAO_AllCoreTables
{
20 private static $tables = NULL;
21 private static $daoToClass = NULL;
22 private static $entityTypes = NULL;
29 public static function init($fresh = FALSE) {
31 if ($init && !$fresh) {
34 Civi
::$statics[__CLASS__
] = [];
36 $file = preg_replace('/\.php$/', '.data.php', __FILE__
);
37 $entityTypes = require $file;
38 CRM_Utils_Hook
::entityTypes($entityTypes);
40 self
::$entityTypes = [];
42 self
::$daoToClass = [];
43 foreach ($entityTypes as $entityType) {
44 self
::registerEntityType(
48 $entityType['fields_callback'] ??
NULL,
49 $entityType['links_callback'] ??
NULL
57 * (Quasi-Private) Do not call externally (except for unit-testing)
59 * @param string $daoName
60 * @param string $className
61 * @param string $tableName
62 * @param string $fields_callback
63 * @param string $links_callback
65 public static function registerEntityType($daoName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
66 self
::$daoToClass[$daoName] = $className;
67 self
::$tables[$tableName] = $className;
68 self
::$entityTypes[$className] = [
70 'class' => $className,
71 'table' => $tableName,
72 'fields_callback' => $fields_callback,
73 'links_callback' => $links_callback,
79 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
81 public static function get() {
83 return self
::$entityTypes;
88 * List of SQL table names.
90 public static function tables() {
99 public static function indices($localize = TRUE) {
102 foreach (self
::$daoToClass as $class) {
103 if (is_callable([$class, 'indices'])) {
104 $indices[$class::getTableName()] = $class::indices($localize);
111 * Modify indices to account for localization options.
113 * @param string $class DAO class
114 * @param array $originalIndices index definitions before localization
117 * index definitions after localization
119 public static function multilingualize($class, $originalIndices) {
120 $domain = new CRM_Core_DAO_Domain();
122 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
123 if (CRM_Utils_System
::isNull($locales)) {
124 return $originalIndices;
126 $classFields = $class::fields();
129 foreach ($originalIndices as $index) {
130 if ($index['localizable']) {
131 foreach ($locales as $locale) {
132 $localIndex = $index;
133 $localIndex['name'] .= "_" . $locale;
135 foreach ($localIndex['field'] as $field) {
136 $baseField = explode('(', $field);
137 if ($classFields[$baseField[0]]['localizable']) {
138 // field name may have eg (3) at end for prefix length
139 // last_name => last_name_fr_FR
140 // last_name(3) => last_name_fr_FR(3)
141 $fields[] = preg_replace('/^([^(]+)(\(\d+\)|)$/', '${1}_' . $locale . '${2}', $field);
147 $localIndex['field'] = $fields;
148 $finalIndices[$localIndex['name']] = $localIndex;
152 $finalIndices[$index['name']] = $index;
155 CRM_Core_BAO_SchemaHandler
::addIndexSignature(self
::getTableForClass($class), $finalIndices);
156 return $finalIndices;
161 * Mapping from brief-names to class-names.
162 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
164 public static function daoToClass() {
166 return self
::$daoToClass;
171 * Mapping from table-names to class-names.
172 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
174 public static function getCoreTables() {
175 return self
::tables();
179 * Determine whether $tableName is a core table.
181 * @param string $tableName
184 public static function isCoreTable($tableName) {
185 return array_key_exists($tableName, self
::tables());
189 * Get the DAO for a BAO class.
191 * @param string $baoName
193 * @return string|CRM_Core_DAO
195 public static function getCanonicalClassName($baoName) {
196 return str_replace('_BAO_', '_DAO_', $baoName);
200 * Get the BAO for a DAO class.
202 * @param string $daoName
204 * @return string|CRM_Core_DAO
206 public static function getBAOClassName($daoName) {
207 $baoName = str_replace('_DAO_', '_BAO_', $daoName);
208 return class_exists($baoName) ?
$baoName : $daoName;
212 * Get a list of all DAO classes.
215 * List of class names.
217 public static function getClasses() {
218 return array_values(self
::daoToClass());
222 * Get the classname for the table.
224 * @param string $tableName
227 public static function getClassForTable($tableName) {
228 //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
229 if (CRM_Core_I18n
::isMultilingual()) {
231 $tableName = str_replace($dbLocale, '', $tableName);
233 return CRM_Utils_Array
::value($tableName, self
::tables());
237 * Given a brief-name, determine the full class-name.
239 * @param string $daoName
241 * @return string|NULL
242 * Ex: 'CRM_Contact_DAO_Contact'.
244 public static function getFullName($daoName) {
245 return CRM_Utils_Array
::value($daoName, self
::daoToClass());
249 * Given a full class-name, determine the brief-name.
251 * @param string $className
252 * Ex: 'CRM_Contact_DAO_Contact'.
253 * @return string|NULL
256 public static function getBriefName($className) {
257 return CRM_Utils_Array
::value($className, array_flip(self
::daoToClass()));
261 * @param string $className DAO or BAO name
262 * @return string|FALSE SQL table name
264 public static function getTableForClass($className) {
265 return array_search(self
::getCanonicalClassName($className),
270 * Convert the entity name into a table name.
272 * @param string $entityBriefName
274 * @return FALSE|string
276 public static function getTableForEntityName($entityBriefName) {
277 return self
::getTableForClass(self
::getFullName($entityBriefName));
281 * Reinitialise cache.
285 public static function reinitializeCache($fresh = FALSE) {
290 * (Quasi-Private) Do not call externally. For use by DAOs.
293 * Ex: 'CRM_Core_DAO_Address'.
294 * @param string $labelName
296 * @param bool $prefix
297 * @param array $foreignDAOs
300 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
301 // Bug-level compatibility -- or sane behavior?
302 $cacheKey = $dao . ':export';
303 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
305 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
307 $fields = $dao::fields();
309 foreach ($fields as $name => $field) {
310 if (!empty($field['export'])) {
312 $exports[$labelName] = & $fields[$name];
315 $exports[$name] = & $fields[$name];
320 foreach ($foreignDAOs as $foreignDAO) {
321 $exports = array_merge($exports, $foreignDAO::export(TRUE));
324 Civi
::$statics[__CLASS__
][$cacheKey] = $exports;
326 return Civi
::$statics[__CLASS__
][$cacheKey];
330 * (Quasi-Private) Do not call externally. For use by DAOs.
333 * Ex: 'CRM_Core_DAO_Address'.
334 * @param string $labelName
336 * @param bool $prefix
337 * @param array $foreignDAOs
340 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
341 // Bug-level compatibility -- or sane behavior?
342 $cacheKey = $dao . ':import';
343 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
345 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
347 $fields = $dao::fields();
349 foreach ($fields as $name => $field) {
350 if (!empty($field['import'])) {
352 $imports[$labelName] = & $fields[$name];
355 $imports[$name] = & $fields[$name];
360 foreach ($foreignDAOs as $foreignDAO) {
361 $imports = array_merge($imports, $foreignDAO::import(TRUE));
364 Civi
::$statics[__CLASS__
][$cacheKey] = $imports;
366 return Civi
::$statics[__CLASS__
][$cacheKey];
370 * (Quasi-Private) Do not call externally. For use by DAOs.
372 * Apply any third-party alterations to the `fields()`.
374 * @param string $className
375 * @param string $event
376 * @param mixed $values
378 public static function invoke($className, $event, &$values) {
380 if (isset(self
::$entityTypes[$className][$event])) {
381 foreach (self
::$entityTypes[$className][$event] as $filter) {
382 $args = [$className, &$values];
383 \Civi\Core\Resolver
::singleton()->call($filter, $args);