Merge pull request #7449 from johanv/CRM-14920-sort_order_labels_custom_search_2nd_try
[civicrm-core.git] / CRM / Utils / System / Joomla.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
b8c71ffa 35 * Joomla specific stuff goes here.
6a488035
TO
36 */
37class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
bb3a214a 38 /**
b8c71ffa 39 * Class constructor.
bb3a214a 40 */
00be9182 41 public function __construct() {
4caaa696
EM
42 /**
43 * 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
44 * functions and leave the codebase oblivious to the type of CMS
45 * @deprecated
46 * @var bool
47 */
6a488035
TO
48 $this->is_drupal = FALSE;
49 }
50
51 /**
17f443df 52 * @inheritDoc
6a488035 53 */
00be9182 54 public function createUser(&$params, $mail) {
6a488035
TO
55 $baseDir = JPATH_SITE;
56 require_once $baseDir . '/components/com_users/models/registration.php';
57
58 $userParams = JComponentHelper::getParams('com_users');
353ffa53
TO
59 $model = new UsersModelRegistration();
60 $ufID = NULL;
6a488035
TO
61
62 // get the default usertype
63 $userType = $userParams->get('new_usertype');
64 if (!$userType) {
65 $userType = 2;
66 }
67
68 if (isset($params['name'])) {
69 $fullname = trim($params['name']);
70 }
71 elseif (isset($params['contactID'])) {
72 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
73 }
74 else {
75 $fullname = trim($params['cms_name']);
76 }
77
78 // Prepare the values for a new Joomla user.
353ffa53
TO
79 $values = array();
80 $values['name'] = $fullname;
81 $values['username'] = trim($params['cms_name']);
6a488035 82 $values['password1'] = $values['password2'] = $params['cms_pass'];
353ffa53 83 $values['email1'] = $values['email2'] = trim($params[$mail]);
6a488035
TO
84
85 $lang = JFactory::getLanguage();
86 $lang->load('com_users', $baseDir);
87
88 $register = $model->register($values);
89
90 $ufID = JUserHelper::getUserId($values['username']);
91 return $ufID;
92 }
93
f4aaa82a 94 /**
17f443df 95 * @inheritDoc
6a488035 96 */
00be9182 97 public function updateCMSName($ufID, $ufName) {
6a488035
TO
98 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
99 $ufName = CRM_Utils_Type::escape($ufName, 'String');
100
101 $values = array();
e851ce06 102 $user = JUser::getInstance($ufID);
6a488035
TO
103
104 $values['email'] = $ufName;
105 $user->bind($values);
106
107 $user->save();
108 }
109
110 /**
94f9f81a 111 * Check if username and email exists in the Joomla db.
6a488035 112 *
77855840
TO
113 * @param array $params
114 * Array of name and mail values.
115 * @param array $errors
116 * Array of errors.
117 * @param string $emailName
118 * Field label for the 'email'.
6a488035 119 */
00be9182 120 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
6a488035
TO
121 $config = CRM_Core_Config::singleton();
122
353ffa53
TO
123 $dao = new CRM_Core_DAO();
124 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
6a488035
TO
125 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
126 //don't allow the special characters and min. username length is two
127 //regex \\ to match a single backslash would become '/\\\\/'
128 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
129 if ($isNotValid || strlen($name) < 2) {
130 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
131 }
132
6a488035
TO
133 $JUserTable = &JTable::getInstance('User', 'JTable');
134
135 $db = $JUserTable->getDbo();
136 $query = $db->getQuery(TRUE);
137 $query->select('username, email');
138 $query->from($JUserTable->getTableName());
139 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
140 $db->setQuery($query, 0, 10);
141 $users = $db->loadAssocList();
142
94f9f81a 143 $row = array();
6a488035
TO
144 if (count($users)) {
145 $row = $users[0];
146 }
147
148 if (!empty($row)) {
149 $dbName = CRM_Utils_Array::value('username', $row);
150 $dbEmail = CRM_Utils_Array::value('email', $row);
151 if (strtolower($dbName) == strtolower($name)) {
152 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
153 array(1 => $name)
154 );
155 }
156 if (strtolower($dbEmail) == strtolower($email)) {
157 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
89374eb2 158 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
6a488035
TO
159 array(1 => $email, 2 => $resetUrl)
160 );
161 }
162 }
163 }
164
165 /**
17f443df 166 * @inheritDoc
6a488035 167 */
00be9182 168 public function setTitle($title, $pageTitle = NULL) {
6a488035
TO
169 if (!$pageTitle) {
170 $pageTitle = $title;
171 }
172
173 $template = CRM_Core_Smarty::singleton();
174 $template->assign('pageTitle', $pageTitle);
175
176 $document = JFactory::getDocument();
177 $document->setTitle($title);
6a488035
TO
178 }
179
180 /**
17f443df 181 * @inheritDoc
6a488035 182 */
00be9182 183 public function appendBreadCrumb($breadCrumbs) {
6a488035
TO
184 $template = CRM_Core_Smarty::singleton();
185 $bc = $template->get_template_vars('breadcrumb');
186
187 if (is_array($breadCrumbs)) {
188 foreach ($breadCrumbs as $crumbs) {
189 if (stripos($crumbs['url'], 'id%%')) {
190 $args = array('cid', 'mid');
191 foreach ($args as $a) {
192 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
193 FALSE, NULL, $_GET
194 );
195 if ($val) {
196 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
197 }
198 }
199 }
200 $bc[] = $crumbs;
201 }
202 }
203 $template->assign_by_ref('breadcrumb', $bc);
6a488035
TO
204 }
205
206 /**
17f443df 207 * @inheritDoc
6a488035 208 */
00be9182 209 public function resetBreadCrumb() {
6a488035
TO
210 }
211
212 /**
17f443df 213 * @inheritDoc
6a488035 214 */
17f443df 215 public function addHTMLHead($string = NULL) {
6a488035
TO
216 if ($string) {
217 $document = JFactory::getDocument();
218 $document->addCustomTag($string);
219 }
220 }
221
222 /**
17f443df 223 * @inheritDoc
6a488035
TO
224 */
225 public function addStyleUrl($url, $region) {
226 if ($region == 'html-header') {
227 $document = JFactory::getDocument();
228 $document->addStyleSheet($url);
229 return TRUE;
230 }
231 return FALSE;
232 }
233
234 /**
17f443df 235 * @inheritDoc
6a488035
TO
236 */
237 public function addStyle($code, $region) {
238 if ($region == 'html-header') {
239 $document = JFactory::getDocument();
240 $document->addStyleDeclaration($code);
241 return TRUE;
242 }
243 return FALSE;
244 }
245
246 /**
17f443df 247 * @inheritDoc
6a488035 248 */
e7483cbe 249 public function url(
17f443df
CW
250 $path = NULL,
251 $query = NULL,
252 $absolute = FALSE,
253 $fragment = NULL,
17f443df
CW
254 $frontend = FALSE,
255 $forceBackend = FALSE
6a488035 256 ) {
353ffa53 257 $config = CRM_Core_Config::singleton();
c80e2dbf 258 $separator = '&';
353ffa53
TO
259 $Itemid = '';
260 $script = '';
261 $path = CRM_Utils_String::stripPathChars($path);
6a488035
TO
262
263 if ($config->userFrameworkFrontend) {
264 $script = 'index.php';
265 if (JRequest::getVar("Itemid")) {
266 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
267 }
268 }
269
270 if (isset($fragment)) {
271 $fragment = '#' . $fragment;
272 }
273
6a488035
TO
274 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
275
276 if (!empty($query)) {
277 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
278 }
279 else {
280 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
281 }
282
283 // gross hack for joomla, we are in the backend and want to send a frontend url
284 if ($frontend && $config->userFramework == 'Joomla') {
285 // handle both joomla v1.5 and v1.6, CRM-7939
286 $url = str_replace('/administrator/index2.php', '/index.php', $url);
287 $url = str_replace('/administrator/index.php', '/index.php', $url);
288
289 // CRM-8215
290 $url = str_replace('/administrator/', '/index.php', $url);
291 }
292 elseif ($forceBackend) {
293 if (defined('JVERSION')) {
294 $joomlaVersion = JVERSION;
0db6c3e1
TO
295 }
296 else {
e7483cbe 297 $jversion = new JVersion();
6a488035
TO
298 $joomlaVersion = $jversion->getShortVersion();
299 }
300
301 if (version_compare($joomlaVersion, '1.6') >= 0) {
302 $url = str_replace('/index.php', '/administrator/index.php', $url);
303 }
304 }
305 return $url;
306 }
307
6a488035 308 /**
fe482240 309 * Set the email address of the user.
6a488035 310 *
77855840
TO
311 * @param object $user
312 * Handle to the user object.
6a488035 313 */
00be9182 314 public function setEmail(&$user) {
6a488035 315 global $database;
94f9f81a
EW
316 $query = $db->getQuery(TRUE);
317 $query->select($db->quoteName('email'))
318 ->from($db->quoteName('#__users'))
319 ->where($db->quoteName('id') . ' = ' . $user->id);
6a488035
TO
320 $database->setQuery($query);
321 $user->email = $database->loadResult();
322 }
323
324 /**
17f443df 325 * @inheritDoc
6a488035 326 */
17f443df 327 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
6a488035
TO
328 require_once 'DB.php';
329
330 $config = CRM_Core_Config::singleton();
ebc28bab 331 $user = NULL;
6a488035
TO
332
333 if ($loadCMSBootstrap) {
334 $bootStrapParams = array();
335 if ($name && $password) {
336 $bootStrapParams = array(
337 'name' => $name,
338 'pass' => $password,
339 );
340 }
bec3fc7c 341 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
6a488035
TO
342 }
343
344 jimport('joomla.application.component.helper');
345 jimport('joomla.database.table');
c1f3c6da
BS
346 jimport('joomla.user.helper');
347
348 $JUserTable = JTable::getInstance('User', 'JTable');
349
350 $db = $JUserTable->getDbo();
351 $query = $db->getQuery(TRUE);
352 $query->select('id, name, username, email, password');
353 $query->from($JUserTable->getTableName());
354 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
355 $db->setQuery($query, 0, 0);
356 $users = $db->loadObjectList();
357
358 $row = array();
359 if (count($users)) {
360 $row = $users[0];
361 }
6a488035 362
e46506b2 363 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
481a74f4 364 if (!defined('JVERSION')) {
ebc28bab 365 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 366 $jversion = new JVersion();
ebc28bab
BS
367 define('JVERSION', $jversion->getShortVersion());
368 }
6a488035 369
c1f3c6da
BS
370 if (!empty($row)) {
371 $dbPassword = $row->password;
372 $dbId = $row->id;
373 $dbEmail = $row->email;
6a488035 374
481a74f4
TO
375 if (version_compare(JVERSION, '2.5.18', 'lt') ||
376 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
c1f3c6da 377 ) {
ebc28bab 378 // now check password
009eff21
EW
379 list($hash, $salt) = explode(':', $dbPassword);
380 $cryptpass = md5($password . $salt);
381 if ($hash != $cryptpass) {
01c77fa9
EW
382 return FALSE;
383 }
6a488035 384 }
c1f3c6da 385 else {
4f99ca55
TO
386 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
387 return FALSE;
e7292422 388 }
9d735153
BS
389
390 //include additional files required by Joomla 3.2.1+
481a74f4 391 if (version_compare(JVERSION, '3.2.1', 'ge')) {
90eac10a
BS
392 require_once $joomlaBase . '/libraries/cms/application/helper.php';
393 require_once $joomlaBase . '/libraries/cms/application/cms.php';
394 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
9d735153 395 }
6a488035
TO
396 }
397
c1f3c6da 398 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
6a488035
TO
399 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
400 if (!$contactID) {
401 return FALSE;
402 }
403 return array($contactID, $dbId, mt_rand());
404 }
c1f3c6da 405
6a488035
TO
406 return FALSE;
407 }
408
bec3fc7c 409 /**
fe482240 410 * Set a init session with user object.
bec3fc7c 411 *
77855840
TO
412 * @param array $data
413 * Array with user specific data.
bec3fc7c 414 */
00be9182 415 public function setUserSession($data) {
bec3fc7c 416 list($userID, $ufID) = $data;
481a74f4 417 $user = new JUser($ufID);
2d8f9c75 418 $session = JFactory::getSession();
bec3fc7c
BS
419 $session->set('user', $user);
420
cb0e36de 421 parent::setUserSession($data);
bec3fc7c
BS
422 }
423
6a488035 424 /**
17f443df 425 * FIXME: Do something
ea3ddccf 426 *
427 * @param string $message
6a488035 428 */
00be9182 429 public function setMessage($message) {
6a488035
TO
430 }
431
bb3a214a 432 /**
17f443df 433 * FIXME: Do something
ea3ddccf 434 *
435 * @param \obj $user
436 *
437 * @return bool
bb3a214a 438 */
00be9182 439 public function loadUser($user) {
6a488035
TO
440 return TRUE;
441 }
442
17f443df
CW
443 /**
444 * FIXME: Use CMS-native approach
445 */
00be9182 446 public function permissionDenied() {
0499b0ad 447 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
448 }
449
17f443df
CW
450 /**
451 * @inheritDoc
452 */
00be9182 453 public function logout() {
6a488035 454 session_destroy();
d42a224c 455 CRM_Utils_System::setHttpHeader("Location", "index.php");
6a488035
TO
456 }
457
458 /**
17f443df 459 * @inheritDoc
6a488035 460 */
00be9182 461 public function getUFLocale() {
6a488035
TO
462 if (defined('_JEXEC')) {
463 $conf = JFactory::getConfig();
4965d8e9 464 $locale = $conf->get('language');
6a488035
TO
465 return str_replace('-', '_', $locale);
466 }
467 return NULL;
468 }
469
fd1f3a26
SV
470 /**
471 * @inheritDoc
472 */
473 public function setUFLocale($civicrm_language) {
474 // TODO
475 return TRUE;
476 }
477
bb3a214a 478 /**
17f443df 479 * @inheritDoc
bb3a214a 480 */
00be9182 481 public function getVersion() {
6a488035 482 if (class_exists('JVersion')) {
e7483cbe 483 $version = new JVersion();
6a488035
TO
484 return $version->getShortVersion();
485 }
486 else {
487 return 'Unknown';
488 }
489 }
490
f4aaa82a 491 /**
fe482240 492 * Load joomla bootstrap.
6a488035 493 *
5a4f6742
CW
494 * @param array $params
495 * with uid or name and password.
496 * @param bool $loadUser
497 * load cms user?.
f4aaa82a
EM
498 * @param bool|\throw $throwError throw error on failure?
499 * @param null $realPath
500 * @param bool $loadDefines
501 *
502 * @return bool
6a488035 503 */
00be9182 504 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
6a488035
TO
505 // Setup the base path related constant.
506 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
507
508 // load BootStrap here if needed
509 // We are a valid Joomla entry point.
353ffa53 510 if (!defined('_JEXEC') && $loadDefines) {
6a488035
TO
511 define('_JEXEC', 1);
512 define('DS', DIRECTORY_SEPARATOR);
513 define('JPATH_BASE', $joomlaBase . '/administrator');
514 require $joomlaBase . '/administrator/includes/defines.php';
515 }
516
517 // Get the framework.
2cb7adde 518 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
2efcf0c2 519 require $joomlaBase . '/libraries/import.legacy.php';
2cb7adde 520 }
6a488035
TO
521 require $joomlaBase . '/libraries/import.php';
522 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
6a488035
TO
523 require $joomlaBase . '/configuration.php';
524
6fde79f5 525 // Files may be in different places depending on Joomla version
481a74f4 526 if (!defined('JVERSION')) {
6fde79f5 527 require $joomlaBase . '/libraries/cms/version/version.php';
e7483cbe 528 $jversion = new JVersion();
6fde79f5
BS
529 define('JVERSION', $jversion->getShortVersion());
530 }
531
481a74f4 532 if (version_compare(JVERSION, '3.0', 'lt')) {
6fde79f5
BS
533 require $joomlaBase . '/libraries/joomla/environment/uri.php';
534 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
535 }
536 else {
87cdafb0 537 require $joomlaBase . '/libraries/cms.php';
6fde79f5 538 require $joomlaBase . '/libraries/joomla/uri/uri.php';
6fde79f5
BS
539 }
540
6a488035 541 jimport('joomla.application.cli');
f4aaa82a 542
182f835d 543 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
544 $config = CRM_Core_Config::singleton();
545 CRM_Utils_Hook::config($config);
6a488035
TO
546
547 return TRUE;
548 }
549
550 /**
17f443df 551 * @inheritDoc
6a488035
TO
552 */
553 public function isUserLoggedIn() {
554 $user = JFactory::getUser();
555 return ($user->guest) ? FALSE : TRUE;
556 }
557
558 /**
17f443df 559 * @inheritDoc
6a488035
TO
560 */
561 public function getLoggedInUfID() {
562 $user = JFactory::getUser();
563 return ($user->guest) ? NULL : $user->id;
564 }
565
2b617cb0 566 /**
17f443df 567 * @inheritDoc
2b617cb0 568 */
00be9182 569 public function getLoggedInUniqueIdentifier() {
2b617cb0
EM
570 $user = JFactory::getUser();
571 return $this->getUniqueIdentifierFromUserObject($user);
572 }
353ffa53 573
32998c82 574 /**
17f443df 575 * @inheritDoc
32998c82 576 */
00be9182 577 public function getUserIDFromUserObject($user) {
32998c82
EM
578 return !empty($user->id) ? $user->id : NULL;
579 }
580
2b617cb0 581 /**
17f443df 582 * @inheritDoc
2b617cb0 583 */
00be9182 584 public function getUniqueIdentifierFromUserObject($user) {
2b617cb0
EM
585 return ($user->guest) ? NULL : $user->email;
586 }
587
6a488035
TO
588 /**
589 * Get a list of all installed modules, including enabled and disabled ones
590 *
a6c01b45
CW
591 * @return array
592 * CRM_Core_Module
6a488035 593 */
00be9182 594 public function getModules() {
6a488035
TO
595 $result = array();
596
597 $db = JFactory::getDbo();
e7292422 598 $query = $db->getQuery(TRUE);
6a488035
TO
599 $query->select('type, folder, element, enabled')
600 ->from('#__extensions')
601 ->where('type =' . $db->Quote('plugin'));
602 $plugins = $db->setQuery($query)->loadAssocList();
603 foreach ($plugins as $plugin) {
604 // question: is the folder really a critical part of the plugin's name?
605 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
606 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
607 }
608
609 return $result;
610 }
611
612 /**
17f443df 613 * @inheritDoc
6a488035
TO
614 */
615 public function getLoginURL($destination = '') {
616 $config = CRM_Core_Config::singleton();
617 $loginURL = $config->userFrameworkBaseURL;
618 $loginURL = str_replace('administrator/', '', $loginURL);
619 $loginURL .= 'index.php?option=com_users&view=login';
091412ab
BS
620
621 //CRM-14872 append destination
481a74f4 622 if (!empty($destination)) {
92fcb95f 623 $loginURL .= '&return=' . urlencode(base64_encode($destination));
091412ab 624 }
6a488035
TO
625 return $loginURL;
626 }
f813f78e 627
bb3a214a 628 /**
17f443df 629 * @inheritDoc
bb3a214a 630 */
6a488035 631 public function getLoginDestination(&$form) {
091412ab
BS
632 $args = NULL;
633
634 $id = $form->get('id');
635 if ($id) {
636 $args .= "&id=$id";
637 }
638 else {
639 $gid = $form->get('gid');
640 if ($gid) {
641 $args .= "&gid=$gid";
642 }
643 else {
644 // Setup Personal Campaign Page link uses pageId
645 $pageId = $form->get('pageId');
646 if ($pageId) {
647 $component = $form->get('component');
648 $args .= "&pageId=$pageId&component=$component&action=add";
649 }
650 }
651 }
652
653 $destination = NULL;
654 if ($args) {
655 // append destination so user is returned to form they came from after login
92fcb95f 656 $args = 'reset=1' . $args;
48341e06 657 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
091412ab
BS
658 }
659
660 return $destination;
6a488035 661 }
9977c6f5 662
663 /**
17f443df 664 * @inheritDoc
9977c6f5 665 */
9b873358 666 public function getDefaultSiteSettings($dir) {
9977c6f5 667 $config = CRM_Core_Config::singleton();
668 $url = preg_replace(
669 '|/administrator|',
670 '',
671 $config->userFrameworkBaseURL
672 );
673 $siteRoot = preg_replace(
674 '|/media/civicrm/.*$|',
675 '',
676 $config->imageUploadDir
677 );
678 return array($url, NULL, $siteRoot);
679 }
59f97da6
EM
680
681 /**
17f443df 682 * @inheritDoc
59f97da6 683 */
00be9182 684 public function getUserRecordUrl($contactID) {
59f97da6
EM
685 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
686 $userRecordUrl = NULL;
55904b50 687 // if logged in user has user edit access, then allow link to other users joomla profile
a8e5af2a 688 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
59f97da6
EM
689 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
690 }
691 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
692 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
693 }
694 }
695
696 /**
17f443df 697 * @inheritDoc
59f97da6 698 */
00be9182 699 public function checkPermissionAddUser() {
59f97da6
EM
700 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
701 return TRUE;
702 }
703 }
f85b1d20
EM
704
705 /**
fe482240 706 * Output code from error function.
f85b1d20
EM
707 * @param string $content
708 */
00be9182 709 public function outputError($content) {
f85b1d20
EM
710 if (class_exists('JErrorPage')) {
711 $error = new Exception($content);
712 JErrorPage::render($error);
713 }
4c9b6178 714 elseif (class_exists('JError')) {
f85b1d20
EM
715 JError::raiseError('CiviCRM-001', $content);
716 }
717 else {
718 parent::outputError($content);
719 }
720 }
e7292422 721
f58e4c2e 722 /**
fe482240 723 * Append Joomla js to coreResourcesList.
ad37ac8e 724 *
725 * @param array $list
f58e4c2e 726 */
00be9182 727 public function appendCoreResources(&$list) {
f58e4c2e
DC
728 $list[] = 'js/crm.joomla.js';
729 }
96025800 730
03d5592a
CW
731 /**
732 * @inheritDoc
733 */
734 public function synchronizeUsers() {
735 $config = CRM_Core_Config::singleton();
736 if (PHP_SAPI != 'cli') {
737 set_time_limit(300);
738 }
739 $id = 'id';
740 $mail = 'email';
741 $name = 'name';
742
743 $JUserTable = &JTable::getInstance('User', 'JTable');
744
745 $db = $JUserTable->getDbo();
746 $query = $db->getQuery(TRUE);
747 $query->select($id . ', ' . $mail . ', ' . $name);
748 $query->from($JUserTable->getTableName());
749 $query->where($mail != '');
750
751 $db->setQuery($query);
752 $users = $db->loadObjectList();
753
754 $user = new StdClass();
755 $uf = $config->userFramework;
756 $contactCount = 0;
757 $contactCreated = 0;
758 $contactMatching = 0;
759 for ($i = 0; $i < count($users); $i++) {
760 $user->$id = $users[$i]->$id;
761 $user->$mail = $users[$i]->$mail;
762 $user->$name = $users[$i]->$name;
763 $contactCount++;
764 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
765 $users[$i]->$id,
766 $users[$i]->$mail,
767 $uf,
768 1,
769 'Individual',
770 TRUE
771 )
772 ) {
773 $contactCreated++;
774 }
775 else {
776 $contactMatching++;
777 }
778 if (is_object($match)) {
779 $match->free();
780 }
781 }
782
783 return array(
784 'contactCount' => $contactCount,
785 'contactMatching' => $contactMatching,
786 'contactCreated' => $contactCreated,
787 );
788 }
789
6a488035 790}