17f128817264fdc7c74190cd1681475b03605dfb
[civicrm-core.git] / CRM / Core / Component / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 * This interface defines methods that need to be implemented
30 * for a component to introduce itself to the system.
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2014
34 * $Id$
35 *
36 */
37
38 abstract class CRM_Core_Component_Info {
39
40 /**
41 * Name of the class (minus component namespace path)
42 * of the component invocation class'es name.
43 */
44 const COMPONENT_INVOKE_CLASS = 'Invoke';
45
46 /**
47 * Name of the class (minus component namespace path)
48 * of the component configuration class'es name.
49 */
50 const COMPONENT_CONFIG_CLASS = 'Config';
51
52 /**
53 * Name of the class (minus component namespace path)
54 * of the component BAO Query class'es name.
55 */
56 const COMPONENT_BAO_QUERY_CLASS = 'BAO_Query';
57
58 /**
59 * Name of the class (minus component namespace path)
60 * of the component user dashboard plugin.
61 */
62 const COMPONENT_USERDASHBOARD_CLASS = 'Page_UserDashboard';
63
64 /**
65 * Name of the class (minus component namespace path)
66 * of the component tab offered to contact record view.
67 */
68 const COMPONENT_TAB_CLASS = 'Page_Tab';
69
70 /**
71 * Name of the class (minus component namespace path)
72 * of the component tab offered to contact record view.
73 */
74 const COMPONENT_ADVSEARCHPANE_CLASS = 'Form_Search_AdvancedSearchPane';
75
76 /**
77 * Name of the directory (assumed in component directory)
78 * where xml resources used by this component live.
79 */
80 const COMPONENT_XML_RESOURCES = 'xml';
81
82 /**
83 * Name of the directory (assumed in xml resources path)
84 * containing component menu definition XML file names.
85 */
86 const COMPONENT_MENU_XML = 'Menu';
87
88 /**
89 * Stores component information.
90 * @var array component settings as key/value pairs
91 */
92 public $info;
93
94 /*
95 * Stores component keyword
96 * @var string name of component keyword
97 */
98 protected $keyword;
99
100 /**
101 * @param string $name
102 * Name of the component.
103 * @param string $namespace
104 * Namespace prefix for component's files.
105 * @param int $componentID
106 */
107 public function __construct($name, $namespace, $componentID) {
108 $this->name = $name;
109 $this->namespace = $namespace;
110 $this->componentID = $componentID;
111 $this->info = $this->getInfo();
112 $this->info['url'] = $this->getKeyword();
113 }
114
115 /**
116 * EXPERIMENTAL: Get a list of AngularJS modules
117 *
118 * @return array list of modules; same format as CRM_Utils_Hook::angularModules(&$angularModules)
119 * @see CRM_Utils_Hook::angularModules
120 */
121 public function getAngularModules() {
122 return array();
123 }
124
125 /**
126 * Provides base information about the component.
127 * Needs to be implemented in component's information
128 * class.
129 *
130 * @return array collection of required component settings
131 *
132 */
133 abstract public function getInfo();
134
135 /**
136 * Get a list of entities to register via API
137 *
138 * @return array list of entities; same format as CRM_Utils_Hook::managedEntities(&$entities)
139 * @see CRM_Utils_Hook::managedEntities
140 */
141 public function getManagedEntities() {
142 return array();
143 }
144
145 /**
146 * Provides permissions that are unwise for Anonymous Roles to have
147 *
148 * @return array list of permissions
149 * @see CRM_Component_Info::getPermissions
150 */
151 public function getAnonymousPermissionWarnings() {
152 return array();
153 }
154
155 /**
156 * Provides permissions that are used by component.
157 * Needs to be implemented in component's information
158 * class.
159 *
160 * NOTE: if using conditionally permission return,
161 * implementation of $getAllUnconditionally is required.
162 *
163 * @param bool $getAllUnconditionally
164 *
165 * @return array|null collection of permissions, null if none
166 */
167 abstract public function getPermissions($getAllUnconditionally = FALSE);
168
169 /**
170 * Determine how many other records refer to a given record
171 *
172 * @param CRM_Core_DAO $dao
173 * The item for which we want a reference count.
174 * @return array each item in the array is an array with keys:
175 * - name: string, eg "sql:civicrm_email:contact_id"
176 * - type: string, eg "sql"
177 * - count: int, eg "5" if there are 5 email addresses that refer to $dao
178 */
179 public function getReferenceCounts($dao) {
180 return array();
181 }
182
183 /**
184 * Provides information about user dashboard element
185 * offered by this component.
186 *
187 * @return array|null collection of required dashboard settings,
188 * null if no element offered
189 *
190 */
191 abstract public function getUserDashboardElement();
192
193 /**
194 * Provides information about user dashboard element
195 * offered by this component.
196 *
197 * @return array|null collection of required dashboard settings,
198 * null if no element offered
199 *
200 */
201 abstract public function registerTab();
202
203 /**
204 * Provides information about advanced search pane
205 * offered by this component.
206 *
207 * @return array|null collection of required pane settings,
208 * null if no element offered
209 *
210 */
211 abstract public function registerAdvancedSearchPane();
212
213 /**
214 * Provides potential activity types that this
215 * component might want to register in activity history.
216 * Needs to be implemented in component's information
217 * class.
218 *
219 * @return array|null collection of activity types
220 *
221 */
222 abstract public function getActivityTypes();
223
224 /**
225 * Provides information whether given component is currently
226 * marked as enabled in configuration.
227 *
228 * @return boolean true if component is enabled, false if not
229 *
230 */
231 public function isEnabled() {
232 $config = CRM_Core_Config::singleton();
233 if (in_array($this->info['name'], $config->enableComponents)) {
234 return TRUE;
235 }
236 return FALSE;
237 }
238
239 /**
240 * Provides component's configuration object.
241 *
242 * @return mixed component's configuration object
243 *
244 */
245 public function getConfigObject() {
246 return $this->_instantiate(self::COMPONENT_CONFIG_CLASS);
247 }
248
249 /**
250 * Provides component's menu definition object.
251 *
252 * @return mixed component's menu definition object
253 *
254 */
255 public function getMenuObject() {
256 return $this->_instantiate(self::COMPONENT_MENU_CLASS);
257 }
258
259 /**
260 * Provides component's invocation object.
261 *
262 * @return mixed component's invocation object
263 *
264 */
265 public function getInvokeObject() {
266 return $this->_instantiate(self::COMPONENT_INVOKE_CLASS);
267 }
268
269 /**
270 * Provides component's BAO Query object.
271 *
272 * @return mixed component's BAO Query object
273 *
274 */
275 public function getBAOQueryObject() {
276 return $this->_instantiate(self::COMPONENT_BAO_QUERY_CLASS);
277 }
278
279 /**
280 * Builds advanced search form's component specific pane.
281 *
282 *
283 */
284 public function buildAdvancedSearchPaneForm(&$form) {
285 $bao = $this->getBAOQueryObject();
286 $bao->buildSearchForm($form);
287 }
288
289 /**
290 * Provides component's user dashboard page object.
291 *
292 * @return mixed component's User Dashboard applet object
293 *
294 */
295 public function getUserDashboardObject() {
296 return $this->_instantiate(self::COMPONENT_USERDASHBOARD_CLASS);
297 }
298
299 /**
300 * Provides component's contact record tab object.
301 *
302 * @return mixed component's contact record tab object
303 *
304 */
305 public function getTabObject() {
306 return $this->_instantiate(self::COMPONENT_TAB_CLASS);
307 }
308
309 /**
310 * Provides component's advanced search pane's template path.
311 *
312 * @return string component's advanced search pane's template path
313 *
314 */
315 public function getAdvancedSearchPaneTemplatePath() {
316 $fullpath = $this->namespace . '_' . self::COMPONENT_ADVSEARCHPANE_CLASS;
317 return str_replace('_', DIRECTORY_SEPARATOR, $fullpath . '.tpl');
318 }
319
320 /**
321 * Provides information whether given component uses system wide search.
322 *
323 * @return boolean true if component needs search integration
324 *
325 */
326 public function usesSearch() {
327 return $this->info['search'] ? TRUE : FALSE;
328 }
329
330 /**
331 * Provides the xml menu files
332 *
333 * @return array array of menu files
334 *
335 */
336 public function menuFiles() {
337 return CRM_Utils_File::getFilesByExtension($this->_getMenuXMLPath(), 'xml');
338 }
339
340 /**
341 * Simple "keyword" getter.
342 * FIXME: It should be protected so the keyword is not
343 * FIXME: accessed from beyond component infrastructure.
344 *
345 * @return string component keyword
346 *
347 */
348 public function getKeyword() {
349 return $this->keyword;
350 }
351
352 /**
353 * Helper for figuring out menu XML file location.
354 *
355 * @return mixed component's element as class instance
356 *
357 */
358 private function _getMenuXMLPath() {
359 global $civicrm_root;
360 $fullpath = $this->namespace . '_' . self::COMPONENT_XML_RESOURCES . '_' . self::COMPONENT_MENU_XML;
361 return CRM_Utils_File::addTrailingSlash($civicrm_root . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $fullpath));
362 }
363
364 /**
365 * Helper for instantiating component's elements.
366 *
367 * @param $cl
368 *
369 * @return mixed component's element as class instance
370 */
371 private function _instantiate($cl) {
372 $className = $this->namespace . '_' . $cl;
373 require_once str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
374 return new $className();
375 }
376 }