<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
// 
// Copyright (C) 2003, 2005 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
// 
// This program 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, 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 should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

include "inc/header_my.inc.php";

$file->user_id = $me['user_id'];


if (isset($_REQUEST['file_id'])) {
	$file_id = $_REQUEST['file_id'];
}


if (isset($_POST['insert_file']) && $_FILES['upload_file']['size'] > 0) {
	$file_upload = $_FILES['upload_file'];
	
	if (!empty($c_node['file']['mime']['browser_path'])) {
	    foreach($c_node['file']['mime']['browser_path'] as $i):
			if ($i['from'] == $file_upload['type']) {
			    $file_upload['type'] = $i['to'];
			}
		endforeach;
	}
	
	$file_upload['file_title'] = $data['file_title'];
	$file->arr_file_upload = $file_upload;
	if ($file->checkFile()) {
		$file->insertFileRecord();
		$file->moveFile();
	}
	
	if (empty($GLOBALS['am_error_log'])) {
	    return header("Location: my_files.php?" . SID);
		exit;
	}
}
elseif (isset($_POST['delete_file'])) {
	$file->file_id = $data['file_id'];
	$delete_file = $file->getFileRecord();
	
	if (isset($delete_file)) {
	    $file->deleteFile($delete_file['file_name'], $delete_file['file_suffix']);
	}
	
	$file->deleteFileRecord($data['file_id']);
	
	if (empty($file->log)) {
	    return header("Location: my_files.php?" . SID);
		exit;
	}
}
elseif (isset($_POST['update_file'])) {

	if (isset($_FILES['upload_file']) && $_FILES['upload_file']['size'] > 0) {
	    $file_upload = $_FILES['upload_file'];
		$file_upload['file_title'] = $data['file_title'];
		$file_upload['file_suffix'] = $data['file_suffix'];
		$file_upload['file_name'] = $data['file_name'];
		$file->file_id = $data['file_id'];
		$file->arr_file_upload = $file_upload;
		
		if ($file->checkFile()) {
			$file->deleteFile($data['file_name'], $data['file_suffix']);
			$file->updateFileRecord();
			$file->moveFile();
		}
	}
	else { //just update database with new title
		$file_upload['file_title'] = $data['file_title'];
		$file->file_id = $data['file_id'];
		$file->arr_file_upload = $file_upload;
		$file->updateFileRecord();
	}
	
	if (empty($file->log)) {
	    return header("Location: my_files.php?file_id=" . $data['file_id'] . "&amp;" . SID);
		exit;
	}
}


if (isset($file_id)) {
	$file->file_id = $file_id;
	$output_file = $file->getFileRecord();
	$display = "edit";
}
elseif (isset($_REQUEST['add_image'])) {
	$display = "add";
}


$file->thumb = $c_node['file']['image']['thumb'][3]['name'];
$file->orderby = "file_title";
$output_files = $file->getFiles();

//storage
$output_storage = $file->getAllocation();



//move to template
if (!empty($output_user)) {
    $body->set('user', $output_user);
}

if (!empty($output_files)) {
    $body->set('files', $output_files);
}

if (!empty($output_file)) {
    $body->set('out_file', $output_file);
}

if (isset($display)) {
    $body->set('display', $display);
}

if (!empty($output_storage)) {
    $body->set('storage', $output_storage);
}



include "inc/footer.inc.php";

?>
