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