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 * Convert possibly underscore separated words to camel case with special handling for 'UF'
213 * e.g membership_payment returns MembershipPayment
215 * @param string $name
216 * @param bool $legacyV3
219 public static function convertEntityNameToCamel(string $name, $legacyV3 = FALSE): string {
220 // This map only applies to APIv3
227 if ($legacyV3 && isset($map[$name])) {
231 $fragments = explode('_', $name);
232 foreach ($fragments as & $fragment) {
233 $fragment = ucfirst($fragment);
234 // Special case: UFGroup, UFJoin, UFMatch, UFField (if passed in without underscores)
235 if (strpos($fragment, 'Uf') === 0 && strlen($name) > 2) {
236 $fragment = 'UF' . ucfirst(substr($fragment, 2));
239 // Special case: UFGroup, UFJoin, UFMatch, UFField (if passed in underscore-separated)
240 if ($fragments[0] === 'Uf') {
241 $fragments[0] = 'UF';
243 return implode('', $fragments);
247 * Convert CamelCase to snake_case, with special handling for some entity names.
249 * Eg. Activity returns activity
250 * UFGroup returns uf_group
251 * OptionValue returns option_value
253 * @param string $name
257 public static function convertEntityNameToLower(string $name): string {
258 if ($name === strtolower($name)) {
261 if ($name === 'PCP' ||
$name === 'IM' ||
$name === 'ACL') {
262 return strtolower($name);
264 return strtolower(ltrim(str_replace('U_F',
266 // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
267 preg_replace('/(?=[A-Z])/', '_$0', $name)
272 * Get a list of all DAO classes.
275 * List of class names.
277 public static function getClasses() {
278 return array_values(self
::daoToClass());
282 * Get the classname for the table.
284 * @param string $tableName
287 public static function getClassForTable($tableName) {
288 //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
289 if (CRM_Core_I18n
::isMultilingual()) {
291 $tableName = str_replace($dbLocale, '', $tableName);
293 return CRM_Utils_Array
::value($tableName, self
::tables());
297 * Given a brief-name, determine the full class-name.
299 * @param string $daoName
301 * @return string|NULL
302 * Ex: 'CRM_Contact_DAO_Contact'.
304 public static function getFullName($daoName) {
305 return CRM_Utils_Array
::value($daoName, self
::daoToClass());
309 * Given a full class-name, determine the brief-name.
311 * @param string $className
312 * Ex: 'CRM_Contact_DAO_Contact'.
313 * @return string|NULL
316 public static function getBriefName($className) {
317 return CRM_Utils_Array
::value($className, array_flip(self
::daoToClass()));
321 * @param string $className DAO or BAO name
322 * @return string|FALSE SQL table name
324 public static function getTableForClass($className) {
325 return array_search(self
::getCanonicalClassName($className),
330 * Convert the entity name into a table name.
332 * @param string $entityBriefName
334 * @return FALSE|string
336 public static function getTableForEntityName($entityBriefName) {
337 return self
::getTableForClass(self
::getFullName($entityBriefName));
341 * Reinitialise cache.
345 public static function reinitializeCache($fresh = FALSE) {
350 * (Quasi-Private) Do not call externally. For use by DAOs.
353 * Ex: 'CRM_Core_DAO_Address'.
354 * @param string $labelName
356 * @param bool $prefix
357 * @param array $foreignDAOs
360 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
361 // Bug-level compatibility -- or sane behavior?
362 $cacheKey = $dao . ':export';
363 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
365 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
367 $fields = $dao::fields();
369 foreach ($fields as $name => $field) {
370 if (!empty($field['export'])) {
372 $exports[$labelName] = & $fields[$name];
375 $exports[$name] = & $fields[$name];
380 foreach ($foreignDAOs as $foreignDAO) {
381 $exports = array_merge($exports, $foreignDAO::export(TRUE));
384 Civi
::$statics[__CLASS__
][$cacheKey] = $exports;
386 return Civi
::$statics[__CLASS__
][$cacheKey];
390 * (Quasi-Private) Do not call externally. For use by DAOs.
393 * Ex: 'CRM_Core_DAO_Address'.
394 * @param string $labelName
396 * @param bool $prefix
397 * @param array $foreignDAOs
400 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
401 // Bug-level compatibility -- or sane behavior?
402 $cacheKey = $dao . ':import';
403 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
405 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
407 $fields = $dao::fields();
409 foreach ($fields as $name => $field) {
410 if (!empty($field['import'])) {
412 $imports[$labelName] = & $fields[$name];
415 $imports[$name] = & $fields[$name];
420 foreach ($foreignDAOs as $foreignDAO) {
421 $imports = array_merge($imports, $foreignDAO::import(TRUE));
424 Civi
::$statics[__CLASS__
][$cacheKey] = $imports;
426 return Civi
::$statics[__CLASS__
][$cacheKey];
430 * (Quasi-Private) Do not call externally. For use by DAOs.
432 * Apply any third-party alterations to the `fields()`.
434 * @param string $className
435 * @param string $event
436 * @param mixed $values
438 public static function invoke($className, $event, &$values) {
440 if (isset(self
::$entityTypes[$className][$event])) {
441 foreach (self
::$entityTypes[$className][$event] as $filter) {
442 $args = [$className, &$values];
443 \Civi\Core\Resolver
::singleton()->call($filter, $args);