Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Component stores all the static and dynamic information of the various | |
30 | * CiviCRM components | |
31 | * | |
32 | * @package CRM | |
06b69b18 | 33 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | } | |
2dd1b730 | 101 | |
b516fae1 | 102 | static public function flushEnabledComponents() { |
2dd1b730 | 103 | self::getEnabledComponents(TRUE); |
3d0e24ec | 104 | } |
6a488035 TO |
105 | |
106 | public static function &getNames($translated = FALSE) { | |
107 | $allComponents = self::getComponents(); | |
108 | ||
109 | $names = array(); | |
110 | foreach ($allComponents as $name => $comp) { | |
111 | if ($translated) { | |
112 | $names[$comp->componentID] = $comp->info['translatedName']; | |
113 | } | |
114 | else { | |
115 | $names[$comp->componentID] = $name; | |
116 | } | |
117 | } | |
118 | return $names; | |
119 | } | |
120 | ||
121 | static function invoke(&$args, $type) { | |
122 | $info = self::_info(); | |
123 | $config = CRM_Core_Config::singleton(); | |
124 | ||
125 | $firstArg = CRM_Utils_Array::value(1, $args, ''); | |
126 | $secondArg = CRM_Utils_Array::value(2, $args, ''); | |
127 | foreach ($info as $name => $comp) { | |
128 | if (in_array($name, $config->enableComponents) && | |
129 | (($comp->info['url'] === $firstArg && $type == 'main') || | |
130 | ($comp->info['url'] === $secondArg && $type == 'admin') | |
131 | ) | |
132 | ) { | |
133 | if ($type == 'main') { | |
134 | // also set the smarty variables to the current component | |
135 | $template = CRM_Core_Smarty::singleton(); | |
136 | $template->assign('activeComponent', $name); | |
a7488080 | 137 | if (!empty($comp->info[$name]['formTpl'])) { |
6a488035 TO |
138 | $template->assign('formTpl', $comp->info[$name]['formTpl']); |
139 | } | |
a7488080 | 140 | if (!empty($comp->info[$name]['css'])) { |
6a488035 TO |
141 | $styleSheets = '<style type="text/css">@import url(' . "{$config->resourceBase}css/{$comp->info[$name]['css']});</style>"; |
142 | CRM_Utils_System::addHTMLHead($styleSheet); | |
143 | } | |
144 | } | |
145 | $inv = $comp->getInvokeObject(); | |
146 | $inv->$type($args); | |
147 | return TRUE; | |
148 | } | |
149 | } | |
150 | return FALSE; | |
151 | } | |
152 | ||
153 | static function xmlMenu() { | |
154 | ||
155 | // lets build the menu for all components | |
156 | $info = self::getComponents(TRUE); | |
157 | ||
158 | $files = array(); | |
159 | foreach ($info as $name => $comp) { | |
160 | $files = array_merge($files, | |
161 | $comp->menuFiles() | |
162 | ); | |
163 | } | |
164 | ||
165 | return $files; | |
166 | } | |
167 | ||
168 | static function &menu() { | |
169 | $info = self::_info(); | |
170 | $items = array(); | |
171 | foreach ($info as $name => $comp) { | |
172 | $mnu = $comp->getMenuObject(); | |
173 | ||
174 | $ret = $mnu->permissioned(); | |
175 | $items = array_merge($items, $ret); | |
176 | ||
177 | $ret = $mnu->main($task); | |
178 | $items = array_merge($items, $ret); | |
179 | } | |
180 | return $items; | |
181 | } | |
182 | ||
183 | static function addConfig(&$config, $oldMode = FALSE) { | |
184 | $info = self::_info(); | |
185 | ||
186 | foreach ($info as $name => $comp) { | |
187 | $cfg = $comp->getConfigObject(); | |
188 | $cfg->add($config, $oldMode); | |
189 | } | |
190 | return; | |
191 | } | |
192 | ||
193 | static function getComponentID($componentName) { | |
194 | $info = self::_info(); | |
195 | ||
196 | return $info[$componentName]->componentID; | |
197 | } | |
198 | ||
199 | static function getComponentName($componentID) { | |
200 | $info = self::_info(); | |
201 | ||
202 | $componentName = NULL; | |
203 | foreach ($info as $compName => $component) { | |
204 | if ($component->componentID == $componentID) { | |
205 | $componentName = $compName; | |
206 | break; | |
207 | } | |
208 | } | |
209 | ||
210 | return $componentName; | |
211 | } | |
212 | ||
213 | static function &getQueryFields() { | |
214 | $info = self::_info(); | |
215 | $fields = array(); | |
216 | foreach ($info as $name => $comp) { | |
217 | if ($comp->usesSearch()) { | |
218 | $bqr = $comp->getBAOQueryObject(); | |
219 | $flds = $bqr->getFields(); | |
220 | $fields = array_merge($fields, $flds); | |
221 | } | |
222 | } | |
223 | return $fields; | |
224 | } | |
225 | ||
226 | static function alterQuery(&$query, $fnName) { | |
227 | $info = self::_info(); | |
228 | ||
229 | foreach ($info as $name => $comp) { | |
230 | if ($comp->usesSearch()) { | |
231 | $bqr = $comp->getBAOQueryObject(); | |
232 | $bqr->$fnName($query); | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | static function from($fieldName, $mode, $side) { | |
238 | $info = self::_info(); | |
239 | ||
240 | $from = NULL; | |
241 | foreach ($info as $name => $comp) { | |
242 | if ($comp->usesSearch()) { | |
243 | $bqr = $comp->getBAOQueryObject(); | |
244 | $from = $bqr->from($fieldName, $mode, $side); | |
245 | if ($from) { | |
246 | return $from; | |
247 | } | |
248 | } | |
249 | } | |
250 | return $from; | |
251 | } | |
252 | ||
253 | static function &defaultReturnProperties($mode, | |
254 | $includeCustomFields = TRUE | |
255 | ) { | |
256 | $info = self::_info(); | |
257 | ||
258 | $properties = NULL; | |
259 | foreach ($info as $name => $comp) { | |
260 | if ($comp->usesSearch()) { | |
261 | $bqr = $comp->getBAOQueryObject(); | |
262 | $properties = $bqr->defaultReturnProperties($mode, $includeCustomFields); | |
263 | if ($properties) { | |
264 | return $properties; | |
265 | } | |
266 | } | |
267 | } | |
268 | return $properties; | |
269 | } | |
270 | ||
271 | static function &buildSearchForm(&$form) { | |
272 | $info = self::_info(); | |
273 | ||
274 | foreach ($info as $name => $comp) { | |
275 | if ($comp->usesSearch()) { | |
276 | $bqr = $comp->getBAOQueryObject(); | |
277 | $bqr->buildSearchForm($form); | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
6a488035 TO |
282 | static function searchAction(&$row, $id) { |
283 | $info = self::_info(); | |
284 | ||
285 | foreach ($info as $name => $comp) { | |
286 | if ($comp->usesSearch()) { | |
287 | $bqr = $comp->getBAOQueryObject(); | |
288 | $bqr->searchAction($row, $id); | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
293 | static function &contactSubTypes() { | |
294 | if (self::$_contactSubTypes == NULL) { | |
295 | self::$_contactSubTypes = array(); | |
296 | } | |
297 | return self::$_contactSubTypes; | |
298 | } | |
299 | ||
300 | ||
301 | static function &contactSubTypeProperties($subType, $op) { | |
302 | $properties = self::contactSubTypes(); | |
303 | if (array_key_exists($subType, $properties) && | |
304 | array_key_exists($op, $properties[$subType]) | |
305 | ) { | |
306 | return $properties[$subType][$op]; | |
307 | } | |
308 | return CRM_Core_DAO::$_nullObject; | |
309 | } | |
310 | ||
6767aa00 CW |
311 | /** |
312 | * FIXME: This function does not appear to do anything. The is_array() check runs on a bunch of objects and (always?) returns false | |
313 | */ | |
6a488035 TO |
314 | static function &taskList() { |
315 | $info = self::_info(); | |
316 | ||
317 | $tasks = array(); | |
318 | foreach ($info as $name => $value) { | |
6767aa00 | 319 | if (is_array($info[$name]) && isset($info[$name]['task'])) { |
6a488035 TO |
320 | $tasks += $info[$name]['task']; |
321 | } | |
322 | } | |
323 | return $tasks; | |
324 | } | |
325 | ||
326 | /** | |
327 | * Function to handle table dependencies of components | |
328 | * | |
329 | * @param array $tables array of tables | |
330 | * | |
331 | * @return null | |
332 | * @access public | |
333 | * @static | |
334 | */ | |
335 | static function tableNames(&$tables) { | |
336 | $info = self::_info(); | |
337 | ||
338 | foreach ($info as $name => $comp) { | |
339 | if ($comp->usesSearch()) { | |
340 | $bqr = $comp->getBAOQueryObject(); | |
341 | $bqr->tableNames($tables); | |
342 | } | |
343 | } | |
344 | } | |
345 | ||
346 | /** | |
347 | * Function to get components info from info file | |
348 | * | |
349 | */ | |
350 | static function getComponentsFromFile($crmFolderDir) { | |
351 | $components = array(); | |
352 | //traverse CRM folder and check for Info file | |
353 | if (is_dir($crmFolderDir)) { | |
354 | $dir = opendir($crmFolderDir); | |
355 | while ($subDir = readdir($dir)) { | |
356 | // skip the extensions diretory since it has an Info.php file also | |
357 | if ($subDir == 'Extension') { | |
358 | continue; | |
359 | } | |
360 | ||
361 | $infoFile = $crmFolderDir . "/{$subDir}/" . self::COMPONENT_INFO_CLASS . '.php'; | |
362 | if (file_exists($infoFile)) { | |
363 | $infoClass = 'CRM_' . $subDir . '_' . self::COMPONENT_INFO_CLASS; | |
364 | require_once (str_replace('_', DIRECTORY_SEPARATOR, $infoClass) . '.php'); | |
365 | $infoObject = new $infoClass(NULL, NULL, NULL); | |
366 | $components[$infoObject->info['name']] = $infoObject; | |
367 | unset($infoObject); | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | return $components; | |
373 | } | |
374 | } | |
375 |