Location: PHPKode > projects > raSMP > rasmp/admin/templates.php
<?php

/*******************************************************************

 Name		: raSMP 2.0
 Copyright	: 2002, Adam Alkins
 Website	: http://www.rasmp.com
 email		: hide@address.com

 $Id: templates.php,v 1.18 2003/03/26 01:21:44 rasadam Exp $: 

*******************************************************************/

/*******************************************************************

This program 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.

This program 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

*******************************************************************/

/*

Template Management

*/

define('RASMP',true);
define('RASMP_ADMIN',true);
define('SCRIPT_PATH','../');
include SCRIPT_PATH.'common/extension.inc';
include SCRIPT_PATH.'common/admin_common.'.FILE_EXT;

// Check authentication
check_auth('templates');

function do_header($title)
{
  	display_header($title);
	display_menu();
	display_body();
}

function display_menu()
{
?>

<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td bgcolor="#0066CC">
      <div align="center"><a class="menulink" href="<?php echo attach_sid("templates.".FILE_EXT."?mode=create"); ?>">Create</a> | <a class="menulink" href="<?php echo attach_sid("templates.".FILE_EXT."?mode=delete"); ?>">Delete</a> | <a class="menulink" href="<?php echo attach_sid("templates.".FILE_EXT."?mode=edit"); ?>">Edit</a> | <a class="menulink" href="<?php echo attach_sid("templates.".FILE_EXT."?mode=index"); ?>">Index</a></div>
    </td>
  </tr>
</table>

<?php

}


