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

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


//1 - TOPIC AND CONTENT
//2 - ASSESSMENT
//3 - SURVEY
//4 - BULLETIN
//5 - GENERAL
//6 - REWARD POINT
//7 - WALLET   

class notification
{
                    
	function __construct($parent)
	{
		//GET PAGE MESSAGE
		$this->page_data=globalfunc::getMessage($parent,$parent->lang,'notification');	
	
		if(count($parent->params)==0)
		{			
			$this->notificationFunc($parent);
		}else
		{
			$qry_params=explode ("?",$parent->params[0]);
			$parameter_val=strtolower($qry_params[0]);
			
			if(strtolower($parent->device_type)=='app')
			{
				
				switch($parameter_val){
							 
					case "read_notification":
					case "remove_notification":
					//case "group":				
						$this->$parameter_val($parent );
					break;
					default:
						//$this->notificationFunc($parent);
					break;
					 
					
				}	
			}else if(strtolower($parent->device_type)=='web')
			{
				switch($parameter_val){
					
					case "read_notification":
					case "remove_notification":	 	
						$this->$parameter_val($parent );
					break;
					 
					default:
						//$this->notificationFunc($parent);
					break;
				}



			}		
		}
		$parent->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
	}

	/* /////////////////////////////// NOTIFICATION FUNCTION //////////////////////////////////////////////// */
	private function notificationFunc($parent){
	
		globalfunc::checkMethodType($parent,array('GET'));	
		globalfunc::checkParamCount($parent,0);
		$user_details=globalfunc::checkAuthrozation($parent,array('type'=>'user'));
        
        //PAGINATION SETTINGS 
        if($parent->device_type!='web')
        {
            $limit_str=globalfunc::setPageLimit($parent,$parent->funcparams[0]);
            $limit_cond=($limit_str)?" LIMIT ".$limit_str:'';		
        }else
        {
            $limit_cond='';
        }
		
        $notification_str="SELECT n.`id`, n.`notificationtitle`, n.`notificationtext`, n.`notificationdata`, n.`notificationtype`,nu.status,nu.read_date,nu.created_on FROM ".TABLE_NOTIFICATION." as n JOIN ".TABLE_NOTIFICATION_USER." as nu ON n.id=nu.notificationid WHERE n.status='1' AND nu.userid='".$parent->current_user_id."' AND nu.status!='2' ORDER BY nu.created_on DESC ".$limit_cond;
        $notification_arr=$parent->getQueryValue($notification_str);
        
        if(count($notification_arr)>0)
        {
            foreach($notification_arr as $nkey=>$res_noti)
            { 
                $notification_arr[$nkey]->notificationdata=($res_noti->notificationdata!='')?json_decode($res_noti->notificationdata):null;                    
            }
        }
        
        //TOTAL COUNT
        $get_user_noti_count=$parent->selectQuery(TABLE_NOTIFICATION." as n JOIN ".TABLE_NOTIFICATION_USER." as nu ON n.id=nu.notificationid","COUNT(n.`id`)as n_count"," nu.userid='".$parent->current_user_id."' AND nu.status!='2'","");
		$total=($get_user_noti_count['nr'])?$get_user_noti_count['result'][0]->n_count:0;
                    
        $last_page=($total && $parent->device_type!='web')?ceil($total/$parent->current_limit)-1:0;
        
         //NOT READ COUNT
        $get_user_noti_count=$parent->selectQuery(TABLE_NOTIFICATION." as n JOIN ".TABLE_NOTIFICATION_USER." as nu ON n.id=nu.notificationid","COUNT(n.`id`)as n_count"," nu.userid='".$parent->current_user_id."' AND nu.status='0'","");
		$not_read_count=($get_user_noti_count['nr'])?$get_user_noti_count['result'][0]->n_count:0;
        
        //LOG
        $logstr='NOTIFICATION LIST';			 
        globalfunc::insertUserLog($parent,$logstr); 
        $parent->response(array('response'=>'success', 'data'=>$notification_arr,'total'=>$total,'last_page'=>$last_page,'current_page'=>$parent->current_page,'message'=>$this->page_data['success1'],'not_read_count'=>$not_read_count),200);             
	}
 	/* //////////////////////////////////////////////////////////////////////////////////////////// */
    
