3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Component stores all the static and dynamic information of the various
33 * @copyright CiviCRM LLC (c) 2004-2019
37 class CRM_Core_Component
{
40 * End part (filename) of the component information class'es name
41 * that needs to be present in components main directory.
43 const COMPONENT_INFO_CLASS
= 'Info';
48 public static $_contactSubTypes = NULL;
53 * @return CRM_Core_Component_Info[]
55 private static function &_info($force = FALSE) {
56 if (!isset(Civi
::$statics[__CLASS__
]['info'])||
$force) {
57 Civi
::$statics[__CLASS__
]['info'] = [];
60 $config = CRM_Core_Config
::singleton();
61 $c = self
::getComponents();
63 foreach ($c as $name => $comp) {
64 if (in_array($name, $config->enableComponents
)) {
65 Civi
::$statics[__CLASS__
]['info'][$name] = $comp;
70 return Civi
::$statics[__CLASS__
]['info'];
75 * @param null $attribute
79 public static function get($name, $attribute = NULL) {
80 $comp = CRM_Utils_Array
::value($name, self
::_info());
82 return CRM_Utils_Array
::value($attribute, $comp->info
);
90 * @return CRM_Core_Component_Info[]
93 public static function &getComponents($force = FALSE) {
94 if (!isset(Civi
::$statics[__CLASS__
]['all']) ||
$force) {
95 Civi
::$statics[__CLASS__
]['all'] = [];
97 $cr = new CRM_Core_DAO_Component();
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)) {
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}).");
110 Civi
::$statics[__CLASS__
]['all'][$cr->name
] = $infoObject;
115 return Civi
::$statics[__CLASS__
]['all'];
120 * Array(string $name => int $id).
122 public static function &getComponentIDs() {
125 $cr = new CRM_Core_DAO_Component();
127 while ($cr->fetch()) {
128 $componentIDs[$cr->name
] = $cr->id
;
131 return $componentIDs;
137 * @return CRM_Core_Component_Info[]
139 public static function &getEnabledComponents($force = FALSE) {
140 return self
::_info($force);
143 public static function flushEnabledComponents() {
144 unset(Civi
::$statics[__CLASS__
]);
148 * @param bool $translated
152 public static function &getNames($translated = FALSE) {
153 $allComponents = self
::getComponents();
156 foreach ($allComponents as $name => $comp) {
158 $names[$comp->componentID
] = $comp->info
['translatedName'];
161 $names[$comp->componentID
] = $name;
173 public static function invoke(&$args, $type) {
174 $info = self
::_info();
175 $config = CRM_Core_Config
::singleton();
177 $firstArg = CRM_Utils_Array
::value(1, $args, '');
178 $secondArg = CRM_Utils_Array
::value(2, $args, '');
179 foreach ($info as $name => $comp) {
180 if (in_array($name, $config->enableComponents
) &&
181 (($comp->info
['url'] === $firstArg && $type == 'main') ||
182 ($comp->info
['url'] === $secondArg && $type == 'admin')
185 if ($type == 'main') {
186 // also set the smarty variables to the current component
187 $template = CRM_Core_Smarty
::singleton();
188 $template->assign('activeComponent', $name);
189 if (!empty($comp->info
[$name]['formTpl'])) {
190 $template->assign('formTpl', $comp->info
[$name]['formTpl']);
192 if (!empty($comp->info
[$name]['css'])) {
193 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
194 CRM_Utils_System
::addHTMLHead($styleSheet);
197 $inv = $comp->getInvokeObject();
208 public static function xmlMenu() {
210 // lets build the menu for all components
211 $info = self
::getComponents(TRUE);
214 foreach ($info as $name => $comp) {
215 $files = array_merge($files,
226 public static function &menu() {
227 $info = self
::_info();
229 foreach ($info as $name => $comp) {
230 $mnu = $comp->getMenuObject();
232 $ret = $mnu->permissioned();
233 $items = array_merge($items, $ret);
235 $ret = $mnu->main($task);
236 $items = array_merge($items, $ret);
242 * @param string $componentName
246 public static function getComponentID($componentName) {
247 $info = self
::_info();
248 if (!empty($info[$componentName])) {
249 return $info[$componentName]->componentID
;
257 * @param int $componentID
259 * @return int|null|string
261 public static function getComponentName($componentID) {
262 $info = self
::_info();
264 $componentName = NULL;
265 foreach ($info as $compName => $component) {
266 if ($component->componentID
== $componentID) {
267 $componentName = $compName;
272 return $componentName;
278 public static function &getQueryFields($checkPermission = TRUE) {
279 $info = self
::_info();
281 foreach ($info as $name => $comp) {
282 if ($comp->usesSearch()) {
283 $bqr = $comp->getBAOQueryObject();
284 $flds = $bqr->getFields($checkPermission);
285 $fields = array_merge($fields, $flds);
293 * @param string $fnName
295 public static function alterQuery(&$query, $fnName) {
296 $info = self
::_info();
298 foreach ($info as $name => $comp) {
299 if ($comp->usesSearch()) {
300 $bqr = $comp->getBAOQueryObject();
301 $bqr->$fnName($query);
307 * @param string $fieldName
313 public static function from($fieldName, $mode, $side) {
314 $info = self
::_info();
317 foreach ($info as $name => $comp) {
318 if ($comp->usesSearch()) {
319 $bqr = $comp->getBAOQueryObject();
320 $from = $bqr->from($fieldName, $mode, $side);
331 * @param bool $includeCustomFields
335 public static function &defaultReturnProperties(
337 $includeCustomFields = TRUE
339 $info = self
::_info();
342 foreach ($info as $name => $comp) {
343 if ($comp->usesSearch()) {
344 $bqr = $comp->getBAOQueryObject();
345 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
355 * @param CRM_Core_Form $form
357 public static function &buildSearchForm(&$form) {
358 $info = self
::_info();
360 foreach ($info as $name => $comp) {
361 if ($comp->usesSearch()) {
362 $bqr = $comp->getBAOQueryObject();
363 $bqr->buildSearchForm($form);
372 public static function searchAction(&$row, $id) {
373 $info = self
::_info();
375 foreach ($info as $name => $comp) {
376 if ($comp->usesSearch()) {
377 $bqr = $comp->getBAOQueryObject();
378 $bqr->searchAction($row, $id);
386 public static function &contactSubTypes() {
387 if (self
::$_contactSubTypes == NULL) {
388 self
::$_contactSubTypes = [];
390 return self
::$_contactSubTypes;
399 public static function &contactSubTypeProperties($subType, $op) {
400 $properties = self
::contactSubTypes();
401 if (array_key_exists($subType, $properties) &&
402 array_key_exists($op, $properties[$subType])
404 return $properties[$subType][$op];
406 return CRM_Core_DAO
::$_nullObject;
410 * Handle table dependencies of components.
412 * @param array $tables
416 public static function tableNames(&$tables) {
417 $info = self
::_info();
419 foreach ($info as $name => $comp) {
420 if ($comp->usesSearch()) {
421 $bqr = $comp->getBAOQueryObject();
422 $bqr->tableNames($tables);
428 * Get components info from info file.
430 * @param string $crmFolderDir
434 public static function getComponentsFromFile($crmFolderDir) {
436 //traverse CRM folder and check for Info file
437 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
438 while ($subDir = readdir($dir)) {
439 // skip the extensions diretory since it has an Info.php file also
440 if ($subDir == 'Extension') {
444 $infoFile = $crmFolderDir . "/{$subDir}/" . self
::COMPONENT_INFO_CLASS
. '.php';
445 if (file_exists($infoFile)) {
446 $infoClass = 'CRM_' . $subDir . '_' . self
::COMPONENT_INFO_CLASS
;
447 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
448 $infoObject = new $infoClass(NULL, NULL, NULL);
449 $components[$infoObject->info
['name']] = $infoObject;