3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Component stores all the static and dynamic information of the various
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Core_Component
{
22 * End part (filename) of the component information class'es name
23 * that needs to be present in components main directory.
25 const COMPONENT_INFO_CLASS
= 'Info';
30 public static $_contactSubTypes = NULL;
35 * @return CRM_Core_Component_Info[]
37 private static function &_info($force = FALSE) {
38 if (!isset(Civi
::$statics[__CLASS__
]['info'])||
$force) {
39 Civi
::$statics[__CLASS__
]['info'] = [];
42 $config = CRM_Core_Config
::singleton();
43 $c = self
::getComponents();
45 foreach ($c as $name => $comp) {
46 if (in_array($name, $config->enableComponents
)) {
47 Civi
::$statics[__CLASS__
]['info'][$name] = $comp;
52 return Civi
::$statics[__CLASS__
]['info'];
57 * @param null $attribute
61 public static function get($name, $attribute = NULL) {
62 $comp = CRM_Utils_Array
::value($name, self
::_info());
64 return $comp->info
[$attribute] ??
NULL;
72 * @return CRM_Core_Component_Info[]
73 * @throws CRM_Core_Exception
75 public static function &getComponents($force = FALSE) {
76 if (!isset(Civi
::$statics[__CLASS__
]['all']) ||
$force) {
77 Civi
::$statics[__CLASS__
]['all'] = [];
79 $cr = new CRM_Core_DAO_Component();
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)) {
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}).");
92 Civi
::$statics[__CLASS__
]['all'][$cr->name
] = $infoObject;
97 return Civi
::$statics[__CLASS__
]['all'];
102 * Array(string $name => int $id).
104 public static function &getComponentIDs() {
107 $cr = new CRM_Core_DAO_Component();
109 while ($cr->fetch()) {
110 $componentIDs[$cr->name
] = $cr->id
;
113 return $componentIDs;
119 * @return CRM_Core_Component_Info[]
121 public static function &getEnabledComponents($force = FALSE) {
122 return self
::_info($force);
125 public static function flushEnabledComponents() {
126 unset(Civi
::$statics[__CLASS__
]);
127 CRM_Core_BAO_Navigation
::resetNavigation();
131 * @param bool $translated
135 public static function &getNames($translated = FALSE) {
136 $allComponents = self
::getComponents();
139 foreach ($allComponents as $name => $comp) {
141 $names[$comp->componentID
] = $comp->info
['translatedName'];
144 $names[$comp->componentID
] = $name;
156 public static function invoke(&$args, $type) {
157 $info = self
::_info();
158 $config = CRM_Core_Config
::singleton();
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')
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']);
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);
180 $inv = $comp->getInvokeObject();
191 public static function xmlMenu() {
193 // lets build the menu for all components
194 $info = self
::getComponents(TRUE);
197 foreach ($info as $name => $comp) {
198 $files = array_merge($files,
209 public static function &menu() {
210 $info = self
::_info();
212 foreach ($info as $name => $comp) {
213 $mnu = $comp->getMenuObject();
215 $ret = $mnu->permissioned();
216 $items = array_merge($items, $ret);
218 $ret = $mnu->main($task);
219 $items = array_merge($items, $ret);
225 * @param string $componentName
229 public static function getComponentID($componentName) {
230 $info = self
::_info();
231 if (!empty($info[$componentName])) {
232 return $info[$componentName]->componentID
;
240 * @param int $componentID
242 * @return int|null|string
244 public static function getComponentName($componentID) {
245 $info = self
::_info();
247 $componentName = NULL;
248 foreach ($info as $compName => $component) {
249 if ($component->componentID
== $componentID) {
250 $componentName = $compName;
255 return $componentName;
261 public static function &getQueryFields($checkPermission = TRUE) {
262 $info = self
::_info();
264 foreach ($info as $name => $comp) {
265 if ($comp->usesSearch()) {
266 $bqr = $comp->getBAOQueryObject();
267 $flds = $bqr->getFields($checkPermission);
268 $fields = array_merge($fields, $flds);
276 * @param string $fnName
278 public static function alterQuery(&$query, $fnName) {
279 $info = self
::_info();
281 foreach ($info as $name => $comp) {
282 if ($comp->usesSearch()) {
283 $bqr = $comp->getBAOQueryObject();
284 $bqr->$fnName($query);
290 * @param string $fieldName
296 public static function from($fieldName, $mode, $side) {
297 $info = self
::_info();
300 foreach ($info as $name => $comp) {
301 if ($comp->usesSearch()) {
302 $bqr = $comp->getBAOQueryObject();
303 $from = $bqr->from($fieldName, $mode, $side);
314 * @param bool $includeCustomFields
318 public static function &defaultReturnProperties(
320 $includeCustomFields = TRUE
322 $info = self
::_info();
325 foreach ($info as $name => $comp) {
326 if ($comp->usesSearch()) {
327 $bqr = $comp->getBAOQueryObject();
328 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
338 * @param CRM_Core_Form $form
340 public static function &buildSearchForm(&$form) {
341 $info = self
::_info();
343 foreach ($info as $name => $comp) {
344 if ($comp->usesSearch()) {
345 $bqr = $comp->getBAOQueryObject();
346 $bqr->buildSearchForm($form);
355 public static function searchAction(&$row, $id) {
356 $info = self
::_info();
358 foreach ($info as $name => $comp) {
359 if ($comp->usesSearch()) {
360 $bqr = $comp->getBAOQueryObject();
361 $bqr->searchAction($row, $id);
369 public static function &contactSubTypes() {
370 if (self
::$_contactSubTypes == NULL) {
371 self
::$_contactSubTypes = [];
373 return self
::$_contactSubTypes;
382 public static function &contactSubTypeProperties($subType, $op) {
383 $properties = self
::contactSubTypes();
384 if (array_key_exists($subType, $properties) &&
385 array_key_exists($op, $properties[$subType])
387 return $properties[$subType][$op];
389 return CRM_Core_DAO
::$_nullObject;
393 * Handle table dependencies of components.
395 * @param array $tables
399 public static function tableNames(&$tables) {
400 $info = self
::_info();
402 foreach ($info as $name => $comp) {
403 if ($comp->usesSearch()) {
404 $bqr = $comp->getBAOQueryObject();
405 $bqr->tableNames($tables);
411 * Get components info from info file.
413 * @param string $crmFolderDir
417 public static function getComponentsFromFile($crmFolderDir) {
419 //traverse CRM folder and check for Info file
420 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
421 while ($subDir = readdir($dir)) {
422 // skip the extensions diretory since it has an Info.php file also
423 if ($subDir == 'Extension') {
427 $infoFile = $crmFolderDir . "/{$subDir}/" . self
::COMPONENT_INFO_CLASS
. '.php';
428 if (file_exists($infoFile)) {
429 $infoClass = 'CRM_' . $subDir . '_' . self
::COMPONENT_INFO_CLASS
;
430 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
431 $infoObject = new $infoClass(NULL, NULL, NULL);
432 $components[$infoObject->info
['name']] = $infoObject;