Joomla 3 helper (useful features for programming in joomla)

  • (3 votes)
  • Written by 
  • Tuesday, 26 April 2016 15:49

Check on the user group membership in joomla:

	/*
	* Function 
	*/
function getAccess($groups=array(), $user_id=0)
	{
		if ($user_id == 0) $user_id = JFactory::getUser()->id;
		echo $user_id;
		if (count($groups) == 0 || $user_id == 0) return false;
		return (count(array_intersect(JFactory::getUser($user_id)->getAuthorisedGroups(), $groups))>0);
	}

// Print groups user id 
	print_r($user_id = $user->getAuthorisedGroups());

// Output Group Access Permissions
    $groups = array(18, 8);// an array of groups separated by commas
	
    if(getAccess( $groups)) echo 'Access granted to groups 18 and 8';
	

Get URL in joomla and parse it:

$app = JFactory::getApplication(); 
		// get URL
	$uri = JURI::getInstance();
		// get URL params
	$router = $app->getRouter();
	$getParams = $router->parse($uri);
	
	echo $uri;
	echo '<pre>'; print_r ($getParams); echo '</pre>';
// get the user id in joomla and print user name, nickname and id
$user = JFactory::getUser(); 
	if (!$user->guest) {
	  echo 'You are logged in as:<br />';
	  echo 'User name: ' . $user->username . '<br />';
	  echo 'Real name: ' . $user->name . '<br />';
	  echo 'User ID  : ' . $user->id . '<br />';
	}
	
// isRoot  verify - whether the user is an admin
$isroot = $user->get('isRoot');
	if (!$isroot) {
		echo '<h1>HI you not root</h1>';
		$redirecturl = 'index.php?option=com_user&view=login';
		$app->redirect(JRoute::_($redirecturl,false));
	}
2799 Read Last modified on Tuesday, 26 April 2016