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

/*	=== EditAttributes ===
 *	Copyright 2009 Eemeli Aro <eemeli@gmail.com>
 *
 *	Edit a page's title, description and other attributes
 *	using separate EditForm fields
 *
 *	Developed and tested using PmWiki 2.2.0,
 *	partly based on Mike Shanley's EditMore
 *
 *	To install, first add the following to your config file,
 *	adjusted for your configuration:

  		$EditAttrFields = array( 'title' => 3, 'keywords' => 3, 'description' => 3 );
  		include_once("$FarmD/cookbook/editattr.php");

 *  Next, add the matching input fields to your Site.EditForm,
 *  these are of the form (:input e_FIELDNAME:) -- eg. (:input e_title:)
 *
 *	$EditAttrFields data format:
 *		  1 (001)	page text directive
 *		  2 (010)	page attribute
 *		  3 (011)	page text directive and page attribute
 *		4,6 (1x0)	page attribute with history
 * 		5,7 (1x1)	page text directive and page attribute with history
 *
 *	use the following to disable automatic title field filling on all pages

		$EditAttrTitleAutofillPatterns = '-*';

 *
 *	For more information, please see the online documentation at
 *		http://www.pmwiki.org/wiki/Cookbook/EditAttributes
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License,
 *	Version 2, as published by the Free Software Foundation.
 *	http://www.gnu.org/copyleft/gpl.html
 */

$RecipeInfo['EditAttributes']['Version'] = '2009-02-24';

if (empty( $EditAttrFields )) return;

$EditFields = array_merge( $EditFields, array_keys($EditAttrFields) );
$SaveProperties = array_diff( $SaveProperties, array_keys($EditAttrFields) );

array_unshift($EditFunctions,'EditAttrBeforePost');
array_push($EditFunctions,'EditAttrAfterPost');

function EditAttrBeforePost( $pagename, &$page, &$new ) {
	global $EnablePost, $PageAttributes, $HandleAuth, $Now, $EditAttrFields;

	foreach( $EditAttrFields as $mf => $mft ) {
		## require attr level authentication to edit password fields
		if ( ( isset( $PageAttributes[$mf] ) || !strncmp( $mf, 'passwd', 6 ) )
			&& !RetrieveAuthPage( $pagename, $HandleAuth['postattr'], FALSE, READPAGE_CURRENT )
		) $new[$mf] = @$page[$mf];

		## save history
		if ( ( $mft >= 4 ) && ( @$new[$mf] != @$page[$mf] ) ) $new["$mf:$Now"] = @$new[$mf];

		## add page text directives
		if ( ( $mft & 1 ) && !empty( $new[$mf] ) )
			$new['text'] .= "\n(:$mf ".str_replace( array( ':)', "\n", "\r", "\x0B" ), array(': )',' ',' ',' '), $new[$mf] ).':)';

		## cache attribute values for page preview
		if ( !$EnablePost && isset($new[$mf]) && ( $mft >= 2 ) ) PCache( $pagename, array( $mf => $new[$mf] ) );

		## remove blanks & page directives only
		if ( empty($new[$mf]) || ( $mft <= 1 ) ) unset($new[$mf]);
	}
}

function EditAttrAfterPost( $pagename, &$page, &$new ) {
	global $MarkupTable, $PageAttributes, $HandleAuth, $InputTags, $SearchPatterns, $EditAttrFields, $EditAttrPatterns, $EditAttrTitleAutofillPatterns;

	SDVA( $EditAttrTitleAutofillPatterns, array(
		'recent' => $SearchPatterns['normal']['recent'],
		'group' => $SearchPatterns['normal']['group'],
		'admin' => '!^(Site|SiteAdmin|PmWiki)\.!',
		'nonbase' => '!\b(SideBar|Draft)$!' ));

	foreach( $EditAttrFields as $mf => $mft ) {
		$pat = isset( $EditAttrPatterns[$mf] ) ? $EditAttrPatterns[$mf] : @$MarkupTable[$mf]['pat'];

		if ($pat) {
			if ( preg_match($pat, $new['text'], $matches) ) {
				$new[$mf] = $matches[1];
			} else {
				if ( ($mf=='title')
					&& empty($new['title'])
					&& !isset($_POST['title'])
					&& MatchPageNames( $pagename, $EditAttrTitleAutofillPatterns )
				) {
					$new['title'] = FmtPageName('$Title',$pagename);
				}
			}
			$new['text'] = preg_replace( $pat[0].'\\n?'.substr($pat,1), '', $new['text'] );
		}

		if ( ( isset( $PageAttributes[$mf] ) || !strncmp( $mf, 'passwd', 6 ) )
			&& !RetrieveAuthPage( $pagename, $HandleAuth['attr'], FALSE, READPAGE_CURRENT )
		) {
			$new[$mf] = '(protected)';
			SDV( $InputTags["e_$mf"]['disabled'], 'disabled' );
		}

		SDVA( $InputTags["e_$mf"], array(
			':html' => "<input type='text' \$InputFormArgs />",
			'name' => $mf, 'size' => '60', 'title' =>'Title',
			'value' => str_replace('$','&#036;',htmlspecialchars(@$new[$mf],ENT_NOQUOTES)) ));
	}
}