Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-09-25-01-46-57
[civicrm-core.git] / CRM / Core / Component.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 private static function &_info($force = FALSE) {
50 if (self::$_info == NULL || $force) {
51 self::$_info = array();
52 $c = array();
53
54 $config = CRM_Core_Config::singleton();
55 $c = self::getComponents();
56
57 foreach ($c as $name => $comp) {
58 if (in_array($name, $config->enableComponents)) {
59 self::$_info[$name] = $comp;
60 }
61 }
62 }
63
64 return self::$_info;
65 }
66
67 static function get($name, $attribute = NULL) {
68 $comp = CRM_Utils_Array::value($name, self::_info());
69 if ($attribute) {
70 return CRM_Utils_Array::value($attribute, $comp->info);
71 }
72 return $comp;
73 }
74
75 public static function &getComponents($force = FALSE) {
76 static $_cache = NULL;
77
78 if (!$_cache || $force) {
79 $_cache = array();
80
81 $cr = new CRM_Core_DAO_Component();
82 $cr->find(FALSE);
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}).");
89 }
90 $_cache[$cr->name] = $infoObject;
91 unset($infoObject);
92 }
93 }
94
95 return $_cache;
96 }
97
98 static public function &getEnabledComponents($force = FALSE) {
99 return self::_info($force);
100 }
101
102 public static function &getNames($translated = FALSE) {
103 $allComponents = self::getComponents();
104
105 $names = array();
106 foreach ($allComponents as $name => $comp) {
107 if ($translated) {
108 $names[$comp->componentID] = $comp->info['translatedName'];
109 }
110 else {
111 $names[$comp->componentID] = $name;
112 }
113 }
114 return $names;
115 }
116
117 static function invoke(&$args, $type) {
118 $info = self::_info();
119 $config = CRM_Core_Config::singleton();
120
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')
127 )
128 ) {
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']);
135 }
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);
139 }
140 }
141 $inv = $comp->getInvokeObject();
142 $inv->$type($args);
143 return TRUE;
144 }
145 }
146 return FALSE;
147 }
148
149 static function xmlMenu() {
150
151 // lets build the menu for all components
152 $info = self::getComponents(TRUE);
153
154 $files = array();
155 foreach ($info as $name => $comp) {
156 $files = array_merge($files,
157 $comp->menuFiles()
158 );
159 }
160
161 return $files;
162 }
163
164 static function &menu() {
165 $info = self::_info();
166 $items = array();
167 foreach ($info as $name => $comp) {
168 $mnu = $comp->getMenuObject();
169
170 $ret = $mnu->permissioned();
171 $items = array_merge($items, $ret);
172
173 $ret = $mnu->main($task);
174 $items = array_merge($items, $ret);
175 }
176 return $items;
177 }
178
179 static function addConfig(&$config, $oldMode = FALSE) {
180 $info = self::_info();
181
182 foreach ($info as $name => $comp) {
183 $cfg = $comp->getConfigObject();
184 $cfg->add($config, $oldMode);
185 }
186 return;
187 }
188
189 static function getComponentID($componentName) {
190 $info = self::_info();
191
192 return $info[$componentName]->componentID;
193 }
194
195 static function getComponentName($componentID) {
196 $info = self::_info();
197
198 $componentName = NULL;
199 foreach ($info as $compName => $component) {
200 if ($component->componentID == $componentID) {
201 $componentName = $compName;
202 break;
203 }
204 }
205
206 return $componentName;
207 }
208
209 static function &getQueryFields() {
210 $info = self::_info();
211 $fields = array();
212 foreach ($info as $name => $comp) {
213 if ($comp->usesSearch()) {
214 $bqr = $comp->getBAOQueryObject();
215 $flds = $bqr->getFields();
216 $fields = array_merge($fields, $flds);
217 }
218 }
219 return $fields;
220 }
221
222 static function alterQuery(&$query, $fnName) {
223 $info = self::_info();
224
225 foreach ($info as $name => $comp) {
226 if ($comp->usesSearch()) {
227 $bqr = $comp->getBAOQueryObject();
228 $bqr->$fnName($query);
229 }
230 }
231 }
232
233 static function from($fieldName, $mode, $side) {
234 $info = self::_info();
235
236 $from = NULL;
237 foreach ($info as $name => $comp) {
238 if ($comp->usesSearch()) {
239 $bqr = $comp->getBAOQueryObject();
240 $from = $bqr->from($fieldName, $mode, $side);
241 if ($from) {
242 return $from;
243 }
244 }
245 }
246 return $from;
247 }
248
249 static function &defaultReturnProperties($mode,
250 $includeCustomFields = TRUE
251 ) {
252 $info = self::_info();
253
254 $properties = NULL;
255 foreach ($info as $name => $comp) {
256 if ($comp->usesSearch()) {
257 $bqr = $comp->getBAOQueryObject();
258 $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields);
259 if ($properties) {
260 return $properties;
261 }
262 }
263 }
264 return $properties;
265 }
266
267 static function &buildSearchForm(&$form) {
268 $info = self::_info();
269
270 foreach ($info as $name => $comp) {
271 if ($comp->usesSearch()) {
272 $bqr = $comp->getBAOQueryObject();
273 $bqr->buildSearchForm($form);
274 }
275 }
276 }
277
278 static function &addShowHide(&$showHide) {
279 $info = self::_info();
280
281 foreach ($info as $name => $comp) {
282 if ($comp->usesSearch()) {
283 $bqr = $comp->getBAOQueryObject();
284 $bqr->addShowHide($showHide);
285 }
286 }
287 }
288
289 static function searchAction(&$row, $id) {
290 $info = self::_info();
291
292 foreach ($info as $name => $comp) {
293 if ($comp->usesSearch()) {
294 $bqr = $comp->getBAOQueryObject();
295 $bqr->searchAction($row, $id);
296 }
297 }
298 }
299
300 static function &contactSubTypes() {
301 if (self::$_contactSubTypes == NULL) {
302 self::$_contactSubTypes = array();
303 }
304 return self::$_contactSubTypes;
305 }
306
307
308 static function &contactSubTypeProperties($subType, $op) {
309 $properties = self::contactSubTypes();
310 if (array_key_exists($subType, $properties) &&
311 array_key_exists($op, $properties[$subType])
312 ) {
313 return $properties[$subType][$op];
314 }
315 return CRM_Core_DAO::$_nullObject;
316 }
317
318 static function &taskList() {
319 $info = self::_info();
320
321 $tasks = array();
322 foreach ($info as $name => $value) {
323 if (CRM_Utils_Array::value('task', $info[$name])) {
324 $tasks += $info[$name]['task'];
325 }
326 }
327 return $tasks;
328 }
329
330 /**
331 * Function to handle table dependencies of components
332 *
333 * @param array $tables array of tables
334 *
335 * @return null
336 * @access public
337 * @static
338 */
339 static function tableNames(&$tables) {
340 $info = self::_info();
341
342 foreach ($info as $name => $comp) {
343 if ($comp->usesSearch()) {
344 $bqr = $comp->getBAOQueryObject();
345 $bqr->tableNames($tables);
346 }
347 }
348 }
349
350 /**
351 * Function to get components info from info file
352 *
353 */
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') {
362 continue;
363 }
364
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;
371 unset($infoObject);
372 }
373 }
374 }
375
376 return $components;
377 }
378 }
379