Location: PHPKode > scripts > Yet Another Link Directory > yald2/admin/edit.php
<?php
/******************************************************************************
* This file is part of Yet Another Link Directory.                            *
*                                                                             *
* Yet Another Link Directory 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.                                         *
*                                                                             *
* Yet Another Link Directory 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 Yet Another Link Directory; if not, write to the Free Software   *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
******************************************************************************/

require('../inc/config.php');
require('../inc/functions.php');
mysql_connect($mysql['host'],$mysql['username'],$mysql['password']);
mysql_select_db($mysql['db']);

$settings = getSettings();

$template = file_get_contents('template.html');

session_start();

if(!isset($_SESSION['yald_admin_logged_in'])){
	header('Location: login.php');
	exit;
}

if(empty($_GET['id']) && empty($_GET['catid'])){
	die('FATAL ERROR: no ID specified in the URL');
}


$yald_body = '';
$yald_head = '';

if(isset($_GET['id'])){

	$query = "SELECT * FROM {$settings['links_table']} WHERE id=".$_GET['id'];
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);

	if(isset($_POST['submitted'])){
		if(empty($_POST['url'])){
			$error = 'Error: you must enter a URL';
		} elseif(empty($_POST['title'])){
			$error = 'Error: you must enter a link title';
		} elseif(empty($_POST['description'])){
			$error = 'Error: you must enter a description';
		} elseif(strlen($_POST['url'])>$settings['max_url_length']){
			$error = 'Error: the URL you entered was more than'.$settings['max_url_length'].' characters';
		} elseif(strlen($_POST['description'])>$settings['max_description_length']){
			$error = 'Error: the description must be under '.$settings['max_description_length'].' characters';
		} elseif(strlen($_POST['title'])>$settings['max_title_length']){
			$error = 'Error: the title must be under '.$settings['max_title_length'].' characters';
		}

		if(!isset($error)){
			$query = 'UPDATE '.$settings['links_table'].' SET url="'.mysql_safe($_POST['url']).'", name="'.mysql_safe($_POST['title']).'", description="'.mysql_safe($_POST['description']).'", category="'.mysql_safe($_POST['category']).'" WHERE id="'.$_GET['id'].'"';
			mysql_query($query);
			if(isset($_GET['ref']) && $_GET['ref']=='approve'){
				$return = '<a href="approve.php">Return to pending links</a>';
			} else {
				if($settings['url_type'] == '2'){
					$returnurl = $settings['mod_rewrite_url'].$_POST['category'].'_'.cleanUrl(categoryName($_POST['category']));
				} else {
					$returnurl = $settings['index_file'].'?viewcat='.$_POST['category'];
				}
				$return = '<a href="../'.$returnurl.'">Return to directory</a>';
			}
			$error = 'Link successfully updated. '.$return;
		}
		if(isset($error)){
			$yald_body .= '<div align="center"><div class="errorbox"><b>'.$error.'</b></div></div>';
		}
	}

	if(!empty($_POST['url'])){
		$sticky_form['url'] = 'value="'.gpcStripSlashes($_POST['url']).'" ';
	} else {
		$sticky_form['url'] = 'value="'.$row['url'].'" ';
	}
	if(!empty($_POST['title'])){
		$sticky_form['title'] = 'value="'.gpcStripSlashes($_POST['title']).'" ';
	} else {
		$sticky_form['title'] = 'value="'.$row['name'].'" ';
	}
	if(!empty($_POST['description'])){
		$sticky_form['description'] = gpcStripSlashes($_POST['description']);
	} else {
		$sticky_form['description'] = $row['description'];
	}
	$category = linkCategory($_GET['id']);
	if(empty($_POST['category']) || $_POST['category'] == $category){
		$menu = categoryMenu('1',$category);
	} else {
		$menu = categoryMenu('1',$_POST['category']);
	}

	$action = $_SERVER['PHP_SELF'].'?id='.$_GET['id'];
	if(isset($_GET['ref'])){
		$action .= '&ref='.$_GET['ref'];
	}

	$yald_body .= <<<EOF
