<?php
/****************************
 * File : banner.php
 * Author : Cygnusa
 * @2018
****************************/

//SECURITY 
if ( ! defined('APPLICATION_PATH')) exit('No direct script access allowed');

class banner
{
	private $banner_col=" `banner_id`, `title`, `image`, `published_date`, `expired_date`";
	private $banner_path="../../hypermupload/banner/";
	
	//FUNCTION LISTS
	function __construct($parent)
	{
		//GET PAGE MESSAGE
		$this->page_data=globalfunc::getMessage($parent,$parent->lang,'banner');
		if(count($parent->params)==0)
		{	
	
			$this->bannerfunc($parent);
		}
       /* else
		{	
			$qry_params=explode ("?",$parent->params[0]);
			$parameter_val=strtolower($qry_params[0]);
			
            if(strtolower($parent->device_type)=='app')
			{
                switch($parameter_val){
                    
                    case "user":
                       $this->bannerfunc($parent);
                    break;
                    
//                    default:
//                        $this->bannerfunc($parent);
//                    break;
                }
            }else if(strtolower($parent->device_type)=='web')
			{	
                switch($parameter_val){
                    
                    case "user":
                         $this->bannerfunc($parent);
                    break;
                    
//                    default:
//                        $this->bannerfunc($parent);
//                    break;
                }	
            }	
		}*/
		
		$parent->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
	}
	
	//FUNCTION
	function bannerfunc($parent)
	{	
		switch($parent->get_request_method())
		{
			case "GET"	: $this->bannerList($parent);  break;
			//case "POST"	: if($parent->device_type=='web'){$this->addBanner($parent);} break;
			//case "DELETE":  if($parent->device_type=='web'){$this->removeBanner($parent);}break;
			default:
				$parent->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
			break;
		}
	}

	/* /////////////////////////////// BANNER LIST //////////////////////////////////////////////// */
	private function  bannerList($parent)
	{
        //BANNER LIST
		globalfunc::checkMethodType($parent,array('GET'));
		globalfunc::checkParamCount($parent,0);
		globalfunc::checkAuthrozation($parent,array('type'=>'user')); 
							
        //DEVICE BASED COND    
        $econd=($parent->device_type=='app')?" AND banner_type=0 ":" AND banner_type=1 ";		
        
        //COMMAN USER FILTER            
        $user_filter_str=projectfunc::getUserfiltervalue($parent,array('role','zone','city','dealer_code')); 
        $user_cond.=$user_filter_str;            
            
        
		$query_banners=$parent->getQueryValue("SELECT  ".$this->banner_col." FROM ".TABLE_BANNERS."  WHERE published_date<='".CUR_DATE."' AND is_deleted=0 ".$econd.$user_cond." ORDER BY banner_id ASC");			
		if(count($query_banners)>0)
		{
			//CONNECT S3
			$s3=awsfileserver::connectS3();
			foreach($query_banners as $key=>$single)
			{
				if($single->image!='')
				{
					//$url=awsfileserver::getS3FileUrl($s3,S3_BUCKET,S3_BANNER_IMAGE_URL.$single->image);     
 
                    $url=SITE_URL."hypermupload/banner/".$single->image."?t=".TIMETOKEN;
					$query_banners[$key]->image=$url;
				}				
			}
		}
        
        //LOG
        $logstr='BANNER';			 
        globalfunc::insertUserLog($parent,$logstr);

		//TOTAL COUNT
		$query_bannerscount=$parent->getQueryValue("SELECT count(banner_id) as bannercount FROM ".TABLE_BANNERS."  WHERE is_deleted=0".$econd." ");
		$parent->response(array('response'=>'success','data'=>$query_banners,'total'=>$query_bannerscount[0]->bannercount,'message'=>$this->page_data['success1']),200);
		
	} 
	/* //////////////////////////////////////////////////////////////////////////////////////////// */
            
}

?>