<?php
if ( !defined( 'SMARTY_DIR' ) ) {
	include_once( 'init.php' );
}

if ($_POST['btnsave'] ) {

	if ($_POST["id"] ) {
		// editing a template

		$osDB->query('UPDATE ! set text = ?, subject = ? where userid = ? and id = ?', array( USERTEMPLATE_TABLE, $_POST["txttemplate"], $_POST['txtsubject'], $_SESSION['UserId'], $_POST['id'] ) );

	} else {
		// adding a new template
		$osDB->query('INSERT INTO ! (subject, text, userid) values (?, ?, ?)', array( USERTEMPLATE_TABLE, $_POST['txtsubject'], $_POST["txttemplate"], $_SESSION['UserId'] ) );
	}

	header( 'location: mytemplates.php?recipient=' . $_POST['recipient'] );
	exit;
}

if ( $_GET['id'] ) {

	$t->assign( 'template',$osDB->getRow( 'SELECT * FROM ! WHERE userid = ? and id = ?', array( USERTEMPLATE_TABLE, $_SESSION['UserId'], $_GET["id"] ) ));

}

$t->assign('rendered_page', $t->fetch('mytemplatesadd.tpl') );

$t->display ( 'index.tpl' );

?>