Page 1 of 1

[Solved] Need some advice on a new plugin/function

Posted: Wed Nov 30, 2011 5:52 am
by Jo Morg
I've been lurking these forums for some years now, and learning, and made a few sites based on CMSMS, with very few glitches, thanks to this community.
I'm can code very efficiently in pascal/delphi and have been slowly learning php mainly out of need. Actually CMSMS helped me a good bit. I've also published 2 plugins, one as my "Hello world!" to both PHP and CMSMS contribution, the other out of need.

Now (again out of need) I'm trying a new one.

Here's the problem:

Two years ago I found a post on these forums that I can't find, and that basically almost solved my problem. As I can't find it, and this time I needed to go a bit further I thought about this way of doing it.

What I needed was to be able to have a page for certain FEU groups (one for each), in which I could base FEU pages (for allowed groups of users) so that each user was responsible for its own page contents.

The other part of the problem was that I needed to be able to have the front end being accessed by a pretty URL like:

Code: Select all

www.domain.com/<usergroup>/<username>.html
The first part was accomplished with FEU module and a Change Settings Template combined with global_content along these lines

Code: Select all

{if isset($feu_edit_mode)}
{if $feu_edit_mode == "1"} 
{* if this is EditDataMode 1 then .... *}
{global_content name='User_Default_ChangeSettingsTemplate'}
{/if}{* My Control mode 1 END *}
{/if}{* My Control isset($feu_edit_mode  *}
To my surprise it worked (I had a problem with not having a possibility to define several different sets of templates in FEU...) and solved this and other several problems....

The second part of the problem was not so easy... I thought about solving it with an UDT but ended up writing a plugin for this purpose.

As I have set the Page URL of each group page to the name of the group, I only needed a Tag to get a user properties based on the parameters passed on the URL.

I took a look at the get_user_props UDT (I think that´s the name...) but it was not exactly what I needed, so I came up with this:

Code: Select all

 /**
 * the input parameters are:
 * 
 *  - properties: the properties you want to get from FEU coma separated; REQUIRED!
 *  - username: the username you want to get the properties from; Optional.
 *  - uid: the user id  you want to get the properties from; Optional.
 * 
 *  (you can set one or the other or none, in which case the plugin tries to
 *   extract the username from the $_GET array. The plugin will work by priorities:
 *    1st- uid, 2nd- username, 3rd- $_GET array)
 *  - show_expired: default FALSE; Optional - set if you need to show expired accounts.
 *  - feu_group: filter user by group; Optional
 *  - if_property: use a FEU property to check for a value; Optional.
 *  - if_value: value of the if_property to compare to; REQUIRED if the if_property is set.
 *  - assign_error: if set you can get a message describing the error; Optional - for debug purposes!   
 *  - assign_error_id: if set you can get an error code (integer) and act on it; Optional - use assign_error to understand the error code;     
 *  - assign_result: should be set to get the result as an array. Optional......
 *      However ... (todo - explain better)
 * 
 * @param mixed $params
 * @param mixed $smarty
 */
function smarty_cms_function_jm_feu_user_props( $params, &$smarty )
{
    //helper - error function
    if (!function_exists(jm_errors))
    {
        function jm_errors($eid, $er)
        {
            if (isset($params['assign_error'])){$smarty->assign(trim($params['assign_error']),$er);}
            if (isset($params['assign_error_id'])){$smarty->assign(trim($params['assign_error_id']),$eid);}    
        }
    }
    
    // lets start by setting some defaults   
	$errorid = 0;
    $error = '';
	$result = '';    
    if (!isset( $params['expired']))
    {
        $params['show_expired'] = FALSE;
    }
    
    
	if (!isset( $params['properties']))  
    {
        $errorid = 1;
        $error = 'Properties parameter MUST be set!';
        jm_errors($errorid, $error);
        return;
    }                 
    
    // we have properties
	$properties = explode(',', $params['properties']);
	
	$gCms = cmsms();
	$feu = cms_utils::get_module('FrontEndUsers');
	
	if (!$feu)
	{
        $errorid = 2;
        $error = 'Cannot connect to FrontEndUsers module!';
        jm_errors($errorid, $error);
        return; 
    }
     
     // we have FEU Module
        
	$conf = $gCms->GetConfig();
    if (isset($params['uid'])) // use ID
    {
        $uid = $params['uid']; 
        $username =  $feu->GetUserName($uid);
    }
    elseif (isset($params['username']))    // use Username
    {
		$username = trim($params['username']);
        $uid = $feu->GetUserID($username);
	}
    else // use $_GET['arg'][1]
    {
         $username = str_replace($conf['page_extension'],'', $_GET['arg'][1]);
         $uid = $feu->GetUserID($username);
    }
    
    if (empty($username)) // the triage failed to produce a username or userid
    {
        $errorid = 3;
        $error = 'could not find a username or user id to compute!';
        jm_errors($errorid, $error);
        return;              
    }
    
    // now let's check for expired user
    if ($feu->IsAccountExpired( $uid ) && (!$params['show_expired']))
    {
        $errorid = 4;
        $error = 'This user account has expired!';
        jm_errors($errorid, $error);
        return;
    }
    
    if (isset($params['feu_group']))
    {
        if (!$feu->GroupExistsByName($params['feu_group']))
        {
            $errorid = 5;
            $error = 'Group does not exist';
            jm_errors($errorid, $error);
            return;   
        }
        
        // group exists
        $gid = $feu->GetGroupID($params['feu_group']);
        
        if (!$feu->MemberOfGroup($uid,$gid))
        {
            $errorid = 6;
            $error = 'User does not belong to this group!';
            jm_errors($errorid, $error);
            return;                       
        } 
    } 
    
    // cleared from group triage -> carry on!
            
			
    if (isset($params['if_property']))
    {
      if (!isset($params['if_value']))
      {
          $errorid = 7;
          $error = 'When using if_property if_value must be used!';
          jm_errors($errorid, $error);
          return;                  
      }  
    }
               
	$feuproperties = $feu->GetUserProperties($uid);

	
	foreach($properties as $property)
	{
		foreach($feuproperties as $key=>$value)
		{
			if ($value["title"] == trim($property))
			{
                if (($value["title"] == trim($params['if_property'])))
                {
                   if (!($value['data'] == trim($params['if_value'])))
                   {
                       //  hopefully hidden user properties can be used here????
                        unset($result);
                        $errorid = 8;
                        $error = 'if_property does not match if_value!';
                        jm_errors($errorid, $error);
                        return;    
                   }
                }
                $result[$value["title"]] = $value['data'];
		    }
		}
	} // foreach($properties as $property) <- end

	

	if (isset($params['assign_result']))      
	{
		$smarty->assign(trim($params['assign_result']),$result);
		return;
	}
	
	return implode('<br /\>', $result);
}
I've been testing it on my local server and apparently it works as expected.
So I'm about to take it for a few more tests and release it on the forge.

OK, so now is were I could use some help, if you gurus kindly would take a look and see if there are any obvious traps.
Also opinions about it's usefulness for others and suggestions?

Is there a better way of getting parameters than from $_GET array?

Thanks in advance!

BTW I noticed that the code here appears to be longer than it really is... so..... sorry for the Loooooong post.

Re: Need some advice on a new plugin/function

Posted: Thu Dec 01, 2011 3:29 pm
by Jo Morg
OK! Coding while tired is not good... I should have seen this one (pretty basic...).

Changed the error function...

Code: Select all

    
if (!function_exists(jm_errors))
    {
        function jm_errors(&$errorid, &$error, &$smarty, &$params)
        {
            if (isset($params['assign_error_id'])){$smarty->assign(trim($params['assign_error_id']),$errorid);}
            if (isset($params['assign_error'])){$smarty->assign(trim($params['assign_error']),&$error);}
        }
    }
Now it's working properly...

The only thing I'm missing here is... the plugin only works properly if the URL is at the root of the site, that is:

Code: Select all

http://www.domain.com/<page>/<user>.html
I would like to be able to trap any page configuration and just extract the <user> bit from the URL. Any ideas?

This is what I have...

Code: Select all

    else // use $_GET['arg'][1]
    {
         $username = str_replace($conf['page_extension'],'', $_GET['arg'][1]);
         $uid = $feu->GetUserID($username);
    }
There must be a better way... but I'm too green in PHP to get there...

TIA for any help.

Re: Need some advice on a new plugin/function

Posted: Thu Dec 01, 2011 11:36 pm
by Jo Morg
OK!...
Granted: the code I posted had toooooo many basic errors; but I believe I caught them all. :)

I did a lot of testing, given my localhost environment, and it passed all the tests I could think of. I even solved the URL bit I was not so sure I could solve on my own. 8)

Given the fact that this is the most complicated piece of code I did with php (apart from playing with php classes...) I'm happy with the results.

So... I'm going to publish it, as soon as I can comment the code and document it in the plugin help function...

Re: Need some advice on a new plugin/function

Posted: Fri Dec 23, 2011 1:31 pm
by psy
Thanks for the time and effort as well as the plugin. Was something I'd been looking for and hoping would come about. :)

Re: Need some advice on a new plugin/function

Posted: Wed Dec 28, 2011 11:07 pm
by Jo Morg
psy wrote:Thanks for the time and effort as well as the plugin. Was something I'd been looking for and hoping would come about. :)
You're welcome. Any questions, just ask. ^-^