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