    /* /////////////////////////////// READ NOTIFICATION //////////////////////////////////////////////// */
	private function read_notification($parent){
	
        globalfunc::checkMethodType($parent,array('GET','POST'));
        if($parent->get_request_method()=='POST')
		{
           	
            globalfunc::checkParamCount($parent,1);
            globalfunc::checkAuthrozation($parent,array('type'=>'user'));
           
            $REQUEST_KEYS=array('notificationid'=>'string');
            globalfunc::mandatoryKey($parent,$REQUEST_KEYS);
            $notificationid=escape_str($parent->_request['notificationid']);
            
            $notificationid_arr=@explode(',',$notificationid);
            $notificationid=(count($notificationid_arr)>0)? @implode(',',$notificationid_arr):"''";
            
        }else if($parent->get_request_method()=='GET')
        {                    
            globalfunc::checkParamCount($parent,2);
            globalfunc::checkAuthrozation($parent,array('type'=>'user'));
            $notificationid = escape_str($parent->params[1]);
        }
        
        //FOR MULIPLE
        $checkcond=(strpos($notificationid,',') !== false)?" AND n.`id` IN (".$notificationid.") ":" AND n.`id`='".$notificationid."'";
        $cond=(strpos($notificationid,',') !== false)?" AND notificationid IN (".$notificationid.") ":" AND notificationid='".$notificationid."'";
        
        $notification_str="SELECT n.`id`, n.`notificationtitle`, n.`notificationtext`, n.`notificationdata`, n.`notificationtype`,  nu.status,nu.read_date FROM ".TABLE_NOTIFICATION." as n JOIN ".TABLE_NOTIFICATION_USER." as nu ON n.id=nu.notificationid WHERE nu.userid='".$parent->current_user_id."' AND nu.status!='2' ".$checkcond." ORDER BY  n.`id` ASC ".$limit_cond;
        $notification_arr=$parent->getQueryValue($notification_str);
        
        //if(!count($notification_arr)>0)  $parent->response(array('response'=>'failed','message'=>$this->page_data["error1"]),406); 
        //STATUS CHANGE
        $parent->executeQuery(" UPDATE ".TABLE_NOTIFICATION_USER." SET  status='1' WHERE userid='".$parent->current_user_id."' ".$cond); 
        
        //LOG
        $logstr='READ NOTIFICATION';			 
        globalfunc::insertUserLog($parent,$logstr); 
        $parent->response(array('response'=>'success','message'=>$this->page_data['success1']),200); 
                    
	}
 	/* //////////////////////////////////////////////////////////////////////////////////////////// */
        
    /* /////////////////////////////// REMOVE NOTIFICATION //////////////////////////////////////////////// */
	private function remove_notification($parent){
	
		globalfunc::checkMethodType($parent,array('GET','POST'));
        if($parent->get_request_method()=='POST')
		{
           	
            globalfunc::checkParamCount($parent,1);
            globalfunc::checkAuthrozation($parent,array('type'=>'user'));
            
            $REQUEST_KEYS=array('notificationid'=>'string');
            globalfunc::mandatoryKey($parent,$REQUEST_KEYS);
            $notificationid=escape_str($parent->_request['notificationid']);
            
            $notificationid_arr=@explode(',',$notificationid);
            $notificationid=(count($notificationid_arr)>0)? @implode(',',$notificationid_arr):"''";
            
            
            
        }else if($parent->get_request_method()=='GET')
        {                    
            globalfunc::checkParamCount($parent,2);
            globalfunc::checkAuthrozation($parent,array('type'=>'user'));
            $notificationid = escape_str($parent->params[1]);
        }
        
        //FOR MULIPLE
        $checkcond=(strpos($notificationid,',') !== false)?" AND n.`id` IN (".$notificationid.") ":" AND n.`id`='".$notificationid."'";
        $cond=(strpos($notificationid,',') !== false)?" AND notificationid IN (".$notificationid.") ":" AND notificationid='".$notificationid."'";
        
        $notification_str="SELECT n.`id`, n.`notificationtitle`, n.`notificationtext`, n.`notificationdata`, n.`notificationtype`, nu.status,nu.read_date FROM ".TABLE_NOTIFICATION." as n JOIN ".TABLE_NOTIFICATION_USER." as nu ON n.id=nu.notificationid WHERE nu.userid='".$parent->current_user_id."' AND nu.status!='2' ".$checkcond." ORDER BY  n.`id` ASC ".$limit_cond;
        $notification_arr=$parent->getQueryValue($notification_str);
        
        //if(!count($notification_arr)>0)  $parent->response(array('response'=>'failed','message'=>$this->page_data["error1"]),406);        
        //STATUS CHANGE                    
        $parent->executeQuery(" UPDATE ".TABLE_NOTIFICATION_USER." SET status='2' WHERE userid='".$parent->current_user_id."' ".$cond); 
        
        //LOG
        $logstr='REMOVE NOTIFICATION';			 
        globalfunc::insertUserLog($parent,$logstr);
        $parent->response(array('response'=>'success','message'=>$this->page_data['success1']),200); 
                    
	}
 	/* //////////////////////////////////////////////////////////////////////////////////////////// */
	
}

?>