<?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');
	}

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

        $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 gallery_tb.user_id = '".$userdata['id']."' and gallery_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 | 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://d1z6a3vjnfowyr.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('sgallery', $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://d1z6a3vjnfowyr.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://d1z6a3vjnfowyr.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://d1z6a3vjnfowyr.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://d1z6a3vjnfowyr.cloudfront.net/".$file_data["process_file_url"]);
		
		force_download($name, $data);

        $data['title'] = 'Gallery | MAKEABL';
		$data['userdata'] = $userdata;
		$data['site_logo'] = 'http://d1z6a3vjnfowyr.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 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 gallery_tb.user_id = '".$userdata['id']."' and gallery_tb.site_id  = '".$site_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));
	}
}
