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