3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 private static $_info = NULL;
47 static $_contactSubTypes = NULL;
54 private static function &_info($force = FALSE) {
55 if (self
::$_info == NULL ||
$force) {
56 self
::$_info = array();
59 $config = CRM_Core_Config
::singleton();
60 $c = self
::getComponents();
62 foreach ($c as $name => $comp) {
63 if (in_array($name, $config->enableComponents
)) {
64 self
::$_info[$name] = $comp;
74 * @param null $attribute
78 public static function get($name, $attribute = NULL) {
79 $comp = CRM_Utils_Array
::value($name, self
::_info());
81 return CRM_Utils_Array
::value($attribute, $comp->info
);
92 public static function &getComponents($force = FALSE) {
93 static $_cache = NULL;
95 if (!$_cache ||
$force) {
98 $cr = new CRM_Core_DAO_Component();
100 while ($cr->fetch()) {
101 $infoClass = $cr->namespace . '_' . self
::COMPONENT_INFO_CLASS
;
102 require_once str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php';
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 $_cache[$cr->name
] = $infoObject;
120 static public function &getEnabledComponents($force = FALSE) {
121 return self
::_info($force);
124 static public function flushEnabledComponents() {
125 self
::getEnabledComponents(TRUE);
129 * @param bool $translated
133 public static function &getNames($translated = FALSE) {
134 $allComponents = self
::getComponents();
137 foreach ($allComponents as $name => $comp) {
139 $names[$comp->componentID
] = $comp->info
['translatedName'];
142 $names[$comp->componentID
] = $name;
154 public static function invoke(&$args, $type) {
155 $info = self
::_info();
156 $config = CRM_Core_Config
::singleton();
158 $firstArg = CRM_Utils_Array
::value(1, $args, '');
159 $secondArg = CRM_Utils_Array
::value(2, $args, '');
160 foreach ($info as $name => $comp) {
161 if (in_array($name, $config->enableComponents
) &&
162 (($comp->info
['url'] === $firstArg && $type == 'main') ||
163 ($comp->info
['url'] === $secondArg && $type == 'admin')
166 if ($type == 'main') {
167 // also set the smarty variables to the current component
168 $template = CRM_Core_Smarty
::singleton();
169 $template->assign('activeComponent', $name);
170 if (!empty($comp->info
[$name]['formTpl'])) {
171 $template->assign('formTpl', $comp->info
[$name]['formTpl']);
173 if (!empty($comp->info
[$name]['css'])) {
174 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
175 CRM_Utils_System
::addHTMLHead($styleSheet);
178 $inv = $comp->getInvokeObject();
189 public static function xmlMenu() {
191 // lets build the menu for all components
192 $info = self
::getComponents(TRUE);
195 foreach ($info as $name => $comp) {
196 $files = array_merge($files,
207 public static function &menu() {
208 $info = self
::_info();
210 foreach ($info as $name => $comp) {
211 $mnu = $comp->getMenuObject();
213 $ret = $mnu->permissioned();
214 $items = array_merge($items, $ret);
216 $ret = $mnu->main($task);
217 $items = array_merge($items, $ret);
224 * @param bool $oldMode
226 public static function addConfig(&$config, $oldMode = FALSE) {
227 $info = self
::_info();
229 foreach ($info as $name => $comp) {
230 $cfg = $comp->getConfigObject();
231 $cfg->add($config, $oldMode);
237 * @param string $componentName
241 public static function getComponentID($componentName) {
242 $info = self
::_info();
243 if (!empty($info[$componentName])) {
244 return $info[$componentName]->componentID
;
252 * @param int $componentID
254 * @return int|null|string
256 public static function getComponentName($componentID) {
257 $info = self
::_info();
259 $componentName = NULL;
260 foreach ($info as $compName => $component) {
261 if ($component->componentID
== $componentID) {
262 $componentName = $compName;
267 return $componentName;
273 public static function &getQueryFields() {
274 $info = self
::_info();
276 foreach ($info as $name => $comp) {
277 if ($comp->usesSearch()) {
278 $bqr = $comp->getBAOQueryObject();
279 $flds = $bqr->getFields();
280 $fields = array_merge($fields, $flds);
288 * @param string $fnName
290 public static function alterQuery(&$query, $fnName) {
291 $info = self
::_info();
293 foreach ($info as $name => $comp) {
294 if ($comp->usesSearch()) {
295 $bqr = $comp->getBAOQueryObject();
296 $bqr->$fnName($query);
302 * @param string $fieldName
308 public static function from($fieldName, $mode, $side) {
309 $info = self
::_info();
312 foreach ($info as $name => $comp) {
313 if ($comp->usesSearch()) {
314 $bqr = $comp->getBAOQueryObject();
315 $from = $bqr->from($fieldName, $mode, $side);
326 * @param bool $includeCustomFields
330 static function &defaultReturnProperties(
332 $includeCustomFields = TRUE
334 $info = self
::_info();
337 foreach ($info as $name => $comp) {
338 if ($comp->usesSearch()) {
339 $bqr = $comp->getBAOQueryObject();
340 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
350 * @param CRM_Core_Form $form
352 public static function &buildSearchForm(&$form) {
353 $info = self
::_info();
355 foreach ($info as $name => $comp) {
356 if ($comp->usesSearch()) {
357 $bqr = $comp->getBAOQueryObject();
358 $bqr->buildSearchForm($form);
367 public static function searchAction(&$row, $id) {
368 $info = self
::_info();
370 foreach ($info as $name => $comp) {
371 if ($comp->usesSearch()) {
372 $bqr = $comp->getBAOQueryObject();
373 $bqr->searchAction($row, $id);
381 public static function &contactSubTypes() {
382 if (self
::$_contactSubTypes == NULL) {
383 self
::$_contactSubTypes = array();
385 return self
::$_contactSubTypes;
395 public static function &contactSubTypeProperties($subType, $op) {
396 $properties = self
::contactSubTypes();
397 if (array_key_exists($subType, $properties) &&
398 array_key_exists($op, $properties[$subType])
400 return $properties[$subType][$op];
402 return CRM_Core_DAO
::$_nullObject;
406 * FIXME: This function does not appear to do anything. The is_array() check runs on a bunch of objects and (always?) returns false
408 public static function &taskList() {
409 $info = self
::_info();
412 foreach ($info as $name => $value) {
413 if (is_array($info[$name]) && isset($info[$name]['task'])) {
414 $tasks +
= $info[$name]['task'];
421 * Handle table dependencies of components
423 * @param array $tables
429 public static function tableNames(&$tables) {
430 $info = self
::_info();
432 foreach ($info as $name => $comp) {
433 if ($comp->usesSearch()) {
434 $bqr = $comp->getBAOQueryObject();
435 $bqr->tableNames($tables);
441 * Get components info from info file
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;