<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class GalleryController extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->library('Paypal_lib');
		$this->load->model('GuestModel');
		$this->load->model('SiteModel');
		$this->load->model('PackageModel');
		$this->load->model('PrepackageModel');
		$this->load->model('GalleryModel');
		$this->load->model('BackgroundModel');
		$this->load->model('AuthModel');
		$this->load->model('NotificationModel');
		$this->load->helper('download');

		$this->load->model('SiteDataModel');
	}

	// public function downloadZipFile()
	// {
	// 	$userdata = $this->session->userdata('makeabl2020');

	// 	$ids = explode(',', $_GET['ids']);
	// 	foreach($ids as $mainid)
	// 	{
	// 		if(!empty($mainid))
	// 		{
	// 			$ids = explode('-', $mainid);
	// 			$id = $ids[0];
	// 			$count = $ids[1];
	// 			$type = $ids[2];

	// 			if($type == 1)
	// 			{
	// 				$this->SiteDataModel->insert_row($userdata["id"], 'Download', 'Photo');		
	// 			}
	// 			else
	// 			{
	// 				$this->SiteDataModel->insert_row($userdata["id"], 'Download', 'Video');		
	// 			}

	// 			$album_info = $this->GalleryModel->get_album_row($id);
	// 			if($album_info['isImage'] == 1)
	// 			{
	// 				if($count > 0)
	// 				{
	// 				    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
	// 				}
	// 				else
	// 				{
	// 				    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
	// 				}	
	// 			}
	// 			else
	// 			{
	// 				if($count == 0)
	// 				{
	// 				    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
	// 				}
	// 				else
	// 				{
	// 				    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
	// 				}		
	// 			}
				
	// 			$this->load->library('zip');

	// 			$array = explode("/", $downloadURL);


	// 			$file_name = end($array);
    
    //         	$host = $downloadURL;
    //             $ch = curl_init();
    //             curl_setopt($ch, CURLOPT_URL, $host);
    //             curl_setopt($ch, CURLOPT_VERBOSE, 1);
    //             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //             curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    //             curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
    //             curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    //             curl_setopt($ch, CURLOPT_HEADER, 0);
    //             $result = curl_exec($ch);
    //             curl_close($ch);
        
    //     		file_put_contents('assets/download/img/'.$file_name, $result);
        		
	// 			// file_put_contents('assets/download/img/'.$file_name, file_get_contents($downloadURL));

	// 			$this->zip->read_file('assets/download/img/'.$file_name);
	// 		}			
	// 	}

	// 	$zipname = 'my_photo.zip';
	// 	$this->zip->archive($zipname); 
	// 	$this->zip->download($zipname);
	// }


	public function downloadSharePhotoVideo()
	{
		$userdata = $this->session->userdata('makeabl2020');
		$images = explode(",", $_POST['images']);
		foreach ($images as $value) 
		{
			if(!empty($value))
			{
				$ids = explode('-', $value);
				$id = $ids[0];
				$clicks = $ids[1];
				$description = $ids[2];

				$this->SiteDataModel->insert_row($userdata["id"], $clicks, $description);	
			}
		}

		$json['success'] = true;
		echo json_encode($json);
	}

	public function downloadShareFile()
	{
		$arrayShare = array();
		$userdata = $this->session->userdata('makeabl2020');

		$ids = explode(',', $_POST['ids']);
		foreach($ids as $mainid)
		{
			if(!empty($mainid))
			{
				$ids = explode('-', $mainid);
				$id = $ids[0];
				$count = $ids[1];
				$type = $ids[2];

				if($type == 1)
				{
					$this->SiteDataModel->insert_row($userdata["id"], 'Share', 'Photo');		
				}
				else
				{
					$this->SiteDataModel->insert_row($userdata["id"], 'Share', 'Video');		
				}
				

				$album_info = $this->GalleryModel->get_album_row($id);
				if($album_info['isImage'] == 1)
				{
					if($count > 0)
					{
					    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
					}
					else
					{
					    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
					}	
				}
				else
				{
					if($count == 0)
					{
					    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
					}
					else
					{
					    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
					}		
				}
				
			
				$array = explode("/", $downloadURL);

				$file_name = end($array);

				// file_put_contents('assets/download/img/'.$file_name, file_get_contents($downloadURL));
				
				
				$host = $downloadURL;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $host);
                curl_setopt($ch, CURLOPT_VERBOSE, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_AUTOREFERER, false);
                curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
                curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                $result = curl_exec($ch);
                curl_close($ch);
        
        		file_put_contents('assets/download/img/'.$file_name, $result);
        		

				$dataGet['name'] = 'assets/download/img/'.$file_name;
				$dataGet['type'] = $type;
				$arrayShare[] = $dataGet;				
			}			
		}


		$data['success'] = $arrayShare;
		$json = json_encode($data);
		echo $json;
	}

	public function downloadShareFileSingle()
	{
		$arrayShare = array();
		$userdata = $this->session->userdata('makeabl2020');
		$downloadURL = $_POST['dataID'];

		$array = explode("/", $downloadURL);

		$file_name = end($array);
		
	
        $host = $downloadURL;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);

		file_put_contents('assets/download/img/'.$file_name, $result);
		
