Welcome Guest, Not a member yet? Register   Sign In
Custom conditionals in html content?
#2

[eluser]CroNiX[/eluser]
I got this figured out and thought I'd post in case anyone else could use this.
This is useful for a client who has an html editor, or area in their application where they create html markup for dynamic page content, to be able to easily have areas within a page only shown to certain usergroups. This is great for cases where you don't want to restrict the whole page.

For instance, the client recently used it on a payment page in a regular website (not shopping cart enabled), where members of paid usergroups get shown certain discounted paypal buttons while unregistered users get shown different buttons.

It is to be used in the following ways and bolded text is required.
The variables are the usergroups who are allowed to see the html contained.

To only display a section of html who is a member of group1.
Quote:blah blah html everyone can see...
[protected=group1]This is the text/html displayed to people of "group1".[/protected]
blah blah html everyone can see...

To only display a section of html to multiple groups.
Just chain the names of the multiple group names with a semi colon for a separator.
Quote:blah blah html everyone can see...
[protected=group1;group2]This is the text/html displayed to people of "group1" OR "group2".[/protected]
blah blah html everyone can see...

If you want to have a section that contains alternate text which is displayed to users who are NOT in the defined groups, use [alt][/alt] tags nested within the [protected][/protected] text area.
Quote:blah blah html everyone can see...
[protected=group1;group2]This is the text/html displayed to people of "group1" or "group2".
[alt]This is the html/text displayed to people who are NOT in usergroup "group1" or "group2".[/alt]
[/protected]
blah blah html everyone can see...

The only thing you really need to do is figure out how to populate the current users usergroups in the $users_groups array in the beginning of the code.

Heres the code. To use it (assuming its in a controller) just:
Code:
$parsed_text = $this->parse_protected($html_text_to_be_parsed);

I hope someone finds it useful.

Code:
//note: the array users_groups would contain the users current usergroups,
//which is how the permissions are determined.  You will need to populate
//this your own way to suit your case.

function parse_protected($text)
{
    //array of current users' usergroups
    $users_groups = array('group1');
    //find protected text
    $exp = '/(\[protected=)(.+)(\])(.+)(\[\/protected\])/iU';
    preg_match_all($exp, $text, $matches, PREG_SET_ORDER);
    //if no matches were found, just return original text
    if(count($matches) < 1)
    {
        return $text;
    }
    else
    {
        //parse
        foreach($matches as $match)
        {
            $original     = $match[0];             //full original text of matched item
            $groups     = split(';', $match[2]);//the usergroups (to right of equals)
            $content     = $match[4];            //the content string

            //check for alternative text: [alt]alternative text[/alt]
            $alt_exp = '/\[alt\](.+?)\[\/alt\]/i';
            preg_match_all($alt_exp, $original, $alt_matches, PREG_SET_ORDER);

            //user is allowed to view, get protected content
            if(count(array_intersect($groups, $users_groups)) > 0)
            {
                //replace the original match with its content
                $new_text = str_replace($original, $content, $original);

                //check to see if there was alt text and remove it
                if(count($alt_matches) > 0)
                {
                    $alt = $alt_matches[0][0];
                    $new_text = str_replace($alt, '', $new_text);
                }
                $text = str_replace($original, $new_text, $text);
            }
            else
            {
                //not allowed, check if there is alternative content.
                //If so, use that else remove entire match
                if(count($alt_matches) > 0)
                {
                    $alt = $alt_matches[0][1];
                    $new_text = str_replace($original, $alt, $original);
                }
                else
                {
                    $new_text = str_replace($original, '', $original);
                }
                $text = str_replace($original, $new_text, $text);
            }
        }
    }
    return $text;
}


Messages In This Thread
Custom conditionals in html content? - by El Forum - 08-05-2010, 01:32 PM
Custom conditionals in html content? - by El Forum - 08-06-2010, 07:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB