3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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;
49 private static function &_info($force = FALSE) {
50 if (self
::$_info == NULL ||
$force) {
51 self
::$_info = array();
54 $config = CRM_Core_Config
::singleton();
55 $c = self
::getComponents();
57 foreach ($c as $name => $comp) {
58 if (in_array($name, $config->enableComponents
)) {
59 self
::$_info[$name] = $comp;
67 static function get($name, $attribute = NULL) {
68 $comp = CRM_Utils_Array
::value($name, self
::_info());
70 return CRM_Utils_Array
::value($attribute, $comp->info
);
75 public static function &getComponents($force = FALSE) {
76 static $_cache = NULL;
78 if (!$_cache ||
$force) {
81 $cr = new CRM_Core_DAO_Component();
83 while ($cr->fetch()) {
84 $infoClass = $cr->namespace . '_' . self
::COMPONENT_INFO_CLASS
;
85 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php');
86 $infoObject = new $infoClass($cr->name
, $cr->namespace, $cr->id
);
87 if ($infoObject->info
['name'] !== $cr->name
) {
88 CRM_Core_Error
::fatal("There is a discrepancy between name in component registry and in info file ({$cr->name}).");
90 $_cache[$cr->name
] = $infoObject;
98 static public function &getEnabledComponents($force = FALSE) {
99 return self
::_info($force);
102 public static function &getNames($translated = FALSE) {
103 $allComponents = self
::getComponents();
106 foreach ($allComponents as $name => $comp) {
108 $names[$comp->componentID
] = $comp->info
['translatedName'];
111 $names[$comp->componentID
] = $name;
117 static function invoke(&$args, $type) {
118 $info = self
::_info();
119 $config = CRM_Core_Config
::singleton();
121 $firstArg = CRM_Utils_Array
::value(1, $args, '');
122 $secondArg = CRM_Utils_Array
::value(2, $args, '');
123 foreach ($info as $name => $comp) {
124 if (in_array($name, $config->enableComponents
) &&
125 (($comp->info
['url'] === $firstArg && $type == 'main') ||
126 ($comp->info
['url'] === $secondArg && $type == 'admin')
129 if ($type == 'main') {
130 // also set the smarty variables to the current component
131 $template = CRM_Core_Smarty
::singleton();
132 $template->assign('activeComponent', $name);
133 if (CRM_Utils_Array
::value('formTpl', $comp->info
[$name])) {
134 $template->assign('formTpl', $comp->info
[$name]['formTpl']);
136 if (CRM_Utils_Array
::value('css', $comp->info
[$name])) {
137 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
138 CRM_Utils_System
::addHTMLHead($styleSheet);
141 $inv = $comp->getInvokeObject();
149 static function xmlMenu() {
151 // lets build the menu for all components
152 $info = self
::getComponents(TRUE);
155 foreach ($info as $name => $comp) {
156 $files = array_merge($files,
164 static function &menu() {
165 $info = self
::_info();
167 foreach ($info as $name => $comp) {
168 $mnu = $comp->getMenuObject();
170 $ret = $mnu->permissioned();
171 $items = array_merge($items, $ret);
173 $ret = $mnu->main($task);
174 $items = array_merge($items, $ret);
179 static function addConfig(&$config, $oldMode = FALSE) {
180 $info = self
::_info();
182 foreach ($info as $name => $comp) {
183 $cfg = $comp->getConfigObject();
184 $cfg->add($config, $oldMode);
189 static function getComponentID($componentName) {
190 $info = self
::_info();
192 return $info[$componentName]->componentID
;
195 static function getComponentName($componentID) {
196 $info = self
::_info();
198 $componentName = NULL;
199 foreach ($info as $compName => $component) {
200 if ($component->componentID
== $componentID) {
201 $componentName = $compName;
206 return $componentName;
209 static function &getQueryFields() {
210 $info = self
::_info();
212 foreach ($info as $name => $comp) {
213 if ($comp->usesSearch()) {
214 $bqr = $comp->getBAOQueryObject();
215 $flds = $bqr->getFields();
216 $fields = array_merge($fields, $flds);
222 static function alterQuery(&$query, $fnName) {
223 $info = self
::_info();
225 foreach ($info as $name => $comp) {
226 if ($comp->usesSearch()) {
227 $bqr = $comp->getBAOQueryObject();
228 $bqr->$fnName($query);
233 static function from($fieldName, $mode, $side) {
234 $info = self
::_info();
237 foreach ($info as $name => $comp) {
238 if ($comp->usesSearch()) {
239 $bqr = $comp->getBAOQueryObject();
240 $from = $bqr->from($fieldName, $mode, $side);
249 static function &defaultReturnProperties($mode,
250 $includeCustomFields = TRUE
252 $info = self
::_info();
255 foreach ($info as $name => $comp) {
256 if ($comp->usesSearch()) {
257 $bqr = $comp->getBAOQueryObject();
258 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
267 static function &buildSearchForm(&$form) {
268 $info = self
::_info();
270 foreach ($info as $name => $comp) {
271 if ($comp->usesSearch()) {
272 $bqr = $comp->getBAOQueryObject();
273 $bqr->buildSearchForm($form);
278 static function &addShowHide(&$showHide) {
279 $info = self
::_info();
281 foreach ($info as $name => $comp) {
282 if ($comp->usesSearch()) {
283 $bqr = $comp->getBAOQueryObject();
284 $bqr->addShowHide($showHide);
289 static function searchAction(&$row, $id) {
290 $info = self
::_info();
292 foreach ($info as $name => $comp) {
293 if ($comp->usesSearch()) {
294 $bqr = $comp->getBAOQueryObject();
295 $bqr->searchAction($row, $id);
300 static function &contactSubTypes() {
301 if (self
::$_contactSubTypes == NULL) {
302 self
::$_contactSubTypes = array();
304 return self
::$_contactSubTypes;
308 static function &contactSubTypeProperties($subType, $op) {
309 $properties = self
::contactSubTypes();
310 if (array_key_exists($subType, $properties) &&
311 array_key_exists($op, $properties[$subType])
313 return $properties[$subType][$op];
315 return CRM_Core_DAO
::$_nullObject;
318 static function &taskList() {
319 $info = self
::_info();
322 foreach ($info as $name => $value) {
323 if (CRM_Utils_Array
::value('task', $info[$name])) {
324 $tasks +
= $info[$name]['task'];
331 * Function to handle table dependencies of components
333 * @param array $tables array of tables
339 static function tableNames(&$tables) {
340 $info = self
::_info();
342 foreach ($info as $name => $comp) {
343 if ($comp->usesSearch()) {
344 $bqr = $comp->getBAOQueryObject();
345 $bqr->tableNames($tables);
351 * Function to get components info from info file
354 static function getComponentsFromFile($crmFolderDir) {
355 $components = array();
356 //traverse CRM folder and check for Info file
357 if (is_dir($crmFolderDir)) {
358 $dir = opendir($crmFolderDir);
359 while ($subDir = readdir($dir)) {
360 // skip the extensions diretory since it has an Info.php file also
361 if ($subDir == 'Extension') {
365 $infoFile = $crmFolderDir . "/{$subDir}/" . self
::COMPONENT_INFO_CLASS
. '.php';
366 if (file_exists($infoFile)) {
367 $infoClass = 'CRM_' . $subDir . '_' . self
::COMPONENT_INFO_CLASS
;
368 require_once (str_replace('_', DIRECTORY_SEPARATOR
, $infoClass) . '.php');
369 $infoObject = new $infoClass(NULL, NULL, NULL);
370 $components[$infoObject->info
['name']] = $infoObject;