<?php
/**********************************************************************
***********************************************************************/
$path_to_root = "..";
$page_security = 'SA_ATTACHMENT';

include_once($path_to_root . "/includes/db_pager.inc");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc"); 
include_once($path_to_root . "/admin/includes/ui/attachments_ui.inc"); 
include_once($path_to_root . "/admin/db/attachments_db_wms.inc");
include_once($path_to_root . "/admin/db/transactions_db.inc");
include_once($path_to_root . "/admin/db/transactions_db.inc");  
include_once($path_to_root . "/services/includes/db/job_activities_db.inc"); 
include_once($path_to_root . "/services/includes/db/job_orders_db.inc"); 
include_once($path_to_root . "/projects/includes/db/projects_db.inc"); 
include_once($path_to_root . "/includes/ui/ui_view_custom.inc"); 

if (isset($_GET['vw'])) {
    $view_id = $_GET['vw'];
} else {
    $view_id = find_submit('view');
}

if ($view_id != -1) {
    $row = get_attachment($view_id);
	if ($row['filename'] != "") {
            if (in_ajax()) {
                $Ajax->popup($_SERVER['PHP_SELF'].'?vw='.$view_id);
            } else {
                $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';	
                header("Content-type: ".$type);
                header('Content-Length: '.$row['filesize']);
                //if ($type == 'application/octet-stream')
                //	header('Content-Disposition: attachment; filename='.$row['filename']);
                //else
                        header("Content-Disposition: inline");
                echo file_get_contents(company_path(). "/attachments/".$row['unique_name']);
                exit();
            }
	}	
}

if (isset($_GET['dl'])) {
    $download_id = $_GET['dl'];
} else {
    $download_id = find_submit('download');
}

if ($download_id != -1) {
    $row = get_attachment($download_id);
	if ($row['filename'] != "") {
            if (in_ajax()) {
                $Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
            } else {
                $type = ($row['filetype']) ? $row['filetype'] : 'application/octet-stream';	
                header("Content-type: ".$type);
                header('Content-Length: '.$row['filesize']);
                header('Content-Disposition: attachment; filename='.$row['filename']);
                echo file_get_contents(company_path()."/attachments/".$row['unique_name']);
                exit();
            }
	}	
}

$js = "";
$js .= get_js_open_window(800, 500);

page(_($help_context = "Attach Documents"), true, false, "", $js);

simple_page_mode(true);

if (isset($_GET['type'])) { 
    $_POST['type'] = $_GET['type'];
} 

if (isset($_GET['trans_no'])) {
    $_POST['trans_no'] = $_GET['trans_no'];
} 

if (isset($_GET['line_id']) || isset($_POST['line_id'])) {
    $_POST['line_id'] = isset($_POST['line_id']) ? $_POST['line_id'] : $_GET['line_id'];
} else {
    $_POST['line_id'] = 0;
}

if ($Mode == 'ADD_ITEM' || $Mode == 'UPDATE_ITEM') {
    global $Ajax;
    
    $msg = upload_attachment($Mode, $_POST['type'], $_POST['trans_no'], $_POST['line_id'], $_POST['description'], $selected_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
            refresh_pager('trans_tbl');
            $Ajax->activate('_page_body');
            $Mode = 'RESET';
        }
}

if ($Mode == 'Delete') {
    $msg = delete_attachment_file($selected_id);
        if ($msg['success'] === false) {
            display_error($msg['message']);
        } else {
            display_notification($msg['message']);
        }
        
	$Mode = 'RESET';
}

if ($Mode == 'RESET') {
    unset($_POST['description']);
    $selected_id = -1;
}

start_form(true);

    global $systypes_array;
    
    if (isset($_POST['type'])) {
        hidden('type', $_POST['type']);
    }
    if (isset($_POST['trans_no'])) {
        hidden('trans_no', $_POST['trans_no']);
    }
    if (isset($_POST['line_id'])) {
        hidden('line_id', $_POST['line_id']);
        $line_text = " and line #".$_POST['line_id'];
    }
    
    if ($_POST['type'] == ST_JOB_ACTIVITY) {
        $act_row = get_job_activity_record($_POST['trans_no']);
            if (! (isset($act_row['id']) && $act_row['id'] > 0)) {
                display_error("Unknown ".$systypes_array[ST_JOB_ACTIVITY]." #".$_POST['trans_no']);
                display_footer_exit();
                exit;
            }
            
        $jo_row = get_job_order_project_id($act_row['jo_id']);
        $user_id = $_SESSION['wa_current_user']->user;
        $cap = can_access_project($jo_row['project_id'], $user_id);
            if ($cap['success'] !== true) {
                display_error($cap["message"]);
                display_footer_exit();
                exit;
            }
    }
    
    $line_text = $_POST['line_id'] > 0 ? $_POST['line_id'] : null; 
    
    display_heading("Attachments of ".$systypes_array[$_POST['type']]." trans #".$_POST['trans_no']."$line_text");    br();

    display_attachments_details($_POST['type'], $_POST['trans_no'], $_POST['line_id']); br(2);

    display_attachment_control($Mode, $selected_id); 

end_form(2);

end_page();

?>