Location: PHPKode > projects > ACollab > library/edit_file.php
<?php
/****************************************************************************************/
/* ACollab                                                                              */
/****************************************************************************************/
/* Copyright (c) 2002-2004  Adaptive Technology Resource Centre / University of Toronto */
/*                                                                                      */
/* http://atutor.ca/acollab                                                             */
/*                                                                                      */
/* This program is free software. You may 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 may access the GNU General Public License at:                                    */
/* http://www.opensource.org/licenses/gpl-license.php                                   */
/*                                                                                      */
/* You may contact the Adaptive Technology Resource Centre at                           */
/* Robarts Library, University of Toronto                                               */
/* 130 St. George Street, Toronto, Ontario, Canada M5S 1A5                              */
/* Further contact information is available at http://www.utoronto.ca/atrc/             */
/****************************************************************************************/
/* Programmer:                                                                          */
/* Joel Kronenberg - ATRC                                                               */
/****************************************************************************************/
// $Id: edit_file.php 234 2004-06-07 19:43:50Z boonhau $

exit('no longer used');

define('AC_INCLUDE_PATH', '../include/');

require(AC_INCLUDE_PATH.'vitals.inc.php');
authenticate(USER_CLIENT, USER_GROUP_ADMIN);

if ($_POST['cancel']) {
	Header('Location: index.php');
	exit;
}

$id = intval($_REQUEST['id']);

if (isset($_POST['submit'])) {
	$_POST['source']     = trim($_POST['source']);
	$_POST['title']	     = trim($_POST['title']);
	$_POST['description']= trim($_POST['description']);
	$pid = intval($_POST['pid']);


	if ($_POST['source'] == '') {
		$error[] = E_DRAFT_EMPTY_SOURCE;
	}

	if ($_POST['title'] == '') {
		$error[] = E_DRAFT_EMPTY_TITLE;
	}

	if ($_POST['replacefile']) {

		if($_FILES['file']['name'] == '') {
			$error[] = E_DRAFT_EMPTY_FILE;
		} else if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
			$error[] = E_DRAFT_UPLOAD;
		} else {
			$ext = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.')+1);
			if (!isset($_file_types[$ext])) {
				$error[] = E_DRAFT_ILLEGAL_FILE_TYPE;
			}
		}
	}

	/*
	if ($_POST['file_type'] == '') {
		$error[] = E_DRAFT_EMPTY_FILE_TYPE;
	}
	*/

	if ($_POST['description'] == '') {
		$error[] = E_DRAFT_EMPTY_DESC;
	}

	if (!is_array($_POST['cats'])) {
		$error[] = E_DRAFT_EMPTY_CAT;
	}

	if (!isset($error)) {
		if ($pid) {
			/* these categories are the parent's */
			$cat_file_id = $pid;
		} else {
			/* I'm the parent so the categories are mine */
			$cat_file_id = $id;
		}

		$sql	= "DELETE FROM ".TABLE_PREFIX."categories_files WHERE file_id=$cat_file_id";
		mysql_query($sql, $db);

		$num_cats = count($_POST['cats']);
		$sql = '';
		for($i = 0; $i < $num_cats; $i++) {
			$sql .= '('.$_POST['cats'][$i].','.$cat_file_id.'),';
		}
		$sql = substr($sql, 0, -1);
		if ($sql != '') {
			$sql = 'INSERT INTO '.TABLE_PREFIX.'categories_files VALUES '.$sql;
			mysql_query($sql, $db);
		}

		if ($_POST['replacefile']) {
			$sql	= "UPDATE ".TABLE_PREFIX."files SET source='$_POST[source]', title='$_POST[title]', description='$_POST[description]', file_name='{$_FILES[file][name]}', file_size='{$_FILES[file][size]}', comments_by='$due_date' WHERE file_id=$id AND member_id=$_SESSION[member_id]";

			if (mysql_query($sql, $db)) {
				$char = substr($id, 0, 1).'/';

				@unlink(UPLOAD_DIR.$char.$id);

				if (move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_DIR.$char.$id)) {
					@chmod(UPLOAD_DIR.$char.$id, 01600);
					header('Location: index.php?f='.F_DRAFT_EDITED);
					exit;
				}
			}
		} else {
			$sql	= "UPDATE ".TABLE_PREFIX."files SET source='$_POST[source]', title='$_POST[title]', description='$_POST[description]', comments_by='$due_date' WHERE file_id=$id AND member_id=$_SESSION[member_id]";

			mysql_query($sql, $db);
			header('Location: index.php?f='.F_DRAFT_EDITED);
			exit;
		}

		$error[] = E_DRAFT_UPLOAD;
	}
}

$_SECTION[0][0] = _AC('home');
$_SECTION[0][1] = 'home.php';
$_SECTION[1][0] = _AC('library');
$_SECTION[1][1] = 'library/';
$_SECTION[2][0] = _AC('edit_file');
$_SECTION[2][1] = 'library/edit_file.php';

require(AC_INCLUDE_PATH.'header.inc.php');

