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

# $InputAttrs are the attributes we allow in output tags
SDV($InputAttrs, array('name', 'value', 'id', 'class', 'rows', 'cols', 
  'checked', 'size', 'action', 'method'));

# Set up formatting for text, submit, hidden, radio, etc. types
foreach(array('text', 'submit', 'hidden', 'password', 'radio', 'checkbox') 
  as $t) SDV($InputTags[$t][':html'], "<input type='$t' \$InputFormArgs />");

# (:input form:)
SDVA($InputTags['form'], array(
  ':args' => array('action', 'method'),
  ':html' => "<form \$InputFormArgs>",
  'method' => 'post'));

# (:input end:)
SDV($InputTags['end'][':html'], '</form>');

# (:input textarea:)
SDVA($InputTags['textarea'], array(
  ':html' => "<textarea \$InputFormArgs></textarea>"));

# (:input ...:) items for edit forms
SDVA($InputTags['e_form'], array(
  ':html' => "<form action='\$PageUrl' method='post'><input type='hidden' name='action' value='edit' />"));
SDVA($InputTags['e_text'], array(
  ':html' => "<textarea \$InputFormArgs>\$EditText</textarea>",
  'name' => 'text', 'id' => 'text', rows => '60', cols => '60'));
SDVA($InputTags['e_author'], array(
  ':html' => "<input type='text' \$InputFormArgs />",
  'name' => 'author', 'value' => $Author));
SDVA($InputTags['e_minor'], array(
  ':html' => "<input type='checkbox' \$InputFormArgs />",
  'name' => 'diffclass', 'value' => 'minor'));
SDVA($InputTags['e_save'], array(
  ':html' => "<input type='submit' \$InputFormArgs />",
  'name' => '_post', 'value' => 'Save'));
SDVA($InputTags['e_preview'], array(
  ':html' => "<input type='submit' \$InputFormArgs />",
  'name' => 'preview', 'value' => 'Preview'));

Markup('input', '>directives', 
  '/\\(:input\\s+(\\w+)(.*?):\\)/ei',
  "Keep(InputMarkup(\$pagename, '$1', PSS('$2')))");

function InputMarkup($pagename, $type, $args) {
  global $InputTags, $InputAttrs, $FmtV;
  if (!$InputTags[$type]) return "(:input $type $args:)";
  $opt = array_merge($InputTags[$type], ParseArgs($args));
  $args = $opt[':args'];
  if (!$args) $args = array('name', 'value');
  while (count($opt['']) > 0 && count($args) > 0) 
    $opt[array_shift($args)] = array_shift($opt['']);
  $attr = array();
  foreach ($InputAttrs as $a) {
    if (!isset($opt[$a])) continue;
    $attr[] = "$a='".str_replace("'", '&#39;', $opt[$a])."'";
  }
  $FmtV['$InputFormArgs'] = implode(' ', $attr);
  return FmtPageName($opt[':html'], $pagename);
}