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