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