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