<?php if (!defined('PmWiki')) exit();
/*
  EditTitle (v 0.8)
  
  Copyright 2005 - Waylan Limberg (waylan@gmail.com)
  
  Description: Adds a Title input field to the edit form of PmWiki.
  For more info and instructions go to http://www.pmwiki.org/wiki/Cookbook/EditTitle
*/


## define pointers to functions (Note: order IS important)
array_push($EditFunctions, 'RemoveTitle');
array_unshift($EditFunctions, 'AddTitle');
array_unshift($EditFunctions, 'RequireTitle');
array_unshift($EditFields, 'title');

## RemoveTitle removes title from markup and  sets it to display in text input field
function RemoveTitle($pagename, &$page, &$new){
  global $InputTags, $EditTitle;
  if ($EditTitle) {
    # pattern should be same as defined in markup
    $pattern = '/\\(:title\\s(.*?):\\)/';
    $new[text] = trim(preg_replace($pattern,'', $new[text] ));
    if (!isset($new[title])) $new[title] = '';
    SDVA($InputTags["e_title"], array(
    ':html' => "<input type='text' \$InputFormArgs />",
    'name' => 'title', 'size' => '60' ,'value' => $new[title]));
  } else {
    SDVA($InputTags["e_title"], array(':html' => ''));
  }
  return;
}

## AddTitle adds title back into markup before saving
function AddTitle($pagename, &$page, &$new){
  global $EditTitle;
  if ($new[title] > '' && $EditTitle) 
    $new[text] = "(:title $new[title]:)\n$new[text]";
  return;
}

## Require that a title be defined on each page
function RequireTitle($pagename, &$page, &$new){
  global $EnablePost, $MessagesFmt, $ForceTitle;
  if ($ForceTitle && isset($new[text]) && trim($new[title]) == '' && trim($new[text]) != 'delete') {
    $EnablePost = 0;
    $MessagesFmt[] = '<h3 class="wikimessage">A Title must be defined for this page</h3>';
  }
  return;
}