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