<strong>Edit link:</strong><br /><br />
<form action="{$action}" method="post">
<table width="100%" border="0">
  <tr>
    <td width="11%">URL:</td>
    <td width="89%"><input type="text" name="url" {$sticky_form['url']}/></td>
  </tr>
  <tr>
    <td>Title:</td>
    <td><input type="text" name="title" {$sticky_form['title']}/></td>
  </tr>
  <tr>
    <td>Description:</td>
    <td><textarea name="description" cols="30" rows="4">{$sticky_form['description']}</textarea>Max 255 chars.</td>
  </tr>
  <tr>
    <td>Category:</td>
    <td>{$menu}</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Update" />
      <input name="Reset" type="reset" id="Reset" value="Reset" /><input type="hidden" name="submitted" value="1" /></td>
  </tr>
</table>
EOF;

} elseif(isset($_GET['catid'])) {
	
	if(categoryExists($_GET['catid'])){


	$query = "SELECT * FROM {$settings['categories_table']} WHERE id=".$_GET['catid'];
	$result = mysql_query($query);
	$row = mysql_fetch_array($result);

	if(isset($_POST['submitted'])){

		if(!isset($_POST['disp_subs'])){
			$disp_subs = 'false';
			$sticky_form['disp_subs'] = '';
		} else {
			$disp_subs = 'true';
			$sticky_form['disp_subs'] = 'checked="checked" ';
		}

		if(empty($_POST['title'])){
			$error = 'Error: you must enter a title';
		} elseif(array_search($_POST['category'],subCategories($row['id']))){
			$error = 'Error: you cannot set this category\'s parent to itself.';
		} elseif(!categoryExists($_POST['category'])){
			$error = 'Error: you specified a parent that does not exist.';
		}
		

		if(!isset($error)){

			if($_POST['category'] != $row['parent']){
				$newparent = $_POST['category'];
			} else {
				$newparent = $row['parent'];
			}

			$query = 'UPDATE '.$settings['categories_table'].' SET name="'.mysql_safe($_POST['title']).'", `disp_subs`="'.$disp_subs.'", parent="'.$_POST['category'].'" WHERE id="'.$_GET['catid'].'"';
			mysql_query($query);
			rebuildTree('1',1);
			
			if($_POST['category'] != $row['parent']){
				updateDbPath($_GET['catid'],true);
			}

			$return = '<a href="categories.php">Return to categories</a>';
			$error = 'Category successfully updated. '.$return;
		}
		if(isset($error)){
			$yald_body .= '<div align="center"><div class="errorbox"><b>'.$error.'</b></div></div>';
		}

	} else {
		if($row['disp_subs']=='true'){
			$sticky_form['disp_subs'] = 'checked="checked" ';
		} else {
			$sticky_form['disp_subs'] = '';
		}
	}

	if(!empty($_POST['title'])){
		$sticky_form['title'] = 'value="'.gpcStripSlashes($_POST['title']).'" ';
	} else {
		$sticky_form['title'] = 'value="'.$row['name'].'" ';
	}

	/*if(!empty($_POST['parent'])){
	$sticky_form['parent'] = $_POST['parent'];
	} else {
	$sticky_form['parent'] = $row['parent'];
	}*/


	$category = $row['parent'];
	if(empty($_POST['category']) || $_POST['category'] == $row['id']){
		$menu = categoryMenu('1',$category,true,$row['id']);
	} else {
		$menu = categoryMenu('1',$_POST['category'],true,$row['id']);
	}

	$yald_body .= <<<EOF
<strong>Edit category:</strong><br /><br />
<form action="{$_SERVER['PHP_SELF']}?catid={$_GET['catid']}&ref={$_GET['ref']}" method="post">
<table width="100%" border="0">
  <tr>
    <td>Title:</td>
    <td><input type="text" name="title" {$sticky_form['title']}/></td>
  </tr>
  <tr>
    <td width="11%">Display subcategories underneath:</td>
    <td width="89%"><input type="checkbox" name="disp_subs" value="1" {$sticky_form['disp_subs']}/></td>
  </tr>
  <tr>
    <td>Parent:</td>
    <td>{$menu}</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Update" />
      <input name="Reset" type="reset" id="Reset" value="Reset" /><input type="hidden" name="submitted" value="1" /></td>
  </tr>
</table>
EOF;

} else {
	$yald_body = 'The category selected does not exist.';
}

}




$template = admin_output('categories');

print $template;
?>
Return current item: Yet Another Link Directory