<?
	require_once('user.php');
	require_once('mysql.php');
	require_once('functions.php');

	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();

	$query = "SELECT SUM(size) AS total_size, COUNT(id) AS files FROM file WHERE record_id=".$_GET['record_id']." AND source='".$_GET['source']."'";
	$mysqldb1->query($query);
	$file = $mysqldb1->fetchArray();

	if ($files == $file_limit) {
		// TODO: Disable file uploader if uploading limit reached
	}

	if ($_GET['record_id'] != 0) {
	if ($file['files'] == 0) {
		print '<p>0 Files Uploaded</p>';
	} else {
		//print '<p>Contains '.$files.' out of '.$file_limit.' files.</p>';
		if ($file['files'] == 1) {
			print '<p>'.$file['files'].' File Uploaded ('.number_format(($file['total_size'] / 1024 / 1024), 2, '.', ',').'MB)</p>';
		} else {
			print '<p>'.$file['files'].' Files Uploaded ('.number_format(($file['total_size'] / 1024 / 1024), 2, '.', ',').'MB)</p>';
		}
	}
	}

	?>
	<ul id="sortable">
	<?
	$query = "SELECT * FROM file WHERE record_id=".$_GET['record_id']." AND source='".$_GET['source']."' ORDER BY sequence ASC, id DESC";
	if ($_GET['record_id'] == 0) {
		$query = "SELECT * FROM file WHERE source='".$_GET['source']."' ORDER BY sequence ASC, id DESC";
	}
	$mysqldb1->query($query);
	$counter = 0;
	while ($file = $mysqldb1->fetchArray()) {

		$rel_attr = '';
		if ($file['type'] == 'image') {
			$rel_attr = 'rel="../../'.$tinyDIR.$file['name'].'"';

			$image_size = '';
			if (file_exists('../../'.$fileDIR.$file['name'])) { // stops warnings that may occur if the file has not finished uploading
				list($image_width, $image_height, $image_type, $image_attr) = getimagesize('../../'.$fileDIR.$file['name']);
				$image_size = '['.$image_width.' x '.$image_height.']';
			}
		}

		$size = '('.number_format(($file['size'] / 1024), 2, '.', ',').'KB)';
		$counter++;
		?>
        <li class="ui-state-default" id="item_<? print $file['id']; ?>">
        <table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td><img src="../styles/default/handle.png" alt="move" width="16" height="16" class="handle" /></td>
            <td width="60"><input name="file_description" id="file_description_<? print $file['id']; ?>" type="text" value="<? print $file['description']; ?>" maxlength="100" style="width:150px;" /></td>
            <td><a href="javascript:updateFileDescription('<? print $_GET['source']; ?>', <? print $_GET['record_id']; ?>, <? print $file['id']; ?>);"><img src="../styles/default/update.png" alt="update" border="0" class="button"/></a></td>
            <td><a href="javascript:deleteFile('<? print $_GET['source']; ?>', <? print $_GET['record_id']; ?>, <? print $file['id']; ?>);"><img src="../styles/default/delete.png" alt="delete" border="0" class="button"/></a></td>
            <td><? if ($file['status'] == 'disabled') { ?><a href="javascript:enableFile('<? print $_GET['source']; ?>', <? print $_GET['record_id']; ?>, <? print $file['id']; ?>);"><img src="../styles/default/disabled.png" alt="disabled" border="0" class="button"/></a><? } ?><? if ($file['status'] == 'enabled') { ?><a href="javascript:disableFile('<? print $_GET['source']; ?>', <? print $_GET['record_id']; ?>, <? print $file['id']; ?>);"><img src="../styles/default/enabled.png" alt="enabled" border="0" class="button"/></a><? } ?></td>
            <td class="file_detail" nowrap><a href="<? print '../../'.$fileDIR.$file['name']; ?>" <? print $rel_attr; ?> target="_blank" class="<? print strtolower(substr(strrchr($file['name'], '.'), 1)); ?>"><? print $file['name']; ?></a> <? print $size; ?> <? if ($file['type'] == 'image') { ?><? print $image_size; ?><? } ?></td>
          </tr>
        </table>
        </li>
		<? } ?>
	</ul>
<?
	$mysqldb1->close();
?>