// 		file_put_contents('assets/download/img/'.$file_name, file_get_contents($downloadURL));

		$dataGet['name'] = 'assets/download/img/'.$file_name;
		$dataGet['type'] = $_POST['type'];
		$arrayShare[] = $dataGet;			

		$data['success'] = $arrayShare;
		$json = json_encode($data);
		echo $json;
	}


	public function downloadShareFileSingleVideo()
	{
		$arrayShare = array();
		$userdata = $this->session->userdata('makeabl2020');
		$downloadURL = $_POST['dataID'];

		$array = explode("/", $downloadURL);

		$file_name = end($array);
    
    
    	$host = $downloadURL;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);

		file_put_contents('assets/download/img/'.$file_name, $result);
        		
// 		file_put_contents('assets/download/img/'.$file_name, file_get_contents($downloadURL));

		

		$arrayShare[] = 'assets/download/img/'.$file_name;	

		$data['success'] = $arrayShare;
		$json = json_encode($data);
		echo $json;
	}


	




	public function index()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		

        // temp solution
        // $userdata['site_id'] = 19;

        $site_row = $this->SiteModel->get_row($userdata['site_id']);

		// $activate_status = $this->PackageModel->get_package_rows(" where package_history_tb.id > 0 and package_history_tb.isDeleted = 0 and package_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$userdata['site_id']."' and package_history_tb.s_from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and package_history_tb.s_to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) and (package_payments_tb.refund_status = 0 or package_payments_tb.refund_status = 2) order by package_history_tb.id desc");
		// $preactivate_status = $this->PrepackageModel->get_prepackage_rows(" where prepackage_history_tb.id > 0 and prepackage_history_tb.isDeleted = 0 and prepackage_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$userdata['site_id']."' and prepackage_tb.s_from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and prepackage_tb.s_to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) order by prepackage_history_tb.id desc");
		

        // if(count($activate_status) > 0 || count($preactivate_status) > 0){
			// $album_list = $this->GalleryModel->get_album_rows(" where album_tb.id > 0 and album_tb.isDeleted = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id = '".$userdata['site_id']."' order by album_tb.id desc");

// 			if($album_list){
// 				foreach($album_list as $key => $val){
// 					if($val['staff_file_url'] && $val['staff_comp_file_url']){
// 						$update_data = array(
// 							'process_file_url' 		=> $val['staff_file_url'],
// 							'process_comp_file_url' => $val['staff_comp_file_url'],
// 							'staff_file_url' 		=> '',
// 							'staff_comp_file_url' 	=> ''
// 						);
// 						$this->GalleryModel->update_album_row($update_data, array('id' => $val['id']));
// 					}
// 				}
// 			}

		// }


		
		$where = " where id > 0 and isDeleted = 0 and user_id = '".$userdata['id']."' and site_id = '".$userdata['site_id']."' and location_id <> 0";
		
		$data['title'] = 'Gallery | GL Clix';
		$data['userdata'] = $userdata;

		$guest_detail = $this->GuestModel->get_guest_row($userdata['id']);
		$data['guest_detail'] = $guest_detail;
		// $data['gallery_count'] = $this->GalleryModel->get_count($where);
		// $data['gallery_list'] = $this->GalleryModel->get_rows($where." order by id desc");


		$data['album_list'] = $this->GalleryModel->get_album_rows(" where album_tb.isDeleted = 0 and album_tb.is_raw_image = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id = '".$userdata['site_id']."' order by album_tb.id desc");
		
		// $data['kiosk_list'] = $this->GalleryModel->get_kiosk_rows(" where gallery_tb.user_id = '".$userdata['id']."' and gallery_tb.site_id = '".$userdata['site_id']."' and orig_file_url LIKE '%kiosk_image%' ORDER by gallery_tb.id desc");

    	$aa = explode('/',$userdata['qr_token']);

		$token = rtrim($aa[4],'"');  
		
