3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
99 $infoObject = new $infoClass($cr->name
, $cr->namespace, $cr->id
);
100 if ($infoObject->info
['name'] !== $cr->name
) {
101 CRM_Core_Error
::fatal("There is a discrepancy between name in component registry and in info file ({$cr->name}).");
103 Civi
::$statics[__CLASS__
]['all'][$cr->name
] = $infoObject;
108 return Civi
::$statics[__CLASS__
]['all'];
113 * Array(string $name => int $id).
115 public static function &getComponentIDs() {
116 $componentIDs = array();
118 $cr = new CRM_Core_DAO_Component();
120 while ($cr->fetch()) {
121 $componentIDs[$cr->name
] = $cr->id
;
124 return $componentIDs;
133 static public function &getEnabledComponents($force = FALSE) {
134 return self
::_info($force);
137 static public function flushEnabledComponents() {
138 self
::getEnabledComponents(TRUE);
142 * @param bool $translated
146 public static function &getNames($translated = FALSE) {
147 $allComponents = self
::getComponents();
150 foreach ($allComponents as $name => $comp) {
152 $names[$comp->componentID
] = $comp->info
['translatedName'];
155 $names[$comp->componentID
] = $name;
167 public static function invoke(&$args, $type) {
168 $info = self
::_info();
169 $config = CRM_Core_Config
::singleton();
171 $firstArg = CRM_Utils_Array
::value(1, $args, '');
172 $secondArg = CRM_Utils_Array
::value(2, $args, '');
173 foreach ($info as $name => $comp) {
174 if (in_array($name, $config->enableComponents
) &&
175 (($comp->info
['url'] === $firstArg && $type == 'main') ||
176 ($comp->info
['url'] === $secondArg && $type == 'admin')
179 if ($type == 'main') {
180 // also set the smarty variables to the current component
181 $template = CRM_Core_Smarty
::singleton();
182 $template->assign('activeComponent', $name);
183 if (!empty($comp->info
[$name]['formTpl'])) {
184 $template->assign('formTpl', $comp->info
[$name]['formTpl']);
186 if (!empty($comp->info
[$name]['css'])) {
187 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
188 CRM_Utils_System
::addHTMLHead($styleSheet);
191 $inv = $comp->getInvokeObject();
202 public static function xmlMenu() {
204 // lets build the menu for all components
205 $info = self
::getComponents(TRUE);
208 foreach ($info as $name => $comp) {
209 $files = array_merge($files,
220 public static function &menu() {
221 $info = self
::_info();
223 foreach ($info as $name => $comp) {
224 $mnu = $comp->getMenuObject();
226 $ret = $mnu->permissioned();
227 $items = array_merge($items, $ret);
229 $ret = $mnu->main($task);
230 $items = array_merge($items, $ret);
236 * @param string $componentName
240 public static function getComponentID($componentName) {
241 $info = self
::_info();
242 if (!empty($info[$componentName])) {
243 return $info[$componentName]->componentID
;
251 * @param int $componentID
253 * @return int|null|string
255 public static function getComponentName($componentID) {
256 $info = self
::_info();
258 $componentName = NULL;
259 foreach ($info as $compName => $component) {
260 if ($component->componentID
== $componentID) {
261 $componentName = $compName;
266 return $componentName;
272 public static function &getQueryFields($checkPermission = TRUE) {
273 $info = self
::_info();
275 foreach ($info as $name => $comp) {
276 if ($comp->usesSearch()) {
277 $bqr = $comp->getBAOQueryObject();
278 $flds = $bqr->getFields($checkPermission);
279 $fields = array_merge($fields, $flds);
287 * @param string $fnName
289 public static function alterQuery(&$query, $fnName) {
290 $info = self
::_info();
292 foreach ($info as $name => $comp) {
293 if ($comp->usesSearch()) {
294 $bqr = $comp->getBAOQueryObject();
295 $bqr->$fnName($query);
301 * @param string $fieldName
307 public static function from($fieldName, $mode, $side) {
308 $info = self
::_info();
311 foreach ($info as $name => $comp) {
312 if ($comp->usesSearch()) {
313 $bqr = $comp->getBAOQueryObject();
314 $from = $bqr->from($fieldName, $mode, $side);
325 * @param bool $includeCustomFields
329 public static function &defaultReturnProperties(
331 $includeCustomFields = TRUE
333 $info = self
::_info();
336 foreach ($info as $name => $comp) {
337 if ($comp->usesSearch()) {
338 $bqr = $comp->getBAOQueryObject();
339 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
349 * @param CRM_Core_Form $form
351 public static function &buildSearchForm(&$form) {
352 $info = self
::_info();
354 foreach ($info as $name => $comp) {
355 if ($comp->usesSearch()) {
356 $bqr = $comp->getBAOQueryObject();
357 $bqr->buildSearchForm($form);
366 public static function searchAction(&$row, $id) {
367 $info = self
::_info();
369 foreach ($info as $name => $comp) {
370 if ($comp->usesSearch()) {
371 $bqr = $comp->getBAOQueryObject();
372 $bqr->searchAction($row, $id);
380 public static function &contactSubTypes() {
381 if (self
::$_contactSubTypes == NULL) {
382 self
::$_contactSubTypes = array();
384 return self
::$_contactSubTypes;
394 public static function &contactSubTypeProperties($subType, $op) {
395 $properties = self
::contactSubTypes();
396 if (array_key_exists($subType, $properties) &&
397 array_key_exists($op, $properties[$subType])
399 return $properties[$subType][$op];
401 return CRM_Core_DAO
::$_nullObject;
405 * FIXME: This function does not appear to do anything. The is_array() check runs on a bunch of objects and (always?) returns false
407 public static function &taskList() {
408 $info = self
::_info();
411 foreach ($info as $name => $value) {
412 if (is_array($info[$name]) && isset($info[$name]['task'])) {
413 $tasks +
= $info[$name]['task'];
420 * Handle table dependencies of components.
422 * @param array $tables
426 public static function tableNames(&$tables) {
427 $info = self
::_info();
429 foreach ($info as $name => $comp) {
430 if ($comp->usesSearch()) {
431 $bqr = $comp->getBAOQueryObject();
432 $bqr->tableNames($tables);
438 * Get components info from info file.
440 * @param string $crmFolderDir
444 public static function getComponentsFromFile($crmFolderDir) {
445 $components = array();
446 //traverse CRM folder and check for Info file
447 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
448 while ($subDir = readdir($dir)) {
449 // skip the extensions diretory since it has an Info.php file also
450 if ($subDir == 'Extension') {
454 $infoFile = $crmFolderDir . "/{$subDir}/" . self
::COMPONENT_INFO_CLASS
. '.php';
455 if (file_exists($infoFile)) {
456 $infoClass = 'CRM_' . $subDir . '_' . self
::COMPONENT_INFO_CLASS
;
457 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
458 $infoObject = new $infoClass(NULL, NULL, NULL);
459 $components[$infoObject->info
['name']] = $infoObject;