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