4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
36 class CRM_Core_DAO_AllCoreTables
{
38 private static $tables = NULL;
39 private static $daoToClass = NULL;
40 private static $entityTypes = NULL;
42 public static function init($fresh = FALSE) {
44 if ($init && !$fresh) {
47 Civi
::$statics[__CLASS__
] = array();
49 $file = preg_replace('/\.php$/', '.data.php', __FILE__
);
50 $entityTypes = require $file;
51 CRM_Utils_Hook
::entityTypes($entityTypes);
53 self
::$entityTypes = array();
54 self
::$tables = array();
55 self
::$daoToClass = array();
56 foreach ($entityTypes as $entityType) {
57 self
::registerEntityType(
61 isset($entityType['fields_callback']) ?
$entityType['fields_callback'] : NULL,
62 isset($entityType['links_callback']) ?
$entityType['links_callback'] : NULL
70 * (Quasi-Private) Do not call externally (except for unit-testing)
72 public static function registerEntityType($daoName, $className, $tableName, $fields_callback = NULL, $links_callback = NULL) {
73 self
::$daoToClass[$daoName] = $className;
74 self
::$tables[$tableName] = $className;
75 self
::$entityTypes[$className] = array(
77 'class' => $className,
78 'table' => $tableName,
79 'fields_callback' => $fields_callback,
80 'links_callback' => $links_callback,
86 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
88 public static function get() {
90 return self
::$entityTypes;
95 * List of SQL table names.
97 public static function tables() {
104 * Mapping from brief-names to class-names.
105 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
107 public static function daoToClass() {
109 return self
::$daoToClass;
114 * Mapping from table-names to class-names.
115 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
117 public static function getCoreTables() {
118 return self
::tables();
122 * Determine whether $tableName is a core table.
124 * @param string $tableName
127 public static function isCoreTable($tableName) {
128 return FALSE !== array_search($tableName, self
::tables());
131 public static function getCanonicalClassName($className) {
132 return str_replace('_BAO_', '_DAO_', $className);
137 * List of class names.
139 public static function getClasses() {
140 return array_values(self
::daoToClass());
143 public static function getClassForTable($tableName) {
144 //CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
145 if (CRM_Core_I18n
::isMultilingual()) {
147 $tableName = str_replace($dbLocale, '', $tableName);
149 return CRM_Utils_Array
::value($tableName, self
::tables());
153 * Given a brief-name, determine the full class-name.
155 * @param string $daoName
157 * @return string|NULL
158 * Ex: 'CRM_Contact_DAO_Contact'.
160 public static function getFullName($daoName) {
161 return CRM_Utils_Array
::value($daoName, self
::daoToClass());
165 * Given a full class-name, determine the brief-name.
167 * @param string $className
168 * Ex: 'CRM_Contact_DAO_Contact'.
169 * @return string|NULL
172 public static function getBriefName($className) {
173 return CRM_Utils_Array
::value($className, array_flip(self
::daoToClass()));
177 * @param string $className DAO or BAO name
178 * @return string|FALSE SQL table name
180 public static function getTableForClass($className) {
181 return array_search(self
::getCanonicalClassName($className),
185 public static function reinitializeCache($fresh = FALSE) {
190 * (Quasi-Private) Do not call externally. For use by DAOs.
193 * Ex: 'CRM_Core_DAO_Address'.
194 * @param string $labelName
196 * @param bool $prefix
197 * @param array $foreignDAOs
200 public static function getExports($dao, $labelName, $prefix, $foreignDAOs) {
201 // Bug-level compatibility -- or sane behavior?
202 $cacheKey = $dao . ':export';
203 // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export');
205 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
207 $fields = $dao::fields();
209 foreach($fields as $name => $field) {
210 if (CRM_Utils_Array
::value('export', $field)) {
212 $exports[$labelName] = & $fields[$name];
214 $exports[$name] = & $fields[$name];
219 foreach ($foreignDAOs as $foreignDAO) {
220 $exports = array_merge($exports, $foreignDAO::export(TRUE));
223 Civi
::$statics[__CLASS__
][$cacheKey] = $exports;
225 return Civi
::$statics[__CLASS__
][$cacheKey];
229 * (Quasi-Private) Do not call externally. For use by DAOs.
232 * Ex: 'CRM_Core_DAO_Address'.
233 * @param string $labelName
235 * @param bool $prefix
236 * @param array $foreignDAOs
239 public static function getImports($dao, $labelName, $prefix, $foreignDAOs) {
240 // Bug-level compatibility -- or sane behavior?
241 $cacheKey = $dao . ':import';
242 // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import');
244 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
246 $fields = $dao::fields();
248 foreach($fields as $name => $field) {
249 if (CRM_Utils_Array
::value('import', $field)) {
251 $imports[$labelName] = & $fields[$name];
253 $imports[$name] = & $fields[$name];
258 foreach ($foreignDAOs as $foreignDAO) {
259 $imports = array_merge($imports, $foreignDAO::import(TRUE));
262 Civi
::$statics[__CLASS__
][$cacheKey] = $imports;
264 return Civi
::$statics[__CLASS__
][$cacheKey];
268 * (Quasi-Private) Do not call externally. For use by DAOs.
270 * Apply any third-party alterations to the `fields()`.
272 * @param string $className
273 * @param string $event
274 * @param mixed $values
276 public static function invoke($className, $event, &$values) {
278 if (isset(self
::$entityTypes[$className][$event])) {
279 foreach (self
::$entityTypes[$className][$event] as $filter) {
280 $args = array($className, &$values);
281 \Civi\Core\Resolver
::singleton()->call($filter, $args);