Merge branch '4.4' of https://github.com/civicrm/civicrm-core into 4.5
[civicrm-core.git] / CRM / Core / Component.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Component stores all the static and dynamic information of the various
30 * CiviCRM components
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2014
34 * $Id$
35 *
36 */
37 class CRM_Core_Component {
38
39 /*
40 * End part (filename) of the component information class'es name
41 * that needs to be present in components main directory.
42 */
43 CONST COMPONENT_INFO_CLASS = 'Info';
44
45 private static $_info = NULL;
46
47 static $_contactSubTypes = NULL;
48
49 /**
50 * @param bool $force
51 *
52 * @return array|null
53 */
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
72 /**
73 * @param $name
74 * @param null $attribute
75 *
76 * @return mixed
77 */
78 static function get($name, $attribute = NULL) {
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
86 /**
87 * @param bool $force
88 *
89 * @return array
90 * @throws Exception
91 */
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;
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}).");
106 }
107 $_cache[$cr->name] = $infoObject;
108 unset($infoObject);
109 }
110 }
111
112 return $_cache;
113 }
114
115 /**
116 * @param bool $force
117 *
118 * @return array|null
119 */
120 static public function &getEnabledComponents($force = FALSE) {
121 return self::_info($force);
122 }
123
124 static public function flushEnabledComponents() {
125 self::getEnabledComponents(TRUE);
126 }
127
128 /**
129 * @param bool $translated
130 *
131 * @return array
132 */
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
148 /**
149 * @param $args
150 * @param $type
151 *
152 * @return bool
153 */
154 static function invoke(&$args, $type) {
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);
170 if (!empty($comp->info[$name]['formTpl'])) {
171 $template->assign('formTpl', $comp->info[$name]['formTpl']);
172 }
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);
176 }
177 }
178 $inv = $comp->getInvokeObject();
179 $inv->$type($args);
180 return TRUE;
181 }
182 }
183 return FALSE;
184 }
185
186 /**
187 * @return array
188 */
189 static function xmlMenu() {
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
204 /**
205 * @return array
206 */
207 static function &menu() {
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
222 /**
223 * @param $config
224 * @param bool $oldMode
225 */
226 static function addConfig(&$config, $oldMode = FALSE) {
227 $info = self::_info();
228
229 foreach ($info as $name => $comp) {
230 $cfg = $comp->getConfigObject();
231 $cfg->add($config, $oldMode);
232 }
233 return;
234 }
235
236 /**
237 * @param $componentName
238 *
239 * @return mixed
240 */
241 static function getComponentID($componentName) {
242 $info = self::_info();
243 if (!empty($info[$componentName])) {
244 return $info[$componentName]->componentID;
245 }
246 else {
247 return;
248 }
249 }
250
251 /**
252 * @param $componentID
253 *
254 * @return int|null|string
255 */
256 static function getComponentName($componentID) {
257 $info = self::_info();
258
259 $componentName = NULL;
260 foreach ($info as $compName => $component) {
261 if ($component->componentID == $componentID) {
262 $componentName = $compName;
263 break;
264 }
265 }
266
267 return $componentName;
268 }
269
270 /**
271 * @return array
272 */
273 static function &getQueryFields() {
274 $info = self::_info();
275 $fields = array();
276 foreach ($info as $name => $comp) {
277 if ($comp->usesSearch()) {
278 $bqr = $comp->getBAOQueryObject();
279 $flds = $bqr->getFields();
280 $fields = array_merge($fields, $flds);
281 }
282 }
283 return $fields;
284 }
285
286 /**
287 * @param $query
288 * @param $fnName
289 */
290 static function alterQuery(&$query, $fnName) {
291 $info = self::_info();
292
293 foreach ($info as $name => $comp) {
294 if ($comp->usesSearch()) {
295 $bqr = $comp->getBAOQueryObject();
296 $bqr->$fnName($query);
297 }
298 }
299 }
300
301 /**
302 * @param $fieldName
303 * @param $mode
304 * @param $side
305 *
306 * @return null
307 */
308 static function from($fieldName, $mode, $side) {
309 $info = self::_info();
310
311 $from = NULL;
312 foreach ($info as $name => $comp) {
313 if ($comp->usesSearch()) {
314 $bqr = $comp->getBAOQueryObject();
315 $from = $bqr->from($fieldName, $mode, $side);
316 if ($from) {
317 return $from;
318 }
319 }
320 }
321 return $from;
322 }
323
324 /**
325 * @param $mode
326 * @param bool $includeCustomFields
327 *
328 * @return null
329 */
330 static function &defaultReturnProperties($mode,
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
348 /**
349 * @param $form
350 */
351 static function &buildSearchForm(&$form) {
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
362 /**
363 * @param $row
364 * @param $id
365 */
366 static function searchAction(&$row, $id) {
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
377 /**
378 * @return array|null
379 */
380 static function &contactSubTypes() {
381 if (self::$_contactSubTypes == NULL) {
382 self::$_contactSubTypes = array();
383 }
384 return self::$_contactSubTypes;
385 }
386
387
388 /**
389 * @param $subType
390 * @param $op
391 *
392 * @return null
393 */
394 static function &contactSubTypeProperties($subType, $op) {
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
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 */
407 static function &taskList() {
408 $info = self::_info();
409
410 $tasks = array();
411 foreach ($info as $name => $value) {
412 if (is_array($info[$name]) && isset($info[$name]['task'])) {
413 $tasks += $info[$name]['task'];
414 }
415 }
416 return $tasks;
417 }
418
419 /**
420 * Function to handle table dependencies of components
421 *
422 * @param array $tables array of tables
423 *
424 * @return null
425 * @access public
426 * @static
427 */
428 static function tableNames(&$tables) {
429 $info = self::_info();
430
431 foreach ($info as $name => $comp) {
432 if ($comp->usesSearch()) {
433 $bqr = $comp->getBAOQueryObject();
434 $bqr->tableNames($tables);
435 }
436 }
437 }
438
439 /**
440 * Function to get components info from info file
441 *
442 */
443 static function getComponentsFromFile($crmFolderDir) {
444 $components = array();
445 //traverse CRM folder and check for Info file
446 if (is_dir($crmFolderDir)) {
447 $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') {
451 continue;
452 }
453
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;
460 unset($infoObject);
461 }
462 }
463 }
464
465 return $components;
466 }
467 }
468