<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

include("../include/config.php");
include_once("../include/functions/import.php");
verify_login_admin();

if($_POST['submitform'] == "1")
{
	$cname = cleanit($_POST['name']);
	$cmsg = cleanit($_POST['message']);
	$uploadedimage = $_FILES['cimage']['tmp_name'];
	$featured = intval($_POST['featured']);
	
	if($cname == "")
	{
		$error = "Error: Please enter a category name.";
		Stemplate::assign('error',$error);
	}
	elseif($cmsg == "")
	{
		$error = "Error: Please enter a category description.";
		Stemplate::assign('error',$error);
	}
	elseif($uploadedimage == "")
	{
		$error = "Error: Please select an image for your category.";
		Stemplate::assign('error',$error);
	}
	else
	{
		$sql = "insert categories_docs set name='".mysql_real_escape_string($cname)."', details='".mysql_real_escape_string($cmsg)."', featured='".mysql_real_escape_string($featured)."'";
		$conn->execute($sql);
		
		$CATID = mysql_insert_id();
		
		$theimageinfo = getimagesize($uploadedimage);
			  
				
		if($theimageinfo[2] == 1)
		{
			$extension .= ".gif";
		}
		elseif($theimageinfo[2] == 2)
		{
			$extension .= ".jpg";
		}
		elseif($theimageinfo[2] == 3)
		{
			$extension .= ".png";
		}
		else
		{
			$extension .= ".jpg";
		}
		
		$thepp = $CATID.$extension;
		
		$mydocimgnew=$config['channelthumbdir']."/o-".$thepp;
		if(file_exists($mydocimgnew))
		{
			unlink($mydocimgnew);
		}
		$myconvertimg = $_FILES['cimage']['tmp_name'];
		
		move_uploaded_file($myconvertimg, $mydocimgnew);
		
		$max_width_thumbs = "124";
		$max_height_thumbs = "93";
		$tothumbdir = $config['channelthumbdir']."/".$thepp;
		if(file_exists($tothumbdir))
		{
			unlink($tothumbdir);
		}
		generatedocthumbs($mydocimgnew,$tothumbdir,$max_width_thumbs,$max_height_thumbs);
		if(file_exists($mydocimgnew))
		{
			unlink($mydocimgnew);
		}
					
		$sql = "UPDATE categories_docs set extension='".mysql_real_escape_string($extension)."' WHERE CATID='".mysql_real_escape_string($CATID)."'";
		$conn->execute($sql);
		
		$message = "Document Category Successfully Added.";
		Stemplate::assign('message',$message);
	}
}

$mainmenu = "3";
$submenu = "1";
Stemplate::assign('mainmenu',$mainmenu);
Stemplate::assign('submenu',$submenu);
STemplate::display("administrator/global_header.tpl");
STemplate::display("administrator/cat_docs_add.tpl");
STemplate::display("administrator/global_footer.tpl");
?>