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