UDT to get custom content based on extra content blocks

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
robbedoes
Forum Members
Forum Members
Posts: 75
Joined: Wed Aug 20, 2008 2:08 pm
Location: Nijmegen, the Netherlands

UDT to get custom content based on extra content blocks

Post by robbedoes »

Hi,

Today I was asked if it is possible to create pages only accessible for some front end users. I said yes because you can.


But I don't want to fix the values for the groups and users in the page content or in a template so this is what I did.

Maybe someone can tell me if this is smart or if there is another way to get it working.

First of all I created a page template for restricted access. Template snippet:

{content assign=pagecontent}
{content block="users" assign="theusers" wysiwyg="false" oneline="true"}
{content block="groups" assign="thegroups" wysiwyg="false" oneline="true"}

In stead of {content} I addes a call to a UDT:

{restricted_to thegroups ="$thegroups" theusers ="$theusers" thepagecontent="$pagecontent"}

Then I wrote a UDT (not complete yet):

$retstr = "";
$groups = FALSE;
$users = FALSE;
if((!isset($params['thegroups']) || empty($params['thegroups'])) && (!isset($params['theusers']) || empty($params['theusers']))){
    echo("Enter at least 1 user or group!");
}else{
   if( isset($params['thegroups']) && !empty($params['thegroups']) ) {
      $groups = explode(';',trim($params['thegroups']));
      $result = count($groups);
      if($result == 0){
          $groups = FALSE;
      }
   }

   if( isset($params['theusers']) && !empty($params['theusers']) ) {
      $users = explode(';',trim($params['theusers']));
      $result = count($users);
      if($result == 0){
          $users = FALSE;
      }
   }
}

global $gCms;
$feusers =& $gCms->modules['FrontEndUsers']['object'];
$userid = $feusers->LoggedInId();

if($userid){
$username = $feusers->LoggedInName();
echo("okay, we we got a user ".$username."");
if($users){
        if(in_array($username, $users)){
echo("hi ". $username."");
echo $params['thepagecontent'];
    }else{
    echo("hi ". $username."");
echo("Not for you");
    }
   
    }else{
    echo("Hmmm, we have got a user ".$username." but the page doesn't provide users");
    }
}else{
    echo("Members only!!!");
    echo("Login and try again");
}
echo $retstr;


Now I can create pages and select the right template to get the "fields" for users and groups. After entering the username(s) the page is automatically restricted.


I will add the grouppart later on, first I want to know if this is a workable solution...


Cheers,
-- Rob
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: UDT to get custom content based on extra content blocks

Post by calguy1000 »

There are much easier way to do this.... no UDT required
using the extra1 property (or extra2 or extra3).... and CustomContent.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
robbedoes
Forum Members
Forum Members
Posts: 75
Joined: Wed Aug 20, 2008 2:08 pm
Location: Nijmegen, the Netherlands

Re: UDT to get custom content based on extra content blocks

Post by robbedoes »

calguy1000 wrote: There are much easier way to do this.... no UDT required
using the extra1 property (or extra2 or extra3).... and CustomContent.
But then you have to add that curly code to your content which will be messed up using tiny...

And the users do have to recal the fields while "users" and "groups" are self explaining.

Ahhhhhh.

{if $ccuser->loggedin() && $ccuser->memberof('members')} in a template.

$ccuser->memberof(XXX) and then pass the value of extrafield to XXX

Right?
Last edited by robbedoes on Fri Jan 16, 2009 5:39 pm, edited 1 time in total.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: UDT to get custom content based on extra content blocks

Post by calguy1000 »

if you had for example:

group1,group2,group3  in the extra1 field of a page

in your template you could do something like:

Code: Select all

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1)}
   {* this page is public *}
{elseif $ccuser->memberof($extra1)}
   {* this page is private and the user is allowed to see the content *}
{else}
   {* this page is private, and the user is not allowed to see its content *}
{/if}
or....

Code: Select all

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1) or $ccuser->memberof($extra1)}
   {* this page is public or this logged in user is allowed to see the content *}
{else}
   {* this page is private, and the user is not allowed to see its content *}
{/if}
You don't need to test for $ccuser->loggedin()..... that's automatic with $ccuser->memberof()

the syntax may be a bit off.... but it's close.
Last edited by calguy1000 on Fri Jan 16, 2009 5:56 pm, edited 1 time in total.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
robbedoes
Forum Members
Forum Members
Posts: 75
Joined: Wed Aug 20, 2008 2:08 pm
Location: Nijmegen, the Netherlands

Re: UDT to get custom content based on extra content blocks

Post by robbedoes »

Thanks for the code example. I didn't know Smarty's capture.

I still don't like the extra field because the backend user has to remember what field to use.
But I can just replace it with a custom template block of course:-)

It would be very nice if future versions of cmsms offer a kind of multy select for this kind of restriction.

Thanks again. I'm going to try it. 
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: UDT to get custom content based on extra content blocks

Post by duclet »

You can also use a syntax highlighter to edit your template where all those Smarty tags are in. The user can just use TinyMCE to edit the page content.
robbedoes
Forum Members
Forum Members
Posts: 75
Joined: Wed Aug 20, 2008 2:08 pm
Location: Nijmegen, the Netherlands

Re: UDT to get custom content based on extra content blocks

Post by robbedoes »

duclet wrote: You can also use a syntax highlighter to edit your template where all those Smarty tags are in. The user can just use TinyMCE to edit the page content.
Yes I've added the functionality to a template and the rules (group) to an extra field. Works fine.
zapperlot
Forum Members
Forum Members
Posts: 17
Joined: Mon Mar 03, 2008 3:39 pm

Re: UDT to get custom content based on extra content blocks

Post by zapperlot »

Hmm I like Calguy's solution. But:
$content_obj->GetPropertyValue('extra1') 

this causes a PHP - Error: PHP Fatal error:  Call to a member function GetPropertyValue() on a non-object in ...

I ve replaced {content} - tag in my template with this:

{capture assign='extra1'}{$content_obj->GetPropertyValue('extra1')}{/capture}
{if empty($extra1) or $ccuser->memberof($extra1)}
  {content}
  {* this page is public or this logged in user is allowed to see the content *}
{else}
  you have no access
  {* this page is private, and the user is not allowed to see its content *}
{/if}

I think I don't have the $content_obj - object . I am using cmsms 1.41

Any hints are welcome.
Greetings from Germany
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: UDT to get custom content based on extra content blocks

Post by calguy1000 »

Upgrade
1.4.1 is no longer supported.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “Developers Discussion”