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;
113 * Mapping from brief-names to class-names.
114 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
116 public static function daoToClass() {
118 return self
::$daoToClass;
123 * Mapping from table-names to class-names.
124 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
126 public static function getCoreTables() {
127 return self
::tables();
131 * Determine whether $tableName is a core table.
133 * @param string $tableName
136 public static function isCoreTable($tableName) {
137 return FALSE !== array_search($tableName, self
::tables());
141 * Get the DAO for the class.
143 * @param string $className
147 public static function getCanonicalClassName($className) {
148 return str_replace('_BAO_', '_DAO_', $className);
152 * Get a list of all DAO classes.
155 * List of class names.
157 public static function getClasses() {
158 return array_values(self
::daoToClass());
162 * Get the classname for the table.
164 * @param string $tableName
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()) {
171 $tableName = str_replace($dbLocale, '', $tableName);
173 return CRM_Utils_Array
::value($tableName, self
::tables());
177 * Given a brief-name, determine the full class-name.
179 * @param string $daoName
181 * @return string|NULL
182 * Ex: 'CRM_Contact_DAO_Contact'.
184 public static function getFullName($daoName) {
185 return CRM_Utils_Array
::value($daoName, self
::daoToClass());
189 * Given a full class-name, determine the brief-name.
191 * @param string $className
192 * Ex: 'CRM_Contact_DAO_Contact'.
193 * @return string|NULL
196 public static function getBriefName($className) {
197 return CRM_Utils_Array
::value($className, array_flip(self
::daoToClass()));
201 * @param string $className DAO or BAO name
202 * @return string|FALSE SQL table name
204 public static function getTableForClass($className) {
205 return array_search(self
::getCanonicalClassName($className),
210 * Reinitialise cache.
214 public static function reinitializeCache($fresh = FALSE) {
219 * (Quasi-Private) Do not call externally. For use by DAOs.
222 * Ex: 'CRM_Core_DAO_Address'.
223 * @param string $labelName
225 * @param bool $prefix
226 * @param array $foreignDAOs
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');
234 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
236 $fields = $dao::fields();
238 foreach ($fields as $name => $field) {
239 if (CRM_Utils_Array
::value('export', $field)) {
241 $exports[$labelName] = & $fields[$name];
244 $exports[$name] = & $fields[$name];
249 foreach ($foreignDAOs as $foreignDAO) {
250 $exports = array_merge($exports, $foreignDAO::export(TRUE));
253 Civi
::$statics[__CLASS__
][$cacheKey] = $exports;
255 return Civi
::$statics[__CLASS__
][$cacheKey];
259 * (Quasi-Private) Do not call externally. For use by DAOs.
262 * Ex: 'CRM_Core_DAO_Address'.
263 * @param string $labelName
265 * @param bool $prefix
266 * @param array $foreignDAOs
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');
274 if (!isset(Civi
::$statics[__CLASS__
][$cacheKey])) {
276 $fields = $dao::fields();
278 foreach ($fields as $name => $field) {
279 if (CRM_Utils_Array
::value('import', $field)) {
281 $imports[$labelName] = & $fields[$name];
284 $imports[$name] = & $fields[$name];
289 foreach ($foreignDAOs as $foreignDAO) {
290 $imports = array_merge($imports, $foreignDAO::import(TRUE));
293 Civi
::$statics[__CLASS__
][$cacheKey] = $imports;
295 return Civi
::$statics[__CLASS__
][$cacheKey];
299 * (Quasi-Private) Do not call externally. For use by DAOs.
301 * Apply any third-party alterations to the `fields()`.
303 * @param string $className
304 * @param string $event
305 * @param mixed $values
307 public static function invoke($className, $event, &$values) {
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);