<?php if (!defined('PmWiki')) exit();
/*  PMWikiListBlock, section lists for pages with templating .
    Copyright (C) 2007 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License, or (at your option) any later version.

    MODULE: PMWikiListBlock
    USAGE: (:listblocks pg=group.page1[#section] fmt=group.page2[#section] /
        if="equal {=$variable} value":)
*/
$RecipeInfo['ListBlocks']['Version'] = '2007-12-03';
Markup('listblocksection', 'directives',"/\(:listblocks\s+(.*):\)/e",
       "ListBlock_Get(PSS('$1'))");

SDV($ListBlockName,"entry");
SDV($ListBlockForm,"List.Template");
/*  ParseBlock */
function ListBlock_Get($args){
    // Create SectionLists of a page
    global $ListBlockName, $ListBlockForm;
    // Parse options passed in markup
    $options = ParseArgs($args);//DEBUG echo "Options<br/>";print_r ($options);
    $data = $options['pg'];$form = $options['fmt'];
    if ((! CondAuth($data, 'read')) || (! CondAuth($template, 'read'))) return 'no access';
    // Read section template $form
    list($formpage,$formsection) = explode("#",$form,2);
    //DEBUG echo "Form Page=".$formpage." Form Section=".$formsection."<br/>";
    $formpage = ($formpage) ? $formpage : $ListBlockForm;
    $form = ReadPage($formpage);
    // Find the template by $formsection
    if (!empty($formsection)){
        if (!@preg_match(ListBlock_SetAnchor($formsection),$form['text'],$match)) return 'no template';
        $form['text'] = $match[0];
    }
    // Read page data $data
    list($datapage,$datasection) = explode("#",$data,2);
    //DEBUG echo "Data Page=".$datapage." Data Section=".$datasection."<br/>";
    $data = ReadPage($datapage);
    // Set the reg exp for the data rows
    $datasection = ($datasection) ? $datasection : $ListBlockName;
    // Fetch data rows and parse
    if (@preg_match_all(ListBlock_SetAnchor($datasection),$data['text'],$dataset)){
        //print_r ($dataset);
        foreach ($dataset[0] as $datarow) {
            $section = $form['text'];
            // Apply format template to $datarow
            if ($datarow) { 
                ListBlock_SetVar($datarow,$section);
                if (ListBlock_CheckRow($options,$datarow)) $newpage .= $section;
        }   }
        // Rewind the page process and return
        return PRR($newpage);
    } else { return 'no section'; }
}
function ListBlock_SetVar ($text_in,&$text_out){
    // Replace PTV names from template with PTV values from section
    global $PageTextVarPatterns;
    foreach((array)$PageTextVarPatterns as $pat) {
        if (preg_match_all($pat,$text_in,$match,PREG_SET_ORDER)){
            // DEBUG print_r ($match);
            foreach($match as $m) $text_out=str_replace("{=\$".$m[2]."}",$m[3],$text_out);
}   }   } 

function ListBlock_CheckRow ($options,$section){
    global $Conditions;
    $condspec = $options['if'];
    
    if (array_key_exists( "if",$options)) {
        ListBlock_SetVar ($section,$condspec);
        if (!preg_match("/^\\s*(!?)\\s*(\\S*)\\s*(.*?)\\s*$/",$condspec,$match)) 
            return 0;
        list($x, $not, $condname, $condparm) = $match;
        if (!isset($Conditions[$condname])) 
            return 1;
        $tf = (int)@eval("return ({$Conditions[$condname]});");
        return (boolean)($tf xor $not);
    } else {return 1;}
}

function ListBlock_SetAnchor ($anchor){
    // Set reg exp pattern to return specified section
    return "/\[\[#".$anchor."\]\](.*?)(?=\[\[#".$anchor.")/esi";
}