<?php
/*
* date last modified 27-06-2018
* Edited by PI-Digi Logical Solutions
* HTML Files - purchase_partner_photos.html.php
* Mysql Tables - tbl_product_categories
*/
require_once('./classes/class.noseo_pagination.php'); # pagination class
require_once('./includes/functions.php'); # general functions
include('includes/imgFunctions.php');

if(isset($_REQUEST['p']) && $_REQUEST['p']!=''){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}

$partner_id = $_REQUEST['id'];



// add photo
if(isset($_POST['add'])) {
	
	if(isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) {		
		if($_FILES['photo']['type'] == 'image/gif' || $_FILES['photo']['type'] == 'image/jpeg' || 
		   $_FILES['photo']['type'] == 'image/png' || $_FILES['photo']['type'] == 'image/pjpeg') {
			$photo_name = $_FILES['photo']['name'];			
			$photo_temp_name = $_FILES['photo']['tmp_name'];
			$photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION);
		} else {
			$photo_name = FALSE;
			$photo_temp_name = FALSE;
			$error = "Invalid Image type. Only gif, jpg and png allowed";
		}
	} else {
		$error = 'Select Image';
	}
	
	if(empty($error)) {
		$insert = mysqli_query($con, 'INSERT INTO tbl_purchase_partner_photos (partner_id) VALUES ('.$partner_id.')');
		$insert_id = mysqli_insert_id($con);
		if(isset($_FILES['photo']) && $_FILES['photo']['size'] > 0) {
			$photo_dir = SHOP_IMG_FOLDER.'shop/';
			$photo_name = $insert_id.'_'.time().'.'.$photo_extension; 
			if(move_uploaded_file($photo_temp_name, $photo_dir.$photo_name)){
				resize2(SHOP_IMG_FOLDER.'shop/'.$photo_name, 700, 650, SHOP_IMG_FOLDER.'shop/'.$photo_name);
				resize2(SHOP_IMG_FOLDER.'shop/'.$photo_name, 200, 150, SHOP_IMG_FOLDER.'shop/thumb/'.$photo_name);
				// update photo
				mysqli_query($con, "UPDATE tbl_purchase_partner_photos SET image = '".$photo_name."' WHERE id = ".$insert_id);
			}
		}
		if($insert) {
			header('Location:index.php?page=purchase_partner_photos&id='.$partner_id);
			exit();
		}
	}
}



if(isset($_GET['action']) && $_GET['action'] == 'delete') {
	/* Delete User */
	if(isset($_GET['del_id']) && $_GET['del_id']!=''){
		$delete = mysqli_query($con,'DELETE FROM tbl_purchase_partner_photos WHERE id='.$_GET['del_id']);
		if($delete) {
			$success = 'Image Deleted';
			?>
	        <script language="javascript" type="text/javascript">			
				window.location='index.php?page=<?php echo $page;?>&success=<?php echo $success; ?>&id='.$_REQUEST['id'];
			</script>
	        <?php
		}
	}
}

$pageVars = '';

// check photo count
$check = mysqli_query($con, 'SELECT id FROM tbl_purchase_partner_photos WHERE partner_id='.$partner_id);

$select = "SELECT * FROM tbl_purchase_partner_photos WHERE partner_id=".$partner_id." ORDER BY id DESC";
//echo $select;
// Using pagination class
$pagination = new pagination_noseo($select);
$execDetails = $pagination->createPages(10);

$pageLnk = 'index.php?page='.$page.'&'.$pageVars;
$pageNums = $pagination->pageNumber(5, $pageLnk);
?>