Merge pull request #4836 from colemanw/edit
[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 (!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 }
101 if (is_admin()) {
102 require_once (ABSPATH . 'wp-admin/admin-header.php');
103 }
104 else {
105 // FIXME: we need to figure out to replace civicrm content on the frontend pages
106 }
107 }
108
109 if ($ret) {
110 return $out;
111 }
112 else {
113 print $out;
114 return NULL;
115 }
116 }
117
118 /**
119 * @return string
120 */
121 public function getDefaultBlockLocation() {
122 return 'left';
123 }
124
125 /**
126 * @return string
127 */
128 public function getVersion() {
129 return 'Unknown';
130 }
131
132 /**
133 * Format the url as per language Negotiation.
134 *
135 * @param string $url
136 * @param bool $addLanguagePart
137 * @param bool $removeLanguagePart
138 *
139 * @return string
140 * Formatted url.
141 * @static
142 */
143 function languageNegotiationURL(
144 $url,
145 $addLanguagePart = TRUE,
146 $removeLanguagePart = FALSE
147 ) {
148 return $url;
149 }
150
151 /**
152 * Determine the location of the CMS root.
153 *
154 * @return string|null
155 * Local file system path to CMS root, or NULL if it cannot be determined
156 */
157 public function cmsRootPath() {
158 return NULL;
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
171 /**
172 * Determine the native ID of the CMS user
173 *
174 * @param string $username
175 *
176 * @throws CRM_Core_Exception
177 * @return int|NULL
178 */
179 public function getUfId($username) {
180 $className = get_class($this);
181 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
182 }
183
184 /**
185 * Set a init session with user object
186 *
187 * @param array $data
188 * Array with user specific data
189 */
190 public function setUserSession($data) {
191 list($userID, $ufID) = $data;
192 $session = CRM_Core_Session::singleton();
193 $session->set('ufID', $ufID);
194 $session->set('userID', $userID);
195 }
196
197 /**
198 * Reset any system caches that may be required for proper CiviCRM
199 * integration.
200 */
201 public function flush() {
202 // nullop by default
203 }
204
205 /**
206 * Flush css/js caches
207 */
208 public function clearResourceCache() {
209 // nullop by default
210 }
211
212 /**
213 * Return default Site Settings
214 *
215 * @param string $dir
216 *
217 * @return array
218 * - $url, (Joomla - non admin url)
219 * - $siteName,
220 * - $siteRoot
221 */
222 public function getDefaultSiteSettings($dir) {
223 $config = CRM_Core_Config::singleton();
224 $url = $config->userFrameworkBaseURL;
225 return array($url, NULL, NULL);
226 }
227
228 /**
229 * Perform any post login activities required by the CMS -
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.
232 *
233 * @param array $params
234 *
235 * FIXME: Document values accepted/required by $params
236 */
237 public function userLoginFinalize($params = array()){
238 }
239
240 /**
241 * Set timezone in mysql so that timestamp fields show the correct time
242 */
243 public function setMySQLTimeZone(){
244 $timeZoneOffset = $this->getTimeZoneOffset();
245 if($timeZoneOffset){
246 $sql = "SET time_zone = '$timeZoneOffset'";
247 CRM_Core_DAO::executequery($sql);
248 }
249 }
250
251
252 /**
253 * Get timezone from CMS
254 *
255 * @return string|false|null
256 */
257 public function getTimeZoneOffset(){
258 $timezone = $this->getTimeZoneString();
259 if ($timezone) {
260 $tzObj = new DateTimeZone($timezone);
261 $dateTime = new DateTime("now", $tzObj);
262 $tz = $tzObj->getOffset($dateTime);
263
264 if (empty($tz)) {
265 return FALSE;
266 }
267
268 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz/60)%60));
269
270 if ($timeZoneOffset > 0) {
271 $timeZoneOffset = '+' . $timeZoneOffset;
272 }
273 return $timeZoneOffset;
274 }
275 return NULL;
276 }
277
278 /**
279 * Over-ridable function to get timezone as a string eg.
280 *
281 * @return string
282 * Time zone, e.g. 'America/Los_Angeles'
283 */
284 public function getTimeZoneString() {
285 return date_default_timezone_get();
286 }
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 */
294 public function getUniqueIdentifierFromUserObject($user) {}
295
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 */
302 public function getUserIDFromUserObject($user) {}
303
304 /**
305 * Get currently logged in user uf id.
306 *
307 * @return int $userID logged in user uf id.
308 */
309 public function getLoggedInUfID() {}
310
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 */
316 public function getLoggedInUniqueIdentifier() {}
317
318 /**
319 * Return a UFID (user account ID from the UserFramework / CMS system being based on the user object
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
326 * @param object $user
327 * @return int $ufid - user ID of UF System
328 */
329 public function getBestUFID($user = NULL) {
330 if($user) {
331 return $this->getUserIDFromUserObject($user);
332 }
333 return $this->getLoggedInUfID();
334 }
335
336 /**
337 * Return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
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 */
345 public function getBestUFUniqueIdentifier($user = NULL) {
346 if($user) {
347 return $this->getUniqueIdentifierFromUserObject($user);
348 }
349 return $this->getLoggedInUniqueIdentifier();
350 }
351
352 /**
353 * Get Url to view user record
354 * @param integer $contactID Contact ID
355 *
356 * @return string
357 */
358 public function getUserRecordUrl($contactID) {
359 return NULL;
360 }
361 /**
362 * Is the current user permitted to add a user
363 * @return bool
364 */
365 public function checkPermissionAddUser() {
366 return FALSE;
367 }
368
369 /**
370 * Output code from error function
371 * @param string $content
372 */
373 public function outputError($content) {
374 echo CRM_Utils_System::theme($content);
375 }
376
377 /**
378 * Log error to CMS
379 */
380 public function logger($message) {
381
382 }
383
384 /**
385 * Append to coreResourcesList
386 */
387 public function appendCoreResources(&$list) {}
388 }