Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * Component stores all the static and dynamic information of the various | |
30 | * CiviCRM components | |
31 | * | |
32 | * @package CRM | |
6b83d5bd | 33 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
34 | * $Id$ |
35 | * | |
36 | */ | |
37 | class CRM_Core_Component { | |
38 | ||
c490a46a CW |
39 | /** |
40 | * End part (filename) of the component information class'es name | |
41 | * that needs to be present in components main directory. | |
42 | */ | |
7da04cde | 43 | const COMPONENT_INFO_CLASS = 'Info'; |
6a488035 | 44 | |
518fa0ee SL |
45 | /** |
46 | * @var array | |
47 | */ | |
48 | public static $_contactSubTypes = NULL; | |
6a488035 | 49 | |
a0ee3941 EM |
50 | /** |
51 | * @param bool $force | |
52 | * | |
9a13600c | 53 | * @return CRM_Core_Component_Info[] |
a0ee3941 | 54 | */ |
6a488035 | 55 | private static function &_info($force = FALSE) { |
edbcbd96 | 56 | if (!isset(Civi::$statics[__CLASS__]['info'])|| $force) { |
be2fb01f CW |
57 | Civi::$statics[__CLASS__]['info'] = []; |
58 | $c = []; | |
6a488035 TO |
59 | |
60 | $config = CRM_Core_Config::singleton(); | |
61 | $c = self::getComponents(); | |
62 | ||
63 | foreach ($c as $name => $comp) { | |
64 | if (in_array($name, $config->enableComponents)) { | |
edbcbd96 | 65 | Civi::$statics[__CLASS__]['info'][$name] = $comp; |
6a488035 TO |
66 | } |
67 | } | |
68 | } | |
69 | ||
edbcbd96 | 70 | return Civi::$statics[__CLASS__]['info']; |
6a488035 TO |
71 | } |
72 | ||
a0ee3941 | 73 | /** |
100fef9d | 74 | * @param string $name |
a0ee3941 EM |
75 | * @param null $attribute |
76 | * | |
77 | * @return mixed | |
78 | */ | |
00be9182 | 79 | public static function get($name, $attribute = NULL) { |
6a488035 TO |
80 | $comp = CRM_Utils_Array::value($name, self::_info()); |
81 | if ($attribute) { | |
82 | return CRM_Utils_Array::value($attribute, $comp->info); | |
83 | } | |
84 | return $comp; | |
85 | } | |
86 | ||
a0ee3941 EM |
87 | /** |
88 | * @param bool $force | |
89 | * | |
9a13600c | 90 | * @return CRM_Core_Component_Info[] |
a0ee3941 EM |
91 | * @throws Exception |
92 | */ | |
6a488035 | 93 | public static function &getComponents($force = FALSE) { |
edbcbd96 | 94 | if (!isset(Civi::$statics[__CLASS__]['all']) || $force) { |
be2fb01f | 95 | Civi::$statics[__CLASS__]['all'] = []; |
6a488035 TO |
96 | |
97 | $cr = new CRM_Core_DAO_Component(); | |
98 | $cr->find(FALSE); | |
99 | while ($cr->fetch()) { | |
100 | $infoClass = $cr->namespace . '_' . self::COMPONENT_INFO_CLASS; | |
4d669903 | 101 | $infoClassFile = str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php'; |
102 | if (!CRM_Utils_File::isIncludable($infoClassFile)) { | |
103 | continue; | |
104 | } | |
105 | require_once $infoClassFile; | |
6a488035 TO |
106 | $infoObject = new $infoClass($cr->name, $cr->namespace, $cr->id); |
107 | if ($infoObject->info['name'] !== $cr->name) { | |
108 | CRM_Core_Error::fatal("There is a discrepancy between name in component registry and in info file ({$cr->name})."); | |
109 | } | |
edbcbd96 | 110 | Civi::$statics[__CLASS__]['all'][$cr->name] = $infoObject; |
6a488035 TO |
111 | unset($infoObject); |
112 | } | |
113 | } | |
114 | ||
edbcbd96 | 115 | return Civi::$statics[__CLASS__]['all']; |
6a488035 TO |
116 | } |
117 | ||
2ce81580 TO |
118 | /** |
119 | * @return array | |
120 | * Array(string $name => int $id). | |
121 | */ | |
122 | public static function &getComponentIDs() { | |
be2fb01f | 123 | $componentIDs = []; |
2ce81580 TO |
124 | |
125 | $cr = new CRM_Core_DAO_Component(); | |
126 | $cr->find(FALSE); | |
127 | while ($cr->fetch()) { | |
128 | $componentIDs[$cr->name] = $cr->id; | |
129 | } | |
130 | ||
131 | return $componentIDs; | |
132 | } | |
133 | ||
a0ee3941 EM |
134 | /** |
135 | * @param bool $force | |
136 | * | |
9a13600c | 137 | * @return CRM_Core_Component_Info[] |
a0ee3941 | 138 | */ |
518fa0ee | 139 | public static function &getEnabledComponents($force = FALSE) { |
6a488035 TO |
140 | return self::_info($force); |
141 | } | |
2dd1b730 | 142 | |
518fa0ee | 143 | public static function flushEnabledComponents() { |
3ea36d17 | 144 | unset(Civi::$statics[__CLASS__]); |
3d0e24ec | 145 | } |
6a488035 | 146 | |
a0ee3941 EM |
147 | /** |
148 | * @param bool $translated | |
149 | * | |
150 | * @return array | |
151 | */ | |
6a488035 TO |
152 | public static function &getNames($translated = FALSE) { |
153 | $allComponents = self::getComponents(); | |
154 | ||
be2fb01f | 155 | $names = []; |
6a488035 TO |
156 | foreach ($allComponents as $name => $comp) { |
157 | if ($translated) { | |
158 | $names[$comp->componentID] = $comp->info['translatedName']; | |
159 | } | |
160 | else { | |
161 | $names[$comp->componentID] = $name; | |
162 | } | |
163 | } | |
164 | return $names; | |
165 | } | |
166 | ||
a0ee3941 EM |
167 | /** |
168 | * @param $args | |
169 | * @param $type | |
170 | * | |
171 | * @return bool | |
172 | */ | |
00be9182 | 173 | public static function invoke(&$args, $type) { |
6a488035 TO |
174 | $info = self::_info(); |
175 | $config = CRM_Core_Config::singleton(); | |
176 | ||
177 | $firstArg = CRM_Utils_Array::value(1, $args, ''); | |
178 | $secondArg = CRM_Utils_Array::value(2, $args, ''); | |
179 | foreach ($info as $name => $comp) { | |
180 | if (in_array($name, $config->enableComponents) && | |
181 | (($comp->info['url'] === $firstArg && $type == 'main') || | |
182 | ($comp->info['url'] === $secondArg && $type == 'admin') | |
183 | ) | |
184 | ) { | |
185 | if ($type == 'main') { | |
186 | // also set the smarty variables to the current component | |
187 | $template = CRM_Core_Smarty::singleton(); | |
188 | $template->assign('activeComponent', $name); | |
a7488080 | 189 | if (!empty($comp->info[$name]['formTpl'])) { |
6a488035 TO |
190 | $template->assign('formTpl', $comp->info[$name]['formTpl']); |
191 | } | |
a7488080 | 192 | if (!empty($comp->info[$name]['css'])) { |
6a488035 TO |
193 | $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>"; |
194 | CRM_Utils_System::addHTMLHead($styleSheet); | |
195 | } | |
196 | } | |
197 | $inv = $comp->getInvokeObject(); | |
198 | $inv->$type($args); | |
199 | return TRUE; | |
200 | } | |
201 | } | |
202 | return FALSE; | |
203 | } | |
204 | ||
a0ee3941 EM |
205 | /** |
206 | * @return array | |
207 | */ | |
00be9182 | 208 | public static function xmlMenu() { |
6a488035 TO |
209 | |
210 | // lets build the menu for all components | |
211 | $info = self::getComponents(TRUE); | |
212 | ||
be2fb01f | 213 | $files = []; |
6a488035 TO |
214 | foreach ($info as $name => $comp) { |
215 | $files = array_merge($files, | |
216 | $comp->menuFiles() | |
217 | ); | |
218 | } | |
219 | ||
220 | return $files; | |
221 | } | |
222 | ||
a0ee3941 EM |
223 | /** |
224 | * @return array | |
225 | */ | |
00be9182 | 226 | public static function &menu() { |
6a488035 | 227 | $info = self::_info(); |
be2fb01f | 228 | $items = []; |
6a488035 TO |
229 | foreach ($info as $name => $comp) { |
230 | $mnu = $comp->getMenuObject(); | |
231 | ||
232 | $ret = $mnu->permissioned(); | |
233 | $items = array_merge($items, $ret); | |
234 | ||
235 | $ret = $mnu->main($task); | |
236 | $items = array_merge($items, $ret); | |
237 | } | |
238 | return $items; | |
239 | } | |
240 | ||
a0ee3941 | 241 | /** |
100fef9d | 242 | * @param string $componentName |
a0ee3941 EM |
243 | * |
244 | * @return mixed | |
245 | */ | |
00be9182 | 246 | public static function getComponentID($componentName) { |
6a488035 | 247 | $info = self::_info(); |
7b60d5b9 | 248 | if (!empty($info[$componentName])) { |
249 | return $info[$componentName]->componentID; | |
250 | } | |
251 | else { | |
252 | return; | |
253 | } | |
6a488035 TO |
254 | } |
255 | ||
a0ee3941 | 256 | /** |
100fef9d | 257 | * @param int $componentID |
a0ee3941 EM |
258 | * |
259 | * @return int|null|string | |
260 | */ | |
00be9182 | 261 | public static function getComponentName($componentID) { |
6a488035 TO |
262 | $info = self::_info(); |
263 | ||
264 | $componentName = NULL; | |
265 | foreach ($info as $compName => $component) { | |
266 | if ($component->componentID == $componentID) { | |
267 | $componentName = $compName; | |
268 | break; | |
269 | } | |
270 | } | |
271 | ||
272 | return $componentName; | |
273 | } | |
274 | ||
a0ee3941 EM |
275 | /** |
276 | * @return array | |
277 | */ | |
5837835b | 278 | public static function &getQueryFields($checkPermission = TRUE) { |
6a488035 | 279 | $info = self::_info(); |
be2fb01f | 280 | $fields = []; |
6a488035 TO |
281 | foreach ($info as $name => $comp) { |
282 | if ($comp->usesSearch()) { | |
353ffa53 | 283 | $bqr = $comp->getBAOQueryObject(); |
5837835b | 284 | $flds = $bqr->getFields($checkPermission); |
6a488035 TO |
285 | $fields = array_merge($fields, $flds); |
286 | } | |
287 | } | |
288 | return $fields; | |
289 | } | |
290 | ||
a0ee3941 EM |
291 | /** |
292 | * @param $query | |
100fef9d | 293 | * @param string $fnName |
a0ee3941 | 294 | */ |
00be9182 | 295 | public static function alterQuery(&$query, $fnName) { |
6a488035 TO |
296 | $info = self::_info(); |
297 | ||
298 | foreach ($info as $name => $comp) { | |
299 | if ($comp->usesSearch()) { | |
300 | $bqr = $comp->getBAOQueryObject(); | |
301 | $bqr->$fnName($query); | |
302 | } | |
303 | } | |
304 | } | |
305 | ||
a0ee3941 | 306 | /** |
100fef9d | 307 | * @param string $fieldName |
a0ee3941 EM |
308 | * @param $mode |
309 | * @param $side | |
310 | * | |
311 | * @return null | |
312 | */ | |
00be9182 | 313 | public static function from($fieldName, $mode, $side) { |
6a488035 TO |
314 | $info = self::_info(); |
315 | ||
316 | $from = NULL; | |
317 | foreach ($info as $name => $comp) { | |
318 | if ($comp->usesSearch()) { | |
319 | $bqr = $comp->getBAOQueryObject(); | |
320 | $from = $bqr->from($fieldName, $mode, $side); | |
321 | if ($from) { | |
322 | return $from; | |
323 | } | |
324 | } | |
325 | } | |
326 | return $from; | |
327 | } | |
328 | ||
a0ee3941 EM |
329 | /** |
330 | * @param $mode | |
331 | * @param bool $includeCustomFields | |
332 | * | |
333 | * @return null | |
334 | */ | |
317fceb4 | 335 | public static function &defaultReturnProperties( |
f9f40af3 | 336 | $mode, |
6a488035 TO |
337 | $includeCustomFields = TRUE |
338 | ) { | |
339 | $info = self::_info(); | |
340 | ||
341 | $properties = NULL; | |
342 | foreach ($info as $name => $comp) { | |
343 | if ($comp->usesSearch()) { | |
344 | $bqr = $comp->getBAOQueryObject(); | |
345 | $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields); | |
346 | if ($properties) { | |
347 | return $properties; | |
348 | } | |
349 | } | |
350 | } | |
351 | return $properties; | |
352 | } | |
353 | ||
a0ee3941 | 354 | /** |
c490a46a | 355 | * @param CRM_Core_Form $form |
a0ee3941 | 356 | */ |
00be9182 | 357 | public static function &buildSearchForm(&$form) { |
6a488035 TO |
358 | $info = self::_info(); |
359 | ||
360 | foreach ($info as $name => $comp) { | |
361 | if ($comp->usesSearch()) { | |
362 | $bqr = $comp->getBAOQueryObject(); | |
363 | $bqr->buildSearchForm($form); | |
364 | } | |
365 | } | |
366 | } | |
367 | ||
a0ee3941 EM |
368 | /** |
369 | * @param $row | |
100fef9d | 370 | * @param int $id |
a0ee3941 | 371 | */ |
00be9182 | 372 | public static function searchAction(&$row, $id) { |
6a488035 TO |
373 | $info = self::_info(); |
374 | ||
375 | foreach ($info as $name => $comp) { | |
376 | if ($comp->usesSearch()) { | |
377 | $bqr = $comp->getBAOQueryObject(); | |
378 | $bqr->searchAction($row, $id); | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
a0ee3941 EM |
383 | /** |
384 | * @return array|null | |
385 | */ | |
00be9182 | 386 | public static function &contactSubTypes() { |
6a488035 | 387 | if (self::$_contactSubTypes == NULL) { |
be2fb01f | 388 | self::$_contactSubTypes = []; |
6a488035 TO |
389 | } |
390 | return self::$_contactSubTypes; | |
391 | } | |
392 | ||
a0ee3941 EM |
393 | /** |
394 | * @param $subType | |
395 | * @param $op | |
396 | * | |
397 | * @return null | |
398 | */ | |
00be9182 | 399 | public static function &contactSubTypeProperties($subType, $op) { |
6a488035 TO |
400 | $properties = self::contactSubTypes(); |
401 | if (array_key_exists($subType, $properties) && | |
402 | array_key_exists($op, $properties[$subType]) | |
403 | ) { | |
404 | return $properties[$subType][$op]; | |
405 | } | |
406 | return CRM_Core_DAO::$_nullObject; | |
407 | } | |
408 | ||
6a488035 | 409 | /** |
0880a9d0 | 410 | * Handle table dependencies of components. |
6a488035 | 411 | * |
6a0b768e TO |
412 | * @param array $tables |
413 | * Array of tables. | |
6a488035 | 414 | * |
6a488035 | 415 | */ |
00be9182 | 416 | public static function tableNames(&$tables) { |
6a488035 TO |
417 | $info = self::_info(); |
418 | ||
419 | foreach ($info as $name => $comp) { | |
420 | if ($comp->usesSearch()) { | |
421 | $bqr = $comp->getBAOQueryObject(); | |
422 | $bqr->tableNames($tables); | |
423 | } | |
424 | } | |
425 | } | |
426 | ||
427 | /** | |
0880a9d0 | 428 | * Get components info from info file. |
ea3ddccf | 429 | * |
430 | * @param string $crmFolderDir | |
431 | * | |
432 | * @return array | |
6a488035 | 433 | */ |
00be9182 | 434 | public static function getComponentsFromFile($crmFolderDir) { |
be2fb01f | 435 | $components = []; |
6a488035 | 436 | //traverse CRM folder and check for Info file |
948d11bf | 437 | if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) { |
6a488035 TO |
438 | while ($subDir = readdir($dir)) { |
439 | // skip the extensions diretory since it has an Info.php file also | |
440 | if ($subDir == 'Extension') { | |
441 | continue; | |
442 | } | |
443 | ||
444 | $infoFile = $crmFolderDir . "/{$subDir}/" . self::COMPONENT_INFO_CLASS . '.php'; | |
445 | if (file_exists($infoFile)) { | |
446 | $infoClass = 'CRM_' . $subDir . '_' . self::COMPONENT_INFO_CLASS; | |
2aa397bc | 447 | require_once str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php'; |
6a488035 TO |
448 | $infoObject = new $infoClass(NULL, NULL, NULL); |
449 | $components[$infoObject->info['name']] = $infoObject; | |
450 | unset($infoObject); | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
455 | return $components; | |
456 | } | |
96025800 | 457 | |
6a488035 | 458 | } |