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