<?php require_once('../../../../../../../Connections/conn_ama.php'); 
include("../../../../../../functions/functions.php");    
    
if (!isset($_SESSION)) {
  session_start();
}

	//$curr_season = $_SESSION['curr_season'];
	//$curr_season = '2018-2019';


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "save-news-update")) {
	
			
	define('MAX_FILE_SIZE',10121200);

		$imagecount = count($_FILES['image']['name']);

		//echo $imagecount;
		
		//$newseason = trim(str_replace("/", "-", $curr_season), "'");
		
		define('UPLOAD_DIR', '../../articles-images/news/');
		// define constants
		define('SOURCE_DIR', '../../articles-images/news/');
		define('THUMBS_DIR', '../../articles-images/news/thumbs/');
		define('MAX_WIDTH', 320);
		define('MAX_HEIGHT', 278);
		
		for($i=0; $i<$imagecount; $i++)
			{		
				$file = trim(str_replace(" ","_",$_FILES['image']['name'][$i]));
				
				$kilobytes = MAX_FILE_SIZE/1024;
				$formatted = number_format($kilobytes, 1);
				$max = $formatted."KB";
				$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png','image/tiff');
				$sizeok = false;
				$typeok = false;
				if($_FILES['image']['size'][$i] > 0 && $_FILES['image']['size'][$i] <= MAX_FILE_SIZE)
					{
						$sizeok = true;
					}
					foreach($permitted as $type){
						if($type==$_FILES['image']['type'][$i])
						{
							$typeok = true;
							break;
						}
					}
				if($sizeok && $typeok)
				{
				switch($_FILES['image']['error'][$i]){
				case 0:
					include('update_both_logo.inc.php');
					break;
					case 3:
					$result = "Error uploading $file. Please try again";
					default:
					$result = "System error uploading $file. Please contact webmaster";
					}
				}
				elseif($_FILES['image']['error'][$i]==4)
				{
					//$result = "No file selected";
					
					$updateSQL = sprintf("UPDATE newsarticles SET headline=%s, gist=%s, content=%s WHERE newsID =%s ",	
					GetSQLValueString(trim($_POST['headline']), "text"),
					GetSQLValueString($_POST['gist'], "text"),
					GetSQLValueString($_POST['content'], "text"),
					GetSQLValueString($_POST['MM_newsID'], "int"));

					$Result1 = mysqli_query($conn_ama, $updateSQL) or die(mysqli_error($conn_ama));
					//$newsID = mysqli_insert_id($conn_ama);	
					if($Result1){
						$result = "datasent";
					}		
				}
				else{ $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file type:
				gif, jpeg, png, tiff";}
			}
		
		if(isset($result)){ echo $result;}

}


?>
