<?php if (!defined('PmWiki')) exit();

# --------------------------------------------------------------------------------------------------
# Page Action markup
# --------------------------------------------------------------------------------------------------
#
# Copyright 2010 Bruce Reidenbach
#
# This file is page-action.php; 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.
#
# This recipe adds "(:pageaction ... :)" markup to control or change the
# default page action when a wiki page is accessed.  Examples:
#
#   (:pageaction print:)           # the wiki page will always be viewed with the print skin
#   (:pageaction print=browse:)    # disable the print skin
#
# To use this recipe, simply copy it into the cookbook/ directory, and
# add the following line to a local customization:
#
#   include_once("$FarmD/cookbook/page-action.php");

$RecipeInfo['PageAction']['Version'] = '2010-09-02';

# Catch all text between (:pageaction and :) and call PageAction with that text

Markup ('PageAction', '<include', '/\\(:pageaction\\s+(.+?):\\)/ei',
        "PageAction ('$pagename', '$action', '$1')");

# Change the page action when the page is opened
#   If no incoming action is specified, i.e., (:pageaction myaction:), redirect with the requested
#   action if the current page action is 'browse'.  Otherwise, redirect with the requested action
#   if the current page action matches the specified incoming page action from the option list.

function PageAction ($pagename, $action, $opt) {
  $args = ParseArgs ($opt);
  foreach ($args as $key => $req) {
    if (strlen ($key) == 0) {  #  No incoming action specified?
      $key = 'browse';         #  Default to 'browse'
      $req = $args[''][0];
    }
    if ($action == $key && $action != $req) {
      Redirect ("{$pagename}?action=$req");
    }
  }
}

?>