comment fixes
[civicrm-core.git] / CRM / Core / Component.php
... / ...
CommitLineData
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
34 * $Id$
35 *
36 */
37class 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 static $_contactSubTypes = NULL;
46
47 /**
48 * @param bool $force
49 *
50 * @return array|null
51 */
52 private static function &_info($force = FALSE) {
53 if (!isset(Civi::$statics[__CLASS__]['info'])|| $force) {
54 Civi::$statics[__CLASS__]['info'] = array();
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)) {
62 Civi::$statics[__CLASS__]['info'][$name] = $comp;
63 }
64 }
65 }
66
67 return Civi::$statics[__CLASS__]['info'];
68 }
69
70 /**
71 * @param string $name
72 * @param null $attribute
73 *
74 * @return mixed
75 */
76 public static function get($name, $attribute = NULL) {
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
84 /**
85 * @param bool $force
86 *
87 * @return array
88 * @throws Exception
89 */
90 public static function &getComponents($force = FALSE) {
91 if (!isset(Civi::$statics[__CLASS__]['all']) || $force) {
92 Civi::$statics[__CLASS__]['all'] = array();
93
94 $cr = new CRM_Core_DAO_Component();
95 $cr->find(FALSE);
96 while ($cr->fetch()) {
97 $infoClass = $cr->namespace . '_' . self::COMPONENT_INFO_CLASS;
98 require_once str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php';
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 }
103 Civi::$statics[__CLASS__]['all'][$cr->name] = $infoObject;
104 unset($infoObject);
105 }
106 }
107
108 return Civi::$statics[__CLASS__]['all'];
109 }
110
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
128 /**
129 * @param bool $force
130 *
131 * @return array|null
132 */
133 static public function &getEnabledComponents($force = FALSE) {
134 return self::_info($force);
135 }
136
137 static public function flushEnabledComponents() {
138 self::getEnabledComponents(TRUE);
139 }
140
141 /**
142 * @param bool $translated
143 *
144 * @return array
145 */
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
161 /**
162 * @param $args
163 * @param $type
164 *
165 * @return bool
166 */
167 public static function invoke(&$args, $type) {
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);
183 if (!empty($comp->info[$name]['formTpl'])) {
184 $template->assign('formTpl', $comp->info[$name]['formTpl']);
185 }
186 if (!empty($comp->info[$name]['css'])) {
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
199 /**
200 * @return array
201 */
202 public static function xmlMenu() {
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
217 /**
218 * @return array
219 */
220 public static function &menu() {
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
235 /**
236 * @param string $componentName
237 *
238 * @return mixed
239 */
240 public static function getComponentID($componentName) {
241 $info = self::_info();
242 if (!empty($info[$componentName])) {
243 return $info[$componentName]->componentID;
244 }
245 else {
246 return;
247 }
248 }
249
250 /**
251 * @param int $componentID
252 *
253 * @return int|null|string
254 */
255 public static function getComponentName($componentID) {
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
269 /**
270 * @return array
271 */
272 public static function &getQueryFields() {
273 $info = self::_info();
274 $fields = array();
275 foreach ($info as $name => $comp) {
276 if ($comp->usesSearch()) {
277 $bqr = $comp->getBAOQueryObject();
278 $flds = $bqr->getFields();
279 $fields = array_merge($fields, $flds);
280 }
281 }
282 return $fields;
283 }
284
285 /**
286 * @param $query
287 * @param string $fnName
288 */
289 public static function alterQuery(&$query, $fnName) {
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
300 /**
301 * @param string $fieldName
302 * @param $mode
303 * @param $side
304 *
305 * @return null
306 */
307 public static function from($fieldName, $mode, $side) {
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
323 /**
324 * @param $mode
325 * @param bool $includeCustomFields
326 *
327 * @return null
328 */
329 public static function &defaultReturnProperties(
330 $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 CRM_Core_Form $form
350 */
351 public 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 int $id
365 */
366 public 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 public 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 public 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 public 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 * Handle table dependencies of components.
421 *
422 * @param array $tables
423 * Array of tables.
424 *
425 */
426 public static function tableNames(&$tables) {
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 /**
438 * Get components info from info file.
439 */
440 public static function getComponentsFromFile($crmFolderDir) {
441 $components = array();
442 //traverse CRM folder and check for Info file
443 if (is_dir($crmFolderDir) && $dir = opendir($crmFolderDir)) {
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;
453 require_once str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php';
454 $infoObject = new $infoClass(NULL, NULL, NULL);
455 $components[$infoObject->info['name']] = $infoObject;
456 unset($infoObject);
457 }
458 }
459 }
460
461 return $components;
462 }
463
464}