Merge pull request #4087 from robinmitra/master
[civicrm-core.git] / CRM / Utils / System / Base.php
1 <?php
2
3 /**
4 * Base class for UF system integrations
5 */
6 abstract class CRM_Utils_System_Base {
7 /**
8 * Deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific
9 * functions and leave the codebase oblivious to the type of CMS
10 * @deprecated
11 * @var bool
12 */
13 var $is_drupal = FALSE;
14
15 /**
16 * Deprecated property to check if this is a joomla install. The correct method is to have functions on the UF classes for all UF specific
17 * functions and leave the codebase oblivious to the type of CMS
18 * @deprecated
19 * @var bool
20 */
21 var $is_joomla = FALSE;
22
23 /**
24 * deprecated property to check if this is a wordpress install. The correct method is to have functions on the UF classes for all UF specific
25 * functions and leave the codebase oblivious to the type of CMS
26 * @deprecated
27 * @var bool
28 */
29 var $is_wordpress = FALSE;
30
31 /**
32 * Does this CMS / UF support a CMS specific logging mechanism?
33 * @todo - we should think about offering up logging mechanisms in a way that is also extensible by extensions
34 * @var bool
35 */
36 var $supports_UF_Logging = FALSE;
37
38 /*
39 * Does the CMS allow CMS forms to be extended by hooks
40 */
41 var $supports_form_extensions = FALSE;
42
43 /**
44 * If we are using a theming system, invoke theme, else just print the
45 * content
46 *
47 * @param string $content the content that will be themed
48 * @param boolean $print are we displaying to the screen or bypassing theming?
49 * @param boolean $maintenance for maintenance mode
50 *
51 * @return void prints content on stdout
52 * @access public
53 */
54 function theme(&$content, $print = FALSE, $maintenance = FALSE) {
55 $ret = FALSE;
56
57 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
58 // but the parts should be copied into cleaner subclass implementations
59 $config = CRM_Core_Config::singleton();
60 if (
61 $config->userSystem->is_drupal &&
62 function_exists('theme') &&
63 !$print
64 ) {
65 if ($maintenance) {
66 drupal_set_breadcrumb('');
67 drupal_maintenance_theme();
68 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
69 CRM_Utils_System::addHTMLHead($region->render(''));
70 }
71 print theme('maintenance_page', array('content' => $content));
72 exit();
73 }
74 $ret = TRUE; // TODO: Figure out why D7 returns but everyone else prints
75 }
76 $out = $content;
77
78 $config = &CRM_Core_Config::singleton();
79 if (
80 !$print &&
81 $config->userFramework == 'WordPress'
82 ) {
83 if (is_admin()) {
84 require_once (ABSPATH . 'wp-admin/admin-header.php');
85 }
86 else {
87 // FIX ME: we need to figure out to replace civicrm content on the frontend pages
88 }
89 }
90
91 if ($ret) {
92 return $out;
93 }
94 else {
95 print $out;
96 }
97 }
98
99 /**
100 * @return string
101 */
102 function getDefaultBlockLocation() {
103 return 'left';
104 }
105
106 /**
107 * @return string
108 */
109 function getVersion() {
110 return 'Unknown';
111 }
112
113 /**
114 * Format the url as per language Negotiation.
115 *
116 * @param string $url
117 *
118 * @param bool $addLanguagePart
119 * @param bool $removeLanguagePart
120 *
121 * @return string $url, formatted url.
122 * @static
123 */
124 function languageNegotiationURL(
125 $url,
126 $addLanguagePart = TRUE,
127 $removeLanguagePart = FALSE
128 ) {
129 return $url;
130 }
131
132 /**
133 * Determine the location of the CMS root.
134 *
135 * @return string|NULL local file system path to CMS root, or NULL if it cannot be determined
136 */
137 function cmsRootPath() {
138 return NULL;
139 }
140
141 /**
142 * Get user login URL for hosting CMS (method declared in each CMS system class)
143 *
144 * @param string $destination - if present, add destination to querystring (works for Drupal only)
145 *
146 * @return string - loginURL for the current CMS
147 * @static
148 */
149 public abstract function getLoginURL($destination = '');
150
151 /**
152 * Determine the native ID of the CMS user
153 *
154 * @param string $username
155 *
156 * @throws CRM_Core_Exception
157 * @return int|NULL
158 */
159 function getUfId($username) {
160 $className = get_class($this);
161 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
162 }
163
164 /**
165 * Set a init session with user object
166 *
167 * @param array $data array with user specific data
168 *
169 * @access public
170 */
171 function setUserSession($data) {
172 list($userID, $ufID) = $data;
173 $session = CRM_Core_Session::singleton();
174 $session->set('ufID', $ufID);
175 $session->set('userID', $userID);
176 }
177
178 /**
179 * Reset any system caches that may be required for proper CiviCRM
180 * integration.
181 */
182 function flush() {
183 // nullop by default
184 }
185
186 /**
187 * Flush css/js caches
188 */
189 function clearResourceCache() {
190 // nullop by default
191 }
192
193 /**
194 * Return default Site Settings
195 *
196 * @param $dir
197 *
198 * @return array array
199 * - $url, (Joomla - non admin url)
200 * - $siteName,
201 * - $siteRoot
202 */
203 function getDefaultSiteSettings($dir) {
204 $config = CRM_Core_Config::singleton();
205 $url = $config->userFrameworkBaseURL;
206 return array($url, NULL, NULL);
207 }
208
209 /**
210 * Perform any post login activities required by the CMS -
211 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
212 * calls hook_user op 'login' and generates a new session.
213 *
214 * @param array params
215 *
216 * FIXME: Document values accepted/required by $params
217 */
218 function userLoginFinalize($params = array()){
219 }
220
221 /**
222 * Set timezone in mysql so that timestamp fields show the correct time
223 */
224 function setMySQLTimeZone(){
225 $timeZoneOffset = $this->getTimeZoneOffset();
226 if($timeZoneOffset){
227 $sql = "SET time_zone = '$timeZoneOffset'";
228 CRM_Core_DAO::executequery($sql);
229 }
230 }
231
232
233 /**
234 * Get timezone from CMS
235 * @return boolean|string
236 */
237 /**
238 * Get timezone from Drupal
239 * @return boolean|string
240 */
241 function getTimeZoneOffset(){
242 $timezone = $this->getTimeZoneString();
243 if($timezone){
244 $tzObj = new DateTimeZone($timezone);
245 $dateTime = new DateTime("now", $tzObj);
246 $tz = $tzObj->getOffset($dateTime);
247
248 if(empty($tz)){
249 return false;
250 }
251
252 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz/60)%60));
253
254 if($timeZoneOffset > 0){
255 $timeZoneOffset = '+' . $timeZoneOffset;
256 }
257 return $timeZoneOffset;
258 }
259 }
260
261 /**
262 * Over-ridable function to get timezone as a string eg.
263 * @return string Timezone e.g. 'America/Los_Angeles'
264 */
265 function getTimeZoneString() {
266 return date_default_timezone_get();
267 }
268
269 /**
270 * Get Unique Identifier from UserFramework system (CMS)
271 * @param object $user object as described by the User Framework
272 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
273 *
274 */
275 function getUniqueIdentifierFromUserObject($user) {}
276
277 /**
278 * Get User ID from UserFramework system (CMS)
279 * @param object $user object as described by the User Framework
280 * @return mixed <NULL, number>
281 *
282 */
283 function getUserIDFromUserObject($user) {}
284
285 /**
286 * Get currently logged in user uf id.
287 *
288 * @return int $userID logged in user uf id.
289 */
290 function getLoggedInUfID() {}
291
292 /**
293 * Get currently logged in user unique identifier - this tends to be the email address or user name.
294 *
295 * @return string $userID logged in user unique identifier
296 */
297 function getLoggedInUniqueIdentifier() {}
298
299 /**
300 * Return a UFID (user account ID from the UserFramework / CMS system being based on the user object
301 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
302 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling
303 * the function
304 *
305 * Note there is already a function getUFId which takes $username as a param - we could add $user
306 * as a second param to it but it seems messy - just overloading it because the name is taken
307 * @param object $user
308 * @return int $ufid - user ID of UF System
309 */
310 function getBestUFID($user = NULL) {
311 if($user) {
312 return $this->getUserIDFromUserObject($user);
313 }
314 return $this->getLoggedInUfID();
315 }
316
317 /**
318 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
319 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
320 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the unique identifier before calling
321 * the function
322 *
323 * @param object $user
324 * @return string $uniqueIdentifier - unique identifier from the UF System
325 */
326 function getBestUFUniqueIdentifier($user = NULL) {
327 if($user) {
328 return $this->getUniqueIdentifierFromUserObject($user);
329 }
330 return $this->getLoggedInUniqueIdentifier();
331 }
332
333 /**
334 * Get Url to view user record
335 * @param integer $contactID Contact ID
336 *
337 * @return string
338 */
339 function getUserRecordUrl($contactID) {
340 return NULL;
341 }
342 /**
343 * Is the current user permitted to add a user
344 * @return bool
345 */
346 function checkPermissionAddUser() {
347 return FALSE;
348 }
349
350 /**
351 * Output code from error function
352 * @param string $content
353 */
354 function outputError($content) {
355 echo CRM_Utils_System::theme($content);
356 }
357
358 /**
359 * Log error to CMS
360 */
361 function logger($message) {
362
363 }
364
365 /**
366 * Append to coreResourcesList
367 */
368 function appendCoreResources(&$list) {}
369 }
370