<?php
/*
   GalleryAdv.php
   
   (c) Bernhard Weichel
   
   $Log: GalleryAdv.php,v $
   Revision 1.1  2003/09/07 19:07:09  beweiche
   *** empty log message ***

   
   
*/

  $DefaultWebMenu = "Main/WebMenu";


// This pattern refers to an image in the Thumbnail page
// should give back three parameters
//   image name, thumbnail size, image title
//   image name is used as a basis for computing resized images
//   thumbnail size is the size of the thumbs printed here
//   image title is printed along with the thumb and also presented
//      in imagetrail

  $ImagePattern = '/^\.image[ ]+([^\t ]+)[ ]+([^\t ]+)(.*)$/e';
  $ImagePatternInline = '/\\[\\[\\$Image[ ]+([^\t ]+)[ ]+([^\t \]]+)[ ]*\\]\\]*/e';
  
  $DoubleBrackets[$ImagePattern] ="FmtImageThumb('$1', '$2', '$3')";
  
//todo: make a good inline markup  
  
  $InlineReplacements[$ImagePatternInline] ="FmtImageThumbInline('$1', '$2')";
  
  
function FmtImageThumbInline($name, $size)
 {
  return "Attach:".$name."x".$size.".jpg";
 }
  
// end of todo
  
function FmtImageThumb($name, $size, $title)
 {
  global $pagename;

  // make sure that the name of the uploaded file is mangled in the
  // same way as wikipages. This makes the image page having the same name as the filename

  $thename= FmtWikiLink('',"{{".$name."}}", NULL,'PageName',$pagename);  // pagename of the current image
  $thename= FmtPageName('$Title', $thename);                                // Extract the title to be used as filename
  
  print "<div class='thumbnail'>";

  PrintText($pagename, "[==][[{{".$thename."}} Attach:".FmtImageFileName($thename, $size)."]]"
                        ." [[{{".$thename."}} ".$title."]]" );
  print "</DIV>";
  return "";
 }
                                 
// the Default text for an image page                          
                          
  $DefaultPageTextFmt = "no remarks ";  

// redefine link formats

   // todo: restrict it on a gallery page

$WikiPageCreateFmt = "<a href='\$PageUrl'>\$LinkText</a>" ;
$WikiPageCreateSpaceFmt = "<a href='\$PageUrl'>\$LinkText</a>" ;

// Making an image filename

function FmtImageFileName($name, $size)
 {
    if ($size == "original") {$size="";}                                  
    else {$size="x".$size;}
    
    return $name.$size.".jpg";

 }

// now the gallery trails
  
$DoubleBrackets["/^\.imagetrail ($FreeLinkPattern)/e"] 
  = 'MakeImageTrailStop("$1");';
$DoubleBrackets["/^\.imagetrail/e"] 
  = 'MakeImageTrailStop("$ImageTrail");';

function MakeImageTrailStop($link) {
  global $pagename;
  $t = ReadImageTrail($link);
  $prev = ''; $next = '';
  for($i=0;$i<count($t);$i++) {
    if ($t[$i]['name']==$pagename) {
      $ImageTitle=$t[$i]['title'];
      if ($i>0) $prev = "[[".$t[$i-1]['link']." &larr;]]";
      if ($i+1<count($t)) $next =  "[[".$t[$i+1]['link']." &rarr;]]";
    }
  }
  return "<center>$prev  $link  $next </center><center><H1>$ImageTitle</H1></center>";
}


function ReadImageTrail($link) {
  global $pagename, $ImagePattern;
  $trailname = FmtWikiLink('',$link,NULL,'PageName');
  $trailpage = ReadPage($trailname);
  if ($trailpage) {
    $trailgroup = FmtPageName('$Group',$trailname);
    $n = 0;
    foreach(explode("\n",$trailpage['text']) as $x) {
      if (preg_match("$ImagePattern", $x,$match)) {
        $t[$n]['img' ] = $match[1];                                                   // name of the current image
        $t[$n]['link' ] = $trailgroup."/{{".$match[1].'}}';                           // Wikilink of the current imabe
        $t[$n]['size' ] = $match[2];                                                  // requested size of the image
        $t[$n]['title'] = $match[3];                                                  // title of the current image
        $t[$n]['name'] = FmtWikiLink('',$t[$n]['link'], NULL,'PageName',$trailpage);  // pagename of the current image

        $n++;
      } 
    }
  }
  return $t;
}


?>