// 		$data['paidusercount'] = $this->GalleryModel->get_paid_user_count(" where pt.price > 0 and gt.id = '" . $userdata['id'] . "' ");

		
		$data['paidusercount'] = $this->GalleryModel->get_paid_user_count("where gt.id = '" . $userdata['id'] . "' " , $token);
	    
	   // print_r($data['paidusercount']);
	   // die();
		
		// $data['album_count'] = $this->GalleryModel->get_album_count(" where album_tb.id > 0 and album_tb.isDeleted = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id = '".$userdata['site_id']."'");

		// $data['video_list'] = $this->BackgroundModel->get_video_rows(" where background_tb.id > 0 and background_tb.isDeleted = 0 and type_id = 1 and background_tb.site_id = '".$userdata['site_id']."' order by background_tb.id desc");

		// $data['video_count'] = $this->BackgroundModel->get_count(" where background_tb.id > 0 and background_tb.isDeleted = 0 and location_tb.type_id = 1 and background_tb.site_id = '".$userdata['site_id']."'");

		// $data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");

		// $data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");

		$data['site_id'] = $userdata['site_id'];
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];

		$data['header_title'] = 'Album';
        
        // $this->SiteDataModel->insert_row();
    
    
        // $getGuest = $this->db->where('id', $userdata['id'])->get('guest_tb')->row();
        $totalFaceUpload = 0;
        if(!empty($guest_detail['photo_url']))
        {
            $totalFaceUpload = 1;
        }

        $sqlExtraFace   = "select * from extra_faces_tb where guest_id = '" . $userdata['id'] . "'";
        $getExtraFacet = $this->db->query($sqlExtraFace);
        $getExtraFacetotal = $getExtraFacet->num_rows();

		$data['totalFaceUpload'] = $getExtraFacetotal + $totalFaceUpload;
		

    	$this->load->helper('url');
		$this->load->library('user_agent');

		$data['browserInfo'] = $this->agent->browser();
        
		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('sgallery', $data);
		$this->load->view('template/footer', $data);
	}
	
	public function video()
    {
        $userdata = $this->session->userdata('makeabl2020');
        
        if(empty($userdata) || !isset($userdata["id"]))
        {
            redirect('login', 'refresh');
        }
        $data['title']      = 'Video | MAKEABL';
        $site_row = $this->SiteModel->get_row($userdata['site_id']);
        $data['site_logo']  = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
        
        $data['album_count'] = $this->GalleryModel->get_album_count(" where album_tb.id > 0 and album_tb.isDeleted = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id = '".$userdata['site_id']."'");
        $aa = explode('/',$userdata['qr_token']);
		$token = rtrim($aa[4],'"');
        $data['paidusercount'] = $this->GalleryModel->get_paid_user_count(" where gt.id = '" . $userdata['id'] . "' ",$token);
        
        $data['paiduserdata'] = $this->GalleryModel->get_paid_user_data(" where gt.id = '" . $userdata['id'] . "' ");
       
        $data['site_id'] = $userdata['site_id'];
        $data['user_id'] = $userdata['id'];
        
        
        $this->load->view('template/header', $data);
        $this->load->view('template/sidebar', $data);
        $this->load->view('video', $data);
        $this->load->view('template/footer', $data);
    }

	public function mgallery()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');

        $site_row = $this->SiteModel->get_row($userdata['site_id']);
		$where = " where id > 0 and isDeleted = 0 and user_id = '".$userdata['id']."' and site_id = '".$userdata['site_id']."'";
		
		$data['title'] = 'Gallery | MAKEABL';
		$data['userdata'] = $userdata;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['gallery_count'] = $this->GalleryModel->get_count($where);
		$data['gallery_list'] = $this->GalleryModel->get_rows($where." order by id desc");
		$data['album_list'] = $this->GalleryModel->get_album_rows(" where album_tb.id > 0 and album_tb.isDeleted = 0 and gallery_tb.user_id = '".$userdata['id']."' and gallery_tb.site_id = '".$userdata['site_id']."' order by album_tb.id desc");
		$data['album_count'] = $this->GalleryModel->get_album_count(" where album_tb.id > 0 and album_tb.isDeleted = 0 and gallery_tb.user_id = '".$userdata['id']."' and gallery_tb.site_id = '".$userdata['site_id']."'");
		$data['video_list'] = $this->BackgroundModel->get_video_rows(" where background_tb.id > 0 and background_tb.isDeleted = 0 and type_id = 1 and background_tb.site_id = '".$userdata['site_id']."' order by background_tb.id desc");
		$data['video_count'] = $this->BackgroundModel->get_count(" where background_tb.id > 0 and background_tb.isDeleted = 0 and location_tb.type_id = 1 and background_tb.site_id = '".$userdata['site_id']."'");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
		$data['site_id'] = $userdata['site_id'];
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];

		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('mgallery', $data);
		$this->load->view('template/footer', $data);
	}

	public function gdetail()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		
		$id = $this->uri->segment(2);
		$site_id = $this->uri->segment(3);
		$site_row = $this->SiteModel->get_row($userdata['site_id']);
		
		$where = " where gallery_tb.id = '".$id."'";
		
		$getsiteResult = $this->SiteModel->get_site_row($site_id);
		
		$data['title'] = 'Gallery Detail | MAKEABL';
		$data['locationName'] = $getsiteResult['locationName'];
		$data['userdata'] = $userdata;
		$data['payg'] = $this->session->userdata('payg-gallery');
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['detail'] = $this->GalleryModel->get_detail_row($where);
		$data['activate_status'] = $this->PackageModel->get_package_rows(" where package_history_tb.id > 0 and package_history_tb.isDeleted = 0 and package_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$site_id."' and package_history_tb.s_from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and package_history_tb.s_to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) and (package_payments_tb.refund_status = 0 or package_payments_tb.refund_status = 2) order by package_history_tb.id desc");
		$data['preactivate_status'] = $this->PrepackageModel->get_prepackage_rows(" where prepackage_history_tb.id > 0 and prepackage_history_tb.isDeleted = 0 and prepackage_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$site_id."' and prepackage_tb.s_from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and prepackage_tb.s_to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) order by prepackage_history_tb.id desc");
		if($site_row['is_single_gallery'] == 0){
		    $data['gallery_list'] = $this->GalleryModel->get_rows(" where id > 0 and isDeleted = 0 and site_id = '".$site_id."' and user_id = '".$userdata['id']."' order by id desc");
		}else{
		    $data['gallery_list'] = $this->GalleryModel->get_rows(" where id > 0 and isDeleted = 0 and site_id = '".$site_id."' and user_id = '".$userdata['id']."' and location_id <> 0 order by id desc");
		}
		$data["background_list"] = $this->GalleryModel->get_background_rows(" where location_tb.isDeleted = 0 and background_tb.site_id = '".$site_id."' order by background_tb.id desc");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];
		
		$this->load->view('template/header', $data);
		$this->load->view('template/sidebar', $data);
		$this->load->view('gallery-detail', $data);
		$this->load->view('template/footer', $data);
	}
	
	public function vdetail()
	{
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
		
		$id = $this->uri->segment(2);
		$site_id = $this->uri->segment(3);
        $site_row = $this->SiteModel->get_row($userdata['site_id']);

        $getsiteResult = $this->SiteModel->get_site_row($site_id);
        
		$data['title'] = 'Video Detail | MAKEABL';
		$data['locationName'] = $getsiteResult['locationName'];
		$data['userdata'] = $userdata;
		$data['payg'] = $this->session->userdata('payg-video');
		$data['video_id'] = $id;
		$data['guest_detail'] = $this->GuestModel->get_row($userdata['id']);
		$data['detail'] = $this->GalleryModel->get_video_detail_row($id);
		$data['gallery_list'] = $this->GalleryModel->get_rows(" where id > 0 and isDeleted = 0 and site_id = '".$site_id."' and user_id = '".$userdata['id']."' order by id desc");
		$data['activate_status'] = $this->PackageModel->get_package_rows(" where package_history_tb.id > 0 and package_history_tb.isDeleted = 0 and package_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$site_id."' and package_history_tb.from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and package_history_tb.to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) order by package_history_tb.id desc");
		$data['preactivate_status'] = $this->PrepackageModel->get_prepackage_rows(" where prepackage_history_tb.id > 0 and prepackage_history_tb.isDeleted = 0 and prepackage_history_tb.user_id = '".$userdata["id"]."' and package_tb.site_id = '".$site_id."' and prepackage_tb.s_from_date <= CAST('".date('Y-m-d H:i:s')."' AS datetime) and prepackage_tb.s_to_date >= CAST('".date('Y-m-d H:i:s')."' AS datetime) order by prepackage_history_tb.id desc");
		$data['notification_list'] = $this->NotificationModel->get_rows(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' order by id desc");
		$data['new_notification_count'] = $this->NotificationModel->get_count(" where id > 0 and isDeleted = 0 and guest_id = '".$userdata["id"]."' and isShow = 0");
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];
		
		$this->load->view('template/header', $data);		
		$this->load->view('template/sidebar', $data);
		$this->load->view('video-detail', $data);
		$this->load->view('template/footer', $data);
	}
	
	public function download(){
	    $userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
			
		$id = $this->uri->segment(3);

        $site_row = $this->SiteModel->get_row($userdata['site_id']);
		$file_data = $this->GalleryModel->get_album_row($id);
		
		$name = $file_data["process_file_url"];
		$data = file_get_contents("http://d38x82mkkml5o1.cloudfront.net/".$file_data["process_file_url"]);
		
		force_download($name, $data);

        $data['title'] = 'Gallery | MAKEABL';
		$data['userdata'] = $userdata;
		$data['site_logo'] = 'http://d38x82mkkml5o1.cloudfront.net/'.$site_row['logo'];
		$data['is_single_gallery'] = $site_row['is_single_gallery'];
		
        $this->load->view('template/header', $data);		
		$this->load->view('template/sidebar', $data);
		$this->load->view('video-detail', $data);
		$this->load->view('template/footer', $data);
	}
	
	public function get_overlay_info()
	{
		$edit_location_id = $this->input->post('edit_location_id');
		$overlay_info = $this->GalleryModel->get_background_rows(" where background_tb.location_id = '".$edit_location_id."'");
		echo json_encode($overlay_info);
	}

	public function get_site_info()
	{
		$edit_site_id = $this->input->post('edit_site_id');
		$site_info = $this->SiteModel->get_rows("and site_tb.id = '".$edit_site_id."'");
		echo json_encode($site_info);
	}
	
	public function get_album_info()
	{
		$edit_id = $this->input->post('edit_id');
		$album_info = $this->GalleryModel->get_album_row($edit_id);
		echo json_encode($album_info);
	}
	public function download_video($id,$count)
	{
		
		$album_info = $this->GalleryModel->get_album_row($id);
		if($album_info['isImage'] == 1)
		{
			if($count > 0)
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
			}
			else
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
			}	
		}
		else
		{
			if($count == 0)
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
			}
			else
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
			}		
		}
			

		$this->load->helper('download');
		$pth    =   file_get_contents($downloadURL);
		$nme    =   "sample_file.mp4";
		force_download($nme, $pth);   

		
		// force_download($downloadURL, NULL);
	
        // redirect($downloadURL);
        
