comment fixes
[civicrm-core.git] / CRM / Core / Component.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * Component stores all the static and dynamic information of the various
30 * CiviCRM components
31 *
32 * @package CRM
e7112fa7 33 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
34 * $Id$
35 *
36 */
37class CRM_Core_Component {
38
c490a46a
CW
39 /**
40 * End part (filename) of the component information class'es name
41 * that needs to be present in components main directory.
42 */
7da04cde 43 const COMPONENT_INFO_CLASS = 'Info';
6a488035 44
6a488035
TO
45 static $_contactSubTypes = NULL;
46
a0ee3941
EM
47 /**
48 * @param bool $force
49 *
50 * @return array|null
51 */
6a488035 52 private static function &_info($force = FALSE) {
edbcbd96
TO
53 if (!isset(Civi::$statics[__CLASS__]['info'])|| $force) {
54 Civi::$statics[__CLASS__]['info'] = array();
6a488035
TO
55 $c = array();
56
57 $config = CRM_Core_Config::singleton();
58 $c = self::getComponents();
59
60 foreach ($c as $name => $comp) {
61 if (in_array($name, $config->enableComponents)) {
edbcbd96 62 Civi::$statics[__CLASS__]['info'][$name] = $comp;
6a488035
TO
63 }
64 }
65 }
66
edbcbd96 67 return Civi::$statics[__CLASS__]['info'];
6a488035
TO
68 }
69
a0ee3941 70 /**
100fef9d 71 * @param string $name
a0ee3941
EM
72 * @param null $attribute
73 *
74 * @return mixed
75 */
00be9182 76 public static function get($name, $attribute = NULL) {
6a488035
TO
77 $comp = CRM_Utils_Array::value($name, self::_info());
78 if ($attribute) {
79 return CRM_Utils_Array::value($attribute, $comp->info);
80 }
81 return $comp;
82 }
83
a0ee3941
EM
84 /**
85 * @param bool $force
86 *
87 * @return array
88 * @throws Exception
89 */
6a488035 90 public static function &getComponents($force = FALSE) {
edbcbd96
TO
91 if (!isset(Civi::$statics[__CLASS__]['all']) || $force) {
92 Civi::$statics[__CLASS__]['all'] = array();
6a488035
TO
93
94 $cr = new CRM_Core_DAO_Component();
95 $cr->find(FALSE);
96 while ($cr->fetch()) {
97 $infoClass = $cr->namespace . '_' . self::COMPONENT_INFO_CLASS;
2aa397bc 98 require_once str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php';
6a488035
TO
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}).");
102 }
edbcbd96 103 Civi::$statics[__CLASS__]['all'][$cr->name] = $infoObject;
6a488035
TO
104 unset($infoObject);
105 }
106 }
107
edbcbd96 108 return Civi::$statics[__CLASS__]['all'];
6a488035
TO
109 }
110
2ce81580
TO
111 /**
112 * @return array
113 * Array(string $name => int $id).
114 */
115 public static function &getComponentIDs() {
116 $componentIDs = array();
117
118 $cr = new CRM_Core_DAO_Component();
119 $cr->find(FALSE);
120 while ($cr->fetch()) {
121 $componentIDs[$cr->name] = $cr->id;
122 }
123
124 return $componentIDs;
125 }
126
127
a0ee3941
EM
128 /**
129 * @param bool $force
130 *
131 * @return array|null
132 */
6a488035
TO
133 static public function &getEnabledComponents($force = FALSE) {
134 return self::_info($force);
135 }
2dd1b730 136
b516fae1 137 static public function flushEnabledComponents() {
2dd1b730 138 self::getEnabledComponents(TRUE);
3d0e24ec 139 }
6a488035 140
a0ee3941
EM
141 /**
142 * @param bool $translated
143 *
144 * @return array
145 */
6a488035
TO
146 public static function &getNames($translated = FALSE) {
147 $allComponents = self::getComponents();
148
149 $names = array();
150 foreach ($allComponents as $name => $comp) {
151 if ($translated) {
152 $names[$comp->componentID] = $comp->info['translatedName'];
153 }
154 else {
155 $names[$comp->componentID] = $name;
156 }
157 }
158 return $names;
159 }
160
a0ee3941
EM
161 /**
162 * @param $args
163 * @param $type
164 *
165 * @return bool
166 */
00be9182 167 public static function invoke(&$args, $type) {
6a488035
TO
168 $info = self::_info();
169 $config = CRM_Core_Config::singleton();
170
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')
177 )
178 ) {
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);
a7488080 183 if (!empty($comp->info[$name]['formTpl'])) {
6a488035
TO
184 $template->assign('formTpl', $comp->info[$name]['formTpl']);
185 }
a7488080 186 if (!empty($comp->info[$name]['css'])) {
6a488035
TO
187 $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>";
188 CRM_Utils_System::addHTMLHead($styleSheet);
189 }
190 }
191 $inv = $comp->getInvokeObject();
192 $inv->$type($args);
193 return TRUE;
194 }
195 }
196 return FALSE;
197 }
198
a0ee3941
EM
199 /**
200 * @return array
201 */
00be9182 202 public static function xmlMenu() {
6a488035
TO
203
204 // lets build the menu for all components
205 $info = self::getComponents(TRUE);
206
207 $files = array();
208 foreach ($info as $name => $comp) {
209 $files = array_merge($files,
210 $comp->menuFiles()
211 );
212 }
213
214 return $files;
215 }
216
a0ee3941
EM
217 /**
218 * @return array
219 */
00be9182 220 public static function &menu() {
6a488035
TO
221 $info = self::_info();
222 $items = array();
223 foreach ($info as $name => $comp) {
224 $mnu = $comp->getMenuObject();
225
226 $ret = $mnu->permissioned();
227 $items = array_merge($items, $ret);
228
229 $ret = $mnu->main($task);
230 $items = array_merge($items, $ret);
231 }
232 return $items;
233 }
234
a0ee3941 235 /**
100fef9d 236 * @param string $componentName
a0ee3941
EM
237 *
238 * @return mixed
239 */
00be9182 240 public static function getComponentID($componentName) {
6a488035 241 $info = self::_info();
7b60d5b9 242 if (!empty($info[$componentName])) {
243 return $info[$componentName]->componentID;
244 }
245 else {
246 return;
247 }
6a488035
TO
248 }
249
a0ee3941 250 /**
100fef9d 251 * @param int $componentID
a0ee3941
EM
252 *
253 * @return int|null|string
254 */
00be9182 255 public static function getComponentName($componentID) {
6a488035
TO
256 $info = self::_info();
257
258 $componentName = NULL;
259 foreach ($info as $compName => $component) {
260 if ($component->componentID == $componentID) {
261 $componentName = $compName;
262 break;
263 }
264 }
265
266 return $componentName;
267 }
268
a0ee3941
EM
269 /**
270 * @return array
271 */
00be9182 272 public static function &getQueryFields() {
6a488035
TO
273 $info = self::_info();
274 $fields = array();
275 foreach ($info as $name => $comp) {
276 if ($comp->usesSearch()) {
353ffa53
TO
277 $bqr = $comp->getBAOQueryObject();
278 $flds = $bqr->getFields();
6a488035
TO
279 $fields = array_merge($fields, $flds);
280 }
281 }
282 return $fields;
283 }
284
a0ee3941
EM
285 /**
286 * @param $query
100fef9d 287 * @param string $fnName
a0ee3941 288 */
00be9182 289 public static function alterQuery(&$query, $fnName) {
6a488035
TO
290 $info = self::_info();
291
292 foreach ($info as $name => $comp) {
293 if ($comp->usesSearch()) {
294 $bqr = $comp->getBAOQueryObject();
295 $bqr->$fnName($query);
296 }
297 }
298 }
299
a0ee3941 300 /**
100fef9d 301 * @param string $fieldName
a0ee3941
EM
302 * @param $mode
303 * @param $side
304 *
305 * @return null
306 */
00be9182 307 public static function from($fieldName, $mode, $side) {
6a488035
TO
308 $info = self::_info();
309
310 $from = NULL;
311 foreach ($info as $name => $comp) {
312 if ($comp->usesSearch()) {
313 $bqr = $comp->getBAOQueryObject();
314 $from = $bqr->from($fieldName, $mode, $side);
315 if ($from) {
316 return $from;
317 }
318 }
319 }
320 return $from;
321 }
322
a0ee3941
EM
323 /**
324 * @param $mode
325 * @param bool $includeCustomFields
326 *
327 * @return null
328 */
317fceb4 329 public static function &defaultReturnProperties(
f9f40af3 330 $mode,
6a488035
TO
331 $includeCustomFields = TRUE
332 ) {
333 $info = self::_info();
334
335 $properties = NULL;
336 foreach ($info as $name => $comp) {
337 if ($comp->usesSearch()) {
338 $bqr = $comp->getBAOQueryObject();
339 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
340 if ($properties) {
341 return $properties;
342 }
343 }
344 }
345 return $properties;
346 }
347
a0ee3941 348 /**
c490a46a 349 * @param CRM_Core_Form $form
a0ee3941 350 */
00be9182 351 public static function &buildSearchForm(&$form) {
6a488035
TO
352 $info = self::_info();
353
354 foreach ($info as $name => $comp) {
355 if ($comp->usesSearch()) {
356 $bqr = $comp->getBAOQueryObject();
357 $bqr->buildSearchForm($form);
358 }
359 }
360 }
361
a0ee3941
EM
362 /**
363 * @param $row
100fef9d 364 * @param int $id
a0ee3941 365 */
00be9182 366 public static function searchAction(&$row, $id) {
6a488035
TO
367 $info = self::_info();
368
369 foreach ($info as $name => $comp) {
370 if ($comp->usesSearch()) {
371 $bqr = $comp->getBAOQueryObject();
372 $bqr->searchAction($row, $id);
373 }
374 }
375 }
376
a0ee3941
EM
377 /**
378 * @return array|null
379 */
00be9182 380 public static function &contactSubTypes() {
6a488035
TO
381 if (self::$_contactSubTypes == NULL) {
382 self::$_contactSubTypes = array();
383 }
384 return self::$_contactSubTypes;
385 }
386
387
a0ee3941
EM
388 /**
389 * @param $subType
390 * @param $op
391 *
392 * @return null
393 */
00be9182 394 public static function &contactSubTypeProperties($subType, $op) {
6a488035
TO
395 $properties = self::contactSubTypes();
396 if (array_key_exists($subType, $properties) &&
397 array_key_exists($op, $properties[$subType])
398 ) {
399 return $properties[$subType][$op];
400 }
401 return CRM_Core_DAO::$_nullObject;
402 }
403
6767aa00
CW
404 /**
405 * FIXME: This function does not appear to do anything. The is_array() check runs on a bunch of objects and (always?) returns false
406 */
00be9182 407 public static function &taskList() {
6a488035
TO
408 $info = self::_info();
409
410 $tasks = array();
411 foreach ($info as $name => $value) {
6767aa00 412 if (is_array($info[$name]) && isset($info[$name]['task'])) {
6a488035
TO
413 $tasks += $info[$name]['task'];
414 }
415 }
416 return $tasks;
417 }
418
419 /**
0880a9d0 420 * Handle table dependencies of components.
6a488035 421 *
6a0b768e
TO
422 * @param array $tables
423 * Array of tables.
6a488035 424 *
6a488035 425 */
00be9182 426 public static function tableNames(&$tables) {
6a488035
TO
427 $info = self::_info();
428
429 foreach ($info as $name => $comp) {
430 if ($comp->usesSearch()) {
431 $bqr = $comp->getBAOQueryObject();
432 $bqr->tableNames($tables);
433 }
434 }
435 }
436
437 /**
0880a9d0 438 * Get components info from info file.
6a488035 439 */
00be9182 440 public static function getComponentsFromFile($crmFolderDir) {
6a488035
TO
441 $components = array();
442 //traverse CRM folder and check for Info file
948d11bf 443 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
6a488035
TO
444 while ($subDir = readdir($dir)) {
445 // skip the extensions diretory since it has an Info.php file also
446 if ($subDir == 'Extension') {
447 continue;
448 }
449
450 $infoFile = $crmFolderDir . "/{$subDir}/" . self::COMPONENT_INFO_CLASS . '.php';
451 if (file_exists($infoFile)) {
452 $infoClass = 'CRM_' . $subDir . '_' . self::COMPONENT_INFO_CLASS;
2aa397bc 453 require_once str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php';
6a488035
TO
454 $infoObject = new $infoClass(NULL, NULL, NULL);
455 $components[$infoObject->info['name']] = $infoObject;
456 unset($infoObject);
457 }
458 }
459 }
460
461 return $components;
462 }
96025800 463
6a488035 464}