Merge pull request #9632 from yashodha/CRM-19795
[civicrm-core.git] / CRM / Core / DAO / AllCoreTables.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2017 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2017
33 * $Id$
34 *
35 */
36 class CRM_Core_DAO_AllCoreTables {
37
38 private static $tables = NULL;
39 private static $daoToClass = NULL;
40 private static $entityTypes = NULL;
41
42 public static function init($fresh = FALSE) {
43 static $init = FALSE;
44 if ($init && !$fresh) {
45 return;
46 }
47 Civi::$statics[__CLASS__] = array();
48
49 $file = preg_replace('/\.php$/', '.data.php', __FILE__);
50 $entityTypes = require $file;
51 CRM_Utils_Hook::entityTypes($entityTypes);
52
53 self::$entityTypes = array();
54 self::$tables = array();
55 self::$daoToClass = array();
56 foreach ($entityTypes as $entityType) {
57 self::registerEntityType(
58 $entityType['name'],
59 $entityType['class'],
60 $entityType['table'],
61 isset($entityType['fields_callback']) ? $entityType['fields_callback'] : NULL,
62 isset($entityType['links_callback']) ? $entityType['links_callback'] : NULL
63 );
64 }
65
66 $init = TRUE;
67 }
68
69 /**
70 * (Quasi-Private) Do not call externally (except for unit-testing)
71 */
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(
76 'name' => $daoName,
77 'class' => $className,
78 'table' => $tableName,
79 'fields_callback' => $fields_callback,
80 'links_callback' => $links_callback,
81 );
82 }
83
84 /**
85 * @return array
86 * Ex: $result['CRM_Contact_DAO_Contact']['table'] == 'civicrm_contact';
87 */
88 public static function get() {
89 self::init();
90 return self::$entityTypes;
91 }
92
93 /**
94 * @return array
95 * List of SQL table names.
96 */
97 public static function tables() {
98 self::init();
99 return self::$tables;
100 }
101
102 /**
103 * @return array
104 * Mapping from brief-names to class-names.
105 * Ex: $result['Contact'] == 'CRM_Contact_DAO_Contact'.
106 */
107 public static function daoToClass() {
108 self::init();
109 return self::$daoToClass;
110 }
111
112 /**
113 * @return array
114 * Mapping from table-names to class-names.
115 * Ex: $result['civicrm_contact'] == 'CRM_Contact_DAO_Contact'.
116 */
117 public static function getCoreTables() {
118 return self::tables();
119 }
120
121 /**
122 * Determine whether $tableName is a core table.
123 *
124 * @param string $tableName
125 * @return bool
126 */
127 public static function isCoreTable($tableName) {
128 return FALSE !== array_search($tableName, self::tables());
129 }
130
131 public static function getCanonicalClassName($className) {
132 return str_replace('_BAO_', '_DAO_', $className);
133 }
134
135 /**
136 * @return array
137 * List of class names.
138 */
139 public static function getClasses() {
140 return array_values(self::daoToClass());
141 }
142
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()) {
146 global $dbLocale;
147 $tableName = str_replace($dbLocale, '', $tableName);
148 }
149 return CRM_Utils_Array::value($tableName, self::tables());
150 }
151
152 /**
153 * Given a brief-name, determine the full class-name.
154 *
155 * @param string $daoName
156 * Ex: 'Contact'.
157 * @return string|NULL
158 * Ex: 'CRM_Contact_DAO_Contact'.
159 */
160 public static function getFullName($daoName) {
161 return CRM_Utils_Array::value($daoName, self::daoToClass());
162 }
163
164 /**
165 * Given a full class-name, determine the brief-name.
166 *
167 * @param string $className
168 * Ex: 'CRM_Contact_DAO_Contact'.
169 * @return string|NULL
170 * Ex: 'Contact'.
171 */
172 public static function getBriefName($className) {
173 return CRM_Utils_Array::value($className, array_flip(self::daoToClass()));
174 }
175
176 /**
177 * @param string $className DAO or BAO name
178 * @return string|FALSE SQL table name
179 */
180 public static function getTableForClass($className) {
181 return array_search(self::getCanonicalClassName($className),
182 self::tables());
183 }
184
185 public static function reinitializeCache($fresh = FALSE) {
186 self::init($fresh);
187 }
188
189 /**
190 * (Quasi-Private) Do not call externally. For use by DAOs.
191 *
192 * @param string $dao
193 * Ex: 'CRM_Core_DAO_Address'.
194 * @param string $labelName
195 * Ex: 'address'.
196 * @param bool $prefix
197 * @param array $foreignDAOs
198 * @return array
199 */
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');
204
205 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
206 $exports = array();
207 $fields = $dao::fields();
208
209 foreach($fields as $name => $field) {
210 if (CRM_Utils_Array::value('export', $field)) {
211 if ($prefix) {
212 $exports[$labelName] = & $fields[$name];
213 } else {
214 $exports[$name] = & $fields[$name];
215 }
216 }
217 }
218
219 foreach ($foreignDAOs as $foreignDAO) {
220 $exports = array_merge($exports, $foreignDAO::export(TRUE));
221 }
222
223 Civi::$statics[__CLASS__][$cacheKey] = $exports;
224 }
225 return Civi::$statics[__CLASS__][$cacheKey];
226 }
227
228 /**
229 * (Quasi-Private) Do not call externally. For use by DAOs.
230 *
231 * @param string $dao
232 * Ex: 'CRM_Core_DAO_Address'.
233 * @param string $labelName
234 * Ex: 'address'.
235 * @param bool $prefix
236 * @param array $foreignDAOs
237 * @return array
238 */
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');
243
244 if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
245 $imports = array();
246 $fields = $dao::fields();
247
248 foreach($fields as $name => $field) {
249 if (CRM_Utils_Array::value('import', $field)) {
250 if ($prefix) {
251 $imports[$labelName] = & $fields[$name];
252 } else {
253 $imports[$name] = & $fields[$name];
254 }
255 }
256 }
257
258 foreach ($foreignDAOs as $foreignDAO) {
259 $imports = array_merge($imports, $foreignDAO::import(TRUE));
260 }
261
262 Civi::$statics[__CLASS__][$cacheKey] = $imports;
263 }
264 return Civi::$statics[__CLASS__][$cacheKey];
265 }
266
267 /**
268 * (Quasi-Private) Do not call externally. For use by DAOs.
269 *
270 * Apply any third-party alterations to the `fields()`.
271 *
272 * @param string $className
273 * @param string $event
274 * @param mixed $values
275 */
276 public static function invoke($className, $event, &$values) {
277 self::init();
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);
282 }
283 }
284 }
285
286 }