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