Joomla 3 helper (useful features for programming in joomla)

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));
	}
1348 Read Last modified on Tuesday, 26 April 2016

Related items

  • [solved] How to delete text symbol from php line?
    in PHP
    $string = 'Some Text .... 1. 2 .3.  Buy';
    $string = preg_replace('~[^0-9]+~','',$string);
    echo $string;
    // 123 
  • How to show all errors in php?
    in PHP

    If you need show some errors in your php code - use this php notation:

    // enable showing errors in PHP
    ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT);
    ini_set('display_errors','On');
  • [solved] Deprecated: Methods with the same name as their class will not be constructors ... www/plugins/content/jw_sigpro/jw_sigpro.php on line 19

    If you update your hosting to v7.1 & more - you can look same notification 'Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; plgContentJw_sigpro has a deprecated constructor in .../www/plugins/content/jw_sigpro/jw_sigpro.php on line 19'

    This code fix this problem you need Open plugins/content/jw_sigpro/jw_sigpro.php find this code in line 28:

    function plgContentJw_sigpro(&$subject, $params)
    	{
    		parent::__construct($subject, $params);
    
    		// Define the DS constant under Joomla! 3.0
    		if (!defined('DS'))
    		{
    			define('DS', DIRECTORY_SEPARATOR);
    		}
    	}

     And replase it to this code:

    public function __construct(&$subject, $params) {
    	parent::__construct($subject, $params);
    
    	// Define the DS constant under Joomla! 3.8.2+
    	if (!defined('DS')){
    		define('DS', DIRECTORY_SEPARATOR);
    	}
    }

    Awesome - it work done! :-)

    If you see same problem with jw_sig.php  - just download free new plugin from GitHub and install it: https://github.com/joomlaworks/simple-image-gallery

  • [php] How to make function for compress css using regular expression
    in PHP
    function compresscss ( $data, $url ) {
            global $current_css_url;
            $current_css_url = $url;
            /* remove comments */
            $data = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $data);
            /* remove tabs, spaces, new lines, etc. */
            $data = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), ' ', $data);
            /* remove unnecessary spaces */
            $data = preg_replace('/[ ]+([{};,:])/', '\1', $data);
            $data = preg_replace('/([{};,:])[ ]+/', '\1', $data);
            /* remove empty class */
            $data = preg_replace('/(\}([^\}]*\{\})+)/', '}', $data);
            /* remove PHP code */
            $data = preg_replace('/<\?(.*?)\?>/mix', '', $data);
            /* replace url*/
            $data = preg_replace_callback('/url\(([^\)]*)\)/', 'replaceurl', $data);
            return $data;
    }

     

  • 7 tips to improve Joomla website performance [part 2]

    To continue with the tips to improve Joomla website performance, in the previous part, I had introduced how to use caching and .htaccess Optimization Rules, in this part, I will show you some other tips and hope you find it useful.

    Joomla Compression

    This is also an effective feature to optimize your website performance. When you enable this feature, the web page will be compressed and of course, the compressed page will take less time to load. Here is the way to enable Joomla Compression: