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

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

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

 $Id: categories.php,v 1.29 2003/03/16 21:45:17 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

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

/*

Categories 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('categories');

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("categories.".FILE_EXT."?mode=create"); ?>">Create</a> | <a class="menulink" href="<?php echo attach_sid("categories.".FILE_EXT."?mode=delete"); ?>">Delete</a> | <a class="menulink" href="<?php echo attach_sid("categories.".FILE_EXT."?mode=edit"); ?>">Edit</a> | <a class="menulink" href="<?php echo attach_sid("categories.".FILE_EXT."?mode=index"); ?>">Index</a></div>
    </td>
  </tr>
</table>

<?php

}

unset($mode);
$mode = strtolower($_GET['mode']);

switch($mode)
{
	case 'create';
		if(!isset($_POST['doadd']))
		{
			do_header("Admin Panel >> Categories Management >> Add");
?>
<form name="form1" id="form1" method="post" action="<?php echo attach_sid("categories.".FILE_EXT."?mode=create"); ?>">
  <blockquote>
    <div align="left">You can enter values to create a new category here. The 
      Short Name will be used in URLs and should have no spaces and odd characters 
      (quotes, e.t.c... they will be stripped if there are nonetheless). The Menu 
      Name appears on the Generated Menu (if Used). You can also set whether you 
      want the category to redirect. If you choose Yes, when clicked in the menu 
      or loaded in a URL, it will automatically redirect to the URL specified. 
      N.B: For the redirect to work, the must be a page set as default! A blank 
      page is created and set as default automatically when you create a new category. 
      It is recommended you keep that page to have redirects work.</div>
  </blockquote>
  <div align="center">
    <p>Short Name: 
      <input name="cat_name" type="text" size="15" maxlength="255" value="<?php echo urldecode($_GET['cat_name']); ?>" />
    </p>
    <p>Menu Name: 
      <input name="cat_pname" type="text" size="25" maxlength="255" value="<?php echo urldecode($_GET['cat_pname']); ?>" />
    </p>
    <p>Make Default: 
      <select name="default_cat">
<?php
			if(isset($_GET['default_cat'])&&($_GET['default_cat']==1||$_GET['default_cat']==0))
			{
				if($_GET['default_cat']==1)
				{
					echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
						 ';
				}
				else
				{
					echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
						 ';
				}
			}
			else
			{
				echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
					 ';
			}
?>
      </select>
    </p>
    <p>Appear in Menu: 
      <select name="appear_menu">
<?php
			if(isset($_GET['appear_menu'])&&($_GET['appear_menu']==1||$_GET['appear_menu']==0))
			{
				if($_GET['appear_menu']==1)
				{
					echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
						 ';
				}
				else
				{
					echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
						 ';
				}
			}
			else
			{
				echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
					 ';
			}
?>
      </select>
    </p>
    <p>Make Redirect: 
      <select name="redirect">
<?php
			if(isset($_GET['redirect'])&&($_GET['redirect']==1||$_GET['redirect']==0))
			{
				if($_GET['redirect']==1)
				{
					echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
						 ';
				}
				else
				{
					echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
						 ';
				}
			}
			else
			{
				echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
					 ';
			}
?>
      </select>
      &nbsp;&nbsp;&nbsp;If Yes, URL: 
      <input name="redirect_url" type="text" <?php
			if(isset($_GET['redirect_url']))
			{
				echo 'value="'.urldecode($_GET['redirect_url']).'" size="30" maxlength="255" />';
			}
			else
			{
				echo 'value="http://" size="30" maxlength="255" />';
			}
?>						 
    </p>
    <p> 
      <input type="submit" name="doadd" value="Create Category" />
    </p>
  </div>
</form>
<?php
			display_footer();
		}
		else
		{
			// if a value was missing
			if(!isset($_POST['cat_name'])||!isset($_POST['cat_pname'])||!isset($_POST['default_cat'])||!isset($_POST['appear_menu'])||!isset($_POST['redirect'])||!isset($_POST['redirect_url']))
			{
				redirect_page('Missing a field value',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			// clean up cat_name
			$_POST['cat_name'] = clean_public($_POST['cat_name']);
			
			// if short name is blank
			if($_POST['cat_name']=='')
			{
				redirect_page('Short Name cannot be blank',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// see if short name already exists
			$query = "SELECT cat_name FROM ".CATEGORIES_TABLE." WHERE cat_name = '".addslashes(htmlentities($_POST['cat_name']))."'";
			if(db_numrows('',$query,'Could not query categories table')!=0)
			{
				redirect_page('Short Name must be Unique!',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// if menu name is blank
			if($_POST['cat_pname']=='')
			{
				redirect_page('Menu Name cannot be blank',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			
			// if default cat is not true or false
			if($_POST['default_cat']!=0&&$_POST['default_cat']!=1)
			{
				redirect_page('Default must be either True or False!',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			// if appear_menu is not true or false
			if($_POST['appear_menu']!=0&&$_POST['appear_menu']!=1)
			{
				redirect_page('Appear Menu value must be either true or false!',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
						
			// if redirect is not true or false
			if($_POST['redirect']!=0&&$_POST['redirect']!=1)
			{
				redirect_page('Redirect must be either True or False!',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// if redirect is true and url is blank			
			if($_POST['redirect']==1&&$_POST['redirect_url']=='')
			{
				redirect_page('Redirect URL must not be blank if redirecting!',attach_sid("categories.".FILE_EXT."?mode=create&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&default_cat=".$_POST['default_cat']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// if redirect is flase, set url to blank then..
			if($_POST['redirect']==0)
			{
				$_POST['redirect_url'] = '';
			}
			else
			{
				$_POST['redirect_url'] = urlencode($_POST['redirect_url']);
			}
				
			unset($sort_order);
			// if its to appear on the menu or not
			if($_POST['appear_menu'] == 1)
			{
				// get last sorted order to insert
				$query = "SELECT sort_order FROM ".CATEGORIES_TABLE." ORDER BY sort_order DESC LIMIT 1";
				$row_data = db_fetchassoc('',$query,'Could not fetch last category');
				$sort_order = $row_data['sort_order'] + 1;
			}
			else
			{
				$sort_order = -1;
			}
			
			// clean cat menu name
			$_POST['cat_pname'] = addslashes(htmlentities($_POST['cat_pname']));			

			// insert category record
			$query = "INSERT INTO ".CATEGORIES_TABLE." (cat_name, cat_pname, page_id, sort_order, default_cat, redirect, redirect_url) VALUES('".$_POST['cat_name']."','".$_POST['cat_pname']."',-1,$sort_order, ".$_POST['default_cat'].", ".$_POST['redirect'].", '".$_POST['redirect_url']."')";
			$cat_id = db_getlastid('',$query,'Could not insert category record');
			
			// insert page for category
			$query = "INSERT INTO ".PAGES_TABLE." (page_name, cat_id, date, page_title, page_content, template_id, use_php) VALUES ('index', $cat_id, ".date("U").", '".$_POST['cat_pname']."', '', -1, 0)";
			$page_id = db_getlastid('',$query,'Could not insert pages record');
			
			// update new record to set new default page
			$query = "UPDATE ".CATEGORIES_TABLE." SET page_id = $page_id WHERE cat_id = $cat_id";
			db_query($query,'Could not update default page');
			
			// change default cat if neccessary
			if($_POST['default_cat']==1)
			{
				$query = "UPDATE ".CATEGORIES_TABLE." SET default_cat = 0 WHERE default_cat = 1 AND cat_id <> $cat_id";
				db_query($query,'Could not update default category');
			}
			
			redirect_page('Category Successfully Created',attach_sid("categories.".FILE_EXT."?mode=index"));
		}
		break;
	case 'delete';
		// if criteria for deleting the category exist...
		if(isset($_POST['dodelete'])&&isset($_POST['cat_id'])&&isset($_POST['newdefault'])&&isset($_POST['moveto']))
		{
			unset($delete_id);
			$delete_id = intval($_POST['cat_id']);
			
			// see if the cat exists
			$query = "SELECT cat_id,default_cat,sort_order FROM ".CATEGORIES_TABLE." WHERE cat_id = $delete_id";
			$result = db_query($query,'Could not select category id');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Category does not exist.',attach_sid("categories.".FILE_EXT."?mode=delete"));
			}
			
			$cat_data = db_fetchassoc($result);
			$row_count = db_rowcount(CATEGORIES_TABLE);
					
			// if the category is default (a new one needs to be set) but isn't the only one
			if($cat_data['default_cat']==1&&$row_count!=1)
			{
				// if the value was -1 meaning the new default was skipped
				if($_POST['newdefault']==-1)
				{
					redirect_page('Invalid Replacement Default',attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=$delete_id"));
				}
			
				$_POST['newdefault'] = intval($_POST['newdefault']);
				
				// see if the specified exists	
				$query = "SELECT cat_id FROM ".CATEGORIES_TABLE." WHERE cat_id = ".$_POST['newdefault'];
				if(db_numrows('',$query,'Could not query categories table')==1)
				{
					// perform change
					$query = "UPDATE ".CATEGORIES_TABLE." SET default_cat = 1 WHERE cat_id = ".$_POST['newdefault'];
					db_query($query,'Could not update new default category');
				}
				else
				{
					redirect_page('Target Default does not exist!',attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=$delete_id"));
				}
			}
			
			// if put to delete the pages..
			if($_POST['moveto']==-1)
			{
				// delete all associated pages
				$query = "DELETE FROM ".PAGES_TABLE." WHERE cat_id = $delete_id";
				db_query($query,'Could not delete from pages table');
			}
			else
			{
				// query for pages with this cat id
				$query = "SELECT page_id FROM ".PAGES_TABLE." WHERE cat_id = $delete_id";
			
				// if there are pages
				if(db_numrows('',$query,'Could not query pages table')>0)
				{
					// checks the value passed from the previous screen
					if($_POST['moveto']==-1)
					{
						redirect_page('Target Moving to Category Invalid!',attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=$delete_id"));
					}
				
					$_POST['moveto'] = intval($_POST['moveto']);
				
					// see if target cat exists
					$query = "SELECT cat_id FROM ".CATEGORIES_TABLE." WHERE cat_id = ".$_POST['moveto'];
					if(db_numrows('',$query,'Could not check target ID')==1)
					{
						//
						// Here we go. Time to transfer the pages to another category. This bit of code
						// will loop through the pages to make sure no others exist. If one does match
						// it will attach a number at the end of the page name and try that. It will keep
						// incrementing the number until one doesn't match. This is to prevent duplicate
						// pages.
						$query = "SELECT page_id, page_name FROM ".PAGES_TABLE." WHERE cat_id = $delete_id ORDER BY page_name ASC";
						$result = db_query($query,'Could not query Pages Table');
						
						$numrows = db_numrows($result);
						$page_rows = db_fetchrows($result);

						for($i=0;$i<$numrows;$i++)
						{					
							$page_rows[$i]['original_page_name'] = $page_rows[$i]['page_name'];
						}
						
						$errorflag = 0;
						$j = 2;
	
						while($errorflag == 0)
						{
							$error = 0;
							for($i=0;$i<$numrows;$i++)
							{
								$query = "SELECT page_name FROM ".PAGES_TABLE." WHERE page_name = '".$page_rows[$i]['page_name']."' AND cat_id = ".$_POST['moveto'];
								if(db_numrows('',$query,'Could not cross check pages table')!=0)
								{
									$page_rows[$i]['page_name'] = $page_rows[$i]['original_page_name'].$j;																		
									$error = 1;
								}
								else
								{
									for($k=0;$k<$numrows;$k++)
									{
										if($page_rows[$k]['page_name']==$page_rows[$i]['page_name']&$i!=$k)
										{
											$page_rows[$i]['original_page_name'] .= 'copy';
											$error = 1;
										}
									}
								}																			
							}
						
							if($error == 0)
							{
								$errorflag = 1;
							}
							else
							{
								$j++;
							}
						}
					
						for($i=0;$i<$numrows;$i++)
						{
							// do update on pages table
							$query = "UPDATE ".PAGES_TABLE." SET cat_id = ".$_POST['moveto'].", page_name = '".$page_rows[$i]['page_name']."' WHERE page_id = ".$page_rows[$i]['page_id'];
							db_query($query,'Could not update pages table');
						}
					}
					else
					{
						redirect_page('Target Moving to Category does not exist!',attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=$delete_id"));
					}
				}
			}
			
			// check if is last in the sorted order (menu)
			$query = "SELECT cat_id,sort_order FROM ".CATEGORIES_TABLE." ORDER BY sort_order DESC LIMIT 1";
			$row_data = db_fetchassoc('',$query,'Could not select last in order from Categories table');
			
			// if not last and appears on the menu
			if($row_data['cat_id']!=$delete_id&&$row_data['sort_order']!=-1)
			{
				// perform query to shift all categories up a notch
				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = sort_order - 1 WHERE sort_order > ".$cat_data['sort_order'];
				db_query($query,'Could not resort categories');
			}
			
			// Do actual delete
			$query = "DELETE FROM ".CATEGORIES_TABLE." WHERE cat_id = $delete_id";
			db_query($query,'Could not delete category');
			
			redirect_page('Successfully Deleted Category',attach_sid("categories.".FILE_EXT."?mode=index"));
			
		}
		else if(isset($_GET['cat_id'])||isset($_POST['cat_id']))
		{
			unset($delete_id);
			if(isset($_GET['cat_id']))
			{
				$delete_id = intval($_GET['cat_id']);
			}
			else
			{
				$delete_id = intval($_POST['cat_id']);
			}
				
			// see if the cat exists
			$query = "SELECT cat_id,cat_pname,default_cat FROM ".CATEGORIES_TABLE." WHERE cat_id = $delete_id";
			$result = db_query($query,'Could not select category id');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Category does not exist.',attach_sid("categories.".FILE_EXT."?mode=delete"));
			}
			
			$cat_data = db_fetchassoc($result);
					
			// get categories for the lists
			$query = "SELECT cat_id, cat_pname FROM ".CATEGORIES_TABLE." WHERE cat_id <> $delete_id";
			$result = db_query($query,'Could not select categories data');
			
			$numrows = db_numrows($result);
			$cat_rows = db_fetchrows($result);

			do_header("Admin Panel >> Categories Management >> Delete");
?>
 <form name="form1" method="post" action="<?php echo attach_sid("categories.".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;Category 
          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">Category Name: <?php echo $cat_data['cat_pname']; ?> (<a href="<?php echo attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=$delete_id"); ?>">change</a>)</div></td>
  </tr>
<?php
			$query = "SELECT page_id FROM ".PAGES_TABLE." WHERE cat_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			
				echo '<option value="-1">Delete All</option>';
				for($i=0;$i < $numrows;$i++)
				{
					echo '<option value="'.$cat_rows[$i]['cat_id'].'">'.$cat_rows[$i]['cat_pname'].'</option>';
				}
?>
          </select>
        </div></td>
  </tr>
<?php				
			}
			else
			{
				echo '<input type="hidden" name="moveto" value="-1" />';
			}
									
			if($cat_data['default_cat']==1&&db_rowcount(CATEGORIES_TABLE)!=1)
			{		
?>
    <tr> 
      <td><div align="center">New default: 
          <select name="newdefault">
<?php
				for($i=0;$i < $numrows;$i++)
				{
					echo '<option value="'.$cat_rows[$i]['cat_id'].'">'.$cat_rows[$i]['cat_pname'].'</option>';
				}
?>			
          </select>
        </div></td>
  </tr>
<?php
			}
			else
			{
				echo '<input type="hidden" name="newdefault" value="-1" />';
			}
?>
</table>
  <p align="center">
    <input type="hidden" name="cat_id" value="<?php echo $delete_id; ?>" />
    <input type="submit" name="dodelete" value="Confirm Delete" />
  </p>
</form>
<?php			
			display_footer();
		}
		else
		{			
			$query = "SELECT cat_id,cat_pname FROM ".CATEGORIES_TABLE." ORDER BY cat_pname ASC";
			$result = db_query($query,'Could not select categories');
			
			do_header("Admin Panel >> Categories Management >> Delete");
?>
<form name="delete" method="post" action="<?php echo attach_sid("categories.".FILE_EXT."?mode=delete"); ?>">
  <div align="center"><p>Select a category to be deleted. You will be presented with another screen with deleting options before the category is actually deleted.</p>
    <p>Category: 
       <select name="cat_id">
<?php
			while($cat_data = db_fetchassoc($result))
			{
				echo '<option value="'.$cat_data['cat_id'].'">'.$cat_data['cat_pname'].'</option>';
			}
?>
  </select>
    </p>
       <input type="submit" name="Submit" value="Delete" />
  </div>
</form>			
<?php
			display_footer();
		}
		break;
	case 'edit';
		if(!isset($_POST['doedit']))
		{
			// if the id isn't passed/entered
			if(!isset($_POST['cat_id'])&&!isset($_GET['cat_id']))
			{
				$query = "SELECT cat_id,cat_pname FROM ".CATEGORIES_TABLE." ORDER BY cat_pname";
				$result = db_query($query,'Could not select categories');

				do_header("Admin Panel >> Categories Management >> Edit");
?>
<form name="delete" method="post" action="<?php echo attach_sid("categories.".FILE_EXT."?mode=edit"); ?>">
  <div align="center"><p>Select a category from the list to Edit.</p>
    <p>Category: 
       <select name="cat_id">
<?
				while($cat_data = db_fetchassoc($result))
				{
					echo '<option value="'.$cat_data['cat_id'].'">'.$cat_data['cat_pname'].'</option>';
				}
?>
  </select>
    </p>
       <input type="submit" name="Submit" value="Edit Category" />
  </div>
</form>			
<?php
				display_footer();
			}
			else
			{
				unset($edit_id);
				if(isset($_GET['cat_id']))
				{
					$edit_id = intval($_GET['cat_id']);
				}
				else
				{
					$edit_id = intval($_POST['cat_id']);
				}
				
				// see if the cat exists
				$query = "SELECT cat_id,cat_name,cat_pname,page_id,sort_order,redirect,redirect_url FROM ".CATEGORIES_TABLE." WHERE cat_id = $edit_id";
				$result = db_query($query,'Could not select category id');
			
				if(db_numrows($result)==0)
				{
					redirect_page('Category does not exist.',attach_sid("categories.".FILE_EXT."?mode=edit"));
				}
			
				$cat_data = db_fetchassoc($result);
				
				do_header("Admin Panel >> Categories Management >> Edit");
?>
<form name="form1" id="form1" method="post" action="<?php echo attach_sid("categories.".FILE_EXT."?mode=edit"); ?>">
  <blockquote>
    <div align="left">Change the values and click the Save Changes button to complete. The 
      Short Name is used in URLs and should have no spaces and odd characters 
      (quotes, e.t.c... they will be stripped if there are nonetheless). 
      N.B: For the redirect to work, the must be a page set as default!</div>
  </blockquote>
  <div align="center">
    <p>Short Name: 
      <input name="cat_name" type="text" size="15" maxlength="255" 
<?php 
				if(isset($_GET['cat_name']))
				{
					echo 'value="'.urldecode($_GET['cat_name']).'"';
				}
				else
				{
					echo 'value="'.$cat_data['cat_name'].'"';
				}
?>
		 />
    </p>
    <p>Menu Name: 
      <input name="cat_pname" type="text" size="25" maxlength="255" 
<?php 
				if(isset($_GET['cat_pname']))
				{
					echo 'value="'.urldecode($_GET['cat_pname']).'"';
				}
				else
				{
					echo 'value="'.stripslashes($cat_data['cat_pname']).'"';
				}
?>
		 />    </p>
    <p>Default Page: 
      <select name="page_id">
<?php
			unset($page_id);
			if(isset($_GET['page_id']))
			{
				$page_id = intval($_GET['page_id']);
			}
			else
			{
				$page_id = $cat_data['page_id'];
			}
			
			// get the pages for this category
			$query = "SELECT page_id,page_name FROM ".PAGES_TABLE." WHERE cat_id = $edit_id";
			$result = db_query($query,'Could not fetch pages for category');
			
			if($page_id == -1)
			{
				echo '<option value="-1" selected="selected">None</option>';
				while($page_data = db_fetchassoc($result))
				{
			 		echo '<option value="'.$page_data['page_id'].'">'.$page_data['page_name'].'</option>';
				}
			}
			else
			{
				echo '<option value="-1">None</option>';
				while($page_data = db_fetchassoc($result))
				{
			 		if($page_id == $page_data['page_id'])
					{
						echo '<option value="'.$page_data['page_id'].'" selected="selected">'.$page_data['page_name'].'</option>';				
					}
					else
					{
						echo '<option value="'.$page_data['page_id'].'">'.$page_data['page_name'].'</option>';
					}
				}
			}
?>
      </select>
    </p>
    <p>Appear on Menu: 
      <select name="appear_menu">
<?php
			if(isset($_GET['appear_menu'])&&($_GET['appear_menu']==1||$_GET['appear_menu']==0))
			{
				if($_GET['appear_menu']==1)
				{
					echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
						 ';
				}
				else
				{
					echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
						 ';
				}
			}
			else if($cat_data['sort_order']==-1)
			{
				echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
					 ';
			}
			else
			{
				echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
					 ';
			}
?>
      </select>
	</p>
    <p>Make Redirect: 
      <select name="redirect">
<?php
			if(isset($_GET['redirect'])&&($_GET['redirect']==1||$_GET['redirect']==0))
			{
				if($_GET['redirect']==1)
				{
					echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
						 ';
				}
				else
				{
					echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
						 ';
				}
			}
			else if($cat_data['redirect']==0)
			{
				echo '
        <option value="1">Yes</option>
        <option value="0" selected="selected">No</option>
					 ';
			}
			else
			{
				echo '
        <option value="1" selected="selected">Yes</option>
        <option value="0">No</option>
					 ';
			}
?>
      </select>
      &nbsp;&nbsp;&nbsp;If Yes, URL: 
      <input name="redirect_url" type="text" <?php
			if(isset($_GET['redirect_url']))
			{
				echo 'value="'.urldecode($_GET['redirect_url']).'" size="30" maxlength="255"';
			}
			else
			{
				echo 'value="'.$cat_data['redirect_url'].'" size="30" maxlength="255"';
			}
?> />						 
    </p>
    <p> 
	  <input type="hidden" name="cat_id" value="<?php echo $edit_id; ?>" />
      <input type="submit" name="doedit" value="Save Changes" />
    </p>
  </div>
</form>
<?php
			display_footer();				
			}
		}
		else
		{
			// if the cat_id is missing
			if(!isset($_POST['cat_id']))
			{
				redirect_page('No Category to edit',attach_sid("categories.".FILE_EXT."?mode=edit"));
			}
			
			$_POST['cat_id'] = intval($_POST['cat_id']);
			
			// if category is not found
			$query = "SELECT cat_id,sort_order FROM ".CATEGORIES_TABLE." WHERE cat_id = ".$_POST['cat_id'];
			$result = db_query($query,'Could not query categories table');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Category doesn not exist',attach_sid("categories.".FILE_EXT."?mode=edit"));
			}
			
			$cat_data = db_fetchassoc($result);
					
			// if a value was missing
			if(!isset($_POST['cat_name'])||!isset($_POST['cat_pname'])||!isset($_POST['page_id'])||!isset($_POST['appear_menu'])||!isset($_POST['redirect'])||!isset($_POST['redirect_url']))
			{
				redirect_page('Missing a field value',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			// clean up cat_name
			$_POST['cat_name'] = clean_public($_POST['cat_name']);
			
			// if redirect is flase, set url to blank then..
			if($_POST['redirect']==0)
			{
				$_POST['redirect_url'] = '';
			}
			else
			{
				$_POST['redirect_url'] = addslashes($_POST['redirect_url']);
			}

			// if short name is blank
			if($_POST['cat_name']=='')
			{
				redirect_page('Short Name must not be blank',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// see if short name already exists
			$query = "SELECT cat_name FROM ".CATEGORIES_TABLE." WHERE cat_name = '".$_POST['cat_name']."' AND cat_id <> ".$_POST['cat_id'];
			if(db_numrows('',$query,'Could not query categories table')==1)
			{
				redirect_page('Short Name must be Unique!',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// if menu name is blank
			if($_POST['cat_pname']=='')
			{
				redirect_page('Menu Name cannot be blank',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// clean cat menu name
			$_POST['cat_pname'] = addslashes(htmlentities($_POST['cat_pname']));
			
			$_POST['page_id'] = intval($_POST['page_id']);
			
			// check if page_id exists			
			$query = "SELECT page_id FROM ".PAGES_TABLE." WHERE page_id = ".$_POST['page_id']." AND cat_id = ".$_POST['cat_id'];
			if(db_numrows('',$query,'Could not query Pages table')==0)
			{
				redirect_page('Default Page is not valid',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			// if appear_menu is not true or false
			if($_POST['appear_menu']!=0&&$_POST['appear_menu']!=1)
			{
				redirect_page('Appear menu must be either true or false',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}		
						
			// if redirect is not true or false
			if($_POST['redirect']!=0&&$_POST['redirect']!=1)
			{
				redirect_page('Redirect must be either true or false',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}
			
			// if redirect is true and url is blank			
			if($_POST['redirect']==1&&$_POST['redirect_url']=='')
			{
				redirect_page('URL Must not be blank if Redirecting',attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$_POST['cat_id']."&cat_name=".urlencode($_POST['cat_name'])."&cat_pname=".urlencode($_POST['cat_pname'])."&page_id=".$_POST['page_id']."&appear_menu=".$_POST['appear_menu']."&redirect=".$_POST['redirect']."&redirect_url=".urlencode($_POST['redirect_url'])));
			}

			// get last in the sorted order (menu)
			$query = "SELECT sort_order FROM ".CATEGORIES_TABLE." ORDER BY sort_order DESC LIMIT 1";
			$row_data = db_fetchassoc('',$query,'Could not select last in order from Categories table');

			unset($sort_order);
			// if current menu is no and appear is no..
			if($_POST['appear_menu']==0&&$cat_data['sort_order']==-1)
			{
				$sort_order = '';
			}
			// if is currently on meny and appear is yes
			else if($_POST['appear_menu']==1&&$cat_data['sort_order']!=-1)
			{	
				$sort_order = '';
			}
			else if($_POST['appear_menu']==0)
			{		
				// if not last and appears on the menu
				if($row_data['cat_id']!=$_POST['cat_id'])
				{
					// perform query to shift all categories up a notch
					$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = sort_order - 1 WHERE sort_order > ".$cat_data['sort_order'];
					db_query($query,'Could not resort categories');
				}
				
				$sort_order = ',sort_order = -1';
			}
			else
			{
				$row_data['sort_order']++;
				$sort_order = ',sort_order = '.$row_data['sort_order'];
			}
			
			// update record with changes
			$query = "UPDATE ".CATEGORIES_TABLE." SET cat_name = '".$_POST['cat_name']."', cat_pname = '".$_POST['cat_pname']."', page_id = ".$_POST['page_id'].", redirect = ".$_POST['redirect'].", redirect_url = '".$_POST['redirect_url']."'$sort_order WHERE cat_id = ".$_POST['cat_id'];
			db_query($query,'Could not update Category Record');
			
			redirect_page('Category Successfully Edited',attach_sid("categories.".FILE_EXT."?mode=index"));		
		}		
	case 'index';
	default:
		// If all the values required to move a category around are there
		if(isset($_GET['move'])&&($_GET['direction']=='up'||$_GET['direction']=='down'))
		{
			$_GET['move'] = intval($_GET['move']);
			
			// counts the amount of categories
			$rowcount = db_rowcount(CATEGORIES_TABLE);
			
			if($rowcount == 1)
			{
				redirect_page('Unable to reorder category',attach_sid("categories.".FILE_EXT."?mode=index"));
			}
			
			// selects the specified category
			$query = "SELECT cat_id,sort_order FROM ".CATEGORIES_TABLE." WHERE cat_id = ".$_GET['move'];
			$result = db_query($query,'Could not get category data');
			
			if(db_numrows($result)==0)
			{
				redirect_page('The category you are trying to reorder does not exist',attach_sid("categories.".FILE_EXT."?mode=index"));
			}
			
			$row_data = db_fetchassoc($result);
			
			if($row_data['sort_order'] == -1)
			{
				redirect_page('This category is not part of the menu',attach_sid("categories.".FILE_EXT."?mode=index"));
			}

			// Get the last sorted ID
			$query = "SELECT cat_id FROM ".CATEGORIES_TABLE." ORDER BY sort_order DESC LIMIT 1";
			$last_id = db_fetchassoc('',$query,'Could not fetch last ID');
			
			if($_GET['direction']=='up')
			{
				// if the category is the first, it can't be moved up anymore
				if($row_data['sort_order']==0)
				{
					redirect_page('This category cannot be moved up any further',attach_sid("categories.".FILE_EXT."?mode=index"));
				}
				
				// update the category that was on the number you are moving to
				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = ".$row_data['sort_order']." WHERE sort_order = ".$row_data['sort_order']." - 1";
				db_query($query,'Could not move category');
				
				// updates the id's sort number
				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = ".$row_data['sort_order']." - 1 WHERE cat_id = ".$row_data['cat_id'];
				db_query($query,'Could not move category');	
			}
			else
			{
				// if its the last id, it can't move down..
				if($row_data['cat_id']==$last_id['cat_id'])
				{
					redirect_page('This category cannot be moved down any further',attach_sid("categories.".FILE_EXT."?mode=index"));
				}

				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = ".$row_data['sort_order']." WHERE sort_order = ".$row_data['sort_order']." + 1";
				db_query($query,'Could not move category');
								
				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = ".$row_data['sort_order']." + 1 WHERE cat_id = ".$row_data['cat_id'];
				db_query($query,'Could not move category');
			}
		}
		
		if(isset($_GET['default']))
		{
			$_GET['default'] = intval($_GET['default']);
			
			// selects specified category
			$query = "SELECT cat_id,default_cat FROM ".CATEGORIES_TABLE." WHERE cat_id = ".$_GET['default'];
			$result = db_query($query,'Could not select category');
			
			if(db_numrows($result)==0)
			{
				redirect_page('Category does not exist',attach_sid("categories.".FILE_EXT."?mode=index"));
			}
			
			$row_data = db_fetchassoc($result);
			
			// if the category is already default
			if($row_data['default_cat']==1)
			{
				redirect_page('Category is already the default!',attach_sid("categories.".FILE_EXT."?mode=index"));
			}
			
			// set old default as non default
			$query = "UPDATE ".CATEGORIES_TABLE." SET default_cat = 0 WHERE default_cat = 1";
			db_query($query,'Could not update default category information');
			
			// set this one as default
			$query = "UPDATE ".CATEGORIES_TABLE." SET default_cat = 1 WHERE cat_id = ".$row_data['cat_id'];
			db_query($query,'Could not update default category information');			
		}
		
		// if required values to resort categories exist
		if(isset($_GET['sort_by'])&&($_GET['sort_by']=='cat_name'||$_GET['sort_by']=='cat_pname'))
		{
			// Get the categories in the order of what's asked
			$query = "SELECT cat_id FROM ".CATEGORIES_TABLE." ORDER BY ".$_GET['sort_by'];
			$result = db_query($query,'Could not get categories data');
			
			$row_data = db_fetchrows($result);
			$numrows = db_numrows($result);
			for($i=0;$i<$numrows;$i++)
			{
				// this will update the sort order based on the position from the loop
				$query = "UPDATE ".CATEGORIES_TABLE." SET sort_order = $i WHERE cat_id = ".$row_data[$i]['cat_id'];
				db_query($query,'Could not resort categories');
			}
		}
				
		do_header("Admin Panel >> Categories Management");
?>
<p align="center">Welcome to the Categories Management Module. From this screen, you can change the order categories are displayed in the menu, Top - Down = Left to Right, (note by click on the header for short name or menu name you can sort all the categories in alphabetical order by the respective title) and set a specific category as the default (the category with your main index page). Links are also provided to Edit (Change the names, set redirects and change the default page), Delete (Permanent!) and create new categories.</p>
<table width="90%" border="1" cellspacing="0" cellpadding="0" align="center" bordercolor="#A8B9FF">
  <tr> 
    <td bgcolor="0066CC" width="100"> 
      <div align="center"><b><font color="#FFFFFF" size="3"><a class="sortlink" href="<?php echo attach_sid("categories.".FILE_EXT."?mode=index&sort_by=cat_name"); ?>">Short Name</a></font></b></div>
    </td>
    <td bgcolor="0066CC"> 
      <div align="center"><font size="3" color="#FFFFFF"><b><a class="sortlink" href="<?php echo attach_sid("categories.".FILE_EXT."?mode=index&sort_by=cat_pname"); ?>">Menu Name</a></b></font></div>
    </td>
    <td bgcolor="0066CC" width="100"> 
      <div align="center"><font size="3" color="#FFFFFF"><b>Menu Order</b></font></div>
    </td>
    <td bgcolor="0066CC" width="100"><div align="center"><font size="3" color="#FFFFFF"><b>Default Page</b></font></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

		// selects the last id (note if it was done before, it would be done again because
		// the last id may have changed
		$query = "SELECT cat_id FROM ".CATEGORIES_TABLE." ORDER BY sort_order DESC LIMIT 1";
		$last_id = db_fetchassoc('',$query,'Could not fetch last ID');
		
		// select categories
		$query = "SELECT cat_id,cat_name,cat_pname,page_id,sort_order,default_cat FROM ".CATEGORIES_TABLE." WHERE sort_order >=0 ORDER BY sort_order ASC";
		$result = db_query($query, 'Could not fetch Categories data');
		
		$numrows = db_numrows($result);
		$cat_data = db_fetchrows($result);
		
		$query = "SELECT cat_id,cat_name,cat_pname,page_id,sort_order,default_cat FROM ".CATEGORIES_TABLE." WHERE sort_order = -1 ORDER BY cat_name ASC";
		$result = db_query($query,'Could not select non menu Categories');
		
		$num_rows = db_numrows($result);
		
		for($i=0;$i<$num_rows;$i++)
		{
			$cat_data[] = db_fetchassoc($result);
		}
		
		$menucount = $numrows;
		$numrows += $num_rows;
		
		for($i=0;$i<$numrows;$i++)
		{
?>
  <tr valign="middle">
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif"><?php echo $cat_data[$i]['cat_name']; ?></font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif"><?php echo stripslashes($cat_data[$i]['cat_pname']); ?></font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php 
			// if there is only one category, no sense providing any move links
			if($menucount == 1 || $cat_data[$i]['sort_order'] == -1)
			{
				echo '-';
			}
			// if its the first, only down will be provided
			else if($cat_data[$i]['sort_order'] == 0)
			{
				echo '<a href="'.attach_sid('categories.'.FILE_EXT.'?mode=index&move='.$cat_data[$i]['cat_id'].'&direction=down').'">Move Down</a>';
			}
			// if last, only up..
			else if($cat_data[$i]['cat_id'] == $last_id['cat_id'])
			{
				echo '<a href="'.attach_sid('categories.'.FILE_EXT.'?mode=index&move='.$cat_data[$i]['cat_id'].'&direction=up').'">Move Up</a>';
			}
			// displays both down and up
			else
			{
				echo '<a href="'.attach_sid('categories.'.FILE_EXT.'?mode=index&move='.$cat_data[$i]['cat_id'].'&direction=up').'">Move Up</a><br />';
				echo '<a href="'.attach_sid('categories.'.FILE_EXT.'?mode=index&move='.$cat_data[$i]['cat_id'].'&direction=down').'">Move Down</a>';
			}
?>
</font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php 
			// if page id is set to none in the db
			if($cat_data[$i]['page_id'] == -1)
			{
				echo 'Not Set';
			}
			else
			{
				// try to get page
				$query = "SELECT page_name FROM ".PAGES_TABLE." WHERE page_id = ".$cat_data[$i]['page_id'];
				$result = db_query($query,'Could not query pages table');
				
				// if it doesn't exist
				if(db_numrows($result)==0)
				{
					// do a little error correcting by setting the page as non for the category
					$query = "UPDATE ".CATEGORIES_TABLE." SET page_id = -1 WHERE page_id = ".$cat_data[$i]['page_id'];
					db_query($query,'Could not update categories table');
			
					echo 'Not Set';
				}
				else
				{
					$page_row = db_fetchassoc($result);
					echo $page_row['page_name'];
				}
			}
?>
	</font></div></td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php 
			echo "<a href=\"".attach_sid("categories.".FILE_EXT."?mode=delete&cat_id=".$cat_data[$i]['cat_id'])."\">Delete</a>";
?>
	</font></div></td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif"><?php echo "<a href=\"".attach_sid("categories.".FILE_EXT."?mode=edit&cat_id=".$cat_data[$i]['cat_id'])."\">Edit</a>"; ?></font></div>
    </td>
    <td> 
      <div align="center"><font size="3" face="Times New Roman, Times, serif">
<?php
			// if the category is default
			if($cat_data[$i]['default_cat']==1)
			{
				echo "Default";
			}
			else
			{	
				echo '<a href="'.attach_sid('categories.'.FILE_EXT.'?mode=index&default='.$cat_data[$i]['cat_id']).'">Make Default</a>';
			}
?>
</font></div>
    </td>
  </tr>
<?php
		}
?>
</table>
<br />
<p aligh="center"><a href="<?php echo attach_sid("categories.".FILE_EXT."?mode=create"); ?>">Create a Category</a></p>
<?php
	display_footer();
}

?>
Return current item: raSMP