//  		header("Content-type: application/x-file-to-save"); 
// 		header("Content-Disposition: attachment; filename=".basename($downloadURL));
// 		ob_end_clean();
// 		readfile($downloadURL);
		exit;
		
	}
	    
	    
    public function download_video_ajax()
	{
		$id = $_POST['edit_id'];
		$count = $_POST['paidUCount'];
		
		$album_info = $this->GalleryModel->get_album_row($id);
		if($album_info['isImage'] == 1)
		{
			if($count > 0)
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
			}
			else
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
			}	
		}
		else
		{
			if($count == 0)
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['process_file_url'];
			}
			else
			{
			    $downloadURL = "https://d38x82mkkml5o1.cloudfront.net/".$album_info['staff_file_url'];
			}		
		}
		
	
        $json['url'] = $downloadURL;

        echo json_encode($json);
	}
	
	
	public function get_order_info()
	{
		$location_id = $this->input->post('location_id');
		$order_info = $this->GalleryModel->get_order_row($location_id);
		if(count($order_info) > 0)
		    echo json_encode(array('state' => "success", 'result' => $order_info));
		else
		    echo json_encode(array('state' => "failed"));
	}

	public function favorite_func()
    { 
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');

		$where =" where album_tb.id > 0 and album_tb.isDeleted = 0 and gallery_tb.user_id = '".$userdata['id']."'";

		$edit_id = $this->input->post('edit_id', TRUE);
		$favorite_state = $this->input->post('favorite_state', TRUE);

		if($edit_id != ""){
			$update_data = array(
				'favourite_id' => $favorite_state
			);

			$this->GalleryModel->update_album_row($update_data, array('id' => $edit_id));
		}

		$album_list = $this->GalleryModel->get_album_rows($where." order by album_tb.id desc");
		echo(json_encode($album_list));
	}
	
	public function srch_album_func()
    { 
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
			
		$where = " where album_tb.id > 0 and album_tb.isDeleted = 0 and gallery_tb.user_id = '".$userdata['id']."'";
		
		$upload_type_id = $this->input->post('upload_type_id', TRUE);
		if($upload_type_id != "")
			$where .= " and album_tb.upload_type_id  = '".$upload_type_id."'";
		
		$favorite_id = $this->input->post('favorite_id', TRUE);
		if($favorite_id != "")
			$where .= " and album_tb.favourite_id  = 1";
			
        $type_id = $this->input->post('type_id', TRUE);
        if($type_id != "")
		    $where .= " and album_tb.isImage  = '".$type_id."'";

		$album_list = $this->GalleryModel->get_album_rows($where." order by id desc");
		
		echo(json_encode($album_list));
	}
	
	public function sel_album_func()
    { 
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
			
        $site_id = $this->input->post('site_id', TRUE);
		$where = " where album_tb.id > 0 and album_tb.isDeleted = 0 and album_tb.isImage = 0 and album_tb.user_id = '".$userdata['id']."' and album_tb.site_id  = '".$site_id."'";

        $album_list = $this->GalleryModel->get_album_rows($where . " order by id desc");

        $userdata          = $this->session->userdata('makeabl2020');
        $temp_visitor_id   = $userdata['id'];
        $visitor_id        = isset($temp_visitor_id) ? $temp_visitor_id : 0;
        $temp_current_page = $this->session->userdata('current_page');
        $entry_id          = $this->session->userdata('site_log_id');

        $url      = $temp_current_page;
        $site_url = $_SERVER['HTTP_HOST'];
        $token    = 'https://' . $site_url;
        $url      = str_replace($token, '', $url);


        $query    = $this->db->query('select * from site_log where requested_url LIKE "' . $url . '" AND site_log_id = "' . $entry_id . '" ');
        $query1   = $query->row();
        $entry_id = $query1->site_log_id;
        $no_of_download = $query1->create_video + 1;
        $id = $userdata['id'];
        $date = date('Y-m-d H:i:s');

        $this->db->query('UPDATE site_log SET  guest_id = '.$id.' ,create_video = ' . $no_of_download . ' , access_date = "' . $date . '"  where site_log_id = "' . $entry_id . '"');


		$album_list = $this->GalleryModel->get_album_rows($where." order by id desc");
		echo(json_encode($album_list));
	}
	
	public function cart_album_func()
    {
		$userdata = $this->session->userdata('makeabl2020');
		if (empty($userdata) || !isset($userdata["id"]))
			redirect('login', 'refresh');
	
		$album_id = $this->input->post('album_id', TRUE);
					
		if( strpos($album_id, ',') !== false ) {
            $where = " where id > 0 and isDeleted = 0 and id in ('" . str_replace(",", "','", $album_id) . "')";
        }else{
            $where = " where id > 0 and isDeleted = 0 and id = '".$album_id."'";
        }

		$gallery_list = $this->GalleryModel->get_cart_album_rows($where);
		echo(json_encode($gallery_list));
	}
	
	public function count_click()
    {
        $userdata          = $this->session->userdata('makeabl2020');
        $temp_visitor_id   = $this->input->post('guest_id');
        $visitor_id        = isset($temp_visitor_id) ? $temp_visitor_id : 0;
        $temp_current_page = $this->session->userdata('current_page');
        $entry_id          = $this->session->userdata('site_log_id');

        $url      = $temp_current_page;
        $site_url = $_SERVER['HTTP_HOST'];
        $token    = 'https://' . $site_url;
        $url      = str_replace($token, '', $url);


        $query    = $this->db->query('select * from site_log where requested_url LIKE "' . $url . '" AND site_log_id = "' . $entry_id . '" ');
        $query1   = $query->row();
        $entry_id = $query1->site_log_id;
        $no_of_download = $query1->download_click + 1;
        $id = 0;
        $date = date('Y-m-d H:i:s');
        if(!empty($userdata['id']))
        {
            $id = $userdata['id'];
            $site_id= $userdata['site_id'];
            
        }else{
            $id = $temp_visitor_id;
            $site_id= $userdata['site_id'];
        }

        $this->db->query('UPDATE site_log SET site_id = '.$site_id.', guest_id = '.$id.' ,download_click = ' . $no_of_download . ' , access_date = "' . $date . '"  where site_log_id = "' . $entry_id . '"');
    }

    public function count_video_click()
    {
        
        $userdata          = $this->session->userdata('makeabl2020');
        $temp_visitor_id   = $this->input->post('guest_id');
        $video_url = (isset($_POST['video_url']) && $_POST['video_url']!='')?$_POST['video_url']:'';
        $visitor_id        = isset($temp_visitor_id) ? $temp_visitor_id : 0;
        $temp_current_page = $this->session->userdata('current_page');
        $entry_id          = $this->session->userdata('site_log_id');
        if($video_url!=''){
            header("Content-type: video/mp4");
header("Content-Disposition:attachment;filename=\"$video_url\"");
 readfile($video_url);
        }
        $url      = $temp_current_page;
        $site_url = $_SERVER['HTTP_HOST'];
        $token    = 'https://' . $site_url;
        $url      = str_replace($token, '', $url);


        $query          = $this->db->query('select * from site_log where requested_url LIKE "' . $url . '" AND site_log_id = "' . $entry_id . '" ');
        $query1         = $query->row();
        $entry_id       = $query1->site_log_id;
        $no_of_download = $query1->video_click + 1;
        $id = 0;
        $date           = date('Y-m-d H:i:s');
        if(!empty($userdata['id']))
        {
            $id = $userdata['id'];
            $site_id= $userdata['site_id'];
            
        }else{
            $id = $temp_visitor_id;
            $site_id= $userdata['site_id'];
        }

        $this->db->query('UPDATE site_log SET site_id = '.$site_id.', guest_id = ' . $id . ' ,video_click = ' . $no_of_download . ' , access_date = "' . $date . '"  where site_log_id = "' . $entry_id . '"');
    }

    public function share_count_click()
    {
        $userdata          = $this->session->userdata('makeabl2020');
        $temp_visitor_id   = $this->input->post('guest_id');
        $visitor_id        = isset($temp_visitor_id) ? $temp_visitor_id : 0;
        $temp_current_page = $this->session->userdata('current_page');
        $entry_id          = $this->session->userdata('site_log_id');

        $url      = $temp_current_page;
        $site_url = $_SERVER['HTTP_HOST'];
        $token    = 'https://' . $site_url;
        $url      = str_replace($token, '', $url);
        $query          = $this->db->query('select * from site_log where requested_url LIKE "' . $url . '" AND site_log_id = "' . $entry_id . '" ');
        $query1         = $query->row();
        $entry_id       = $query1->site_log_id;
        $no_of_download = $query1->share_click + 1;
        $id = 0;
        if(!empty($userdata['id']))
        {
            $id = $userdata['id'];
            $site_id= $userdata['site_id'];
            
        }else{
            $id = $temp_visitor_id;
            $site_id= $userdata['site_id'];
        }
        $date           = date('Y-m-d H:i:s');

        $this->db->query('UPDATE site_log SET site_id = '.$site_id.', guest_id = '.$id.' ,share_click = ' . $no_of_download . ', access_date = "' . $date . '"  where site_log_id = "' . $entry_id . '"');
    }
}
