<?php if (!defined('PmWiki')) exit();
/*
  EditTitle (v 0.5)
  
  Waylan Limberg (waylan@gmail.com)
  
  Description: Adds an 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 pulls title from markup and sets it to display in text input field
function RemoveTitle($pagename, &$page, &$new){
  # pattern should be same as defined in markup
  $pattern = '/\\(:title\\s(.*?):\\)/';
  $new[text] = trim(preg_replace_callback(
      $pattern,
      create_function(
          '$matches',
          '
            global $InputTags;
            SDVA($InputTags["e_title"], array(
            ":html" => "<input type=\'text\' \$InputFormArgs />",
            "name" => "title", "size" => "60" ,"value" => $matches[1])); 
            return "";
          '
      ),
      $new[text]
  ));
  return;
}

## AddTitle adds title back into markup before saving
function AddTitle($pagename, &$page, &$new){
  if ($new[title] > '') $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]) == '') {
    $EnablePost = 0;
    $MessagesFmt[] = '<h3 class="wikimessage">A Title must be defined for this page</h3>';
  }
  return;
}