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

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

class bulletin
{
                    
	//FUNCTION LISTS
	function __construct($parent)
	{
		//GET PAGE MESSAGE
		$this->page_data=globalfunc::getMessage($parent,$parent->lang,'bulletin');
		if(count($parent->params)==0)
		{
            $this->bulletinfunc($parent);
		}else
		{
			$qry_params=explode ("?",$parent->params[0]);
			$parameter_val=strtolower($qry_params[0]);
            switch($parameter_val){

                case "bulletin_status":  
                case "bulletin_viewed":    

                    $this->$parameter_val($parent );
                break;

                default:
                     $parent->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
                break;
            }	
                    
		} 
		
		$parent->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
	}
	
	/* /////////////////////////////// BULLETIN LIST //////////////////////////////////////////////// */
	private function bulletinfunc($parent)
	{	
                    
		globalfunc::checkMethodType($parent,array('GET'));		 
		globalfunc::checkParamCount($parent,0);
        globalfunc::checkAuthrozation($parent,array('type'=>'user'));	
                    
        //PAGINATION SETTINGS
        $limit_cond='';
		$data_arr=array();  
		$limit_str=globalfunc::setPageLimit($parent,$parent->funcparams[0]);
		$limit_cond=($limit_str)?" LIMIT ".$limit_str:'';		
		//$data=@explode('?',$parent->params[0]);
                    
        
        //USER FILTER CONDITION APPLY            
        $user_cond='';
        //COMMAN USER FILTER
        $user_filter_str=projectfunc::getUserfiltervalue($parent,array('role','zone','city','dealer_code')); 
        $user_cond.=$user_filter_str;
        
        //NEW ROLE CONDITION
        //$user_cond.=($parent->current_user_role>7 && $parent->current_user_role<14)?" AND user_type=0 ":" AND user_type=1 ";
                    
        //NEW SEGMENT CONDITION
        if($parent->current_user_role>7 && $parent->current_user_role<14)
        {            
            $user_cond.=" AND (segment='0' OR segment='".$parent->current_user_data->segment."')";
        }
        $get_bulletin_arr=$parent->selectQuery(TABLE_BULLETIN ,"*"," status='1' AND is_deleted='0' ".$user_cond." ORDER BY published_date DESC ".$limit_cond,"");
		
		//$parent->executeQuery("INSERT INTO `".TABLE_QUERY_LOG."`(  `query_str`, `query_type`, `created_on`) VALUES ('". escape_str("status='1' AND is_deleted='0' ".$user_cond." ORDER BY published_date DESC")."', 'assessment', '".NOW."')");
		
        if($get_bulletin_arr['nr'])
		{
			//CONNECT S3
			$s3=awsfileserver::connectS3();			
			foreach($get_bulletin_arr['result'] as $res)
			{
                $res->image_url=BULLETIN_NOIMAGE_URL;
				if($res->image)
				{
                    $res->image_url=awsfileserver::getS3FileUrl($s3,S3_BUCKET,S3_BULLETIN_URL.$res->image); 
				}
                $data_arr[]=$res;
            }
        }
        
        //GET COUNT
        $get_bulletin_count=$parent->selectQuery(TABLE_BULLETIN,"COUNT(bulletin_id)as bulletin_count"," status='1' AND is_deleted='0' ".$user_cond,"1");
		$total=($get_bulletin_count['nr'])?$get_bulletin_count['result'][0]->bulletin_count:0;
		
        //LOG        
        $logstr='BULLETIN';			 
        globalfunc::insertUserLog($parent,$logstr);
        
		$last_page=($total)?ceil($total/$parent->current_limit)-1:0;
		$parent->response(array('response'=>'success', 'data'=>$data_arr,'total'=>$total,'last_page'=>$last_page,'current_page'=>$parent->current_page,'message'=>$this->page_data['success1']),200);
	}
    /* //////////////////////////////////////////////////////////////////////////////////////////// */
    
