INFRA-132 - CRM/Utils - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Joomla specific stuff goes here
38 */
39 class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
40 /**
41 *
42 */
43 public function __construct() {
44 /**
45 * 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
46 * functions and leave the codebase oblivious to the type of CMS
47 * @deprecated
48 * @var bool
49 */
50 $this->is_drupal = FALSE;
51 }
52
53 /**
54 * Create a user of Joomla.
55 *
56 * @param array $params
57 * Associated array.
58 * @param string $mail
59 * Email id for cms user.
60 *
61 * @return uid if user exists, false otherwise
62 *
63 */
64 public function createUser(&$params, $mail) {
65 $baseDir = JPATH_SITE;
66 require_once $baseDir . '/components/com_users/models/registration.php';
67
68 $userParams = JComponentHelper::getParams('com_users');
69 $model = new UsersModelRegistration();
70 $ufID = NULL;
71
72 // get the default usertype
73 $userType = $userParams->get('new_usertype');
74 if (!$userType) {
75 $userType = 2;
76 }
77
78 if (isset($params['name'])) {
79 $fullname = trim($params['name']);
80 }
81 elseif (isset($params['contactID'])) {
82 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
83 }
84 else {
85 $fullname = trim($params['cms_name']);
86 }
87
88 // Prepare the values for a new Joomla user.
89 $values = array();
90 $values['name'] = $fullname;
91 $values['username'] = trim($params['cms_name']);
92 $values['password1'] = $values['password2'] = $params['cms_pass'];
93 $values['email1'] = $values['email2'] = trim($params[$mail]);
94
95 $lang = JFactory::getLanguage();
96 $lang->load('com_users', $baseDir);
97
98 $register = $model->register($values);
99
100 $ufID = JUserHelper::getUserId($values['username']);
101 return $ufID;
102 }
103
104 /**
105 * Change user name in host CMS
106 *
107 * @param integer $ufID User ID in CMS
108 * @param string $ufName User name
109 */
110 public function updateCMSName($ufID, $ufName) {
111 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
112 $ufName = CRM_Utils_Type::escape($ufName, 'String');
113
114 $values = array();
115 $user = JUser::getInstance($ufID);
116
117 $values['email'] = $ufName;
118 $user->bind($values);
119
120 $user->save();
121 }
122
123 /**
124 * Check if username and email exists in the drupal db
125 *
126 * @param array $params
127 * Array of name and mail values.
128 * @param array $errors
129 * Array of errors.
130 * @param string $emailName
131 * Field label for the 'email'.
132 *
133 * @return void
134 */
135 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
136 $config = CRM_Core_Config::singleton();
137
138 $dao = new CRM_Core_DAO();
139 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
140 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
141 //don't allow the special characters and min. username length is two
142 //regex \\ to match a single backslash would become '/\\\\/'
143 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
144 if ($isNotValid || strlen($name) < 2) {
145 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
146 }
147
148
149 $JUserTable = &JTable::getInstance('User', 'JTable');
150
151 $db = $JUserTable->getDbo();
152 $query = $db->getQuery(TRUE);
153 $query->select('username, email');
154 $query->from($JUserTable->getTableName());
155 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
156 $db->setQuery($query, 0, 10);
157 $users = $db->loadAssocList();
158
159 $row = array();;
160 if (count($users)) {
161 $row = $users[0];
162 }
163
164 if (!empty($row)) {
165 $dbName = CRM_Utils_Array::value('username', $row);
166 $dbEmail = CRM_Utils_Array::value('email', $row);
167 if (strtolower($dbName) == strtolower($name)) {
168 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
169 array(1 => $name)
170 );
171 }
172 if (strtolower($dbEmail) == strtolower($email)) {
173 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
174 $errors[$emailName] = ts('The email address %1 is already registered. <a href="%2">Have you forgotten your password?</a>',
175 array(1 => $email, 2 => $resetUrl)
176 );
177 }
178 }
179 }
180
181 /**
182 * Sets the title of the page
183 *
184 * @param string $title
185 * Title to set.
186 * @param string $pageTitle
187 *
188 * @return void
189 */
190 public function setTitle($title, $pageTitle = NULL) {
191 if (!$pageTitle) {
192 $pageTitle = $title;
193 }
194
195 $template = CRM_Core_Smarty::singleton();
196 $template->assign('pageTitle', $pageTitle);
197
198 $document = JFactory::getDocument();
199 $document->setTitle($title);
200
201 return;
202 }
203
204 /**
205 * Append an additional breadcrumb tag to the existing breadcrumb
206 *
207 * @param $breadCrumbs
208 *
209 * @internal param string $title
210 * @internal param string $url
211 *
212 * @return void
213 */
214 public function appendBreadCrumb($breadCrumbs) {
215 $template = CRM_Core_Smarty::singleton();
216 $bc = $template->get_template_vars('breadcrumb');
217
218 if (is_array($breadCrumbs)) {
219 foreach ($breadCrumbs as $crumbs) {
220 if (stripos($crumbs['url'], 'id%%')) {
221 $args = array('cid', 'mid');
222 foreach ($args as $a) {
223 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
224 FALSE, NULL, $_GET
225 );
226 if ($val) {
227 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
228 }
229 }
230 }
231 $bc[] = $crumbs;
232 }
233 }
234 $template->assign_by_ref('breadcrumb', $bc);
235 return;
236 }
237
238 /**
239 * Reset an additional breadcrumb tag to the existing breadcrumb
240 *
241 * @internal param string $bc the new breadcrumb to be appended
242 *
243 * @return void
244 */
245 public function resetBreadCrumb() {
246 return;
247 }
248
249 /**
250 * Append a string to the head of the html file
251 *
252 * @param null $string
253 *
254 * @internal param string $head the new string to be appended
255 *
256 * @return void
257 */
258 public static function addHTMLHead($string = NULL) {
259 if ($string) {
260 $document = JFactory::getDocument();
261 $document->addCustomTag($string);
262 }
263 }
264
265 /**
266 * Add a script file
267 *
268 * @param $url: string, absolute path to file
269 * @param $region
270 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
271 *
272 * Note: This function is not to be called directly
273 * @see CRM_Core_Region::render()
274 *
275 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
276 */
277 public function addScriptUrl($url, $region) {
278 return FALSE;
279 }
280
281 /**
282 * Add an inline script
283 *
284 * @param $code: string, javascript code
285 * @param $region
286 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
287 *
288 * Note: This function is not to be called directly
289 * @see CRM_Core_Region::render()
290 *
291 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
292 */
293 public function addScript($code, $region) {
294 return FALSE;
295 }
296
297 /**
298 * Add a css file
299 *
300 * @param $url: string, absolute path to file
301 * @param $region
302 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
303 *
304 * Note: This function is not to be called directly
305 * @see CRM_Core_Region::render()
306 *
307 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
308 */
309 public function addStyleUrl($url, $region) {
310 if ($region == 'html-header') {
311 $document = JFactory::getDocument();
312 $document->addStyleSheet($url);
313 return TRUE;
314 }
315 return FALSE;
316 }
317
318 /**
319 * Add an inline style
320 *
321 * @param $code: string, css code
322 * @param $region
323 * String, location within the document: 'html-header', 'page-header', 'page-footer'.
324 *
325 * Note: This function is not to be called directly
326 * @see CRM_Core_Region::render()
327 *
328 * @return bool TRUE if we support this operation in this CMS, FALSE otherwise
329 */
330 public function addStyle($code, $region) {
331 if ($region == 'html-header') {
332 $document = JFactory::getDocument();
333 $document->addStyleDeclaration($code);
334 return TRUE;
335 }
336 return FALSE;
337 }
338
339 /**
340 * Generate an internal CiviCRM URL
341 *
342 * @param $path
343 * String The path being linked to, such as "civicrm/add".
344 * @param $query
345 * String A query string to append to the link.
346 * @param $absolute
347 * Boolean Whether to force the output to be an absolute link (beginning with http:).
348 * Useful for links that will be displayed outside the site, such as in an
349 * RSS feed.
350 * @param $fragment
351 * String A fragment identifier (named anchor) to append to the link.
352 * @param $htmlize
353 * Boolean whether to convert to html eqivalant.
354 * @param $frontend
355 * Boolean a gross joomla hack.
356 *
357 * @param bool $forceBackend
358 *
359 * @return string an HTML string containing a link to the given path.
360 */
361 function url($path = NULL, $query = NULL, $absolute = TRUE,
362 $fragment = NULL, $htmlize = TRUE,
363 $frontend = FALSE, $forceBackend = FALSE
364 ) {
365 $config = CRM_Core_Config::singleton();
366 $separator = $htmlize ? '&amp;' : '&';
367 $Itemid = '';
368 $script = '';
369 $path = CRM_Utils_String::stripPathChars($path);
370
371 if ($config->userFrameworkFrontend) {
372 $script = 'index.php';
373 if (JRequest::getVar("Itemid")) {
374 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
375 }
376 }
377
378 if (isset($fragment)) {
379 $fragment = '#' . $fragment;
380 }
381
382 if (!isset($config->useFrameworkRelativeBase)) {
383 $base = parse_url($config->userFrameworkBaseURL);
384 $config->useFrameworkRelativeBase = $base['path'];
385 }
386 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
387
388 if (!empty($query)) {
389 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
390 }
391 else {
392 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
393 }
394
395 // gross hack for joomla, we are in the backend and want to send a frontend url
396 if ($frontend && $config->userFramework == 'Joomla') {
397 // handle both joomla v1.5 and v1.6, CRM-7939
398 $url = str_replace('/administrator/index2.php', '/index.php', $url);
399 $url = str_replace('/administrator/index.php', '/index.php', $url);
400
401 // CRM-8215
402 $url = str_replace('/administrator/', '/index.php', $url);
403 }
404 elseif ($forceBackend) {
405 if (defined('JVERSION')) {
406 $joomlaVersion = JVERSION;
407 } else {
408 $jversion = new JVersion;
409 $joomlaVersion = $jversion->getShortVersion();
410 }
411
412 if (version_compare($joomlaVersion, '1.6') >= 0) {
413 $url = str_replace('/index.php', '/administrator/index.php', $url);
414 }
415 }
416 return $url;
417 }
418
419 /**
420 * Rewrite various system urls to https
421 *
422 * @return void
423 * access public
424 */
425 public function mapConfigToSSL() {
426 // dont need to do anything, let CMS handle their own switch to SSL
427 return;
428 }
429
430 /**
431 * Figure out the post url for the form
432 *
433 * @param $action
434 * The default action if one is pre-specified.
435 *
436 * @return string the url to post the form
437 */
438 public function postURL($action) {
439 if (!empty($action)) {
440 return $action;
441 }
442
443 return $this->url(CRM_Utils_Array::value('task', $_GET),
444 NULL, TRUE, NULL, FALSE
445 );
446 }
447
448 /**
449 * Set the email address of the user
450 *
451 * @param object $user
452 * Handle to the user object.
453 *
454 * @return void
455 */
456 public function setEmail(&$user) {
457 global $database;
458 $query = "SELECT email FROM #__users WHERE id='$user->id'";
459 $database->setQuery($query);
460 $user->email = $database->loadResult();
461 }
462
463 /**
464 * Authenticate the user against the joomla db
465 *
466 * @param string $name
467 * The user name.
468 * @param string $password
469 * The password for the above user name.
470 * @param $loadCMSBootstrap
471 * Boolean load cms bootstrap?.
472 *
473 * @return mixed false if no auth
474 * array(
475 contactID, ufID, unique string ) if success
476 */
477 public function authenticate($name, $password, $loadCMSBootstrap = FALSE) {
478 require_once 'DB.php';
479
480 $config = CRM_Core_Config::singleton();
481 $user = NULL;
482
483 if ($loadCMSBootstrap) {
484 $bootStrapParams = array();
485 if ($name && $password) {
486 $bootStrapParams = array(
487 'name' => $name,
488 'pass' => $password,
489 );
490 }
491 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
492 }
493
494 jimport('joomla.application.component.helper');
495 jimport('joomla.database.table');
496 jimport('joomla.user.helper');
497
498 $JUserTable = JTable::getInstance('User', 'JTable');
499
500 $db = $JUserTable->getDbo();
501 $query = $db->getQuery(TRUE);
502 $query->select('id, name, username, email, password');
503 $query->from($JUserTable->getTableName());
504 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
505 $db->setQuery($query, 0, 0);
506 $users = $db->loadObjectList();
507
508 $row = array();
509 if (count($users)) {
510 $row = $users[0];
511 }
512
513 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
514 if ( !defined('JVERSION') ) {
515 require $joomlaBase . '/libraries/cms/version/version.php';
516 $jversion = new JVersion;
517 define('JVERSION', $jversion->getShortVersion());
518 }
519
520 if (!empty($row)) {
521 $dbPassword = $row->password;
522 $dbId = $row->id;
523 $dbEmail = $row->email;
524
525 if ( version_compare(JVERSION, '2.5.18', 'lt') ||
526 ( version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt') )
527 ) {
528 // now check password
529 if (strpos($dbPassword, ':') === FALSE) {
530 if ($dbPassword != md5($password)) {
531 return FALSE;
532 }
533 }
534 else {
535 list($hash, $salt) = explode(':', $dbPassword);
536 $cryptpass = md5($password . $salt);
537 if ($hash != $cryptpass) {
538 return FALSE;
539 }
540 }
541 }
542 else {
543 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) return FALSE;
544
545 //include additional files required by Joomla 3.2.1+
546 if ( version_compare(JVERSION, '3.2.1', 'ge') ) {
547 require_once $joomlaBase . '/libraries/cms/application/helper.php';
548 require_once $joomlaBase . '/libraries/cms/application/cms.php';
549 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
550 }
551 }
552
553 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
554 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
555 if (!$contactID) {
556 return FALSE;
557 }
558 return array($contactID, $dbId, mt_rand());
559 }
560
561 return FALSE;
562 }
563
564 /**
565 * Set a init session with user object
566 *
567 * @param array $data
568 * Array with user specific data.
569 *
570 */
571 public function setUserSession($data) {
572 list($userID, $ufID) = $data;
573 $user = new JUser( $ufID );
574 $session = JFactory::getSession();
575 $session->set('user', $user);
576
577 parent::setUserSession($data);
578 }
579
580 /**
581 * Set a message in the UF to display to a user
582 *
583 * @param string $message
584 * The message to set.
585 *
586 */
587 public function setMessage($message) {
588 return;
589 }
590
591 /**
592 * @param $user
593 *
594 * @return bool
595 */
596 public function loadUser($user) {
597 return TRUE;
598 }
599
600 public function permissionDenied() {
601 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
602 }
603
604 public function logout() {
605 session_destroy();
606 header("Location:index.php");
607 }
608
609 /**
610 * Get the locale set in the hosting CMS
611 *
612 * @return string the used locale or null for none
613 */
614 public function getUFLocale() {
615 if (defined('_JEXEC')) {
616 $conf = JFactory::getConfig();
617 $locale = $conf->get('language');
618 return str_replace('-', '_', $locale);
619 }
620 return NULL;
621 }
622
623 /**
624 * @return string
625 */
626 public function getVersion() {
627 if (class_exists('JVersion')) {
628 $version = new JVersion;
629 return $version->getShortVersion();
630 }
631 else {
632 return 'Unknown';
633 }
634 }
635
636 /**
637 * Load joomla bootstrap
638 *
639 * @param $params
640 * Array with uid or name and password.
641 * @param $loadUser
642 * Boolean load cms user?.
643 * @param bool|\throw $throwError throw error on failure?
644 * @param null $realPath
645 * @param bool $loadDefines
646 *
647 * @return bool
648 */
649 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
650 // Setup the base path related constant.
651 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
652
653 // load BootStrap here if needed
654 // We are a valid Joomla entry point.
655 if ( ! defined( '_JEXEC' ) && $loadDefines ) {
656 define('_JEXEC', 1);
657 define('DS', DIRECTORY_SEPARATOR);
658 define('JPATH_BASE', $joomlaBase . '/administrator');
659 require $joomlaBase . '/administrator/includes/defines.php';
660 }
661
662 // Get the framework.
663 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
664 require $joomlaBase . '/libraries/import.legacy.php';
665 }
666 require $joomlaBase . '/libraries/import.php';
667 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
668 require $joomlaBase . '/configuration.php';
669
670 // Files may be in different places depending on Joomla version
671 if ( !defined('JVERSION') ) {
672 require $joomlaBase . '/libraries/cms/version/version.php';
673 $jversion = new JVersion;
674 define('JVERSION', $jversion->getShortVersion());
675 }
676
677 if( version_compare(JVERSION, '3.0', 'lt') ) {
678 require $joomlaBase . '/libraries/joomla/environment/uri.php';
679 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
680 }
681 else {
682 require $joomlaBase . '/libraries/cms.php';
683 require $joomlaBase . '/libraries/joomla/uri/uri.php';
684 }
685
686 jimport('joomla.application.cli');
687
688 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
689 $config = CRM_Core_Config::singleton();
690 CRM_Utils_Hook::config($config);
691
692 return TRUE;
693 }
694
695 /**
696 * Check is user logged in.
697 *
698 * @return boolean true/false.
699 */
700 public function isUserLoggedIn() {
701 $user = JFactory::getUser();
702 return ($user->guest) ? FALSE : TRUE;
703 }
704
705 /**
706 * Get currently logged in user uf id.
707 *
708 * @return int logged in user uf id.
709 */
710 public function getLoggedInUfID() {
711 $user = JFactory::getUser();
712 return ($user->guest) ? NULL : $user->id;
713 }
714
715 /**
716 * Get currently logged in user unique identifier - this tends to be the email address or user name.
717 *
718 * @return string $userID logged in user unique identifier
719 */
720 public function getLoggedInUniqueIdentifier() {
721 $user = JFactory::getUser();
722 return $this->getUniqueIdentifierFromUserObject($user);
723 }
724 /**
725 * Get User ID from UserFramework system (Joomla)
726 * @param object $user
727 * Object as described by the CMS.
728 * @return mixed <NULL, number>
729 */
730 public function getUserIDFromUserObject($user) {
731 return !empty($user->id) ? $user->id : NULL;
732 }
733
734 /**
735 * Get Unique Identifier from UserFramework system (CMS)
736 * @param object $user
737 * Object as described by the User Framework.
738 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
739 *
740 */
741 public function getUniqueIdentifierFromUserObject($user) {
742 return ($user->guest) ? NULL : $user->email;
743 }
744
745 /**
746 * Get a list of all installed modules, including enabled and disabled ones
747 *
748 * @return array CRM_Core_Module
749 */
750 public function getModules() {
751 $result = array();
752
753 $db = JFactory::getDbo();
754 $query = $db->getQuery(true);
755 $query->select('type, folder, element, enabled')
756 ->from('#__extensions')
757 ->where('type =' . $db->Quote('plugin'));
758 $plugins = $db->setQuery($query)->loadAssocList();
759 foreach ($plugins as $plugin) {
760 // question: is the folder really a critical part of the plugin's name?
761 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
762 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
763 }
764
765 return $result;
766 }
767
768 /**
769 * Get user login URL for hosting CMS (method declared in each CMS system class)
770 *
771 * @param string $destination
772 * If present, add destination to querystring (works for Drupal only).
773 *
774 * @return string - loginURL for the current CMS
775 * @static
776 */
777 public function getLoginURL($destination = '') {
778 $config = CRM_Core_Config::singleton();
779 $loginURL = $config->userFrameworkBaseURL;
780 $loginURL = str_replace('administrator/', '', $loginURL);
781 $loginURL .= 'index.php?option=com_users&view=login';
782
783 //CRM-14872 append destination
784 if ( !empty($destination) ) {
785 $loginURL .= '&return='.urlencode(base64_encode($destination));
786 }
787 return $loginURL;
788 }
789
790 /**
791 * @param CRM_Core_Form $form
792 */
793 public function getLoginDestination(&$form) {
794 $args = NULL;
795
796 $id = $form->get('id');
797 if ($id) {
798 $args .= "&id=$id";
799 }
800 else {
801 $gid = $form->get('gid');
802 if ($gid) {
803 $args .= "&gid=$gid";
804 }
805 else {
806 // Setup Personal Campaign Page link uses pageId
807 $pageId = $form->get('pageId');
808 if ($pageId) {
809 $component = $form->get('component');
810 $args .= "&pageId=$pageId&component=$component&action=add";
811 }
812 }
813 }
814
815 $destination = NULL;
816 if ($args) {
817 // append destination so user is returned to form they came from after login
818 $args = 'reset=1'.$args;
819 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, TRUE, TRUE);
820 }
821
822 return $destination;
823 }
824
825 /**
826 * Return default Site Settings
827 *
828 * @param $dir
829 *
830 * @return array array
831 * - $url, (Joomla - non admin url)
832 * - $siteName,
833 * - $siteRoot
834 */
835 public function getDefaultSiteSettings($dir){
836 $config = CRM_Core_Config::singleton();
837 $url = preg_replace(
838 '|/administrator|',
839 '',
840 $config->userFrameworkBaseURL
841 );
842 $siteRoot = preg_replace(
843 '|/media/civicrm/.*$|',
844 '',
845 $config->imageUploadDir
846 );
847 return array($url, NULL, $siteRoot);
848 }
849
850 /**
851 * Get Url to view user record
852 * @param int $contactID
853 * Contact ID.
854 *
855 * @return string
856 */
857 public function getUserRecordUrl($contactID) {
858 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
859 $userRecordUrl = NULL;
860 // if logged in user is super user, then he can view other users joomla profile
861 if (JFactory::getUser()->authorise('core.admin')) {
862 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
863 }
864 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
865 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
866 }
867 }
868
869 /**
870 * Is the current user permitted to add a user
871 * @return bool
872 */
873 public function checkPermissionAddUser() {
874 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
875 return TRUE;
876 }
877 }
878
879 /**
880 * Output code from error function
881 * @param string $content
882 */
883 public function outputError($content) {
884 if (class_exists('JErrorPage')) {
885 $error = new Exception($content);
886 JErrorPage::render($error);
887 }
888 else if (class_exists('JError')) {
889 JError::raiseError('CiviCRM-001', $content);
890 }
891 else {
892 parent::outputError($content);
893 }
894 }
895
896 /**
897 * Append to coreResourcesList
898 */
899 public function appendCoreResources(&$list) {
900 $list[] = 'js/crm.joomla.js';
901 }
902 }