We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

prep_conditionals

Development and Programming

vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

Can someone please please please help me with the following..

I have several variable pairs inside a plugin. I have the data prepared but need to use the prep_conditionals function to sort.

Inside my tag I have:

{statuses}
 {if status == current_status}{status}{/if}
{statuses}

{fields}
 {if field_type = 'textarea'}do this{/if}
 {if field_type = 'text'}do this{/if}
{/fields}

in the plugin i have this

... get the query data for statuses
... get the query data for fields

$tagdata = TMPL->tagdata;

foreach ($field_data as $row) {
.. blah blah
$cond = $row;
$tagdata = $FNS->prep_conditionals($tagdata, $cond);
}

foreach ($status_data as $row) {
.. blah blah
$cond = $row;
$tagdata = $FNS->prep_conditionals($tagdata, $cond);
}

then variable pairs…

foreach ($TMPL->var_pair as $key => $val) {    

        if ($key == 'statuses') {
            $statuses = '';
            preg_match( "/".LD.$key.RD."(.*?)".LD.SLASH.$key.RD."/s", $tagdata, $match); 
            foreach ($status_data as $row) {
                $str = $match['1'];
                foreach ($row as $k => $v ) {
                    $str = str_replace( LD.$k.RD, $v, $str); 
                }
                $statuses .= $str;
            }
            $tagdata = str_replace($match['0'], $statuses, $tagdata);      
        }
        
        
        
        
        if ($key == 'custom_fields') {
            $custom_fields = '';
            preg_match( "/".LD.$key.RD."(.*?)".LD.SLASH.$key.RD."/s", $tagdata, $match); 
            foreach ($field_data as $row) {
                $str = $match['1']; 
                foreach ($row as $k => $v ) { 
                    $str = str_replace( LD.$k.RD, $v, $str ); 
                }
                $custom_fields .= $str; 
            }
            $tagdata = str_replace($match['0'], $custom_fields, $tagdata);
        }
        
    }                

return $tagdata;

its not parsing the conditionals properly.

Can someone (i.e one of you EE geniuses) please help?

Mod Edit: Moved to the Plugins: Technical forum.

       
Derek Jones's avatar
Derek Jones
7,561 posts
17 years ago
Derek Jones's avatar Derek Jones

You need to operate on a copy of $TMPL->tagdata, or more specifically, a copy of the variable pairs’ tagdata, and process the conditionals for each loop within the variable pair. Look at the Weblog module’s {categories} tag pair handling in parse_weblog_entries() for an example.

       
vinci's avatar
vinci
85 posts
17 years ago
vinci's avatar vinci

I thought that was the case. I dont have time to try it out right now, but for the time being ive just made the two loops as separate plugins… Thanks for the help Derek

       
vinci's avatar
vinci
85 posts
about 17 years ago
vinci's avatar vinci

Derek - its working, and I think its working they way you mentioned… But can you quickly cast your eye over this, Thanks 😊

$w_sql = "SELECT blog_title AS a_weblog_title, weblog_id AS a_weblog_id, blog_name AS a_weblog_name FROM exp_weblogs ";
    $w_query = $DB->query($w_sql);
    
    $g_sql = "SELECT group_id AS allowed_group_id, group_title AS allowed_group_title FROM exp_member_groups ";
    $g_query = $DB->query($g_sql);
    
    
    
    $tagdata = $TMPL->tagdata;s
    if (preg_match("/".LD."member_weblogs".RD."(.*?)".LD.SLASH.'member_weblogs'.RD."/s", $TMPL->tagdata, $w_match)) {
        $weblog_chunk = $w_match['1'];
    }
    
    if (preg_match("/".LD."allowed_groups".RD."(.*?)".LD.SLASH.'allowed_groups'.RD."/s", $TMPL->tagdata, $g_match)) {
        $groups_chunk = $g_match['1'];
    }
    
    
    
    $w = '';
    foreach ($w_query->result as $row) {
        foreach ($TMPL->var_pair as $key => $val) {
            if (ereg("^member_weblogs", $key)) {    
                $temp = $weblog_chunk;
                $cond = $row;
                $temp = $FNS->prep_conditionals($temp, $cond);
                foreach ($row as $k => $v ) {
                    $temp = str_replace( LD.$k.RD, $v, $temp ); 
                }
                $w    .= $temp;    
            }    
        }    
    }
    //echo $w;
        
    $g = '';
    foreach ($g_query->result as $row) {
        foreach ($TMPL->var_pair as $key => $val) {
            if (ereg("^allowed_groups", $key)) {    
                $temp = $groups_chunk;
                $cond = $row;
                $temp = $FNS->prep_conditionals($temp, $cond);
                foreach ($row as $k => $v ) {
                    $temp = str_replace( LD.$k.RD, $v, $temp ); 
                }
                $g    .= $temp;    
            }    
        }    
    }
    //echo $g;    
        
    $tagdata = str_replace($g_match['0'], $g, $tagdata);
    $tagdata = str_replace($w_match['0'], $w, $tagdata);
    
    return $tagdata;
       
Derek Jones's avatar
Derek Jones
7,561 posts
about 17 years ago
Derek Jones's avatar Derek Jones

Yes, though you don’t appear to be using $key nor $val from your $TMPL->var_pair foreach statements, so you can forego those, and I know in 1.x the weblog module still uses it, but you’d want to use PCRE regex functions instead of POSIX. It won’t matter though since you’ll just be removing it altogether along with the $TMPL->var_pair foreach statements.

       
vinci's avatar
vinci
85 posts
about 17 years ago
vinci's avatar vinci

so you recommend removing the lines:

foreach ($TMPL->var_pair as $key => $val) {
    if (ereg("^member_weblogs", $key)) {

Yes that does work, so it ends up being THAT simple. Thanks for the help Derek…

next problem is… (kidding)

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.