if (isset($error)) {
	print_errors($error);
	unset($error);
}

	$sql = "SELECT * FROM ".TABLE_PREFIX."files WHERE file_id=$id AND group_id=$_SESSION[group_id]";
	$result = mysql_query($sql,$db);
	if (!($row = mysql_fetch_array($result))) {
		$errors[] = E_FILE_NOT_FOUND;
		print_errors($errors);
		require (AC_INCLUDE_PATH.'footer.inc.php');
		exit;
	} else if (($row['member_id'] != $_SESSION['member_id'])  && !authenticate(USER_GROUP_ADMIN, USER_RETURN_CHECK)){
		/* actually permission is denied, but for security we fake a "FNF" */
		$errors[] = E_FILE_NOT_FOUND;
		print_errors($errors);
		require (AC_INCLUDE_PATH.'footer.inc.php');
		exit;
	}

	if (isset($_POST['submit'])) {
		$row['source']      = $_POST['source'];
		$row['title']	    = $_POST['title'];
		$row['description'] = $_POST['description'];

		$my_cats = $_POST['cats'];

		if ($_POST['replacefile']) {
			$repy = ' checked="checked"';
		} else {
			$repn = ' checked="checked"';
		}
	} else {
		$repn = ' checked="checked"';

		if ($row['parent_file_id']) {
			$cat_file_id = $row['parent_file_id'];
		} else {
			$cat_file_id = $id;
		}
		$sql	= "SELECT category_id FROM ".TABLE_PREFIX."categories_files WHERE file_id=$cat_file_id";
		$result	= mysql_query($sql, $db);
		$my_cats = array();
		while ($temp_row = mysql_fetch_assoc($result)) {
			$my_cats[] = $temp_row['category_id'];
		}
	}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form" id="form">
	<input type="hidden" name="id" value="<?php echo $id; ?>">
	<input type="hidden" name="pid" value="<?php echo $row['parent_file_id']; ?>">
	<p>
	<table border="0" cellspacing="0" cellpadding="2" align="center" class="box2">
	<tr>
		<th colspan="5" class="box"><h3><?php echo _AC('edit_file'); ?></h3></th>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td colspan="3" class="row1"><img src="images/clr.gif" height="1" width="1" alt="" /><br /><?php
			echo _AC('denotes_required', '<img src="images/required.gif" height="14" width="14" alt="'._AC('required_field').'" />');
		?><br /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right"><label for="source"><b><?php echo _AC('source'); ?>:</b></label></td>
		<td class="row1"><img src="images/required.gif" height="14" width="14" alt="<?php echo _AC('required_field'); ?>" /></td>
		<td class="row1"><input type="text" name="source" class="input" size="40" maxlength="100" id="source" value="<?php echo $row['source']; ?>" onfocus="this.className='input highlight'" onblur="this.className='input'" /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right"><label for="title"><b><?php echo _AC('title'); ?>:</b></label></td>
		<td class="row1"><img src="images/required.gif" height="14" width="14" alt="<?php echo _AC('required_field'); ?>" /></td>
		<td class="row1"><input type="text" name="title" class="input" size="40" maxlength="100" id="title" value="<?php echo $row['title']; ?>" onfocus="this.className='input highlight'" onblur="this.className='input'" /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right" valign="top"><b><?php echo _AC('file'); ?>:</b></td>
		<td class="row1" valign="top"><img src="images/required.gif" height="14" width="14" alt="<?php echo _AC('required_field'); ?>" class="img" /></td>
		<td class="row1"><small><?php echo _AC('drafting_replace_file'); ?></small><br /><input type="radio" name="replacefile" id="replacefile1" value="0" <?php echo $repn; ?> /><label for="replacefile1"><?php echo $row['file_name']; ?></label><br />
		<input type="radio" name="replacefile"  value="1" <?php echo $repy; ?> /><input type="file" name="file" id="file" class="input" size="30" onChange='selectFile(this);' onfocus="this.className='input highlight'" onblur="this.className='input'" /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right" valign="top"><label for="description"><b><?php echo _AC('description'); ?>:</b></label></td>
		<td class="row1" valign="top"><img src="images/required.gif" height="14" width="14" alt="<?php echo _AC('required_field'); ?>" class="img" /></td>
		<td class="row1"><textarea name="description" id="description" class="input" onfocus="this.className='input highlight'" onblur="this.className='input'" cols="40" rows="3"><?php echo $row['description']; ?></textarea></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" align="right" valign="top"><label for="cat"><b><?php echo _AC('categories'); ?>:</b></label><br /><small><small><?php echo _AC('categories_select'); ?></small></small></td>
		<td class="row1" valign="top"><img src="images/required.gif" class="img" height="14" width="14" alt="<?php echo _AC('required_field'); ?>" /></td>
		<td class="row1" valign="top"><?php

						$categories = get_categories('title', 'ASC');
						if ($categories) {
							$num_cat = min(count($categories), 5);
							$num_cat = count($categories);
							echo '<select name="cats[]" id="cat" size="'.$num_cat.'" multiple="multiple" class="input" onfocus="this.className=\'input highlight\'" onblur="this.className=\'input\'">';
							foreach ($categories as $cat) {
								echo '<option value="'.$cat['category_id'].'"';
								if (is_array($my_cats) && in_array($cat['category_id'], $my_cats)) {
									echo ' selected="selected"';
								}
								echo '>'.$cat['title'].'</option>';		
							}
							echo '</select>';
						} else {
							echo _AC('no_categories_found');
						}
		?></td>
		<td class="row1">&nbsp;</td>
	</tr>
	<tr>
		<td class="row1">&nbsp;</td>
		<td class="row1" colspan="3" align="right"><br /><input type="submit" name="submit" value=" <?php echo _AC('edit'); ?> " class="submitY" onfocus="this.className='submitY highlight'" onblur="this.className='submitY'" /> &nbsp; <input type="submit" name="cancel" value="<?php echo _AC('cancel'); ?>" class="submitN" onfocus="this.className='submitN highlight'" onblur="this.className='submitN'" /><br /><br /></td>
		<td class="row1">&nbsp;</td>
	</tr>
	</table>
	</p>
</form>

<script language="JavaScript">
<!--
	function selectFile(o)
	{
		// Check upload option
		if (form.replacefile[1]) {
			form.replacefile[1].checked = true;
		}

	}
//-->
</script>

<?php
	require(AC_INCLUDE_PATH.'footer.inc.php');
?>
Return current item: ACollab