4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2017 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2017
34 class CRM_Core_DAO_AllCoreTables
{
36 private static $tables = NULL;
37 private static $daoToClass = NULL;
38 private static $entityTypes = NULL;
45 public static function init($fresh = FALSE) {
47 if ($init && !$fresh) {
50 Civi
::$statics[__CLASS__
] = array();
52 $file = preg_replace('/\.php$/', '.data.php', __FILE__
);
53 $entityTypes = require $file;
54 CRM_Utils_Hook
::entityTypes($entityTypes);
56 self
::$entityTypes = array();
57 self
::$tables = array();
58 self
::$daoToClass = array();
59 foreach ($entityTypes as $entityType) {
60 self
::registerEntityType(
64 isset($entityType['fields_callback']) ?
$entityType['fields_callback'] : NULL,
65 isset($entityType['links_callback']) ?
$entityType['links_callback'] : NULL
73 * (Quasi-Private) Do not call externally (except for unit-testing)
75 * @param string $daoName
76 * @param string $className
77 * @param string $tableName
78 * @param string $fields_callback
79 * @param string $links_callback
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(
86 'class' => $className,
87 'table' => $tableName,
88 'fields_callback' => $fields_callback,
89 'links_callback' => $links_callback,
95 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
97 public static function get() {
99 return self
::$entityTypes;
104 * List of SQL table names.
106 public static function tables() {
108 return self
::$tables;
115 public static function indices($localize = TRUE) {
118 foreach (self
::$daoToClass as $class) {
119 if (is_callable(array($class, 'indices'))) {
120 $indices[$class::getTableName()] = $class::indices($localize);
127 * Modify indices to account for localization options.
129 * @param CRM_Core_DAO $class DAO class
130 * @param array $originalIndices index definitions before localization
133 * index definitions after localization
135 public static function multilingualize($class, $originalIndices) {
136 $domain = new CRM_Core_DAO_Domain();
138 $locales = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $domain->locales
);
139 if (CRM_Utils_System
::isNull($locales)) {
140 return $originalIndices;
142 $classFields = $class::fields();
144 $finalIndices = array();
145 foreach ($originalIndices as $index) {
146 if ($index['localizable']) {
147 foreach ($locales as $locale) {
148 $localIndex = $index;
149 $localIndex['name'] .= "_" . $locale;
151 foreach ($localIndex['field'] as $field) {
152 $baseField = explode('(', $field);
153 if ($classFields[$baseField[0]]['localizable']) {
154 // field name may have eg (3) at end for prefix length
155 // last_name => last_name_fr_FR
156 // last_name(3) => last_name_fr_FR(3)
157 $fields[] = preg_replace('/^([^(]+)(\(\d+\)|)$/', '${1}_' . $locale . '${2}', $field);
163 $localIndex['field'] = $fields;
164 $finalIndices[$localIndex['name']] = $localIndex;
168 $finalIndices[$index['name']] = $index;
171 CRM_Core_BAO_SchemaHandler
::addIndexSignature(self
::getTableForClass($class), $finalIndices);
172 return $finalIndices;
177 * Mapping from brief-names to class-names.
178 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
180 public static function daoToClass() {
182 return self
::$daoToClass;
187 * Mapping from table-names to class-names.
188 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
190 public static function getCoreTables() {
191 return self
::tables();
195 * Determine whether $tableName is a core table.
197 * @param string $tableName
200 public static function isCoreTable($tableName) {
201 return FALSE !== array_search($tableName, self
::tables());
205 * Get the DAO for the class.
207 * @param string $className
211 public static function getCanonicalClassName($className) {
212 return str_replace('_BAO_', '_DAO_', $className);
216 * Get a list of all DAO classes.
219 * List of class names.
221 public static function getClasses() {
222 return array_values(self
::daoToClass());
226 * Get the classname for the table.
228 * @param string $tableName
231 public static function getClassForTable($tableName) {
232 //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
233 if (CRM_Core_I18n
::isMultilingual()) {
235 $tableName = str_replace($dbLocale, '', $tableName);
237 return CRM_Utils_Array
::value($tableName, self
::tables());
241 * Given a brief-name, determine the full class-name.
243 * @param string $daoName
245 * @return string|NULL
246 * Ex: 'CRM_Contact_DAO_Contact'.
248 public static function getFullName($daoName) {
249 return CRM_Utils_Array
::value($daoName, self
::daoToClass());
253 * Given a full class-name, determine the brief-name.
255 * @param string $className
256 * Ex: 'CRM_Contact_DAO_Contact'.
257 * @return string|NULL
260 public static function getBriefName($className) {
261 return CRM_Utils_Array
::value($className, array_flip(self
::daoToClass()));
265 * @param string $className DAO or BAO name
266 * @return string|FALSE SQL table name
268 public static function getTableForClass($className) {
269 return array_search(self
::getCanonicalClassName($className),
274 * Reinitialise cache.
278 public static function reinitializeCache($fresh = FALSE) {
283 * (Quasi-Private) Do not call externally. For use by DAOs.
286 * Ex: 'CRM_Core_DAO_Address'.
287 * @param string $labelName
289 * @param bool $prefix
290 * @param array $foreignDAOs
293 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
294 // Bug-level compatibility -- or sane behavior?
295 $cacheKey = $dao . ':export';
296 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
298 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
300 $fields = $dao::fields();
302 foreach ($fields as $name => $field) {
303 if (CRM_Utils_Array
::value('export', $field)) {
305 $exports[$labelName] = & $fields[$name];
308 $exports[$name] = & $fields[$name];
313 foreach ($foreignDAOs as $foreignDAO) {
314 $exports = array_merge($exports, $foreignDAO::export(TRUE));
317 Civi
::$statics[__CLASS__
][$cacheKey] = $exports;
319 return Civi
::$statics[__CLASS__
][$cacheKey];
323 * (Quasi-Private) Do not call externally. For use by DAOs.
326 * Ex: 'CRM_Core_DAO_Address'.
327 * @param string $labelName
329 * @param bool $prefix
330 * @param array $foreignDAOs
333 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
334 // Bug-level compatibility -- or sane behavior?
335 $cacheKey = $dao . ':import';
336 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
338 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
340 $fields = $dao::fields();
342 foreach ($fields as $name => $field) {
343 if (CRM_Utils_Array
::value('import', $field)) {
345 $imports[$labelName] = & $fields[$name];
348 $imports[$name] = & $fields[$name];
353 foreach ($foreignDAOs as $foreignDAO) {
354 $imports = array_merge($imports, $foreignDAO::import(TRUE));
357 Civi
::$statics[__CLASS__
][$cacheKey] = $imports;
359 return Civi
::$statics[__CLASS__
][$cacheKey];
363 * (Quasi-Private) Do not call externally. For use by DAOs.
365 * Apply any third-party alterations to the `fields()`.
367 * @param string $className
368 * @param string $event
369 * @param mixed $values
371 public static function invoke($className, $event, &$values) {
373 if (isset(self
::$entityTypes[$className][$event])) {
374 foreach (self
::$entityTypes[$className][$event] as $filter) {
375 $args = array($className, &$values);
376 \Civi\Core\Resolver
::singleton()->call($filter, $args);