<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005 Patrick R. Michaud (pmichaud@pobox.com)
    This file is newpagebox.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.  

    To use this script, simply place it into the cookbook/ folder
    and add the line

        include_once('cookbook/newpagebox.php');

    to a local customization file.
*/

Markup('newpagebox', 'directives',
  '/\\(:newpagebox\\s*(.*?):\\)/ei',
  "NewPageBox(\$pagename, PSS('$1'))");
$HandleActions['new'] = 'HandleNew';

function NewPageBox($pagename, $opt) {
  global $ScriptUrl;
  $defaults = array('base' => $pagename, 'template' => '',
    'label' => FmtPageName(' $[Create a new page called:] ', $pagename));
  $opt = array_merge($defaults, ParseArgs($opt));
  return "<form ".Keep("class='newpage' action='$PageUrl' 
    method='post'><input type='hidden' name='n' value='$pagename' /><input
    type='hidden' name='action' value='new' /><input
    type='hidden' name='base' value='{$opt['base']}' /><input
    type='hidden' name='template' value='{$opt['template']}' />
    <input class='newpagebutton' type='submit' value='{$opt['label']}' />
    <input class='newpagetext' type='text' name='name' value='' /></form>");
}

function HandleNew($pagename) {
  $name = @$_REQUEST['name'];
  if (!$name) Redirect($pagename);
  $base = MakePageName($pagename, $_REQUEST['base']);
  $newpage = MakePageName($base, $name);
  $urlfmt = '$PageUrl?action=edit';
  if (@$_REQUEST['template']) 
    $urlfmt .= '&template=' . MakePageName($base, $_REQUEST['template']);
  Redirect($newpage, $urlfmt);
}