    /* /////////////////////////////// BULLETIN VIEW //////////////////////////////////////////////// */
	private function bulletin_viewed($parent)
	{
        globalfunc::checkMethodType($parent,array('GET'));		 
		globalfunc::checkParamCount($parent,2);
        globalfunc::checkAuthrozation($parent,array('type'=>'user'));	
        $bulletin_id = escape_str($parent->params['1']);	           
                    
        $user_cond='';
        //COMMAN USER FILTER
        $user_filter_str=projectfunc::getUserfiltervalue($parent,array('role','zone','city','dealer_code')); 
        $user_cond.=$user_filter_str;
        //NEW SEGMENT CONDITION
        if($parent->current_user_role>7 && $parent->current_user_role<14)
        {            
            $user_cond.=" AND (segment='0' OR segment='".$parent->current_user_data->segment."')";
        }
        $get_bulletin_arr=$parent->selectQuery(TABLE_BULLETIN ,"*"," status='1' AND is_deleted='0' AND bulletin_id='".$bulletin_id."' ".$user_cond." ","1");
        if($get_bulletin_arr['nr'])
		{
            $insert_str="INSERT INTO ".TABLE_BULLETINSTATUS." (`bulletin_id`, `userid`, `created_on`, `modified_on`) VALUES ('".$bulletin_id."','".$parent->current_user_id."','".NOW."','".NOW."') ";   
            $parent->executeQuery($insert_str);
            
        }else
        {
            $parent->response(array('response'=>'failed', 'message'=>$this->page_data['error2']),406);
        }
        
        //LOG        
        $logstr='BULLETIN VIEW';			 
        globalfunc::insertUserLog($parent,$logstr,$bulletin_id);
                    
		$parent->response(array('response'=>'success',  'message'=>$this->page_data['success1']),200);
    }                
    /* //////////////////////////////////////////////////////////////////////////////////////////// */
    
    /* /////////////////////////////// BULLETIN LIST //////////////////////////////////////////////// */
	private function bulletin_status($parent)
	{
        globalfunc::checkMethodType($parent,array('GET'));		 
		globalfunc::checkParamCount($parent,2);
        globalfunc::checkAuthrozation($parent,array('type'=>'user'));	
        $bulletin_id = escape_str($parent->params['1']);	           
                    
        $user_cond='';
        //COMMAN USER FILTER
        $user_filter_str=projectfunc::getUserfiltervalue($parent,array('role','zone','city','dealer_code')); 
        $user_cond.=$user_filter_str;
        //NEW SEGMENT CONDITION
        if($parent->current_user_role>7 && $parent->current_user_role<14)
        {            
            $user_cond.=" AND (segment='0' OR segment='".$parent->current_user_data->segment."')";
        }
        $get_bulletin_arr=$parent->selectQuery(TABLE_BULLETIN ,"*"," status='1' AND is_deleted='0' AND bulletin_id='".$bulletin_id."' ".$user_cond." ","1");
        if($get_bulletin_arr['nr'])
		{
			//CONNECT S3
			$s3=awsfileserver::connectS3();			
			foreach($get_bulletin_arr['result'] as $res)
			{
                $res->image_url=BULLETIN_NOIMAGE_URL;
				if($res->image)
				{
                    $res->image_url=awsfileserver::getS3FileUrl($s3,S3_BUCKET,S3_BULLETIN_URL.$res->image); 
				}
                $data_arr[]=$res;
            }
        }else
        {
             $parent->response(array('response'=>'failed', 'message'=>$this->page_data['error2']),406);
        }
        
        //LOG        
        $logstr='BULLETIN STATUS';			 
        globalfunc::insertUserLog($parent,$logstr,$bulletin_id);
                    
		$parent->response(array('response'=>'success', 'data'=>$data_arr[0],'message'=>$this->page_data['success1']),200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */
}

?>