<?php

require_once "../includes/sessions.php";
require_once "../includes/db-connection.php";
require_once "../includes/functions.php";
$doc_id = isset($_GET['doc_id']) && intval($_GET['doc_id']) > 0 ? $_GET['doc_id'] : "";
$fileContentType = array('pdf' => 'application/pdf', 'doc' => 'application/msword', 'xls' => 'application/vnd.ms-excel');
$docRow = get_doc_by_id($doc_id);
$path = "../document/";
$file = $docRow['filename'];
$title = $docRow['title'];
header('Content-type: ' . $fileContentType[$docRow['file_type']]);
header('Content-Disposition: inline; filename="' . $title . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path . $file));
header('Accept-Ranges: bytes');
@readfile($path . $file);
require_once "../includes/validation-functions.php";
?>

<?php 
$page = "edit-document.php";
?>

<?php 
include '../includes/layouts/header.php';
?>

<?php 
$cat = get_all_category();
// If user does not exist, redirect back to Manage Users Page
$id = isset($_GET["doc_id"]) ? $_GET["doc_id"] : "";
$doc = get_doc_by_id($id);
if (!$doc) {
    redirect_to("manage-document.php");
}
?>

<?php 
if (isset($_POST['submit-edit-document'])) {
    // The Edit User Form was submitted
    // Validate Edit User Form inputs
    $fields_with_max_lengths = array("title" => 250, "filename" => 250);
    foreach ($fields_with_max_lengths as $field => $max) {
        $value = trim($_POST[$field]);
        if (!value_within_range($value, 1, $max)) {
            $error_messages[$field] = ucfirst($field) . " is too long.";
        }