switch($_GET['mode'])
{
	case 'create':
		if(!isset($_POST['doadd']))
		{
			if(ini_get('file_uploads')==1)
			{
				$form_type = ' enctype="multipart/form-data"';
			}
			else
			{
				$form_type = '';	
			}
			
			do_header("Admin Panel >> Template Management >> Add");
?>
<div align="center"> 
  <form name="form1" id="form1" method="post" action="<?php echo attach_sid("templates.".FILE_EXT."?mode=create"); ?>"<?php $form_type; ?>>
    <p align="center">Enter information to create a new template. Notes: Template 
      Name can have spaces and odd characters. Both the header and footer fields 
      parse PHP. From the beginning, its treated as PHP. Therefore, if you want 
      to enter HTML, leave the ?&gt; and add html after.</p>
    <p align="center">Template Name: 
      <input type="text" name="template_name" value="<?php echo htmlentities(urldecode($_GET['template_name'])); ?>" />
    </p>
    <p align="center">Header: <br />
<?php
			if(!isset($_GET['header']))
			{
				echo '
      <textarea name="header" cols="75" rows="5">?>
<html>
<body>
</textarea>
					 ';
			}
			else
			{
				echo '
      <textarea name="header" cols="75" rows="5">'.htmlentities(urldecode($_GET['header'])).'</textarea>			
    				 ';
			}
?>
    </p>
<?php
			if(ini_get('file_uploads')==1)
			{
?>
		       <p align="center"> Or upload header: 
          <input type="hidden" name="MAX_FILE_SIZE" value="30000000" /><input name="uploaded_header" type="file" id="uploaded_header" />
        <br />&nbsp;</p>		
<?php
			}
?>
    <p align="center">Footer:<br />
<?php
			if(!isset($_GET['footer']))
			{
				echo '
      <textarea name="footer" cols="75" rows="5">?>
</body>
</html>
</textarea>
					 ';				 
			}
			else
			{
				echo '
      <textarea name="footer" cols="75" rows="5">'.htmlentities(urldecode($_GET['footer'])).'</textarea>			
    				 ';
			}
?>
	</p>
<?php
			if(ini_get('file_uploads')==1)
			{
?>
		       <p align="center"> Or upload footer: 
          <input name="uploaded_footer" type="file" id="uploaded_footer" />
        <br />&nbsp;</p>		
<?php
			}
?>
    <p align="center">Make Default: 
      <select name="default_template">
<?php
			if(isset($_GET['default_template'])&&$_GET['default_template']==1)
			{
				echo '
        <option value="1">Yes</option>
        <option value="0">No</option>
					 ';
			}
			else
			{
				echo '
        <option value="0">No</option>
        <option value="1">Yes</option>
					 ';			
			}
?>
      </select>
    </p>
    <p align="center">
      <input type="submit" name="doadd" value="Create Template" />
    </p>
  </form>
</div>
<?php
			display_footer();
		}
		else
		{
			// check for missing values
			if(!isset($_POST['template_name'])||!isset($_POST['header'])||!isset($_POST['footer'])||!isset($_POST['default_template']))
			{
				redirect_page('Missing a Field Value',attach_sid("templates.".FILE_EXT."?mode=create")."&template_name=".urlencode($_POST['template_name'])."&header=".urlencode($_POST['header'])."&footer=".urlencode($_POST['footer'])."&default_template=".$_POST['default_template']);
			}
			
			// if template name is blank
			if($_POST['template_name']=='')
			{
				redirect_page('Template Name cannot be blank',attach_sid("templates.".FILE_EXT."?mode=create")."&template_name=".urlencode($_POST['template_name'])."&header=".urlencode($_POST['header'])."&footer=".urlencode($_POST['footer'])."&default_template=".$_POST['default_template']);
			}	

			$_POST['template_name'] = addslashes(htmlentities($_POST['template_name']));
			
			// if default template value isn't true/false
			if($_POST['default_template']!=0 && $_POST['default_template']!=1)
			{
				redirect_page('Default Template value incorrect',attach_sid("templates.".FILE_EXT."?mode=create")."&template_name=".urlencode($_POST['template_name'])."&header=".urlencode($_POST['header'])."&footer=".urlencode($_POST['footer']));
			}
			
			$header = $_POST['header'];
			$footer = $_POST['footer'];
			
			if(ini_get('file_uploads')==1)
			{
				if(is_uploaded_file($_FILES['uploaded_header']['tmp_name'])&&$_FILES['uploaded_header']['size']!=0)
				{
					$fp = fopen($_FILES['uploaded_header']['tmp_name'],'r');
					
					$header = fread($fp, $_FILES['uploaded_header']['size']);

					fclose($fp);
				}
				
				if(is_uploaded_file($_FILES['uploaded_footer']['tmp_name'])&&$_FILES['uploaded_footer']['size']!=0)
				{
					$fp = fopen($_FILES['uploaded_footer']['tmp_name'],'r');
					
					$header = fread($fp, $_FILES['uploaded_footer']['size']);

					fclose($fp);
				}
			}
			
			$header = addslashes($header);
			$footer = addslashes($footer);
			
			$query = "SELECT MAX(template_id) as last_id FROM ".TEMPLATES_TABLE;
			$rows = db_fetchassoc('',$query,'Could not select last ID');
			$insert_id = $rows['last_id'] + 1;
			
			// Lets insert the template
			$query = "INSERT INTO ".TEMPLATES_TABLE." (template_id, template_name, header, footer, default_template) VALUES ($insert_id,'".$_POST['template_name']."','".$header."','".$footer."',".$_POST['default_template'].")";
			
			switch(DB_TYPE)
			{
				case 'pgsql':
					db_query($query,'Could not insert new template record');
					
					$query = "SELECT template_id FROM ".TEMPLATES_TABLE." WHERE template_name = '".$_POST['template_name']."'";
					$row = db_fetchassoc('',$query,'Could not get last ID');
					
					$insert_id = $row['template_id'];
					break;
				case 'mysql':
				default:
					$insert_id = db_getlastid('',$query,'Could not insert new template record');
			}
			
			// undefault other record if need be
			if($_POST['default_template']==1)
			{
				$query = "UPDATE ".TEMPLATES_TABLE." SET default_template = 0 WHERE default_template = 1 AND template_id <> $insert_id";
				db_query($query,'Could not update default template value');
			}
			
			redirect_page('Template Successfully Created',attach_sid("templates.".FILE_EXT."?mode=index"));		
		}
	case 'delete':
		if(!isset($_POST['dodelete']))
		{
			if(!isset($_POST['template_id'])&&!isset($_GET['template_id']))
			{
				// get list of templates
				$query = "SELECT template_id,template_name FROM ".TEMPLATES_TABLE." WHERE template_id <> -1 ORDER BY template_name ASC";
				$result = db_query($query,'Could not fetch templates list');
				
				if(db_numrows($result)==0)
				{
					redirect_page('No templates to Delete',attach_sid("templates.".FILE_EXT."?mode=index"));
				}
								
				do_header("Admin Panel >> Template Management >> Delete");
?>
<div align="center"> Select a template from the list to Delete. 
  <form name="form1" id="form1" method="post" action="<?php echo attach_sid("templates.".FILE_EXT."?mode=delete"); ?>">
    <p>Template: 
      <select name="template_id">
<?php
				while($template_data = db_fetchassoc($result))
				{
					echo '<option value="'.$template_data['template_id'].'">'.stripslashes($template_data['template_name']).'</option>';
				}
?>
      </select>
    </p>
    <p>
      <input type="submit" name="Submit" value="Delete Template" />
    </p>
  </form>
</div>
<?php			

				display_footer();
			}
			else
			{			
				unset($delete_id);
				if(isset($_GET['template_id']))
				{
					$delete_id = intval($_GET['template_id']);
				}
				else
				{
					$delete_id = intval($_POST['template_id']);
				}
				
				if($delete_id == -1)
				{
					redirect_page('Cannot delete Empty Template',attach_sid("templates.".FILE_EXT."?mode=index"));
				}
				
				// see if the cat exists
				$query = "SELECT template_id,template_name,default_template FROM ".TEMPLATES_TABLE." WHERE template_id = $delete_id";
				$result = db_query($query,'Could not select template from Templates table');
			
				if(db_numrows($result)==0)
				{
					redirect_page('Template does not exist.',attach_sid("templates.".FILE_EXT."?mode=delete"));
				}
			
				$template_data = db_fetchassoc($result);
				
				// get categories for the lists
				$query = "SELECT template_id, template_name FROM ".TEMPLATES_TABLE." WHERE template_id <> $delete_id";
				$result = db_query($query,'Could not select Templates list');
			
				$numrows = db_numrows($result);
				$template_rows = db_fetchrows($result);

				do_header("Admin Panel >> Templates Management >> Delete");
?>
 <form name="form1" method="post" action="<?php echo attach_sid("templates.".FILE_EXT."?mode=delete"); ?>">
  <p align="center"> Note: Deletes are <em><strong>Permanent!</strong></em></p>
  <table border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="A8B9FF">
  <tr>
    <td height="23" valign="middle" bgcolor="0066CC"><div align="center"><font color="#FFFFFF" size="3"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Template 
          Delete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong></font></div></td>
  </tr>
  <tr>
    <td height="28"><div align="center">Template Name: <?php echo stripslashes($template_data['template_name']); ?> (<a href="<?php echo attach_sid("templates.".FILE_EXT."?mode=delete"); ?>">change</a>)</div></td>
  </tr>
<?php
				$query = "SELECT page_id FROM ".PAGES_TABLE." WHERE template_id = $delete_id";
				if(db_numrows('',$query,'Could not query pages table')>0)
				{
?>
  <tr>
    <td><div align="center">Move Pages to: 
          <select name="moveto">
<?php			
					for($i=0;$i < $numrows;$i++)
					{
						echo '<option value="'.$template_rows[$i]['template_id'].'">'.stripslashes($template_rows[$i]['template_name']).'</option>';
					}
?>
          </select>
        </div></td>
  </tr>
<?php				
				}
				else
				{
					echo '<input type="hidden" name="moveto" value="-1" />';
				}
						
				if($template_data['default_template']==1)
				{		
?>
    <tr> 
      <td><div align="center">New default: 
          <select name="newdefault">
<?php
				for($i=0;$i < $numrows;$i++)
				{
					echo '<option value="'.$template_rows[$i]['template_id'].'">'.stripslashes($template_rows[$i]['template_name']).'</option>';
				}
?>			
          </select>
        </div></td>
  </tr>
<?php
				}
				else
				{
					echo '<input type="hidden" name="newdefault" value="-1" />';
				}
?>
</table>
  <p align="center">
    <input type="hidden" name="template_id" value="<?php echo $delete_id; ?>" />
    <input type="submit" name="dodelete" value="Confirm Delete" />
  </p>
</form>
<?php			
				display_footer();
			}
		}
		else
		{
			if(!isset($_POST['template_id']))
			{
				redirect_page('No Template to Delete',attach_sid("templates.".FILE_EXT."?mode=delete"));
			}
							
			if($_POST['template_id']==-1)
			{
				redirect_page('Cannot Delete empty Template',attach_sid("templates.".FILE_EXT."?mode=index"));
			}			
			
			$_POST['template_id'] = intval($_POST['template_id']);
			
			// see if template exists
			$query = "SELECT template_id,default_template FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_POST['template_id'];
			$result = db_query($query,'Could net select template from templates table');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Template does not exist',attach_sid("templates.".FILE_EXT."?mode=delete"));
			}
			
			$template_data = db_fetchassoc($result);
			
			if(!isset($_POST['newdefault'])&&!isset($_POST['moveto']))
			{
				redirect_page('Missing a value',attach_sid("templates.".FILE_EXT."?mode=delete&template_id=".$_POST['template_id']));
			}
			
			$_POST['newdefault'] = intval($_POST['newdefault']);
			$_POST['moveto'] = intval($_POST['moveto']);
			
			// see if there are pages using this template
			$query = "SELECT template_id FROM ".PAGES_TABLE." WHERE template_id = ".$_POST['template_id'];
			if(db_numrows('',$query,'Could not check Pages table')>0)
			{
				// see if the moveto id exists
				$query = "SELECT template_id FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_POST['moveto'];
				if(db_numrows('',$query,'Could not check move destination ID')==0)
				{
					redirect_page('Move to Template does not exist',attach_sid("templates.".FILE_EXT."?mode=delete&template_id=".$_POST['template_id']));
				}
				
				// update the pages with the new template
				$query = "UPDATE ".PAGES_TABLE." SET template_id = ".$_POST['moveto']." WHERE template_id = ".$_POST['template_id'];
				db_query($query,'Could not change template IDs on Pages Table');
			}
			
			// if this template is default
			if($template_data['default_template']==1)
			{
				// see if the new one exists
				$query = "SELECT template_id FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_POST['newdefault'];
				if(db_numrows('',$query,'Could not check new default destination ID')==0)
				{
					redirect_page('Move to Template does not exist',attach_sid("templates.".FILE_EXT."?mode=delete&template_id=".$_POST['template_id']));
				}
				
				// change the default
				$query = "UPDATE ".TEMPLATES_TABLE." SET default_template = 1 WHERE template_id = ".$_POST['newdefault'];
				db_query($query,'Could not change default template');
			}
			
			// delete the template
			$query = "DELETE FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_POST['template_id'];
			db_query($query,'Could not delete template record');
			
			redirect_page('Successfully Deleted Template',attach_sid("templates.".FILE_EXT."?mode=index"));				
		}
	case 'edit':
		if(!isset($_POST['doedit']))
		{
			if(!isset($_GET['template_id'])&&!isset($_POST['template_id']))
			{
				// get list of templates
				$query = "SELECT template_id,template_name FROM ".TEMPLATES_TABLE." WHERE template_id <> -1 ORDER BY template_name ASC";
				$result = db_query($query,'Could not fetch templates list');
				
				if(db_numrows($result)==0)
				{
					redirect_page('No templates to edit',attach_sid("templates.".FILE_EXT."?mode=index"));
				}
								
				do_header("Admin Panel >> Template Management >> Edit");
?>
<div align="center"> Select a template from the list to edit. 
  <form name="form1" id="form1" method="post" action="<?php echo attach_sid("templates.".FILE_EXT."?mode=edit"); ?>">
    <p>Template: 
      <select name="template_id">
<?php
				while($template_data = db_fetchassoc($result))
				{
					echo '<option value="'.$template_data['template_id'].'">'.stripslashes($template_data['template_name']).'</option>';
				}
?>
      </select>
    </p>
    <p>
      <input type="submit" name="Submit" value="Edit Template" />
    </p>
  </form>
</div>
<?php			

				display_footer();
			}
			else
			{
				if(ini_get('file_uploads')==1)
				{
					$form_type = ' enctype="multipart/form-data"';
				}
				else
				{
					$form_type = '';	
				}
				
				unset($edit_id);
				unset($template_name);
				unset($header);
				unset($footer);
				
				if(isset($_GET['template_id']))
				{
					$edit_id = intval($_GET['template_id']);
				}
				else
				{
					$edit_id = intval($_POST['template_id']);
				}
				
				if($edit_id == -1)
				{
					redirect_page('Cannot edit empty Template!',attach_sid("templates.".FILE_EXT."?mode=index"));
				}
				
				$query = "SELECT template_name,header,footer FROM ".TEMPLATES_TABLE." WHERE template_id = $edit_id";
				$result = db_query($query,'Could net select template from templates table');
				
				if(db_numrows($result)==0)
				{
					redirect_page('Template does not exist',attach_sid("templates.".FILE_EXT."?mode=edit"));
				}
				
				$template_data = db_fetchassoc($result);
				
				if(isset($_GET['template_name']))
				{
					$template_name = htmlentities(urldecode(urldecode($_GET['template_name'])));
				}
				else
				{
					$template_name = htmlentities(stripslashes($template_data['template_name']));
				}
				
				if(isset($_GET['header']))
				{
					$header = htmlentities(urldecode($_GET['header']));
				}
				else
				{
					$header = htmlentities(stripslashes($template_data['header']));
				}

				if(isset($_GET['footer']))
				{
					$footer = htmlentities(urldecode($_GET['footer']));
				}
				else
				{
					$footer = htmlentities(stripslashes($template_data['footer']));
				}
				
				do_header("Admin Panel >> Template Management >> Edit");
?>
<div align="center"> 
  <form name="form1" id="form1" method="post" action="<?php echo attach_sid("templates.".FILE_EXT."?mode=edit"); ?>"<?php echo $form_type; ?>>
    <p align="center">Modify the Template information and Click Save Changes Notes: Template 
      Name can have spaces and odd characters. Both the header and footer fields 
      parse PHP. From the beginning, its treated as PHP.</p>
    <p align="center">Template Name: 
      <input type="text" name="template_name" value="<?php echo $template_name; ?>" />
    </p>
    <p align="center">Header: <br />
      <textarea name="header" cols="75" rows="5"><?php echo $header; ?></textarea>
    </p>
<?php
			if(ini_get('file_uploads')==1)
			{
?>
		       <p align="center"> Or upload header: 
          <input type="hidden" name="MAX_FILE_SIZE" value="30000000" /><input name="uploaded_header" type="file" id="uploaded_header" />
        <br />&nbsp;</p>		
<?php
			}
?>
    <p align="center">Footer:<br />
      <textarea name="footer" cols="75" rows="5"><?php echo $footer; ?></textarea>
	</p>
<?php
			if(ini_get('file_uploads')==1)
			{
?>
		       <p align="center"> Or upload footer: 
          <input name="uploaded_footer" type="file" id="uploaded_footer" />
        <br />&nbsp;</p>		
<?php
			}
?>
    <p align="center">
	  <input type="hidden" name="template_id" value="<?php echo $edit_id; ?>" />
      <input type="submit" name="doedit" value="Save Changes" />
    </p>
  </form>
</div>
<?php
				display_footer();
			}
		}
		else
		{
			if(!isset($_POST['template_id']))
			{
				redirect_page('No Template to edit',attach_sid("templates.".FILE_EXT."?mode=edit"));
			}
							
			if($_POST['template_id']==-1)
			{
				redirect_page('Cannot Edit empty Template',attach_sid("templates.".FILE_EXT."?mode=index"));
			}			
			
			$_POST['template_id'] = intval($_POST['template_id']);
			
			// see if template exists
			$query = "SELECT template_id FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_POST['template_id'];
			$result = db_query($query,'Could net select template from templates table');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Template does not exist',attach_sid("templates.".FILE_EXT."?mode=edit"));
			}				

			// check for missing values
			if(!isset($_POST['template_name'])||!isset($_POST['header'])||!isset($_POST['footer']))
			{
				redirect_page('Missing a Field Value',attach_sid("templates.".FILE_EXT."?mode=create&template_id=".$_POST['template_id'])."&template_name=".urlencode($_POST['template_name'])."&header=".urlencode($_POST['header'])."&footer=".urlencode($_POST['footer']));
			}
			
			// if template name is blank
			if($_POST['template_name']=='')
			{
				redirect_page('Template Name Cannot be Blank',attach_sid("templates.".FILE_EXT."?mode=create&template_id=".$_POST['template_id'])."&template_name=".urlencode($_POST['template_name'])."&header=".urlencode($_POST['header'])."&footer=".urlencode($_POST['footer']));
			}	

			$_POST['template_name'] = addslashes(htmlentities($_POST['template_name']));
					
			$header = $_POST['header'];
			$footer = $_POST['footer'];
			
			if(ini_get('file_uploads')==1)
			{
				if(is_uploaded_file($_FILES['uploaded_header']['tmp_name'])&&$_FILES['uploaded_header']['size']!=0)
				{
					$fp = fopen($_FILES['uploaded_header']['tmp_name'],'r');
					
					$header = fread($fp, $_FILES['uploaded_header']['size']);

					fclose($fp);
				}
				
				if(is_uploaded_file($_FILES['uploaded_footer']['tmp_name'])&&$_FILES['uploaded_footer']['size']!=0)
				{
					$fp = fopen($_FILES['uploaded_footer']['tmp_name'],'r');
					
					$header = fread($fp, $_FILES['uploaded_footer']['size']);

					fclose($fp);
				}
			}
			
			$header = addslashes($header);
			$footer = addslashes($footer);
			
			// Lets update the record..
			$query = "UPDATE ".TEMPLATES_TABLE." SET template_name = '".$_POST['template_name']."', header = '".$header."', footer = '".$footer."' WHERE template_id = ".$_POST['template_id'];
			db_query($query,'Could not update template record');
			
			redirect_page('Template Successfully Edited',attach_sid("templates.".FILE_EXT."?mode=index"));		
		}
	case 'index':
	default:
		if(isset($_GET['default']))
		{
			$_GET['default'] = intval($_GET['default']);
			
			// selects specified template
			$query = "SELECT template_id,default_template FROM ".TEMPLATES_TABLE." WHERE template_id = ".$_GET['default'];
			$result = db_query($query,'Could not select template from database');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Template does not exist',attach_sid("templates.".FILE_EXT."?mode=index"));
			}
			
			$row_data = db_fetchassoc($result);
			
			// if the template is already default
			if($row_data['default_template']==1)
			{
				redirect_page('Template is already the default!',attach_sid("templates.".FILE_EXT."?mode=index"));
			}
			
			// set old default as non default
			$query = "UPDATE ".TEMPLATES_TABLE." SET default_template = 0 WHERE default_template = 1";
			db_query($query,'Could not update default template information');
			
			// set this one as default
			$query = "UPDATE ".TEMPLATES_TABLE." SET default_template = 1 WHERE template_id = ".$row_data['template_id'];
			db_query($query,'Could not update default template information');			
		}
			
		do_header("Admin Panel >> Template Management");
