forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComponent.php
492 lines (448 loc) · 12.7 KB
/
Component.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Component stores all the static and dynamic information of the various
* CiviCRM components
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Core_Component {
/**
* End part (filename) of the component information class'es name
* that needs to be present in components main directory.
*/
const COMPONENT_INFO_CLASS = 'Info';
/**
* @param bool $force
*
* @return CRM_Core_Component_Info[]
*/
private static function &_info($force = FALSE) {
if (!isset(Civi::$statics[__CLASS__]['info']) || $force) {
Civi::$statics[__CLASS__]['info'] = [];
foreach (self::getComponents() as $name => $comp) {
if (self::isEnabled($name)) {
Civi::$statics[__CLASS__]['info'][$name] = $comp;
}
}
}
return Civi::$statics[__CLASS__]['info'];
}
/**
* @param string $name
* @param null $attribute
*
* @return mixed
*/
public static function get($name, $attribute = NULL) {
$comp = self::_info()[$name] ?? NULL;
if ($attribute) {
return $comp->info[$attribute] ?? NULL;
}
return $comp;
}
/**
* @param bool $force
*
* @return CRM_Core_Component_Info[]
* @throws CRM_Core_Exception
*/
public static function &getComponents($force = FALSE) {
if (!isset(Civi::$statics[__CLASS__]['all']) || $force) {
Civi::$statics[__CLASS__]['all'] = [];
$cr = new CRM_Core_DAO_Component();
$cr->find(FALSE);
while ($cr->fetch()) {
$infoClass = $cr->namespace . '_' . self::COMPONENT_INFO_CLASS;
$infoClassFile = str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php';
if (!CRM_Utils_File::isIncludable($infoClassFile)) {
continue;
}
require_once $infoClassFile;
$infoObject = new $infoClass($cr->name, $cr->namespace, $cr->id);
if ($infoObject->info['name'] !== $cr->name) {
throw new CRM_Core_Exception("There is a discrepancy between name in component registry and in info file ({$cr->name}).");
}
Civi::$statics[__CLASS__]['all'][$cr->name] = $infoObject;
unset($infoObject);
}
}
return Civi::$statics[__CLASS__]['all'];
}
/**
* @deprecated
* @return array
* Array(string $name => int $id).
*/
public static function &getComponentIDs() {
CRM_Core_Error::deprecatedFunctionWarning('getComponents');
$componentIDs = [];
$cr = new CRM_Core_DAO_Component();
$cr->find(FALSE);
while ($cr->fetch()) {
$componentIDs[$cr->name] = $cr->id;
}
return $componentIDs;
}
/**
* @param bool $force
*
* @return CRM_Core_Component_Info[]
*/
public static function &getEnabledComponents($force = FALSE) {
return self::_info($force);
}
/**
* @param bool $translated
*
* @return array
*/
public static function &getNames($translated = FALSE) {
$allComponents = self::getComponents();
$names = [];
foreach ($allComponents as $name => $comp) {
if ($translated) {
$names[$comp->componentID] = $comp->info['translatedName'];
}
else {
$names[$comp->componentID] = $name;
}
}
return $names;
}
/**
* @param $args
* @param $type
*
* @return bool
*/
public static function invoke(&$args, $type) {
$info = self::_info();
$firstArg = $args[1] ?? '';
$secondArg = $args[2] ?? '';
foreach ($info as $name => $comp) {
if (self::isEnabled($name) &&
(($comp->info['url'] === $firstArg && $type == 'main') ||
($comp->info['url'] === $secondArg && $type == 'admin')
)
) {
if ($type == 'main') {
// also set the smarty variables to the current component
$template = CRM_Core_Smarty::singleton();
$template->assign('activeComponent', $name);
if (!empty($comp->info[$name]['formTpl'])) {
$template->assign('formTpl', $comp->info[$name]['formTpl']);
}
}
$inv = $comp->getInvokeObject();
$inv->$type($args);
return TRUE;
}
}
return FALSE;
}
/**
* Get menu files from all components
* @return array
*/
public static function xmlMenu() {
$info = self::getComponents(TRUE);
$files = [];
foreach ($info as $comp) {
$files = array_merge($files, $comp->menuFiles());
}
return $files;
}
/**
* @param string $componentName
*
* @return int|null
*/
public static function getComponentID($componentName) {
$info = self::getComponents();
if (!empty($info[$componentName])) {
return $info[$componentName]->componentID;
}
return NULL;
}
/**
* @param int $componentID
*
* @return string|null
*/
public static function getComponentName($componentID) {
foreach (self::getComponents() as $compName => $component) {
if ($component->componentID == $componentID) {
return $compName;
}
}
return NULL;
}
/**
* @return array
*/
public static function &getQueryFields($checkPermission = TRUE) {
$info = self::_info();
$fields = [];
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$flds = $bqr->getFields($checkPermission);
$fields = array_merge($fields, $flds);
}
}
return $fields;
}
/**
* @param $query
* @param string $fnName
*/
public static function alterQuery(&$query, $fnName) {
$info = self::_info();
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$bqr->$fnName($query);
}
}
}
/**
* @param string $fieldName
* @param $mode
* @param $side
*
* @return null
*/
public static function from($fieldName, $mode, $side) {
$info = self::_info();
$from = NULL;
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$from = $bqr->from($fieldName, $mode, $side);
if ($from) {
return $from;
}
}
}
return $from;
}
/**
* @param $mode
* @param bool $includeCustomFields
*
* @return null
*/
public static function &defaultReturnProperties(
$mode,
$includeCustomFields = TRUE
) {
$info = self::_info();
$properties = NULL;
foreach ($info as $name => $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
if ($properties) {
return $properties;
}
}
}
if (!$properties) {
$properties = CRM_Contact_BAO_Query_Hook::singleton()->getDefaultReturnProperties($mode);
}
return $properties;
}
/**
* @param CRM_Core_Form $form
*/
public static function &buildSearchForm(&$form) {
$info = self::_info();
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$bqr->buildSearchForm($form);
}
}
}
/**
* @param $row
* @param int $id
*/
public static function searchAction(&$row, $id) {
$info = self::_info();
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$bqr->searchAction($row, $id);
}
}
}
/**
* Unused function.
*
* @return array|null
*
* @deprecated
*/
public static function contactSubTypes() {
CRM_Core_Error::deprecatedWarning('unused');
return [];
}
/**
* Unused function.
*
* @param string $subType
* @param string $op
*
* @return null|string
*
* @deprecated
*/
public static function contactSubTypeProperties($subType, $op): ?string {
CRM_Core_Error::deprecatedWarning('unused');
$properties = self::contactSubTypes();
if (array_key_exists($subType, $properties) &&
array_key_exists($op, $properties[$subType])
) {
return $properties[$subType][$op];
}
return NULL;
}
/**
* Handle table dependencies of components.
*
* @param array $tables
* Array of tables.
*
*/
public static function tableNames(&$tables) {
$info = self::_info();
foreach ($info as $comp) {
if ($comp->usesSearch()) {
$bqr = $comp->getBAOQueryObject();
$bqr->tableNames($tables);
}
}
}
/**
* Get components info from info file.
*
* @param string $crmFolderDir
*
* @return array
*/
public static function getComponentsFromFile($crmFolderDir) {
$components = [];
//traverse CRM folder and check for Info file
if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
while ($subDir = readdir($dir)) {
// skip the extensions diretory since it has an Info.php file also
if ($subDir === 'Extension') {
continue;
}
$infoFile = $crmFolderDir . "/{$subDir}/" . self::COMPONENT_INFO_CLASS . '.php';
if (file_exists($infoFile)) {
$infoClass = 'CRM_' . $subDir . '_' . self::COMPONENT_INFO_CLASS;
$infoObject = new $infoClass(NULL, NULL, NULL);
$components[$infoObject->info['name']] = $infoObject;
unset($infoObject);
}
}
}
return $components;
}
/**
* Is the specified component enabled.
*
* @param string $component
* Component name - ie CiviMember, CiviContribute, CiviEvent...
*
* @return bool
* Is the component enabled.
*/
public static function isEnabled(string $component): bool {
return in_array($component, Civi::settings()->get('enable_components'), TRUE);
}
/**
* Callback for the "enable_components" setting (pre change)
*
* Before a component is disabled, disable reverse-dependencies (all extensions dependent on it).
*
* This is imperfect because it only goes one-level deep:
* it doesn't deal with any extensions that depend on the ones being disabled.
* The proper fix for that would probably be something like a CASCADE mode for
* disabling an extension with all its reverse dependencies (which would render this function moot).
*
* @param array $oldValue
* List of component names.
* @param array $newValue
* List of component names.
*
* @throws \CRM_Core_Exception.
*/
public static function preToggleComponents($oldValue, $newValue): void {
if (is_array($oldValue) && is_array($newValue)) {
$disabledComponents = array_diff($oldValue, $newValue);
}
if (empty($disabledComponents)) {
return;
}
$disabledExtensions = array_map(['CRM_Utils_String', 'convertStringToSnakeCase'], $disabledComponents);
$manager = CRM_Extension_System::singleton()->getManager();
$extensions = $manager->getStatuses();
foreach ($extensions as $extension => $status) {
if ($status === CRM_Extension_Manager::STATUS_INSTALLED) {
$info = $manager->mapper->keyToInfo($extension);
if (array_intersect($info->requires, $disabledExtensions)) {
$manager->disable($extension);
}
}
}
}
/**
* Callback for the "enable_components" setting (post change)
*
* When a component is enabled or disabled, ensure the corresponding module-extension is also enabled/disabled.
*
* @param array $oldValue
* List of component names.
* @param array $newValue
* List of component names.
*
* @throws \CRM_Core_Exception.
*/
public static function postToggleComponents($oldValue, $newValue): void {
if (CRM_Core_Config::isUpgradeMode()) {
return;
}
$manager = CRM_Extension_System::singleton()->getManager();
$toEnable = $toDisable = [];
foreach (self::getComponents() as $component) {
$componentEnabled = in_array($component->name, $newValue);
$extName = $component->getExtensionName();
$extensionEnabled = $manager->getStatus($extName) === $manager::STATUS_INSTALLED;
if ($componentEnabled && !$extensionEnabled) {
$toEnable[] = $extName;
}
elseif (!$componentEnabled && $extensionEnabled) {
$toDisable[] = $extName;
}
}
if ($toEnable) {
CRM_Extension_System::singleton()->getManager()->install($toEnable);
}
if ($toDisable) {
CRM_Extension_System::singleton()->getManager()->disable($toDisable);
}
}
}