<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; 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.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>

<div class="athenatitleshadow">Edit Templates<div class="athenatitle">Edit Templates</div></div>
<br />

<a href="<?php echo $_SERVER['PHP_SELF']; ?>">« Start Over</a><br>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method=post>
<table width="100%" cellpadding="10"><tr><td>
<ul class="text">

<?php
  require_once 'db.php';
  require_once 'template.php';
  require_once 'server_functions.php';

  globalize_request_keys('add_name', 'template_id', 'del', 'add_text',
    'submit', 'template_name', 'template_text');

  $template = new Template ();

  if ($add_name and $add_text) {
    $new_tid = $template->Add ( array (
      'template_name' => $add_name,
      'template_text' => $add_text,
      'crm_id' => $REMOTE_USER_CRM_ID,
    ));
  }

  $tmpl_list = $template->List_All ();

  $valid_ids = array($template_id, $del);

  ## validate the template they are trying to modify
  $ok = 0;
  foreach ($tmpl_list as $r) {
    if (in_array($r['template_id'], $valid_ids)) $ok = 1;
  }

  if (!$ok) {
    $template_id = '';
    $del = '';
  }

  if ($del) {
    if (!$template->Delete ($del)) {
      print "<font color=\"red\">error deleting template.</font>\n";
    }
    $tmpl_list = $template->List_All ();
  }

  if (!$template_id) {
    foreach ($tmpl_list as $r) {
      print "<li><a href =\"" . $_SERVER['PHP_SELF'] . "?del=$r[template_id]\">Del</a> - <a href=\"" . $_SERVER['PHP_SELF'] . "?template_id=$r[template_id]\">";
      print "$r[template_name]</li></a><br>\n";
    }
?>

</ul>

</td><td width="10">&nbsp;</td>

<td align="right" valign="top">

<table border="0" cellpadding="0" cellspacing="0" bgcolor="#e6f0ce">
<tr> <td> <img src="../img/elipsefill-top-corn-left.gif" width="16" height="15"></td> <td background="../img/elipsefill-vert-line-top.gif"><img src="../img/cldot.gif" width="1" height="15"></td> <td> <img src="../img/elipsefill-top-corn-right.gif" width="15" height="15"></td> </tr>
<tr> <td background="../img/elipsefill-vert-line-left.gif"><img src="../img/cldot.gif" width="16" height="1"></td>
<td bgcolor="#e6f0ce" class="text" align="left" nowrap="nowrap">

<b>Add Template</b>

<!-- START Form Table -->
<table border="0">
<tr>
  <td class="text">Name:<br /><input type="text" name="add_name"></td>
</tr>
<tr>
  <td class="text">Text:<br /><textarea name="add_text" cols="30" rows="8" wrap="soft"></textarea></td>
</tr><tr><td class="text" align="center">

<table cellspacing="0" cellpadding="1" border="0" bgcolor="#de0000">
<tr>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</td></tr>
</table>

<!-- END Form Table -->

</td> <td background="../img/elipsefill-vert-line-right.gif"><img src="../img/cldot.gif" width="15" height="1"></td> </tr>
<tr> <td> <img src="../img/elipsefill-btm-corn-left.gif" width="16" height="15"></td> <td background="../img/elipsefill-vert-line-btm.gif"><img src="../img/cldot.gif" width="1" height="15"></td> <td> <img src="../img/elipsefill-btm-corn-right.gif" width="15" height="15"></td> </tr>
</table>

</td></tr></table>

</form>


<?php
    return;
  }

  $template->Get_Info ($template_id);

  if ($submit == 'Update') {
    $template->Set ('template_name', $template_name);
    $template->Set ('template_text', $template_text);
    $template->Update ();

    $template->Get_Info ($template_id);
  }

?>

<input type="hidden" name="template_id" value="<?php echo $template_id; ?>" />

<table cellpadding="5" cellspacing="0">
<tr>
  <td class="text"><b>Template Name:</b><br /><input type="text" name="template_name" value="<?php echo $template->Get ('template_name'); ?>" /></td>
</tr>
<tr valign="top">
  <td class="text"><b>Template:</b><br /><textarea name="template_text" cols="50" rows="8"><?php echo $template->Get ('template_text'); ?></textarea></td>
</tr>
<tr>
  <td align="center" colspan="2"><input type="submit" name="submit" value="Update" /></td>
</tr>
</table>

</td></tr></table>

</form>