?>
<p align="center">Welcome to the Template Management Module. From this screen, you can set a specific template as the default (the template that will show on missing pages, e.t.c.). Links are also provided to Edit (Change the names, edit headers and footers), Delete (Permanent!) and create new templates. (Note: You cannot delete the "None" template)</p>
<table width="90%" border="1" cellspacing="0" cellpadding="0" align="center" bordercolor="#A8B9FF">
  <tr> 
    <td bgcolor="0066CC"> 
      <div align="center"><b><font color="#FFFFFF" size="3">Template Name</font></b></div>
    </td>
    <td bgcolor="0066CC" width="50">&nbsp;</td>
    <td bgcolor="0066CC" width="50">&nbsp;</td>
    <td bgcolor="0066CC" width="100">&nbsp;</td>
  </tr>
<?php
		// select templates
		$query = "SELECT template_id,template_name,default_template FROM ".TEMPLATES_TABLE." ORDER BY template_name ASC";
		$result = db_query($query, 'Could not fetch Templates data');
		
		$numrows = db_numrows($result);
		while($template_data = db_fetchassoc($result))
		{
?>
  <tr valign="middle">
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif"><?php echo stripslashes($template_data['template_name']); ?></font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php 
			// if none
			if($template_data['template_id']==-1)
			{
				echo '-';
			}
			else
			{
				echo "<a href=\"".attach_sid("templates.".FILE_EXT."?mode=delete&template_id=".$template_data['template_id'])."\">Delete</a>";
			}		
?>
	</font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php 
			// if none
			if($template_data['template_id']==-1)
			{
				echo '-';
			}
			else
			{
				echo "<a href=\"".attach_sid("templates.".FILE_EXT."?mode=edit&template_id=".$template_data['template_id'])."\">Edit</a>";
			}		
?>
	</font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php
			// if the template is default
			if($template_data['default_template']==1)
			{
				echo "Default";
			}
			else
			{	
				echo '<a href="'.attach_sid('templates.'.FILE_EXT.'?mode=index&default='.$template_data['template_id']).'">Make Default</a>';
			}
?>
</font></div>
    </td>
  </tr>
<?php
		}
?>
</table>
<br />
<p align="center"><a href="<?php echo attach_sid("templates.".FILE_EXT."?mode=create"); ?>">Create a new Template</a></p>
<?php
	display_footer();
}
?>
Return current item: raSMP