3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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';
45 static $_contactSubTypes = NULL;
52 private static function &_info($force = FALSE) {
53 if (!isset(Civi
::$statics[__CLASS__
]['info'])||
$force) {
54 Civi
::$statics[__CLASS__
]['info'] = array();
57 $config = CRM_Core_Config
::singleton();
58 $c = self
::getComponents();
60 foreach ($c as $name => $comp) {
61 if (in_array($name, $config->enableComponents
)) {
62 Civi
::$statics[__CLASS__
]['info'][$name] = $comp;
67 return Civi
::$statics[__CLASS__
]['info'];
72 * @param null $attribute
76 public static function get($name, $attribute = NULL) {
77 $comp = CRM_Utils_Array
::value($name, self
::_info());
79 return CRM_Utils_Array
::value($attribute, $comp->info
);
90 public static function &getComponents($force = FALSE) {
91 if (!isset(Civi
::$statics[__CLASS__
]['all']) ||
$force) {
92 Civi
::$statics[__CLASS__
]['all'] = array();
94 $cr = new CRM_Core_DAO_Component();
96 while ($cr->fetch()) {
97 $infoClass = $cr->namespace . '_' . self
::COMPONENT_INFO_CLASS
;
98 $infoClassFile = str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
99 if (!CRM_Utils_File
::isIncludable($infoClassFile)) {
102 require_once $infoClassFile;
103 $infoObject = new $infoClass($cr->name
, $cr->namespace, $cr->id
);
104 if ($infoObject->info
['name'] !== $cr->name
) {
105 CRM_Core_Error
::fatal("There is a discrepancy between name in component registry and in info file ({$cr->name}).");
107 Civi
::$statics[__CLASS__
]['all'][$cr->name
] = $infoObject;
112 return Civi
::$statics[__CLASS__
]['all'];
117 * Array(string $name => int $id).
119 public static function &getComponentIDs() {
120 $componentIDs = array();
122 $cr = new CRM_Core_DAO_Component();
124 while ($cr->fetch()) {
125 $componentIDs[$cr->name
] = $cr->id
;
128 return $componentIDs;
137 static public function &getEnabledComponents($force = FALSE) {
138 return self
::_info($force);
141 static public function flushEnabledComponents() {
142 self
::getEnabledComponents(TRUE);
146 * @param bool $translated
150 public static function &getNames($translated = FALSE) {
151 $allComponents = self
::getComponents();
154 foreach ($allComponents as $name => $comp) {
156 $names[$comp->componentID
] = $comp->info
['translatedName'];
159 $names[$comp->componentID
] = $name;
171 public static function invoke(&$args, $type) {
172 $info = self
::_info();
173 $config = CRM_Core_Config
::singleton();
175 $firstArg = CRM_Utils_Array
::value(1, $args, '');
176 $secondArg = CRM_Utils_Array
::value(2, $args, '');
177 foreach ($info as $name => $comp) {
178 if (in_array($name, $config->enableComponents
) &&
179 (($comp->info
['url'] === $firstArg && $type == 'main') ||
180 ($comp->info
['url'] === $secondArg && $type == 'admin')
183 if ($type == 'main') {
184 // also set the smarty variables to the current component
185 $template = CRM_Core_Smarty
::singleton();
186 $template->assign('activeComponent', $name);
187 if (!empty($comp->info
[$name]['formTpl'])) {
188 $template->assign('formTpl', $comp->info
[$name]['formTpl']);
190 if (!empty($comp->info
[$name]['css'])) {
191 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
192 CRM_Utils_System
::addHTMLHead($styleSheet);
195 $inv = $comp->getInvokeObject();
206 public static function xmlMenu() {
208 // lets build the menu for all components
209 $info = self
::getComponents(TRUE);
212 foreach ($info as $name => $comp) {
213 $files = array_merge($files,
224 public static function &menu() {
225 $info = self
::_info();
227 foreach ($info as $name => $comp) {
228 $mnu = $comp->getMenuObject();
230 $ret = $mnu->permissioned();
231 $items = array_merge($items, $ret);
233 $ret = $mnu->main($task);
234 $items = array_merge($items, $ret);
240 * @param string $componentName
244 public static function getComponentID($componentName) {
245 $info = self
::_info();
246 if (!empty($info[$componentName])) {
247 return $info[$componentName]->componentID
;
255 * @param int $componentID
257 * @return int|null|string
259 public static function getComponentName($componentID) {
260 $info = self
::_info();
262 $componentName = NULL;
263 foreach ($info as $compName => $component) {
264 if ($component->componentID
== $componentID) {
265 $componentName = $compName;
270 return $componentName;
276 public static function &getQueryFields($checkPermission = TRUE) {
277 $info = self
::_info();
279 foreach ($info as $name => $comp) {
280 if ($comp->usesSearch()) {
281 $bqr = $comp->getBAOQueryObject();
282 $flds = $bqr->getFields($checkPermission);
283 $fields = array_merge($fields, $flds);
291 * @param string $fnName
293 public static function alterQuery(&$query, $fnName) {
294 $info = self
::_info();
296 foreach ($info as $name => $comp) {
297 if ($comp->usesSearch()) {
298 $bqr = $comp->getBAOQueryObject();
299 $bqr->$fnName($query);
305 * @param string $fieldName
311 public static function from($fieldName, $mode, $side) {
312 $info = self
::_info();
315 foreach ($info as $name => $comp) {
316 if ($comp->usesSearch()) {
317 $bqr = $comp->getBAOQueryObject();
318 $from = $bqr->from($fieldName, $mode, $side);
329 * @param bool $includeCustomFields
333 public static function &defaultReturnProperties(
335 $includeCustomFields = TRUE
337 $info = self
::_info();
340 foreach ($info as $name => $comp) {
341 if ($comp->usesSearch()) {
342 $bqr = $comp->getBAOQueryObject();
343 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
353 * @param CRM_Core_Form $form
355 public static function &buildSearchForm(&$form) {
356 $info = self
::_info();
358 foreach ($info as $name => $comp) {
359 if ($comp->usesSearch()) {
360 $bqr = $comp->getBAOQueryObject();
361 $bqr->buildSearchForm($form);
370 public static function searchAction(&$row, $id) {
371 $info = self
::_info();
373 foreach ($info as $name => $comp) {
374 if ($comp->usesSearch()) {
375 $bqr = $comp->getBAOQueryObject();
376 $bqr->searchAction($row, $id);
384 public static function &contactSubTypes() {
385 if (self
::$_contactSubTypes == NULL) {
386 self
::$_contactSubTypes = array();
388 return self
::$_contactSubTypes;
398 public static function &contactSubTypeProperties($subType, $op) {
399 $properties = self
::contactSubTypes();
400 if (array_key_exists($subType, $properties) &&
401 array_key_exists($op, $properties[$subType])
403 return $properties[$subType][$op];
405 return CRM_Core_DAO
::$_nullObject;
409 * FIXME: This function does not appear to do anything. The is_array() check runs on a bunch of objects and (always?) returns false
411 public static function &taskList() {
412 $info = self
::_info();
415 foreach ($info as $name => $value) {
416 if (is_array($info[$name]) && isset($info[$name]['task'])) {
417 $tasks +
= $info[$name]['task'];
424 * Handle table dependencies of components.
426 * @param array $tables
430 public static function tableNames(&$tables) {
431 $info = self
::_info();
433 foreach ($info as $name => $comp) {
434 if ($comp->usesSearch()) {
435 $bqr = $comp->getBAOQueryObject();
436 $bqr->tableNames($tables);
442 * Get components info from info file.
444 * @param string $crmFolderDir
448 public static function getComponentsFromFile($crmFolderDir) {
449 $components = array();
450 //traverse CRM folder and check for Info file
451 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
452 while ($subDir = readdir($dir)) {
453 // skip the extensions diretory since it has an Info.php file also
454 if ($subDir == 'Extension') {
458 $infoFile = $crmFolderDir . "/{$subDir}/" . self
::COMPONENT_INFO_CLASS
. '.php';
459 if (file_exists($infoFile)) {
460 $infoClass = 'CRM_' . $subDir . '_' . self
::COMPONENT_INFO_CLASS
;
461 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
462 $infoObject = new $infoClass(NULL, NULL, NULL);
463 $components[$infoObject->info
['name']] = $infoObject;