<?php
/**********************************************************
* Source Copyright 2014 Author                            *
* Unauthorized reproduction is not allowed                *
* Author: JPK Software Solutions Pvt. Ltd.                *
* Email: sales@visionhelpdesk.com                         *
* Owner: www.visionhelpdesk.com                           *
**********************************************************/
if(!defined('_VIS_')) die('Error: Call from undefined function');
class client {
    var $error;
   
    function client_display($operation, $and_conditions, $or_conditions, $filter='', $page=0, $sort_by='') 
    {
		global $vis_language;
        $data=parse_form_array($operation['vis_action']);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $database=new class_database();   
        if($and_conditions!='' && count($and_conditions)!=0) 
        {
            $filter="(";
            foreach($and_conditions as $key => $value) 
            {
                if(!strstr($key, '.') && $key == 'invoice_status='.'"' ) $filter.=VIS_TABLE_PREFIX.'invoice.';
                else if(!strstr($key, '.')) $filter.=VIS_TABLE_PREFIX.'client.';
               
                $filter.=$key.$value." and ";
            }
            $filter=substr($filter, 0, -4);
            
            $filter.=") ";
        }
        if($or_conditions!="" && count($or_conditions)!=0) 
        {
            $filter.="(";
            foreach($or_conditions as $key => $value) {
                if(!strstr($key, '.')) $filter.=VIS_TABLE_PREFIX.'ticket.';
                $filter.=$key.$value.' or ';
            }
            $filter=substr($filter, 0, -3);
            $filter.=") ";
        }
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $object=new class_client($database);
        $staff=new class_staff($database);
        list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);
		  list($staffgroup_info, $staff_group)=$setting_object->get_staffgroup('', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA); 
        if(isset($operation['vis_export']) && $operation['vis_export']==true){ $limit = ''; $listflag = 0;
								}else{ $limit=($page*$view_info['page_count']).','.$view_info['page_count']; $listflag = 1; }
		  
        list($client_info, $client_details)=$object->get_client($filter, $limit,$page, $sort_by, VIS_STAFF_ID, VIS_LOCATION_AREA,$listflag);   
		$domain_list=$this->get_domain_access_list();
		
		$filter=' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1';
        $client_group=$object->get_client_group($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $ticket_object=new class_ticket($database);
		$filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
		$domain_details=$ticket_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
	//	$contact_details=$object->get_client('', '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $view_info['page_count']=($client_info['total']==0)?0:ceil($client_info['total']/$view_info['page_count']);
        $database->database_disconnect();
        if($client_details!=-1) 
        {
        $client_info['client_count']=(!$client_details || $client_details==-1)?0:count($client_details);
		$client_info['page']=$page;
        }
        $view_info['view_count']=count($view_details);
        $view_info['view_title']=$view_details['TXTCLIENTS'];
        $view_info['form_name'] = $operation['vis_form'];
        if(isset($operation['vis_export']) && $operation['vis_export']==true) 
        {
              include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'export.php');
              $export=new export();
              $j=0;
              foreach($client_details as $i)
              {
                    foreach($i as $key=>$val) 
                    {
                        if($key=='first_name' ) $report_exp[$j][$key]=$val;
                        elseif($key=='last_name') $report_exp[$j][$key]=$val;
                        elseif($key=='email') $report_exp[$j][$key]=$val;
                        elseif($key=='credit') $report_exp[$j][$key]=$val;
                        elseif($key=='balance') $report_exp[$j][$key]=$val;
                        elseif($key=='domain_name') $report_exp[$j][$key]=$val;
                        elseif($key=='group_name') $report_exp[$j][$key]=$val;
                    }
                    $j++;
              }    
              return $export->export_csv($report_exp, 'Client_list');   
        }
		if((isset($operation['vis_filter_enable']) && $operation['vis_filter_enable']==1)){
			if(isset($operation['vis_check_pagination']) && $operation['vis_check_pagination']=='1'){
				$operation['view_title']=$vis_language['TXTCLIENTS']; 
			}else{
				$operation['view_title']=$vis_language['TXTFILTERRESULT'];
			}
			ob_start();
			include(VIS_PATH_TEMPLATE.'client.php');
			$client_details = ob_get_clean();
			ob_end_flush();	
			$select='';
			$select .= "<div class=\"pull-right\">";
			$select .= "<select name=\"pageing\" id=\"paging\" class=\"input-sm form-control selectpicker paging pull-right\"  onchange=\"javascript:vis_apply_filter(this.value);\">";					  
			for($i=0;$i<$view_info['page_count'];$i++)
			$select.='<option value="'.$i.'" '.(($client_info['page']==$i)?'selected="selected"':'').'>'.($i+1).'</option>';
			$select .= "</select><div class=\"text-muted pull-right label_page\">".$vis_language['TXTPAGE'].":</div>";
			echo json_encode( array('total_cnt'=>$client_info['total'], 'paging' => $select, 'list'=> $client_details, 'filter_id'=>$operation['vis_filter_enable']));
		}
        else
        {	
			include(VIS_PATH_TEMPLATE.'client.php');
        }
        return true;
    }
    function get_clients($operation) {
        $database=new class_database();
        global $vis_language;
        $filter='';
        if(isset($operation['vis_filter']) && $operation['vis_filter']!='') $filter=$operation['vis_filter'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $limit=(isset($operation['vis_limit']) && $operation['vis_limit']!='')?$operation['vis_limit']:'';
        list($client_info, $client_details)=$object->get_client($filter, $limit, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        $client_info['client_count']=(!$client_details || $client_details==-1)?0:count($client_details);
        $data=(isset($operation['vis_details_req']) && $operation['vis_details_req']==1)?array('details'=>$client_info, 'data'=> $client_details):$client_details;
        if($client_details==-1) echo $vis_language['TXTNOMATCHFOUND'];
        else echo get_xml($data, 'clients');
        return true;
    }
	function check_twitter_login(){
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=17 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);		
		define('YOUR_CONSUMER_KEY', $row3[0]['host_name']);
		define('YOUR_CONSUMER_SECRET',  $row3[0]['user_name']);
		define('YOUR_REDIRECTION_URL',$row3[0]['db_password'].'index.php?/client/twitter_login/');		
		require(VIS_PATH_PLUGINS.'twitter_login/twitter/twitteroauth.php');
		session_start();		
		$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
		// Requesting authentication tokens, the parameter is the URL we will be redirected to
		$request_token = $twitteroauth->getRequestToken(YOUR_REDIRECTION_URL);		
		// Saving them into the session		
		$_SESSION['oauth_token'] = $request_token['oauth_token'];
		$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];		
		if ($twitteroauth->http_code == 200) {	// Let's generate the URL and redirect
			$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
			header('Location: ' . $url);
		} else { // It's a bad idea to kill the script, but we've got to know when there's an error.
			die('Something wrong happened.');
		}
	}
	
	function twitter_login($operation){
		ob_start();			
		global $request_data,$operation_hnd;
	    $request_data=$operation;
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=17 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);		
		define('YOUR_CONSUMER_KEY', $row3[0]['host_name']);
		define('YOUR_CONSUMER_SECRET',  $row3[0]['user_name']);
		define('YOUR_REDIRECTION_URL',$row3[0]['db_password'].'index.php?/client/twitter_login/');		
		require(VIS_PATH_PLUGINS.'twitter_login/twitter/twitteroauth.php');
		session_start();			
		if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['oauth_token']) && !empty($_SESSION['oauth_token_secret'])) { // We've got everything we need
			$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
		// Let's request the access token
			$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
		// Save it in a session var
			$_SESSION['access_token'] = $access_token;
		// Let's get the user's info
			$user_info = $twitteroauth->get('account/verify_credentials');		
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		$request_data['vis_email'] = '';
		$request_data['vis_firstname'] = $user_info->name;
		$request_data['vis_lastname'] = '';
		$request_data['vis_username'] = $user_info->screen_name;
		$request_data['vis_module'] = 'home';
		$request_data['vis_is_approved'] = '1';
		$request_data['vis_group'] = '1';
		$request_data['vis_domain'] = VIS_DOMAIN;
		$request_data['vis_department'] = '0';
		$request_data['vis_login_as'] = '3';
		$filter1= "user_name='".$request_data['vis_username']."'";
		list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);				
		if($row_count==-1){
			$register = $object->add_client($request_data,'0','1');	
			$filter = "user_name='".$request_data['vis_username']."'";			
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);			
			$operation_hnd->is_login();		
		} else {
			$filter = "user_name='".$request_data['vis_username']."'";
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);			
			$operation_hnd->is_login();
		}			
	}
	function gmail_login($operation){
	   global $request_data,$operation_hnd;
	   $request_data=$operation;
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=18 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$google_client_id 		= $row3[0]['host_name'];
		$google_client_secret 	= $row3[0]['user_name'];
		$google_redirect_url 	= $row3[0]['db_password']; 
		$google_developer_key 	= $row3[0]['db_name'];		
		//include google api files
		include(VIS_PATH_PLUGINS.'gmail_login/src/Google_Client.php');
		include(VIS_PATH_PLUGINS.'gmail_login/src/contrib/Google_Oauth2Service.php');
		//start session
		session_start();		
		$gClient = new Google_Client();
		$gClient->setApplicationName('Login to vision.com');
		$gClient->setClientId($google_client_id);
		$gClient->setClientSecret($google_client_secret);
		$gClient->setRedirectUri($google_redirect_url);
		$gClient->setDeveloperKey($google_developer_key);		
		$google_oauthV2 = new Google_Oauth2Service($gClient);		
		//If user wish to log out, we just unset Session variable
		if (isset($_REQUEST['reset'])) {
			unset($_SESSION['token']);
			$gClient->revokeToken();
			header('Location: ' .filter_var($google_redirect_url, FILTER_SANITIZE_URL)); //redirect user back to page
		}	
		//If code is empty, redirect user to google authentication page for code.
		//Code is required to aquire Access Token from google
		//Once we have access token, assign token to session variable
		//and we can redirect user back to page and login.
		if (isset($operation['code'])) { 
			$gClient->authenticate($operation['code']);
			$_SESSION['token'] = $gClient->getAccessToken();
			  header('Location: ' .filter_var($google_redirect_url, FILTER_SANITIZE_URL));
			return;
		}
		if (isset($_SESSION['token'])) { 
			$gClient->setAccessToken($_SESSION['token']);
		}
		if ($gClient->getAccessToken()) {  //For logged in user, get details from google using access token
			  $user 				= $google_oauthV2->userinfo->get();
			  $user_id 				= $user['id'];
			  $user_name 			= filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
			  $email 				= filter_var($user['email'], FILTER_SANITIZE_EMAIL);
			  $profile_url 			= filter_var($user['link'], FILTER_VALIDATE_URL);
			  $profile_image_url 	= filter_var($user['picture'], FILTER_VALIDATE_URL);
			  $personMarkup 		= "$email<div><img src='$profile_image_url?sz=50'></div>";
			  $_SESSION['token'] 	= $gClient->getAccessToken();
		} else 	{ //For Guest user, get google login url
			$authUrl = $gClient->createAuthUrl();
		}
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		$request_data['vis_email'] = $user['email'];
		$request_data['vis_firstname'] = $user['given_name'];
		$request_data['vis_lastname'] = $user['family_name'];
		$request_data['vis_username'] =$user['email'];
		$request_data['vis_module'] = 'home';
		$request_data['vis_is_approved'] = $user['verified_email'];
		$request_data['vis_group'] = '1';
		$request_data['vis_domain'] = VIS_DOMAIN;
		$request_data['vis_department'] = '0';
		$request_data['vis_login_as'] = '2';
		$filter1= "email='".$request_data['vis_email']."'";
		list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);
		if($row_count==-1){
			$register = $object->add_client($request_data,'0','1');	
			$filter = "email='".$request_data['vis_email']."'";			
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);
			$request_data['vis_client_id'] =$user_details[1][0]['client_id'];
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
			$request_data['vis_operation'] = 'add_client';			
			$request_data['authentication']=$user_details[1][0]['authentication'];				
			$operation_hnd->check_automation($request_data);
			$operation_hnd->operation_flow($request_data, $request_data['vis_client_id'], VIS_LOCATION_AREA, $database);
			$request_data['vis_operation'] = 'gmail_login';
			unset($_SESSION['token']);
			$gClient->revokeToken();
			$operation_hnd->is_login();		
		} else {
			$filter = "email='".$request_data['vis_email']."'";
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
			$request_data['vis_operation'] = 'gmail_login';
			unset($_SESSION['token']);
			$gClient->revokeToken();
			$operation_hnd->is_login();
		}				
	}
   function fb_login($operation){		
	   global $request_data,$operation_hnd;
	   $request_data=$operation;		
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        
		$user_profile = $operation;
		if($user_profile["oauth_uid"]) {
			
			$user_fbid	= $user_profile["oauth_uid"];
			$user_email = $user_profile["email"];
			$fname = $user_profile["first_name"];
			$lname = $user_profile["last_name"];
			$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
			$database=new class_database();
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
			$object=new class_client($database);
			$request_data['vis_email'] = $user_email;
			$request_data['vis_firstname'] = $fname;
			$request_data['vis_lastname'] = $lname;
			$request_data['vis_username'] = $user_email;
			$request_data['vis_module'] = 'home';
			$request_data['vis_is_approved'] = '1';
			$request_data['vis_group'] = '1';
			$request_data['vis_domain'] = VIS_DOMAIN;
			$request_data['vis_department'] = '0';
			$request_data['vis_login_as'] = '1';
			$filter1= "email='".$request_data['vis_email']."'";
			list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);
			if($row_count==-1){
				$register = $object->add_client($request_data,'0','1');	
				$filter = "email='".$request_data['vis_email']."'";			
				$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);				
				$request_data['vis_client_id'] =$user_details[1][0]['client_id'];
				$request_data['vis_operation'] = 'add_client';					
				$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
				$request_data['authentication']=$user_details[1][0]['authentication'];					
				$operation_hnd->check_automation($request_data);
				$operation_hnd->operation_flow($request_data, $request_data['vis_client_id'], VIS_LOCATION_AREA, $database);
				$request_data['vis_operation'] = 'fb_login';			
				$operation_hnd->is_login();
			} else {
				$filter = "email='".$request_data['vis_email']."'";
				$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);
				$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
				$request_data['vis_operation'] = 'fb_login';
				$operation_hnd->is_login();
			}			
		}	
	}
	function salesforce($operation){
	   global $request_data,$operation_hnd;
	    $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
		$request_data = $operation;
        $filter='login_type=23 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$client_id 		= $row3[0]['host_name'];
		$client_secret 	= $row3[0]['user_name'];
		$redirect_url 	 = $row3[0]['db_password'];
		$url = 'https://login.salesforce.com/services/oauth2/token';
		$ch = curl_init($url);
		$post_fields ='grant_type=authorization_code&client_id='.$client_id.'&client_secret='.$client_secret.'&redirect_uri='.$redirect_url.'&code='.$operation['/client/salesforce/?code'].''; ; 
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);
		curl_close($ch);
		$json_response = json_decode($response);
		//$result = file_get_contents($json_response->id.'?access_token='.$json_response->access_token.'');
		$url = $json_response->id;
		$ch = curl_init($url);
		$post_fields ='access_token='.$json_response->access_token.''; ; 
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($ch);
		curl_close($ch);
		$json_response = json_decode($result);
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		$request_data['vis_email'] = $json_response->email;
		$request_data['vis_firstname'] = $json_response->first_name;
		$request_data['vis_lastname'] = $json_response->last_name;
		$request_data['vis_username'] =$json_response->username;
		$request_data['vis_module'] = 'home';
		$request_data['vis_is_approved'] = 1;
		$request_data['vis_group'] = '1';
		$request_data['vis_domain'] = VIS_DOMAIN;
		$request_data['vis_department'] = '0';
		$request_data['vis_login_as'] = '11';
		$filter1= "email='".$request_data['vis_email']."'";
		list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);
		if($row_count==-1){
			$register = $object->add_client($request_data,'0','1');	
			$filter = "email='".$request_data['vis_email']."'";			
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);
			$request_data['vis_client_id'] =$user_details[1][0]['client_id'];
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
			$request_data['vis_operation'] = 'add_client';			
			$request_data['authentication']=$user_details[1][0]['authentication'];				
			$operation_hnd->check_automation($request_data);
			$operation_hnd->operation_flow($request_data, $request_data['vis_client_id'], VIS_LOCATION_AREA, $database);
			$request_data['vis_operation'] = 'sf_login';
			$operation_hnd->is_login();		
		} else {
			$filter = "email='".$request_data['vis_email']."'";
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
			$request_data['vis_operation'] = 'sf_login';
			$operation_hnd->is_login();
		}				
	}
    function view_clients($operation) {
        global $vis_language;
		$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
		if(!isset($operation['vis_action']) && $operation['vis_action'] == '')
			$data = $operation;
      	else if(strstr($operation['vis_action'], '/')) 
 			$data=explode($spliter, $operation['vis_action']); 
		else
			$data = parse_form_array($operation['vis_action']) ;
		
        if($operation['vis_page'] != '')
        {
            $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        }
        else
        {
            $page = $operation['pageing']; 
        }
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
		if(isset($operation['vis_group_id']) && $operation['vis_group_id'] !='')
        {
			$and_operations=array('parent_id='=>0);
			if($operation['vis_group_id']!='')$and_operations['group_id="'] = $operation['vis_group_id'].'"';
		}
        if(isset($operation['vis_filter_enable']) && $operation['vis_filter_enable']==1)
        {
			$operation['view_title']=$vis_language['TXTFILTERRESULT'];
			$and_operations=array('parent_id='=>0);
		   if($data['vis_firstname']!='') $and_operations['first_name like "%'] = $data['vis_firstname'].'%"';
		   if($data['vis_lastname']!='') $and_operations['last_name like "%'] = $data['vis_lastname'].'%"';
		   if($data['vis_email']!='')$and_operations['email="'] = $data['vis_email'].'"';
		   if(VIS_SATELLITE_HELPDESK ==2 || VIS_SATELLITE_HELPDESK==3)
		   {if($data['vis_domain']!='')$and_operations['optionB_domain_id="'] = $data['vis_domain'].'"';}
		   else
		   {if($data['vis_domain']!='')$and_operations['domain_id="'] = $data['vis_domain'].'"';}
		   if($data['vis_group']!='')$and_operations['group_id="'] = $data['vis_group'].'"';
		   if($data['vis_invoice']!='')$and_operations['invoice_status="'] = $data['vis_invoice'].'"';
		   if($data['vis_status'] == 1) $and_operations['is_banned='] = 1;
		   else if($data['vis_status'] == 2) $and_operations['is_spam='] = 1;
		   else if($data['vis_status'] == 3) $and_operations['is_approved='] = 0;
		   else if($data['vis_status'] == 4) { $and_operations['is_approved='] = 1; $and_operations['is_banned='] = 0 ; $and_operations['is_spam='] = 0 ; }
        }
        else
        {
			if(isset($operation['vis_group_id']) && $operation['vis_group_id'] !='')
			{
				$operation['view_title']=$vis_language['TXTCLIENTS']; 
				$and_operations=array('parent_id='=>0);
				if($operation['vis_group_id']!='')$and_operations['group_id="'] = $operation['vis_group_id'].'"';
			}
			else
			{
				  $operation['view_title']=$vis_language['TXTCLIENTS']; 
				  $operation['vis_group_name'] = $data[3];
				  $operation['vis_group_id'] = $data[4];
				  $operation['vis_form'] = 'vis_client_form';
				   $and_operations=array('parent_id='=>0);
				  if($operation['vis_group_id']!='')$and_operations['group_id="'] = $operation['vis_group_id'].'"';				 
			}
        }
		return $this->client_display($operation, $and_operations, '', '', $page, $sort_by);
    }

    function get_summary($operation) {
        global $vis_language;
        if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
            $database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
            $object=new class_client($database);
            $filter=VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
            if(VIS_LOCATION_AREA==0){
                list($client_info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
                $client_details=$client_details[0];
            }else{
                $client_info=$object->get_client_parentid($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
                $client_details['parent_id']=$client_info['parent_id'];
                $client_details['client_id']=$operation['vis_client_id'];
            }
            if(!$client_details) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
            if(VIS_LOCATION_AREA==0){
                include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
                include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'billing.php');
                $ticket_object=new class_ticket($database);
                $bill_object=new class_billing($database);
                $quick_view=$ticket_object->get_filter_quick_counts('client_id='.$operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
                $invoice=$bill_object->get_invoice_counts('client_id='.$operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
                $note_details=(VIS_NOTE_ACCESS==0)?-1:$ticket_object->get_note(0, $operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
                echo get_xml(array('client'=>$client_details, 'counts'=>$quick_view, 'billing'=>$invoice, 'note'=>$note_details), 'client_summary');
            }
            else include_once(VIS_PATH_TEMPLATE.'client_summary.php');
            $database->database_disconnect();
            return true;
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCLIENDIDMISS'].'"); </script>';
            return false;
        }
    }

    function get_summary_counts($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'billing.php');
        $ticket_object=new class_ticket($database);
        $bill_object=new class_billing($database);
        $quick_view=$ticket_object->get_filter_quick_counts('client_id='.$operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $invoice=$bill_object->get_invoice_counts('client_id='.$operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $note_details=(VIS_NOTE_ACCESS==0)?-1:$ticket_object->get_note(0, $operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details['client_id']=$operation['vis_client_id'];
        $database->database_disconnect();
        include_once(VIS_PATH_TEMPLATE.'client_summary_counts.php');
        return true;
    }

    function list_clients($operation) {
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff_object=new class_staff($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        if(VIS_ADMIN_ACCESS_LEVEL!=1){ 
		$staff_access=$staff_object->get_staff('staff_id='.VIS_STAFF_ID,VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_department=$staff_access[0]['department_access'];
         if($staff_department==-2)
        {
			$filter = VIS_TABLE_PREFIX.'staff_group.group_id = '.VIS_TABLE_PREFIX.'staff.group_id and '.VIS_TABLE_PREFIX.'staff_group.group_id='.$staff_access[0]['group_id'].' and '.VIS_TABLE_PREFIX.'staff.staff_id='.VIS_STAFF_ID;
			list($staffgroup_info, $staff_group)=$setting_object->get_staffgroup_ofstaff($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			$staff_department_array=explode(',', $staffgroup_info['department_access']);
		}
		else
		{
			$staff_department_array=explode(',', $staff_department);
		}
		$dept=array();
		$domain_access=array();
		for($i=0;$i<count($staff_department_array); $i++)
		{
			$department=$object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'department.department_id='.$staff_department_array[$i], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$dept[]=$department[0];
		}
		for($i=0;$i<count($dept); $i++)
		{
			if($dept[$i]!='')
			$domain_access[]=$dept[$i]['domain_id'];
		}
		$domain_access=array_unique($domain_access);
		$domain_access=implode(',', $domain_access);
        if(!isset($operation['vis_domain']) && !is_numeric($operation['vis_domain'])) 
        $filter.=' and '.VIS_TABLE_PREFIX.'client.domain_id in ('.$domain_access.')';
	    }
        if($operation['vis_selectedclient']==1 && $operation['vis_channel_id']!=2){
					$filter1=' (additional_email="'.$operation['vis_data'].'")';
						$filter='(email="'.$operation['vis_data'].'")';
		}
		else if($operation['vis_event']==1){
					$filter1=' (additional_email like "%'.$operation['vis_data'].'%")';
					$filter='((email != "" and contact_number like "%'.$operation['vis_data'].'%") or email like "%'.$operation['vis_data'].'%")';
		}
		else{
				$filter1=' (additional_email like "%'.$operation['vis_data'].'%")';
				$filter='(contact_number like "%'.$operation['vis_data'].'%" or email like "%'.$operation['vis_data'].'%" or first_name like "%'.$operation['vis_data'].'%" or last_name like "%'.$operation['vis_data'].'%" or user_name like  "%'.$operation['vis_data'].'%")';
		}
        if(isset($operation['vis_domain_id']) && $operation['vis_domain_id']!='' && $operation['vis_domain_id']!=0)
        {
			$vis_domain =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $operation['vis_domain_id'];
			$filter .= ' and '.VIS_TABLE_PREFIX.'client.domain_id = '.$vis_domain;
			$filter1 .=' and '.VIS_TABLE_PREFIX.'client.domain_id = '.$vis_domain;
		 }
        if(isset($operation['vis_domain']) && is_numeric($operation['vis_domain'])) {
			$vis_domain =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $operation['vis_domain'];
			$filter.=' and '.VIS_TABLE_PREFIX.'client.domain_id='.$vis_domain;
			$filter1 .=' and '.VIS_TABLE_PREFIX.'client.domain_id='.$vis_domain;
		}
        if(isset($operation['vis_parent_id']) && $operation['vis_parent_id']==0){
									 $filter.=' and '.VIS_TABLE_PREFIX.'client.parent_id=0 ';
										$filter1.=' and '.VIS_TABLE_PREFIX.'client.parent_id=0 ';
											}
        if(isset($operation['vis_notmatch_id']) && is_numeric($operation['vis_notmatch_id'])){
									 $filter.=' and '.VIS_TABLE_PREFIX.'client.client_id!='.$operation['vis_notmatch_id'];
										$filter1.=' and '.VIS_TABLE_PREFIX.'client.client_id!='.$operation['vis_notmatch_id'];
								}
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
								
								 $filters='module_id=3 and title="vis_additional_emails" and domain_id='.$vis_domain;
        $setting_detailsc=$setting_object->get_settings($filters, VIS_STAFF_ID, VIS_LOCATION_AREA);
								$checkaddtionalemail_setting=($setting_detailsc!=-1)?$setting_detailsc[0]['content']:0;
								//if($operation['vis_searchclient']==1){
		if(isset($operation['vis_channel_id']) && $operation['vis_channel_id']==1 )
		$channelFilter = ' and email!=""';
		else if($operation['vis_channel_id']== 2)
		$channelFilter = ' and contact_number!=""';
		$filter = $filter.$channelFilter;
        $client=$object->get_client_list_new($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
								if($checkaddtionalemail_setting==1){
								$client_addtionalemail=$object->get_addtionalclient_list_new($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
									if($client_addtionalemail!=-1){
										if($client!=-1){
									$client=array_merge($client,$client_addtionalemail);
										}else{
										$client=$client_addtionalemail;
									}
								}
								}
							
								//}else{
								//	$client=$object->get_client_list($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
								//}
        $database->database_disconnect();
        if($client==-1){ if(VIS_LOCATION_AREA==0) echo 'No data found'; else echo '{}'; return false;}
        $count=count($client);
        $result='[';
        if($client!=-1) for($i=0;$i<$count;$i++) {
			str_replace("\'","",trim($client[$i]['first_name']));
            $email=($client[$i]['email']=='' && $client[$i]['twitter_id']!='')?$client[$i]['twitter_id']:$client[$i]['email'];
            $email=($email=='' && $client[$i]['facebook_id']!='')?$client[$i]['facebook_id']:$email;
			$domain_name=($client[$i]['domain_name']=='' && $client[$i]['email']!='')?$client[$i]['email']:$client[$i]['domain_name'];
			$domain_id=($client[$i]['domain_id']=='' && $client[$i]['email']!='')?$client[$i]['email']:$client[$i]['domain_id'];
			$contactDetails = ($client[$i]['email']!='' && ($operation['vis_channel_id']!=2 && $operation['vis_channel_id']!=19))?$email:((addslashes(trim($client[$i]['contact_number']))!='')?addslashes(trim($client[$i]['contact_number'])): $client[$i]['user_name']);
            $result.='{"id":"'.$client[$i]['client_id'].'","client_type":"'.$client[$i]['client_type'].'","contact_number":"'.addslashes(trim($client[$i]['contact_number'])).'","domain_id":"'.addslashes($domain_id).'","domain_name":"'.addslashes($domain_name).'", "email":"'.$email.'", "user_name":"'.$client[$i]['user_name'].'", "full_name":"'.str_replace("\'","'",trim($client[$i]['first_name'])).' '.str_replace("\'","'",trim($client[$i]['last_name'])).'", "contactDetails":"'.$contactDetails.'"},';
        }
        $result=substr($result, 0, -1);
        if($result=='') return false;
        $result.=']';
        if(VIS_LOCATION_AREA==0) echo get_xml($client, 'client');
        else echo $result;
        return true;
    }

    function new_client($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$setting_object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        
        if(isset($operation['vis_client_id']) && $operation['vis_client_id'] != 0){
            $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
            $client_details = $object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
            $client_details = $client_details[1];
        }
        $domain_list=$this->get_domain_access_list();
        $client_group=$object->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
        $filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
        $domain_details=$object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $location[0] = 0;
        include_once(VIS_FUNCTION_PREFIX.'ticket.php');
        $ticket=new ticket();
        $filt= ' location=0 and  is_enabled= 1 ';
        $custom_check = $setting_object->get_custom_group_list($filt, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
        if($custom_check[0]['visible_to']==2 or $custom_check[0]['visible_to']==0) 
		{
			$custom_field_var=$ticket->get_custom_fields($location, 0, 0, 0, $database);
       	}
        $database->database_disconnect();
        if(isset($operation['vis_static_area']) && $operation['vis_static_area']=='vis_client_add_modal')
			include(VIS_PATH_TEMPLATE.'client_new_modal.php');
        else
			include(VIS_PATH_TEMPLATE.'client_new.php');
        return true;
    }
    function get_client_delete($operation) {
        global $vis_language;
        $vis_static_area =''; 
        //$subaccount_info['client_id']=$operation['vis_client_id'];
        $message_info['vis_form_title']=$vis_language['TXTCLIENTDELETE'];
        $message_info['vis_message']=$vis_language['MSGCLTDELETE'];
   
      	if($operation['vis_form'] != '')
      	{
      		$vis_static_area = ",'vis_form':'".$operation['vis_form']."'";
        }
        $message_info['vis_event']="$(this).visExecution('submit_form', {'vis_module':'client','vis_operation':'delete_client','vis_client_id':".$operation['vis_client_id'].$vis_static_area.",'vis_parent_client_id':'".$operation['vis_parent_client_id']."'})";
        
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }

    function edit_client_group($operation) {
        global $vis_language;
        $spliter=(strstr($operation['vis_action'], '/'))?'/':',';
        $action=explode($spliter, $operation['vis_action']);
        if(!isset($action[4]) || trim($action[4])=='') {
            $message=$vis_language['MSGCLGRPIDNTFND'];
            return false;
        }
        if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
		$ticket_object=new class_ticket($database);
		$filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
		$domain_details=$ticket_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $group=$object->get_client_group('group_id='.$action[4], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $group=$group[0];
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'client_group_edit.php');
        return true;
	}
    }

    function update_client_group($operation) {
        global $vis_language;
        $data=parse_form_array($operation['vis_action']);
        if(!isset($data['vis_name']) || trim($data['vis_name'])=='') {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFLDTIT'].'"); </script>';
            return false;
        }
        if(!isset($data['vis_order']) || $data['vis_order']=='') {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFILDISORDER'].'"); </script>';
            return false;
        } 
        else if(!is_numeric($data['vis_order']))
        {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFILDISORDERNUM'].'"); </script>';
            return false;         
        } 
        if(isset($data['vis_group_id']) && $data['vis_group_id']!=1){
			if(trim($data['vis_email'])!='')
			{
				list($message, $flag) = check_email_validation($data['vis_email']);
				if(!$flag) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
					return false;
				} 
			}
		}
		$string=$data['vis_domain_list'];
		  if(strpos($string, ' ') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTSPACEERRORDOMAINLIST'].'"); </script>';
            return false;
            }
		   if(strpos($string, '@') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTERRORDOMAINLIST'].'"); </script>';
            return false;
            }
             if(strpos($string, '#') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTHASHERRORDOMAINLIST'].'"); </script>';
            return false;
            }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
		// ================= Newly Added code for validation of custom fields(if added) ===================
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object=new class_ticket($database);
		if(isset($data['vis_add_org'])){
			$location[0] = 12;
			$check_custom=$ticket_object->check_custom_data($data, $location[0], $data['vis_group_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
			/* if($check_custom == false){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].'"); </script>';
				return false;
			} */
			if($check_custom!=1 && $check_custom[0]==0){ 
			if(isset($check_custom[1])){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].' ('.$check_custom[1].') "); </script>';
				return false;
            }
			}
		}
		// ================================================================================================
        $ret=$object->update_client_group($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
			$activity_log=$setting_object->add_staff_activity_log("Modified organization ".$data['vis_name'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#short_summary").visExecution("load_area",{"vis_module":"client", "vis_operation":"get_orgshort_summary","vis_organization_id":"'.$data['vis_group_id'].'"}); </script>';
		/*	echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTORGANIZATION'].'",{"vis_module":"client", "vis_operation":"view_client_group"}); </script>';*/
		}
        $database->database_disconnect();
        return true;
    }

    function new_client_group($operation) {
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
		$ticket_object=new class_ticket($database);
		$filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
		$domain_details=$ticket_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		// ======== Newly Added Code Starts here =========
		include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
		$ticket=new ticket();
		$location[0] = 12;
		$filter1=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;                   
        $departments=$ticket_object->get_department($filter1, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];
		//$custom_field_var=$ticket->get_custom_fields($location, 0, $departmentid, 0, $database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
		      
        $filt= ' location=12 and  is_enabled= 1 ';
        $custom_check = $setting_object->get_custom_group_list($filt, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($custom_check[0]['visible_to']==2 or $custom_check[0]['visible_to']==0) 
		{
				$custom_field_var=$ticket->get_custom_fields($location, 0, $departmentid, 0, $database);
       	}
		// ======== Newly Added Code Ends Here ===========
		include(VIS_PATH_TEMPLATE.'client_group_new.php');
        return true;
    }

    function view_client_group($operation) {
        $database=new class_database();
        $operation['vis_current_operation']='view_client_group'; 
        $operation['vis_current_module']='client'; 
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff=new class_staff($database);
        list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);	
		$page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;         
   		$limit=($page*$view_info['page_count']).','.$view_info['page_count']; 	
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
	    if(isset($operation['vis_filter_enable']) && $operation['vis_filter_enable']==1)
        {
				$data= isset($operation['vis_action']) && $operation['vis_action']!='' ? parse_form_array($operation['vis_action']) : $operation;
				 if($data['vis_status']!='')
					$filter = VIS_TABLE_PREFIX.'client_group.is_enable='.$data['vis_status'];
				 if($data['vis_title']!='')
				 {
				 	if($filter!='')
						$filter .= ' and '.VIS_TABLE_PREFIX.'client_group.group_name like "%'.$data['vis_title'].'%"';			
					else
						$filter = VIS_TABLE_PREFIX.'client_group.group_name like "%'.$data['vis_title'].'%"';						
				 }	
		}
		$domain_list=$this->get_domain_access_list();
		if($filter=='') $filter=' (domain_id in ('.$domain_list.') or domain_id=0 ) ';
		else $filter .=' and (domain_id in ('.$domain_list.') or domain_id=0 ) ';
        list($group_info,$group)=$object->get_client_group_view($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,$limit);
		$view_info['page_count']=($group_info['total']==0)?0:ceil($group_info['total']/$view_info['page_count']);
		$group_info['count']= (!$group || $group==-1)?0:count($group);
		$group_info['view_count']=count($group);
		//$view_info['page_count']=($group_info['count']==0)?0:ceil($group_info['count']/$view_info['page_count']);
		$filter=VIS_TABLE_PREFIX.'client.parent_id=0';
		//list($client_info, $client_details)=$object->get_client($filter, '','', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object=new class_ticket($database);
   /*     for($j=0;$j<count($client_details);$j++)
		{
			if($client_id=='')
			 $client_id =$client_details[$j]['client_id'];
			else
			 $client_id = $client_id.','.$client_details[$j]['client_id'];
		}
        $filter='('.VIS_TABLE_PREFIX.'ticket.client_id IN ('.$client_id.' ) ) and is_deleted=0 and draft_id=0 and status_id!=3';
        list($ticket_info,$ticket_details)=$ticket_object->get_ticket($filter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);*/
						for($i=0; $i<count($group); $i++)
		{
		$ticketfilter=' '.VIS_TABLE_PREFIX.'ticket.ticket_id IN(select distinct ticket_id from '.VIS_TABLE_PREFIX.'ticket, '.VIS_TABLE_PREFIX.'client, '.VIS_TABLE_PREFIX.'client_group where '.VIS_TABLE_PREFIX.'client.group_id='.VIS_TABLE_PREFIX.'client_group.group_id and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_TABLE_PREFIX.'client.client_id and '.VIS_TABLE_PREFIX.'client_group.group_id='.$group[$i]['group_id'].' and '.VIS_TABLE_PREFIX.'ticket.is_deleted=0 and '.VIS_TABLE_PREFIX.'ticket.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket.status_id!=3 and '.VIS_TABLE_PREFIX.'client.parent_id=0)';
			list($ticket_info,$ticket_details)=$ticket_object->get_ticket_listing_count($ticketfilter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$group[$i]['ticketcount']=$ticket_info['total'];
			$filter=VIS_TABLE_PREFIX.'client.parent_id=0 and '.VIS_TABLE_PREFIX.'client_group.group_id='.$group[$i]['group_id'];
		$client_info=$object->get_client_count($filter, '','', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
$group[$i]['clientcount']=$client_info['total'];
			
		}		
						
        $database->database_disconnect();
        $group_info['page']=$page;	 
		if((isset($operation['vis_filter_enable']) && $operation['vis_filter_enable']==1)){
			ob_start();
			include(VIS_PATH_TEMPLATE.'client_group_list.php');
			$client_group_list = ob_get_clean();
			ob_end_flush();
			$select='';
			$select .= "<div class=\"pull-right\">";
			$select .= "<select name=\"pageing\" id=\"paging\" class=\"form-control input-sm selectpicker pull-right paging v-middle\"  onchange=\"javascript:vis_apply_filter(this.value);\">";
			for($i=0;$i<$view_info['page_count'];$i++)
				$select.='<option value="'.$i.'" '.(($page==$i)?'selected="selected"':'').'>'.($i+1).'</option>';
			$select .= "</select><div class=\"text-muted pull-right label_page\">".$vis_language['TXTPAGE'].":</div>";
			echo json_encode( array('total_cnt'=>$group_info['count'], 'paging' => $select, 'list'=> $client_group_list));
		}else include(VIS_PATH_TEMPLATE.'client_group.php');
        return true;
    }
	
    function add_client_group($operation) {
        global $vis_language;
        $data=parse_form_array($operation['vis_action']);
        if(!isset($data['vis_name']) || trim($data['vis_name'])=='') {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFLDTIT'].'"); </script>';
            return false;
        }
        if(!isset($data['vis_order']) || $data['vis_order']=='') {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFILDISORDER'].'"); </script>';
            return false;
        } 
        else if(!is_numeric($data['vis_order']))
        {
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFILDISORDERNUM'].'"); </script>';
            return false;         
        } 
        if(isset($data['vis_email']) && trim($data['vis_email'])!='')
		 {
			list($message, $flag) = check_email_validation($data['vis_email']);
			if(!$flag) {
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
				return false;
			} 
		 }
		 $string=$data['vis_domain_list'];
		  if(strpos($string, ' ') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTSPACEERRORDOMAINLIST'].'"); </script>';
            return false;
            }
		   if(strpos($string, '@') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTERRORDOMAINLIST'].'"); </script>';
            return false;
            }
             if(strpos($string, '#') !== false) {
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTHASHERRORDOMAINLIST'].'"); </script>';
            return false;
            }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
		// ================= Newly Added code for validation of custom fields(if added) ===================
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object=new class_ticket($database);
		if(isset($data['vis_add_org'])){
			$location[0] = 12;
			$check_custom=$ticket_object->check_custom_data($data, $location[0], 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
			/* if($check_custom == false){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].'"); </script>';
				return false;
			} */
			if($check_custom!=1 && $check_custom[0]==0){ 
			if(isset($check_custom[1])){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].' ('.$check_custom[1].')'.'"); </script>';
				return false;
            }
			}
		}
		// ================================================================================================
        $ret=$object->add_client_group($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
			$activity_log=$setting_object->add_staff_activity_log("Added new organization ".$data['vis_name'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTORGANIZATION'].'",{"vis_module":"client", "vis_operation":"view_client_group"}); </script>';
		}
        $database->database_disconnect();
        return true;
    }

    function add_pinup($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $ret=$object->add_pinup($operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            if(VIS_LOCATION_AREA==0) echo 'error';
            else echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
            if(VIS_LOCATION_AREA==0) echo 'success';
            else echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");</script>';
        }
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_object=new class_setting($database);
		$client_res=$object->get_client_list('client_id='.$operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
		$name=((isset($client_res[0]['first_name']) && $client_res[0]['first_name']!='') || (isset($client_res[0]['last_name']) && $client_res[0]['last_name']!=''))?$client_res[0]['first_name']." ".$client_res[0]['last_name']:"";
		$email=(isset($client_res[0]['email']) && $client_res[0]['email']!='')? "( ".$client_res[0]['email']." ) ":"";
        $activity_log=$setting_object->add_staff_activity_log("Added pinup to ".$name.$email, VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
        $database->database_disconnect();
        return true;
    }

   /* function get_client_ban($operation) {
        global $vis_language;
        //if(isset($operation['vis_remove_ban']) && $operation['vis_remove_ban'] ==1)
        //$message_info['vis_form_title']=$vis_language['TXTREMOVEBAN'];
       $message_info['vis_form_title']=$vis_language['TXTBANCLIENT'];
        $message_info['vis_message']=$vis_language['MSGSURE'];
        $message_info['vis_event']="$(this).visExecution('load_operations', {'vis_module':'client', 'vis_operation':'ban_client', 'vis_client_id':".$operation['vis_client_id'].", 'vis_remove_ban':".$operation['vis_remove_ban']."})";
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }*/

    function delete_client($operation){        
        global $vis_language, $operation_hnd;
        $database = new class_database();    
        if(!isset($operation['vis_client_id']) && isset($operation['vis_action']) && $operation['vis_action']!='')
        {
			$data = explode('/',$operation['vis_action']);
			$operation['vis_client_id'] = $data[4];
		}  
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object = new class_client($database);
        $filter='client_id='.$operation['vis_client_id'];
        $client_details = $client_object->get_client($filter,'',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_details=$client_details[1][0];
        if(IS_LOGIN==0 || ($client_details['parent_id']!=VIS_CLIENT_ID && VIS_LOCATION_AREA==1)){
            include(VIS_PATH_TEMPLATE.'header.php');
            echo '<script> $("#vis_submit_err_div").show(); $("#vis_submit_err_div").html("'.$vis_language['MSGACCSSDND'].'");</script>';
            include(VIS_PATH_TEMPLATE.'footer.php');
            return false;
        }
        $subaccount_info['client_id']=$operation['vis_client_id'];
        $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_parent_client_id'];
        $client_parent_details =$client_object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_parent_details = $client_parent_details[1];
        $filter=VIS_TABLE_PREFIX.'client.parent_id='.$operation['vis_parent_client_id'];
        $subaccount_details=$client_object->get_subaccount($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,'');
        $no_sub_client=count($subaccount_details);
        if(is_array($client_details)) $operation=array_merge($client_details, $operation);
        $current_client_res=$client_object->get_client_list('client_id='.$operation['vis_client_id'],VIS_STAFF_ID,VIS_LOCATION_AREA);
        $result = $client_object->delete_client($operation['vis_client_id'],VIS_STAFF_ID,VIS_LOCATION_AREA);
        if($result) $operation_hnd->operation_flow($operation, VIS_STAFF_ID, VIS_LOCATION_AREA, $database);
        if(VIS_LOCATION_AREA == 1)  
        {
            $operation['reg_message']=(!$result)?('<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>'):('<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>');
            if(IS_LOGIN) $this->get_subaccounts($operation);
            else $this->register($operation);       
        }   
        else
        {
            if($operation['vis_form'] == 'vis_client_form')
            {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTCLIENTS'].'",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
            }
             else if($operation['vis_form'] == 'vis_child_client_form')
            {
				if($no_sub_client==1)
				{
					echo '<script>$("li#contact").css("display","none");	</script>';
					echo '<script>$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#summary").addClass("active");$("#vis_details_list").visExecution("load_operations",{"vis_module":"client", "vis_operation":"get_summary", "vis_action":"details", "vis_client_id":'.$operation['vis_parent_client_id'].'});</script>';
			    }
			    else
			    {
			    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_details_list").visExecution("load_operations",{"vis_module":"client", "vis_operation":"get_subaccounts", "vis_client_id":"'.$client_parent_details[0]['client_id'].'"});</script>';
				}
            } 
            else if($operation['vis_form'] == 'vis_client_search_form')
            {
                 echo '<script>  $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#conform_delete_form").dialog("close");
                          $("#vis_client_search_details").visExecution("load_operations",{"vis_module":"client", "vis_operation":"client_search", "vis_form":"vis_client_search_form"}); 
                          </script>';  
            }
	        else if($result && $operation['vis_static_area'] == '')
	        {
                 echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTCLIENTS'].'",{"vis_module":"client", "vis_operation":"view_clients"});</script>';    
	        } 
	        else
	        {
	             echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); $("#vis_client_delete_form").dialog("close");  </script>';
	        }
        }
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object = new class_setting($database);
        $name=((isset($current_client_res[0]['first_name']) && $current_client_res[0]['first_name']!='') || (isset($current_client_res[0]['last_name']) && $current_client_res[0]['last_name']!=''))?$current_client_res[0]['first_name']." ".$current_client_res[0]['last_name']:"";
		$email=(isset($current_client_res[0]['email']) && $current_client_res[0]['email']!='')? "( ".$current_client_res[0]['email']." ) ":"";
		$user_id=(VIS_LOCATION_AREA==1)?VIS_CLIENT_ID:VIS_STAFF_ID;
		if(VIS_LOCATION_AREA==1 || (isset($operation['vis_parent_client_id']) && $operation['vis_parent_client_id']!='')){
			$activity_log=$setting_object->add_staff_activity_log("Removed contact ".$name.$email,$user_id,1,VIS_LOCATION_AREA);
		}
		else{
			$activity_log=$setting_object->add_staff_activity_log("Removed client ".$name.$email,$user_id,1,VIS_LOCATION_AREA);
		}
		$database->database_disconnect();
        return true;
    }

    function reset_send_password($operation) {
        global $vis_language;
        $database = new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object = new class_client($database);
        $data['vis_newpassword'] = generate_random('alfa', 10);
        $data['vis_client_id'] = $operation['vis_client_id'];
		$data['email'] = $operation['vis_email'];
        $result = $client_object->set_password($data,VIS_STAFF_ID,VIS_LOCATION_AREA);
          if($result!==-1){
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
            $mobject=new class_mail($database);
            $data['vis_module']='client';
            $data['authentication']=password_decrypt($data['vis_newpassword'],VIS_PASSWORD_KEY);
            $data['vis_username']=$operation['vis_username'];
			$ret=$mobject->email_notify($data, 'send_password', VIS_STAFF_ID, VIS_LOCATION_AREA);
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'."<div class='alert-success'>".$vis_language['MSGEMAILSENTTORESETPASSWORD'].'</div>"); </script>';
        }
        else{
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
        }
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_object=new class_setting($database);
		$client_res=$client_object->get_client_list('client_id='.$data['vis_client_id'],VIS_STAFF_ID, VIS_LOCATION_AREA);
		$name=((isset($client_res[0]['first_name']) && $client_res[0]['first_name']!='') || (isset($client_res[0]['last_name']) && $client_res[0]['last_name']!=''))?$client_res[0]['first_name']." ".$client_res[0]['last_name']:"";
		$email=(isset($client_res[0]['email']) && $client_res[0]['email']!='')? "( ".$client_res[0]['email']." ) ":"";
		$activity_log=$setting_object->add_staff_activity_log("Modified the ".$name.$email." password", VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		$database->database_disconnect();
        return true;
    }

    function set_password($operation){
        global $vis_language;
        $database = new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
		
		$filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$security_password = ($security_password_details==-1)?50:$security_password_details[0]['content'];
		if($operation['password_strength_per']<$security_password) {
           
            $operation['message']='<div class="vis_error_message">'.$vis_language['TXTPASSWORDNOTALLOWED'].'</div>';
            return $this->change_password($operation);
        }
        if($operation['vis_newpassword']=='') {
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGREQFLDPSWD'].'</div>';
            return $this->change_password($operation);
        }
        if($operation['vis_newpassword']!=$operation['vis_confirm']) {
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGPASSMISSMTCH'].'</div>';
            return $this->change_password($operation);
        }
        
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object = new class_client($database);
        $result=$client_object->set_password($operation, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if($result){
            $operation['profile_message_pswd_updt']='<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>';
            $operation['success']=1;
            return $this->view_profile($operation);
        }
        else{
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>';
            return $this->change_password($operation);
        }
        return true;
    }

    function ban_client($operation) {  
        global $vis_language;
        $operation['vis_current_operation']='client_details'; 
        $operation['vis_current_module']='client'; 
        $database = new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object = new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object = new class_setting($database);
        if(!IS_LOGIN) return false;
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
        list($client_info, $client_details)=$client_object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0];
        $client_name = $client_details['first_name']." ".$client_details['last_name'];
		if (isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
			$list_chk[] = $operation['vis_client_id'];
		} else {
			$options = parse_form_array($operation['vis_action']);
			foreach ($options as $key => $value) {
				if (preg_match('/vis_global_mass_chk_[0-9]/', $key))
					$list_chk[] = $value;
			}
		}
        $clientFilter = $filter=VIS_TABLE_PREFIX.'client.parent_id in ('.implode(',',$list_chk).') ';
        list($contact_info, $contact_details)=$client_object->get_client($filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
      
        if($contact_details!=-1){
            for($k=0;$k<count($contact_details);$k++){
                $list_chk[]=$contact_details[$k]['client_id'];
            }
        }
        $count = count($list_chk);        
		$condition = '(';
		$conditionParent = '(';
		for ($i = 0; $i < $count; $i++) {
			$condition .= ' client_id = ' . $list_chk[$i] . ' or';
			$conditionParent .= ' parent_id = ' . $list_chk[$i] . ' or';
		}
		$condition = substr($condition, 0, -2);
		$conditionParent = substr($conditionParent, 0, -2);
		$condition .= ')';
		$conditionParent .= ')';
		$filter = $condition;
		if (isset($operation['vis_remove_ban']) && $operation['vis_remove_ban'] == 1)
		{
			$result = $client_object->removeban_client($filter, $conditionParent, VIS_STAFF_ID, VIS_LOCATION_AREA);
			$bann = 0;
		}
		else
		{
			$result = $client_object->ban_client($filter, $conditionParent, VIS_STAFF_ID, VIS_LOCATION_AREA);
			$bann = 1;
		}	
	   
		if (VIS_LOCATION_AREA == 0) {
			echo $result ? 'success' : 'error';
		} else {
			if ($result) {
				if($bann == 1 )echo '<script> $("#clientStatus").val(3);</script>';
				else if($client_details['is_spam'] == 1) echo '<script> $("#clientStatus").val(4);</script>';
				else if($client_details['is_approved'] == 0)  echo '<script> $("#clientStatus").val(2);</script>';
				else if($client_details['is_verified'] == 0) echo '<script> $("#clientStatus").val(5);</script>';
				else if($client_details['is_approved'] != 0 || $bann != 1 || $client_details['is_spam'] != 1 || $client_details['is_verified'] != 0 ) echo '<script> $("#clientStatus").val(1);</script>';
					
				if (isset($operation['vis_ticket_id']) && $operation['vis_ticket_id'] != "") {
					$vis_remove_ban = $ban = $txt = "";
					if ($operation['vis_remove_ban'] == 1) {
						$ban = $vis_language['TXTBAN'];
						$vis_remove_ban = 0;
					} else {
						$ban = $vis_language['TXTREMOVEBAN'];
						$vis_remove_ban = 1;
					}
					$txt = '<a href="javascript:void(0);"  onclick="$(\'#ban_status\').visExecution(\'submit_form\',{\'vis_module\':\'client\', \'vis_operation\':\'ban_client\', \'vis_action\':\'ban\', \'vis_ticket_id\':' . $operation['vis_ticket_id'] . ', \'vis_remove_ban\':' . $vis_remove_ban . ', \'vis_client_id\':' . $operation['vis_client_id'] . '});">' . $ban . '</a>';
					echo $txt . '<script>$("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSUCCOPRT'] . '");</script>';
				} else if (isset($operation['vis_action']) && $operation['vis_action'] != "") {
					if ($operation['vis_form'] == 'vis_client_form') {
						echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSUCCOPRT'] . '");$("#vis_workpanel_div").visExecution("load_area",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
					} else {
						echo '<script>  $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSUCCOPRT'] . '");
							  $("#vis_client_search_details").visExecution("load_operations",{"vis_module":"client", "vis_operation":"client_search", "vis_form":"vis_client_search_form"}); 
							  </script>';
					}
				} else {
					$is_banned = $is_banned_checked = $is_banned_html = $res = '';
					$res = $vis_language['TXTBAN'];
					if ($operation['vis_remove_ban'] == 1) {
						$is_banned = 0;
						echo '<script>$("#ban_label").attr("style", "display: none !important");</script>';
						if ($client_details['is_spam'] == 1) {
							echo '<script>$("#spam_label").attr("style", "display: block");$("#approve_label").attr("style", "display: none !important");</script>';
						} else if ($client_details['is_spam'] == 0) {
							echo '<script>$("#spam_label").attr("style", "display: none !important");</script>';
							if ($client_details['is_approved'] == 1) {
								echo '<script>$("#approve_label").attr("class", "label vh-green-bg inline ");$("#approve_label").attr("style", "display: block ");$("#approve_label").html("' . $vis_language['TXTAPPROVED'] . '");</script>';
							} else if ($client_details['is_approved'] == 0) {
								echo '<script>$("#approve_label").attr("class", "label label-default inline");$("#approve_label").attr("style", "display: block ");$("#approve_label").html("' . $vis_language['TXTPENDING'] . '");</script>';
							}
						}
					} else {
						$is_banned = 1;
						$is_banned_checked = 'checked="checked"';
						echo '<script>$("#ban_label").attr("style", "display: block ");$("#spam_label").attr("style", "display: none !important");$("#approve_label").attr("style", "display: none !important");</script>';
						//$ban_label_status='"display","block"';
					}
					//<input type="checkbox" id="is_banned" name="is_banned" data-toggle="ajaxModal" data-modal-arguments="vis_module=client&vis_operation=get_client_ban&vis_static_area=vis_client_delete_form&vis_remove_ban='.$is_banned.'&vis_client_id='.$operation['vis_client_id'].'" '.$is_banned_checked.' />
					echo $is_banned_html .= '<label class="col-xs-10 col-md-6 m-l-n control-label">' . $res . '</label><div class="col-sm-2"><label class="switch"><input type="checkbox" onchange="$(\'#ban_status\').visExecution(\'load_area\', {\'vis_module\':\'client\', \'vis_operation\':\'ban_client\', \'vis_client_id\':' . $operation['vis_client_id'] . ',\'vis_remove_ban\':' . $is_banned . '});"' . $is_banned_checked . '><span></span></label></div>';
					/*echo $is_banned_html.'<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); </script>';*/
				}
			} else
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGERROPRT'] . '"); </script>';
		}
        $client_status=($operation['vis_remove_ban']==1)?'Remove ban':'Ban';
        $name=((isset($client_details['first_name']) && $client_details['first_name']!='') || (isset($client_details['last_name']) && $client_details['last_name']!=''))?$client_name:"";
		$email=(isset($client_details['email']) && $client_details['email']!='')? "( ".$client_details['email']." ) ":"";
        $activity_log=$setting_object->add_staff_activity_log("Changed client ".$name.$email." status as ".$client_status, VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		$database->database_disconnect();     
        return true;
    }

    function client_approval($operation) {
		global $vis_language;
		global $operation_hnd;
		$database = new class_database();
		if(!IS_LOGIN) return false;
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'client.php');
		$client_object = new class_client($database);
		include_once(VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php');
		$setting_object = new class_setting($database);
		$filter = 'client_id=' . $operation['vis_client_id'];
		list($client_info, $client_details) = $client_object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$client_details = $client_details[0];
		$operation['vis_approve'] = (!isset($operation['vis_approve']) || $operation['vis_approve'] == 1) ? 1 : 0;
		$result = $client_object->client_approval($operation, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$operation_hnd->operation_flow($operation, VIS_STAFF_ID, VIS_LOCATION_AREA, $database);
		if (VIS_LOCATION_AREA == 0) {
			echo $result ? 'success' : 'error';
		} else {
			if ($result) {
				if($client_details['is_banned'] == 1 )echo '<script> $("#clientStatus").val(3);</script>';
				else if($client_details['is_spam'] == 1) echo '<script> $("#clientStatus").val(4);</script>';
				else if($operation['vis_approve'] == 0)  echo '<script> $("#clientStatus").val(2);</script>';
				else if($client_details['is_verified'] == 0) echo '<script> $("#clientStatus").val(5);</script>';
				else if($operation['vis_approve'] != 0 || $client_details['is_banned'] != 1 || $client_details['is_spam'] != 1 || $client_details['is_verified'] != 0 ) echo '<script> $("#clientStatus").val(1);</script>';
				if (isset($operation['vis_action']) && $operation['vis_action'] != '') {
					if ($operation['vis_form'] == 'vis_client_form') {
						echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSUCCOPRT'] . '");$("#vis_workpanel_div").visExecution("load_area",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
					} else {
						echo '<script>  $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGSUCCOPRT'] . '");
						  $("#vis_client_search_details").visExecution("load_operations",{"vis_module":"client", "vis_operation":"client_search", "vis_form":"vis_client_search_form"}); 
						  </script>';
					}
				} else {
					$vis_approve_label = $vis_language['TXTAPPROVE'];
					$vis_approve = $vis_approve_checked = $vis_approve_html = '';
					if ($operation['vis_approve'] == 1) {
						$vis_approve = '0';
						$vis_approve_checked = 'checked="checked"';
						if ($client_details['is_spam'] == 0 && $client_details['is_banned'] == 0) {
							echo '<script>$("#approve_label").attr("class", "label vh-green-bg inline ");</script>';
							echo '<script>$("#approve_label").attr("style", "display: block ");</script>';
							echo '<script>$("#approve_label").html("' . $vis_language['TXTAPPROVED'] . '");</script>';
						}
					} else {
						$vis_approve = '1';
						if ($client_details['is_spam'] == 0 && $client_details['is_banned'] == 0) {
							echo '<script>$("#approve_label").attr("class", "label label-default inline");</script>';
							echo '<script>$("#approve_label").attr("style", "display: block ");$("#ban_label").attr("style", "display: none !important");$("#spam_label").attr("style", "display: none !important");</script>';
							echo '<script>$("#approve_label").html("' . $vis_language['TXTPENDING'] . '");</script>';
							//$("#submittername").text("testing");
						}
					}
					echo $vis_approve_html .= '<label class="col-xs-10 col-md-6 m-l-n control-label">' . $vis_approve_label . '</label><div class="col-sm-2"><label class="switch"><input type="checkbox" onchange="$(\'#approve_status\').visExecution(\'load_area\', {\'vis_module\':\'client\', \'vis_operation\':\'client_approval\', \'vis_client_id\':' . $operation['vis_client_id'] . ', \'vis_approve\':' . $vis_approve . '});"' . $vis_approve_checked . ' /><span></span></label></div>';
					/*echo $vis_approve_html.'<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");</script>';*/
				}
			} else
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "' . $vis_language['MSGERROPRT'] . '"); </script>';
		}
		$client_status = ($operation['vis_approve'] == 1) ? 'Approve' : 'Disapprove';
		$name=((isset($client_details['first_name']) && $client_details['first_name']!='') || (isset($client_details['last_name']) && $client_details['last_name']!=''))?$client_details['first_name']." ".$client_details['last_name']:"";
		$email=(isset($client_details['email']) && $client_details['email']!='')? "( ".$client_details['email']." ) ":"";
		$activity_log = $setting_object->add_staff_activity_log("Changed client ".$name.$email." status as " . $client_status, VIS_STAFF_ID, 1,VIS_LOCATION_AREA);
		$database->database_disconnect();
		return true;
	}
      
   function add_client($operation) {
        global $vis_language;
        if(isset($operation['vis_action']) && $operation['vis_action']!='')
        $data=parse_form_array($operation['vis_action']);
        else
        {
			 foreach($operation as $key=>$val) {
				  if(!is_array($val))
				  $data[$key] = addslashes($val);
				  else
				  $data[$key] = $val;
			  }
		}
		if(!isset($data['vis_firstname']) && isset($data['vis_fullname'])){
			$vis_fullname = explode(' ',$data['vis_fullname']);
			$data['vis_firstname'] = $vis_fullname[0];
			$data['vis_lastname'] = $vis_fullname[1];
		}
        $message='';
        $flag=true;
        $check_custom = true;
        $data['is_verified'] = 1;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$setting_object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if(isset($operation['vis_domain']) && is_numeric($operation['vis_domain'])) $data['vis_domain']=$operation['vis_domain']; 
		if(isset($operation['group_id']) && is_numeric($operation['group_id'])) $data['vis_group']=$operation['group_id'];
		if(isset($operation['vis_office_name']) && is_numeric($operation['vis_office_name'])) $data['vis_office_name']=$operation['vis_office_name'];
		$data['vis_firstname']=strip_tags($data['vis_firstname']);
        $data['vis_lastname']=strip_tags($data['vis_lastname']);   
        $data['vis_email']=strip_tags($data['vis_email']);   
        $data['vis_username']=strip_tags($data['vis_username']);
		$data['vis_password']=strip_tags(str_replace(' ','',$data['vis_password']));  
		$data['vis_confirm']=strip_tags(str_replace(' ','',$data['vis_confirm']));  
        $data['vis_contact_number']=strip_tags($data['vis_contact_number']);
        $data['vis_is_manager']=strip_tags($data['vis_is_manager']);
        
        if(VIS_LOCATION_AREA == 1 && $data['vis_honeypot']!='') {
            if($message!='') $message.='<br />';
            $message .=$vis_language['MSGERROPRT'];
            $flag=false;
        }
        if(!isset($data['vis_firstname']) || trim($data['vis_firstname'])=='') {
            if($message!='') $message.='<br />';
			if(VIS_LOCATION_AREA == 0) {
				echo $vis_language['MSGINFOMISS']; return false;
			}
            $message .=$vis_language['MSGFNAMEMISS'];
            $flag=false;
        }
        else if(VIS_LOCATION_AREA == 1 || ((VIS_LOCATION_AREA == 2 || VIS_LOCATION_AREA == 0) && isset($data['vis_client_type']) && $data['vis_client_type']==0)){
		    if((isset($data['vis_email']) || trim($data['vis_email']!=''))){
				list($message, $flag) = check_email_validation($data['vis_email']);
			 }		   
			if((!isset($data['vis_password']) || $data['vis_password']=='') && VIS_LOCATION_AREA ==1 && (!isset($data['vis_auto_pass']))){ 
				if($message!='') $message.='<br />';
                $message .=   $vis_language['MSGREQFLDPSWD'];
                $flag=false;
			}else if(strcmp($data['vis_password'],$data['vis_confirm']) && VIS_LOCATION_AREA ==1 && (!isset($data['vis_auto_pass']))){  
				if($message!='') $message.='<br />';
                $message .=   $vis_language['MSGPASSMISSMTCH'];
                $flag=false;
			}///here
 else if(VIS_LOCATION_AREA!=2 && (isset($data['captchacode']) && ((trim($data['captchacode'])!=strtoupper($_SESSION['random_number'])) && (trim($data['captchacode'])!=$_SESSION['random_number'])))) {
	             if($message!='') $message.='<br />';
                $message .= $vis_language['MSGIMGVERIFY'];
                $flag=false;
            }
        }
        else if((VIS_LOCATION_AREA==2 || VIS_LOCATION_AREA==0) && (isset($data['vis_client_type']) && $data['vis_client_type']==1 )) {
			if($data['vis_username'] == '')
			{
				if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0) {
					echo $vis_language['TXTCLIENTUSERNAMEMISSING']; return false;
				}
				$message .= $vis_language['TXTCLIENTUSERNAMEMISSING'];
				$flag=false;
			}
			else if($data['vis_contact_number'] == '')
			{
				if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0) {
					echo $vis_language['MSGCONTACHNUMBERREQ']; return false;
				}
				$message .= $vis_language['MSGCONTACHNUMBERREQ'];
				$flag=false;
			}
        }
        else if(VIS_LOCATION_AREA!=2 && (isset($data['captchacode']) && trim($data['captchacode'])=='' && ((trim($data['captchacode'])!=strtoupper($_SESSION['random_number'])) && (trim($data['captchacode'])!=$_SESSION['random_number'])))) {
            if($message!='') $message.='<br />';
            $message .= $vis_language['MSGIMGVERIFY'];
            $flag=false;
        }
        else {
		   $data['vis_password'] = generate_random('alfanum', 8);     
		}
        if((!isset($data['vis_group']) || !is_numeric($data['vis_group'])) && VIS_LOCATION_AREA != 2) $data['vis_group']=1;
		if(VIS_LOCATION_AREA==1) $data['vis_domain']=VIS_DOMAIN; 
        if(VIS_LOCATION_AREA==1 || VIS_LOCATION_AREA==2) {
			list($dinfo, $domain)=$setting_object->get_domain(VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN, '', 0, '', VIS_STAFF_ID,VIS_LOCATION_AREA);
			$data['vis_language']=$domain[0]['language_id'];
        }
		
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $check_custom = true;
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object=new class_ticket($database);
		$data['vis_additional_domain'] =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $data['vis_domain'];
        $email=$object->check_email($data);
        $data['vis_department']=0;
		$filter = (VIS_LOCATION_AREA==1)?VIS_TABLE_PREFIX.'client.client_id='.$data['vis_client_id']:VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_parent_client_id'];
        $client_parent_details =$object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_parent_details = $client_parent_details[1];
        if(isset($data['vis_parent_id']) && $data['vis_parent_id']==0) { 
		}
		if((!isset($operation['vis_parent_client_id']) ||$operation['vis_parent_client_id']=='') && !isset($operation['vis_client_id'])){
		$data['vis_operation']='add_client';
		$check_custom=$ticket_object->check_custom_data($data, 0, 0, VIS_STAFF_ID, VIS_LOCATION_AREA); 
		$customerrormessage=' ('.$check_custom[1].')';
		if($check_custom!=1 && $check_custom[0]==0){ $check_custom=false; }else {$check_custom=true;}
		}
		else
		{
			$check_custom=true;
		}
		if($flag && $data['vis_email']!='' && $email!=-1){
                if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0){
					echo $vis_language['MSGCLTEMAILEXIST']; return false;
				}
                $message .= $vis_language['MSGCLTEMAILEXIST'].' ('.$data['vis_email'].')';
                $flag=false;
		}
		if($flag && ((isset($data['vis_client_type']) && VIS_LOCATION_AREA==2) || VIS_LOCATION_AREA==1 || VIS_LOCATION_AREA==0)) {
			$vis_domain =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $data['vis_domain'];	
			$vis_domain_filter =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? '' :  ' and '.VIS_TABLE_PREFIX.'client.domain_id='.$vis_domain;
			// $confilter = ($data['vis_client_type'] == 0 || )? 'email="'.$data['vis_email'].'"  ':'contact_number="'.$data['vis_contact_number'].'"  ';	
			       if($data['vis_email']!=''){
			        $confilter = ($data['vis_email'] != '' )? 'email="'.$data['vis_email'].'"  ':' ';
                    list($clinfo, $client)=$object->get_client($confilter.$vis_domain_filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
					if($client!=-1) {
						if($message!='') $message.='<br />';
						if(VIS_LOCATION_AREA == 0){
							echo $vis_language['MSGCLTEMAILEXIST']; return false;
						}
					   $validationMsg = $vis_language['MSGCLTEMAILEXIST'].' ('.$data['vis_email'].')';
						$message .=  $validationMsg;
						$flag=false;
					}
				}
					if($data['vis_client_type'] == 1 && $data['vis_contact_number']!='')
					{
						 $confilter = ' contact_number="'.$data['vis_contact_number'].'"';
						 list($clinfo, $client)=$object->get_client($confilter.$vis_domain_filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
						 if($client!=-1) {
						if($message!='') $message.='<br />';
						if(VIS_LOCATION_AREA == 0){
							echo $vis_language['MSGCONTACTNOEXISTED']; return false;
						}
						$validationMsg = $vis_language['MSGCONTACTNOEXISTED'].' ('.$data['vis_contact_number'].')';
						$message .=  $validationMsg;
						$flag=false;
					}
			     
			}
			 
			
        }
        if($flag && (((VIS_LOCATION_AREA==2 || VIS_LOCATION_AREA==0) && isset($data['vis_username']) && $data['vis_username']!='') || VIS_LOCATION_AREA!=2)) {//&& $data['vis_client_type']==1 
			
			$vis_domain_filter =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? '' :  ' and '.VIS_TABLE_PREFIX.'client.domain_id='.$data['vis_domain'];	
            list($clinfo, $client)=$object->get_client_list('user_name="'.$data['vis_username'].'"'.$vis_domain_filter.' and '.VIS_TABLE_PREFIX.'client.client_id!='.$data['vis_client_id'], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
            if($client!=-1 && $client!='') {
                if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0){
					echo $vis_language['MSGCLTUSERNAMEEXIST']; return false;
				}
                $message .= $vis_language['MSGCLTUSERNAMEEXIST'].' ('.$data['vis_username'].')';
                $flag=false;
            }
        }
		
        if(!$flag) {
            if(VIS_LOCATION_AREA==2) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
            }
            elseif(VIS_LOCATION_AREA==0) {
				
                echo $message;
            }
            elseif(VIS_LOCATION_AREA==1) {
                $operation['reg_message']='<div class="vis_error_message">'.$message.'</div>';
                if(IS_LOGIN) {$this->add_contact($operation); }
                else $this->register($operation);
            }
            return false;
        }

       if($check_custom==false)
		{
			if(VIS_LOCATION_AREA == 0){
				echo $vis_language['MSGINFOMISS'].''.$customerrormessage;
				return false;
			}
			if(VIS_LOCATION_AREA == 2 || VIS_LOCATION_AREA == 3){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].''.$customerrormessage.'"); </script>';
				return false;
			}
			if(VIS_LOCATION_AREA==1){
				$operation['reg_message']= '<div class="vis_error_message">'.$vis_language['MSGINFOMISS'].''.$customerrormessage.'</div>';
				$this->register($operation);
				return false;
			}
		}
//--------	

        if($flag) {
			$vis_domain_filter =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? '' :  ' and '.VIS_TABLE_PREFIX.'client.domain_id='.$data['vis_domain'];	
            list($clinfo, $client)=$object->get_client('user_name="'.$data['vis_username'].'" '.$vis_domain_filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
            if($client!=-1) {
                if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0){
					echo $vis_language['MSGCLTUSERNAMEEXIST']; return false;
				}
                $message .= $vis_language['MSGCLTUSERNAMEEXIST'].' ('.$data['vis_username'].')';
                $flag=false;
            }
        }
        if(!$flag) {
            if(VIS_LOCATION_AREA==2) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
            }
            elseif(VIS_LOCATION_AREA==0) {
                echo $message;
            }
            elseif(VIS_LOCATION_AREA==1) {
                $operation['reg_message']='<div class="vis_error_message">'.$message.'</div>';
                if(IS_LOGIN) $this->add_contact($operation);
                else $this->register($operation);
            }
            return false;
        }
        $data['vis_is_approved']=1;
        if(VIS_LOCATION_AREA!=2 && (isset($data['vis_parent_id']) && $data['vis_parent_id']==0)) {
            $val=$object->get_setting('module_id=3 and title="vis_approval" and domain_id='.VIS_DOMAIN, VIS_STAFF_ID, VIS_LOCATION_AREA);
            if(isset($val['content']) && $val['content']==0) $data['vis_is_approved']=0;
        }
        if(VIS_LOCATION_AREA == 1){
            if(IS_LOGIN){
                if($data['vis_client_id'] == VIS_CLIENT_ID)
         $ret=$object->add_client($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
                else{
                $operation['reg_message']=(!$ret)?('<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>'):('<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>');
                if(IS_LOGIN) $this->get_subaccounts($operation);
                else $this->register($operation);
            }            
            }else if(!isset($data['vis_client_id'])) $ret=$object->add_client($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
            else{
                include(VIS_PATH_TEMPLATE.'header.php');
                echo '<script> $("#vis_submit_err_div").show(); $("#vis_submit_err_div").html("'.$vis_language['MSGACCSSDND'].'");</script>';
                include(VIS_PATH_TEMPLATE.'footer.php');
                return false;
            }
        }
        else $ret=$object->add_client($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        
        $twofactorsetting=$object->get_setting('title="vis_two_factor_auth" ', VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(isset($twofactorsetting['content']) && $twofactorsetting['content']==1){
                $object->unverify_email($ret, VIS_STAFF_ID, VIS_LOCATION_AREA);
                include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
                $mobject=new class_mail($database);
                $data['vis_module']='client';
                $data['vis_client_id'] = $ret ;
                $data['is_verified']= 0;
                $ret=$mobject->email_notify($data, 'two_factor_auth', VIS_STAFF_ID, VIS_LOCATION_AREA);
             
        }
        $data['set_tick']=isset($data['vis_sent_mail'])?1:0;
        if($ret) {
			if(VIS_LOCATION_AREA==2){
				if($data['is_verified'] != 0){
					global $operation_hnd;
					$data=array_merge($data, $operation);
					$data['vis_client_id']=$ret;
					$operation_hnd->check_automation($data);
					$operation_hnd->operation_flow($data, $ret, VIS_LOCATION_AREA, $database);
				}
				
			} else {   
				    if($data['is_verified'] != 0)   
				    {   
						global $operation_hnd;
						$data=array_merge($data, $operation);
						$data['vis_client_id']=$ret;
						$operation_hnd->check_automation($data);
						$operation_hnd->operation_flow($data, $ret, VIS_LOCATION_AREA, $database);
					}
			}
        }
         if(VIS_LOCATION_AREA==2) {
			if(!$ret) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
			else if(isset($operation['vis_org_operation']) && $operation['vis_org_operation']=='open_new_ticket'){
				$content = ($data['vis_email']!='')?'contactDetails:"'.$data['vis_email'].'"':'contactDetails:"'.$data['vis_contact_number'].'"';
				
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");if($("#vis_new_ticket_list_client").val()=="") $("#vis_new_ticket_list_client").tokenInput("add", {id: '.$data['vis_client_id'].', '.$content.'}); $(".modal").modal("toggle");</script>';
			}
			else if(isset($operation['vis_org_operation']) && $operation['vis_org_operation']=='client_details'){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");javascript:active_contact_tab();$(".modal").modal("toggle");</script>';
			} 
			else{
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTCLIENTS'].'",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
			}
			$name=((isset($client_parent_details[0]['first_name']) && $client_parent_details[0]['first_name']!='') || (isset($client_parent_details[0]['last_name']) && $client_parent_details[0]['last_name']!=''))?$client_parent_details[0]['first_name']." ".$client_parent_details[0]['last_name']:"";
			$email=(isset($client_parent_details[0]['email']) && $client_parent_details[0]['email']!='')? "( ".$client_parent_details[0]['email']." ) ":"";
			$content=(isset($operation['vis_org_operation']) && ($operation['vis_org_operation']=='open_new_ticket' || $operation['vis_org_operation']=='client_details'))?"Added new contact ".$data['vis_firstname']." ".$data['vis_lastname']." ( ".$data['vis_email']." ) for ".$name.$email:"Added new client ".$data['vis_firstname']." ".$data['vis_lastname']." ( ".$data['vis_email']." ) ";
			$activity_log=$setting_object->add_staff_activity_log($content, VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		}
        elseif(VIS_LOCATION_AREA==1) {
			if($ret && !isset($data['vis_parent_id'])){
			$name=((isset($client_parent_details[0]['first_name']) && $client_parent_details[0]['first_name']!='') || (isset($client_parent_details[0]['last_name']) && $client_parent_details[0]['last_name']!=''))?$client_parent_details[0]['first_name']." ".$client_parent_details[0]['last_name']:"";
			$email=(isset($client_parent_details[0]['email']) && $client_parent_details[0]['email']!='')? "( ".$client_parent_details[0]['email']." ) ":"";
			$content="Added new contact ".$data['vis_firstname']." ".$data['vis_lastname']." ( ".$data['vis_email']." ) for ".$name.$email;
			$activity_log=$setting_object->add_staff_activity_log($content, VIS_CLIENT_ID ,1,VIS_LOCATION_AREA);
			}
            $operation['reg_message']=(!$ret)?('<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>'):('<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>');
            $operation['success']=(!$ret)?0:1;
            if(IS_LOGIN) $this->get_subaccounts($operation);
            else {
                $header_title=$vis_language['TXTLOGIN'];
                include(VIS_PATH_TEMPLATE.'header.php');
                include(VIS_PATH_TEMPLATE.'login.php');
                include(VIS_PATH_TEMPLATE.'footer.php');               
            }
        }
        elseif(VIS_LOCATION_AREA==0) {
            echo (!$ret)?'error':'success';
        }	
		$database->database_disconnect();
        return true;
    }

    function update_client($operation) { 
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        if(isset($operation['vis_action']) && $operation['vis_action']!=''){
        $data=parse_form_array($operation['vis_action']);
        }else
        { 
			 foreach($operation as $key=>$val) {
				  if(!is_array($val))
				  $data[$key] = addslashes($val);
				  else
				  $data[$key] = $val;
			  }
		}
        $message='';
        $flag=true;
        $client=$operation;
        if(isset($operation['vis_domain']) && is_numeric($operation['vis_domain'])) $data['vis_domain']=$operation['vis_domain'];
        if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) $data['vis_client_id']=$operation['vis_client_id'];
        $data['vis_firstname']=strip_tags($data['vis_firstname']);
        $data['vis_lastname']=strip_tags($data['vis_lastname']);
        $data['vis_username']=strip_tags($data['vis_username']);
        $data['vis_contact_number']=strip_tags($data['vis_contact_number']);
        $data['vis_city']=strip_tags($data['vis_city']);
        $data['vis_state']=strip_tags($data['vis_state']);
        $data['vis_zipcode']=strip_tags($data['vis_zipcode']);
        $data['vis_country']=strip_tags($data['vis_country']);
        $data['vis_website']=strip_tags($data['vis_website']);
        $data['vis_office_name']=strip_tags($data['vis_office_name']);
        $data['vis_department']=strip_tags($data['vis_department']);
        $data['vis_job_title']=strip_tags($data['vis_job_title']);
        $data['vis_office_website']=strip_tags($data['vis_office_website']);
        $data['vis_is_manager']=strip_tags($data['vis_is_manager']);
        
        
        if(VIS_LOCATION_AREA == 2 && (isset($operation['vis_parent_client_id']) && $operation['vis_parent_client_id']!=0))
        {
			    $data['vis_parent_client_id'] = $operation['vis_parent_client_id'];
			    $data['is_ban']=(isset($data['is_ban']) &&  $data['is_ban'] == 'on')?1:0;
				$data['is_spam']=(isset($data['is_spam']) &&  $data['is_spam'] == 'on')?1:0;
				$data['is_approved']=(isset($data['is_approved']) &&  $data['is_approved'] == 'on')?1:0;
		}
        if(!IS_LOGIN)return false;
        if(isset($data['vis_client_type']) && $data['vis_client_type']==0)
        {
			 if(trim($data['vis_email'])!='')
			{
				list($message, $flag) = check_email_validation($data['vis_email']);
				if(!$flag) {
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
					return false;
				} 
			}
		}
		if((isset($data['vis_client_type']) && $data['vis_client_type']==1) && $data['vis_username'] == '' )
		{  
			if(VIS_LOCATION_AREA==1) 
            $message.=$vis_language['TXTCLIENTUSERNAMEMISSING'];
            else
            $message=$vis_language['TXTCLIENTUSERNAMEMISSING'];
            $flag=false;
		}
        if(!isset($data['vis_client_id']) || trim($data['vis_client_id'])=='') {
			if(VIS_LOCATION_AREA==1) 
            $message.=$vis_language['MSGCLIENDIDMISS'];
            else
            $message=$vis_language['MSGCLIENDIDMISS'];
            $flag=false;
        }
        if(isset($data['vis_firstname']) && trim($data['vis_firstname'])=='') {
			if(VIS_LOCATION_AREA==1) 
			{
            if($message!='') $message.='<br />';
            $message=$vis_language['MSGFNAMEMISS'];
		    }
            else
            $message=$vis_language['MSGFNAMEMISS'];
            $flag=false;
        }
		if(isset($data['vis_email']) && trim($data['vis_email'])=='' && VIS_LOCATION_AREA==1) {
            if($message!='') $message.='<br />';
            $message=$vis_language['MSGCLIENDEMLMISS'];
            $flag=false;
        }
        if(isset($data['vis_username']) && trim($data['vis_username'])=='') {
			$data['vis_username']=$data['vis_email'];
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		$data['vis_additional_domain'] =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $data['vis_domain'];
        $email=$object->check_email($data);
        if(VIS_LOCATION_AREA==1) $data['vis_domain']=VIS_DOMAIN;
		
		if($flag && $data['vis_email']!='' && $email!=-1){
                if($message!='') $message.='<br />';
				if(VIS_LOCATION_AREA == 0){
					echo $vis_language['MSGCLTEMAILEXIST']; return false;
				}
                $message .= $vis_language['MSGCLTEMAILEXIST'].' ('.$data['vis_email'].')';
                $flag=false;
		}
		if($flag && ((isset($data['vis_client_type']) && ($data['vis_client_type']==0 || $data['vis_client_type']==1) && VIS_LOCATION_AREA==2) || VIS_LOCATION_AREA!=2)){
            $client_res=$object->get_client_list('email="'.$data['vis_email'].'" and '.VIS_TABLE_PREFIX.'domain.domain_id='.$data['vis_domain'].' and '.VIS_TABLE_PREFIX.'client.client_id!='.$data['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            if($client_res!=-1) {
                $message.=$vis_language['MSGCLTEMAILEXIST'];
                $flag=false;
                $database->database_disconnect();
            }  
        }
        if($flag && (isset($data['vis_username']) && $data['vis_username']!='')) { //&& $data['vis_client_type']==1
            $client_res=$object->get_client_list('user_name="'.$data['vis_username'].'" and '.VIS_TABLE_PREFIX.'client.domain_id='.$data['vis_domain'].' and '.VIS_TABLE_PREFIX.'client.client_id!='.$data['vis_client_id'], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
            if($client_res!=-1 && $client_res!='') {
                if($message!='') $message.='<br />';
                $message.=$vis_language['MSGCLTUSERNAMEEXIST'].' ('.$data['vis_username'].')';
                $flag=false;
            }
        }
       $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_parent_client_id'];
        $client_parent_details =$object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_parent_details = $client_parent_details[1];     
        if(!$flag) {
            if(VIS_LOCATION_AREA==2) echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'"); </script>';
            elseif(VIS_LOCATION_AREA==0) echo $message;
            elseif(VIS_LOCATION_AREA==1) {                
                $operation['profile_message']='<div class="vis_error_message">'.$message.'</div>';
                if($data['vis_client_id']==VIS_CLIENT_ID)$this->view_profile($operation);
                else $this->contact_details($operation);
            }
            return false;
        }
		
        if(VIS_LOCATION_AREA==1){  
            $client_res=$object->get_client_list(VIS_TABLE_PREFIX.'client.client_id='.$data['vis_client_id'],VIS_CLIENT_ID,VIS_LOCATION_AREA);
			if($data['vis_client_id']==VIS_CLIENT_ID || (isset($client_res[0]['parent_id']) && VIS_CLIENT_ID == $client_res[0]['parent_id'])){  
				$ret=$object->update_client($data, VIS_CLIENT_ID, VIS_LOCATION_AREA);
				
				if(isset($data['vis_customfields'])){
					include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php'); 
					$object_ticket = new ticket();
					$data['vis_department']=0;
					$customdt=array_merge($data,array("vis_operation"=>"update_client"));
		            $res=$object_ticket->update_customfields($customdt,1);
					if($res=='0')return false;
					
				}
			}
            else $operation['profile_message']='<div class="vis_error_message">'.$message.'</div>';
        }
        else{
            $ret=$object->update_client($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
			
            if(isset($data['vis_customfields'])){
                include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php'); 
                $object_ticket = new ticket();
                $data['vis_department']=$data['vis_department_id'];

				$customdt=array_merge($data,array("vis_operation"=>"update_client"));
               $res=$object_ticket->update_customfields($customdt);
            }
        }
		if(VIS_LOCATION_AREA!=1)
        {
			$database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
			$setting_object=new class_setting($database);
			$client_res=$object->get_client_list(VIS_TABLE_PREFIX.'client.client_id='.$data['vis_client_id'],VIS_CLIENT_ID,VIS_LOCATION_AREA);
		    $emailId = ($data['vis_email']!='')?$data['vis_email']:$client_res[0]['email'];
			$msg=($operation['vis_form'] != 'vis_update_client_form' && VIS_LOCATION_AREA==2)?"Modified client profile " :"Modified contact profile ";
            $activity_log=$setting_object->add_staff_activity_log($msg.$data['vis_firstname']." ".$data['vis_lastname']." ( ".$emailId." ) ", VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		
		}
        if(VIS_LOCATION_AREA==2) {
			 if($operation['vis_form'] == 'vis_update_client_form')
			 {
				 echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_details_list").visExecution("load_operations",{"vis_module":"client", "vis_operation":"get_subaccounts", "vis_client_id":"'.$client_parent_details[0]['client_id'].'"});$(".modal").modal("toggle");</script>';
			 } 
            else if($operation['vis_form'] != 'vis_update_client_form')
            {        
        
			echo "<script>$('#vis_header').html('<h3 class=\'m-b-xs text-black\'>".addslashes($data['vis_firstname'])." &lt; ".$data['vis_email']." &gt;</h3><div><ul class=\'breadcrumb bg-empty no-border m-b-xxs no-padder\'><li><a class=\'link\' href=\'#/home/quick_view\'>".$vis_language['TXTHOME']."</a></li> <li><a class=\'link\' href=\'#/client/view_clients/".$vis_language['TXTCLIENTS']."\'>".$vis_language['TXTCLIENTS']."</a></li><li class=\'active\'>".addslashes($data['vis_firstname'])." &lt; ".$data['vis_email']." &gt;</li></ul></div>');</script>";
			echo '<script>$("#short_summary").visExecution("load_area",{"vis_module":"client", "vis_operation":"display_short_summary", "vis_client_id":"'.$operation['vis_client_id'].'"});</script>';
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_details_list").visExecution("load_area",{"vis_module":"client", "vis_operation":"get_details", "vis_action":"details", "vis_client_id":"'.$operation['vis_client_id'].'"});</script>';
             
			}
			else if(!$ret) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
        }
        elseif(VIS_LOCATION_AREA==1) { 
			$database=new class_database();
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
	        $setting_object=new class_setting($database);
	        $msg=($data['vis_parent_client_id'] != 0)?"Modified contact profile " :"Modified client profile ";
            $activity_log=$setting_object->add_staff_activity_log($msg.$data['vis_firstname']." ".$data['vis_lastname']." ( ".$data['vis_email']." ) ", VIS_CLIENT_ID ,1,VIS_LOCATION_AREA);          
			
            $header_title='Profile';
            $operation['profile_message']=(!$ret)?('<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>'):('<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>');
            $operation['success']=(!$ret)?0:1;
            if(isset($object->error) && $object->error!='') $operation['profile_message'].='<div class="vis_error_message">'.$object->error.'</div>';
            if($data['vis_client_id']==VIS_CLIENT_ID)$this->view_profile($operation);
            else $this->contact_details($operation);
        }
        elseif(VIS_LOCATION_AREA==0) {
            echo (!$ret)?'error':'success';
        }
        $database->database_disconnect();
        return true;
    }

    function view_profile($operation) {
        global $vis_language;
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
        }
        $header_title = $vis_language['TXTPROFILE'];
        $breadcrumb['Home'] = 'index.php'; 
        $breadcrumb[$vis_language['TXTPROFILE']] = '?/client/view-profile'; 
        $breadcrumb[$vis_language['TXTUPDATEPROFILE']] = ''; 
       
        $vis_title = $vis_language['TXTPROFILE'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $object=new class_client($database);
        list($client_info, $client)=$object->get_client('client_id='.VIS_CLIENT_ID,'',0,'',0,1);
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
        $ticket_module = new ticket();
        $location=array(0=>0, 1=>0);
        $custom_field_var = $ticket_module->edit_custom_fields($location, VIS_CLIENT_ID, 0, $database,0,0,1);
        if($client!=-1) $client=$client[0];
        else return false;
        $database->database_disconnect();

        $header_title=$vis_language['TXTPROFILE'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'profile.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function register($operation) {
        global $vis_language; 
        if( VIS_LOCATION_AREA == 1 && ( IS_LOGIN != 0 || VIS_HEAD_MENU_REGISTER != 1 ) ){
             include(VIS_PATH_TEMPLATE.'header.php');
             echo '<script> $("#vis_submit_err_div").show(); $("#vis_submit_err_div").html("'.$vis_language['MSGACCSSDND'].'");</script>';
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
        }
        $breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREGISTER']] = ''; 
        $vis_title = $header_title=$vis_language['TXTREGISTER'];
        $location[0]=0;   
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');                
        //include_once(VIS_FUNCTION_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');    
        $database=new class_database();
        $ticket=new ticket();
        $setting_obj=new class_setting($database);
        $ticket_obj=new class_ticket($database);        
       
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$setting_obj->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
        $filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;                   
        $departments=$ticket_obj->get_department($filter, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];    
       
        ////////////////
        $filt= ' location=0 and  is_enabled= 1 ';
        $custom_check = $setting_obj->get_custom_group_list($filt, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($custom_check[0]['visible_to']==1 or $custom_check[0]['visible_to']==0) 
		{
			$custom_field_var=$ticket->get_custom_fields($location, 0, $departmentid, 0, $database);
		}
		/////////////////
        $filter='module_id=3 and title="vis_captcha" and domain_id='.VIS_DOMAIN;
        $setting_details=$setting_obj->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'register.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function add_contact($operation) {
        global $vis_language;
        if((defined('VIS_CLIENT_PARENT') && VIS_CLIENT_PARENT!=-1) || VIS_CLIENT_ID==0) {
            $operation['profile_message']=$vis_language['MSGACCSSDND'];
            return $this->view_profile($operation);
        }
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		$filter = VIS_TABLE_PREFIX.'client.client_id='.VIS_CLIENT_ID;
		$client_details = $object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
		$client_details = $client_details[1];
	
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$setting_object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTCONTACTS']] = '?/client/contacts'; 
		$breadcrumb[$vis_language['TXTADDCONTACT']] = ''; 
		$vis_title = $vis_language['TXTCONTACTS'];
		
        $header_title=$vis_language['TXTADDCONTACT'];
        $parent_id=VIS_CLIENT_ID;
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'contact_new.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function client_details($operation) {
		
        global $vis_language;
        if(defined('VIS_CLIENT_ID') && is_numeric(VIS_CLIENT_ID)) {
            $client_id=VIS_CLIENT_ID;
        }
        else {
            if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
                $client_id=$operation['vis_client_id'];
            }
            else {
                $spliter=(strstr($operation['vis_action'], '/'))?'/':',';
                $action=explode($spliter, $operation['vis_action']);
                if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) {
                    $client_id=$operation['vis_client_id']=$action[4];
                }
                else {
                    $this->error=$vis_language['MSGCLIENDIDMISS'];
                    return false;
                }
            }
        }
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'billing.php');
        $object=new class_billing($database);
        $filter = ' ticket_balance = 1 and client_id='.$client_id ;
        $ticketCredit = $object->get_credit_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $filter = ' ticket_balance = 0 and client_id='.$client_id;
        $amountCredit = $object->get_credit_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $filter='('.VIS_TABLE_PREFIX.'order.client_id='.$operation['vis_client_id'].' or '. VIS_TABLE_PREFIX.'client.parent_id='.$operation['vis_client_id'].')';
        if(VIS_LOCATION_AREA==2) {
            $unpaid_invoice_cnt=$object->get_unpaid_invoice_count('('.VIS_TABLE_PREFIX.'invoice.client_id='.$operation['vis_client_id'].' or '. VIS_TABLE_PREFIX.'client.parent_id='.$operation['vis_client_id'].') and invoice_status=0', VIS_STAFF_ID, VIS_LOCATION_AREA);
        }
        $orders=$object->get_orders_count($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
        $filter=VIS_TABLE_PREFIX.'invoice.client_id='.$operation['vis_client_id']. ' and ' .VIS_TABLE_PREFIX.'invoice.invoice_status=1';
        $client_product=$object->get_client_product_list_count($filter, VIS_STAFF_ID, VIS_LOCATION_AREA); 
        $filter='('.VIS_TABLE_PREFIX.'invoice.client_id='.$operation['vis_client_id'].' or '. VIS_TABLE_PREFIX.'client.parent_id='.$operation['vis_client_id'].')';
        $invoices=$object->get_invoice_count($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $filter=VIS_TABLE_PREFIX.'balance.client_id='.$operation['vis_client_id'];
        $transactions=$object->get_transactions_count($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object=new class_client($database);
        $ticket_object=new class_ticket($database);
        $parent=$client_object->get_client_parentid('client_id='.$operation['vis_client_id'],VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $client='';
        if($parent['parent_id']==0 || $parent['parent_id']==-1){
            $client_details=$client_object->get_client_list('parent_id='.$operation['vis_client_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
            if($client_details!=-1) for($i=0;$i<count($client_details);$i++) if($client_details[$i]['client_id']!=VIS_CLIENT_ID) $client.=$client_details[$i]['client_id'].',';
        }
        if($client!='') $filter=VIS_TABLE_PREFIX.'ticket.client_id in ('.$client.$operation['vis_client_id'].')';
        else $filter=VIS_TABLE_PREFIX.'ticket.client_id='.$operation['vis_client_id'];
        $filter.=' and is_deleted=0 and draft_id=0';
        $ticket_details=$ticket_object->get_client_ticket_count($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $sett_obj=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$client_id;
        list($client_info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0]; 
         $filter='module_id=3 and title="vis_additional_emails" and domain_id='.$client_details['domain_id'];
        $setting_details=$object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        //print_r($setting_details);
        $email=$object->display_email($client_id,'', VIS_LOCATION_AREA);
		list($dpinf, $domain)=$sett_obj->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.domain_id='.$client_details['domain_id'], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($domain!=-1) $domain=$domain[0];
        $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_parent_client_id'];
        $client_parent_details =$object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_parent_details = $client_parent_details[1];
        $client_group=$object->get_client_group('is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $subaccount_details=$object->get_subaccount_count(VIS_TABLE_PREFIX.'client.parent_id='.$client_id, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$client_details['parent_id'];
        $parent_details=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$parent_details=$parent_details[1][0];
		$twofactorsetting=$object->get_setting('title="vis_two_factor_auth" ', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$twoFactorAuth = $twofactorsetting['content'];
        if(VIS_LOCATION_AREA==2) {
            list($info, $modules)=$sett_obj->get_module('is_enabled=1', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			foreach($modules as $var) $module[$var['module']]=1;
            $filter=' location=0 and is_enabled=1 ';
            $custom_group=$sett_obj->get_custom_group_list($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			$twilio_filter='integration_type=27 AND domain_id=0 AND is_enabled=1';
			list($twilio_info, $twilio_details)=$sett_obj->get_integration_setting($twilio_filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			if($twilio_details==-1){
				$twilio_filter='integration_type=27 AND domain_id='.VIS_DOMAIN.' AND is_enabled=1';
				list($twilio_info, $twilio_details)=$sett_obj->get_integration_setting($twilio_filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			}
        }
        if(VIS_LOCATION_AREA==2)
        {
              if($client_details==-1 || $client_details==''){
                 echo '<section class="wrapper fullFrame b-light"><div class="fullMiddle">'.$vis_language['MSGACCSSDND'].'</div></section>';
                 return false;
              }
			  include(VIS_PATH_TEMPLATE.'client_view.php');
			  if( $client_details['is_banned']==1 ) 
			  {
			  	 echo '<script>$("#ban_label").attr("style", "display: block");$("#spam_label").attr("style", "display: none !important");$("#approve_label").attr("style", "display: none !important");</script>';
			  }
			  else 
			  {
				  echo '<script>$("#ban_label").attr("style", "display: none !important");</script>';
			  }
			  if($client_details['is_banned']!=1 && $client_details['is_spam']==1 ) 
			   {
				    echo '<script>$("#spam_label").attr("style", "display: block");$("#ban_label").attr("style", "display: none !important");$("#approve_label").attr("style", "display: none !important");</script>';
			   }
			  else 
			  {
				   echo '<script>$("#spam_label").attr("style", "display: none !important");</script>';
			  }
			  if($client_details['is_banned']!=1 && $client_details['is_spam']!=1 && $client_details['is_approved']==1 ) 
			   {
				 echo '<script>$("#approve_label").attr("style", "display: block");$("#ban_label").attr("style", "display: none !important");$("#spam_label").attr("style", "display: none !important");</script>';
			 }
			  else  if($client_details['is_banned']!=1 && $client_details['is_spam']!=1 && $client_details['is_approved']==0 ) 
			  {
				   echo '<script>$("#approve_label").attr("style", "display: block");$("#ban_label").attr("style", "display: none !important");$("#spam_label").attr("style", "display: none !important");</script>';
		      }
		}
        elseif(VIS_LOCATION_AREA==1) {
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'subcontact.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        return true;
    }

    function contact_details($operation) {
        global $vis_language;
        if(isset($operation['vis_action']) && $operation['vis_action']!='')
        {
			$data = explode('/',$operation['vis_action']);
			$client_id = $operation['vis_client_id'] = $data[4];
		}  
        if(defined('VIS_CLIENT_ID') && is_numeric(VIS_CLIENT_ID) && VIS_CLIENT_ID!=0 && isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
            $client_id=$operation['vis_client_id'];
        }
        else {
            $operation['message']=$vis_language['MSGCLIENDIDMISS'];
            $this->view_profile($operation);
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$client_id.' and parent_id='.VIS_CLIENT_ID;
        list($client_info, $client)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client=$client[0];
        $client_setting=$object->get_subclient_setting($client['client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_group=$object->get_client_group('is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        if($client){
        if(VIS_LOCATION_AREA==2) include(VIS_PATH_TEMPLATE.'client_view.php');
        elseif(VIS_LOCATION_AREA==1) {
			$breadcrumb['Home'] = 'index.php';  
			$breadcrumb[$vis_language['TXTCONTACTS']] = '?/client/contacts'; 
			$breadcrumb[$client['first_name'].' '.$client['last_name']] = '';  
			
			$vis_title = $vis_language['TXTCONTACTS'];
            $header_title = $vis_language['TXTCONTACTS'];
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'subaccount.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        }
        else{
            include(VIS_PATH_TEMPLATE.'header.php');
            echo '<div class="vis_message"><br/>'.$vis_language['MSGACCSSDND'].'<br/></div>';
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        return true;
    }

    function get_details($operation) {
        global $vis_language;
        if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
            $database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
            $object=new class_client($database);include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
			$setting_object=new class_setting($database);
			list($language_info, $language_list)=$setting_object->get_language('is_enabled=1', '', 0, 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
            $filter=VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
            list($client_info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
            $client_details=$client_details[0];
            //print_r( $client_details);
            $option_domain_id = (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? $client_details['optionB_domain_id'] : $client_details['domain_id'];
            $client_group=$object->get_client_group('is_enable=1 and (domain_id='.$option_domain_id .' or domain_id=0)', VIS_STAFF_ID, VIS_LOCATION_AREA);
            $client_setting=$object->get_subclient_setting($operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
            $ticket=new class_ticket($database);
            $ticket_module = new ticket();
            $location=array(0=>0, 1=>0);
            $filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;       
            $departments=$ticket->get_department($filter, VIS_CLIENT_ID, VIS_LOCATION_AREA);
            $departmentid = $departments[0]['department_id'];
            $readonly=0;
            $custom_field_var = $ticket_module->edit_custom_fields($location, $operation['vis_client_id'], $departmentid, $database,$readonly,1);
            $database->database_disconnect();
            if(!$client_details) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
            include_once(VIS_PATH_TEMPLATE.'client_details.php');
            return true;
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCLIENDIDMISS'].'"); </script>';
            return false;
        }
    }

    function get_subaccounts($operation) {
        global $vis_language; 
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
         }       
        if(VIS_LOCATION_AREA==1 && defined('VIS_CLIENT_ID') && VIS_CLIENT_ID!=0 && VIS_CLIENT_ID!=-1) {
            $client_id=VIS_CLIENT_ID;
        }        
        elseif(VIS_LOCATION_AREA==2 && isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
            $client_id=$operation['vis_client_id'];
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCLIENDIDMISS'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $object=new class_client($database);
        $filter=VIS_TABLE_PREFIX.'client.parent_id='.$client_id;
        $staff=new class_staff($database);
        list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);	
        if(VIS_LOCATION_AREA==1) $view_info['page_count']=10;
		$page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;         
   		$limit=($page*$view_info['page_count']).','.$view_info['page_count'];	
        $subaccount_details=$object->get_subaccount($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,$limit);
        $subaccount_info['client_id']=$client_id;
        $subaccount_details_total=$object->get_subaccount($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);	
	    $subaccount_info['count']=($subaccount_details_total==-1)?0:count($subaccount_details_total);
		$view_info['page_count']=($subaccount_info['count']==0)?0:ceil($subaccount_info['count']/$view_info['page_count']);		
        $database->database_disconnect();
		$subaccount_info['page']=$page;	
        if(!$subaccount_details) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        $header_title=$vis_language['TXTCONTACTS'];
        if(VIS_LOCATION_AREA==1) {
			$breadcrumb['Home'] = 'index.php';  
			$breadcrumb[$vis_language['TXTCONTACTS']] = ''; 
			$vis_count = $subaccount_info['count'];
			$vis_title = $vis_language['TXTCONTACTS'];
			   if(isset($operation['vis_page']) && is_numeric($operation['vis_page'])){
				if($operation['vis_change']==1){
				include(VIS_PATH_TEMPLATE.'subaccount_list.php');
			   }
			   else {	
				include(VIS_PATH_TEMPLATE.'header.php');
				include_once(VIS_PATH_TEMPLATE.'view_subaccount.php');
				include(VIS_PATH_TEMPLATE.'footer.php');
			   }
			} else {
				include(VIS_PATH_TEMPLATE.'header.php');
				include_once(VIS_PATH_TEMPLATE.'view_subaccount.php');
				include(VIS_PATH_TEMPLATE.'footer.php');
			}
		}
        elseif(VIS_LOCATION_AREA==2) include_once(VIS_PATH_TEMPLATE.'client_subaccounts.php');
        return true;
    }

    function change_password($operation) {
        global $vis_language;
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
         }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_obj=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$setting_obj->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
        $breadcrumb['Home'] = 'index.php'; 
        $breadcrumb[$vis_language['TXTPROFILE']] = '?/client/view-profile'; 
        $breadcrumb[$vis_language['TXTCHNGPASS']] = ''; 
        $vis_title = $vis_language['TXTPROFILE'];
        $header_title=$vis_language['TXTCHNGPASS'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'change_password.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function quick_view($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $object=new class_client($database);
        $staff=new class_staff($database);
        $quick_view=$object->get_quick_counts('', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $ticket_activities=$staff->get_recent_audit('location=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'client_quickview.php');
        return true;
    }

    function send_mass_mail($operation) {
    }

    function custom_field_details($operation) {
        $database=new class_database();
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        
        $ticket_module = new ticket();
        $ticket=new class_ticket($database);
        $filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;       
        $departments=$ticket->get_department($filter, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];    
        
        $location=array(0=>0, 1=>0); 
        $custom_field_var = $ticket_module->edit_custom_fields($location, $operation['vis_client_id'], $departmentid, $database);
        $database->database_disconnect();
        echo $custom_field_var;
        return true;
    }

    function get_client_group_delete($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        list($clinfo, $client)=$object->get_client(VIS_TABLE_PREFIX.'client.group_id='.$operation['vis_group_id'], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if($client==-1) {
            $message_info['vis_form_title']=$vis_language['TXTDELETEORGANIZATION'];
            $message_info['vis_message']=$vis_language['MSGSURE'];
            $message_info['vis_event']="$('#vis_global_msg_div').visExecution('load_operations', {'vis_module':'client', 'vis_operation':'delete_group', 'vis_group_id':".$operation['vis_group_id']."})";
        }
        else {
            $message_info['vis_form_title']=$vis_language['TXTDELETEORGANIZATION'];
            $message_info['vis_message']=$vis_language['MSGCLGRDEL'];
        }
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }

    function delete_group($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $client_group_details=$object->get_client_group("group_id=".$operation['vis_group_id'], '','', '');
		$ret=$object->delete_group($operation['vis_group_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); $("#vis_group_delete_form").dialog("close"); </script>';
            return false;
        }
        else {
			$activity_log=$setting_object->add_staff_activity_log("Removed organization ".$client_group_details[0]['group_name'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTORGANIZATION'].'",{"vis_module":"client", "vis_operation":"view_client_group"});</script>';
		}
        $database->database_disconnect();
        return true;
    }

    function unapproved_client($operation) {
        global $vis_language;
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $operation['view_title']=$vis_language['TXTUNAPPROVED'];
        return $this->client_display($operation, '', '', ' (is_banned=0 and is_approved=0) and '.VIS_TABLE_PREFIX.'client.parent_id=0 ', $page, $sort_by);
    }

    function toggle_spam($operation) {
        global $vis_language;
        $database = new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object = new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object = new class_setting($database);
        if(!IS_LOGIN) return false;
        $operation['vis_spam']=(!isset($operation['vis_spam']) || $operation['vis_spam']==1)?1:0;
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
        list($client_info, $client_details)=$client_object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0];
        //$result = $client_object->toggle_spam($operation['vis_client_id'], $operation['vis_spam'], VIS_STAFF_ID,VIS_LOCATION_AREA);
       // $message=($result)?$vis_language['MSGSUCCOPRT']:$vis_language['MSGERROPRT'];
        $res=$vis_language['TXTSPAM'];
        if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) 
	    {
             $list_chk[]=$operation['vis_client_id'];
        }
        else 
        {
            $options=parse_form_array($operation['vis_action']);
            foreach($options as $key=>$value) 
            {
            if(preg_match('/vis_global_mass_chk_[0-9]/', $key)) $list_chk[]=$value;
            }
            $operation['vis_client_id']=$list_chk[0];
        }
        $filter=VIS_TABLE_PREFIX.'client.parent_id in ('.implode(',',$list_chk).') ';
        list($contact_info, $contact_details)=$client_object->get_client($filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        if($contact_details!=-1){
        for($k=0;$k<count($contact_details);$k++){
            $list_chk[]=$contact_details[$k]['client_id'];
        }
		}
        $count=count($list_chk);
        $condition='(';
        $conditionParent='(';
        for($i=0;$i<$count;$i++) 
        {
              $condition.=' client_id = '.$list_chk[$i].' or';
              $conditionParent.=' parent_id = '.$list_chk[$i].' or';
        }
        $condition=substr($condition, 0, -2);
        $conditionParent=substr($conditionParent, 0, -2);
        $condition.=')';
        $conditionParent.=')';
        
        $filter = $condition ; 
        if( $operation['vis_form'] == 'vis_client_form')
        {
        $operation['vis_spam']=(!isset($operation['vis_spam']) || $operation['vis_spam']==1)?0:1;
        }
        $result = $client_object->toggle_spam($condition, $operation['vis_spam'], VIS_STAFF_ID,VIS_LOCATION_AREA);
        if(VIS_LOCATION_AREA==0) {
            echo $result?'success':'error';
        }
        else {
			    if($client_details['is_banned'] == 1 )echo '<script> $("#clientStatus").val(3);</script>';
			    else if($operation['vis_spam'] == 1) echo '<script> $("#clientStatus").val(4);</script>';
			    else if($client_details['is_approved'] == 0)  echo '<script> $("#clientStatus").val(2);</script>';
				else if($client_details['is_verified'] == 0) echo '<script> $("#clientStatus").val(5);</script>';
				else if($client_details['is_approved'] != 0 || $client_details['is_banned'] != 1 || $operation['vis_spam'] != 1 || $client_details['is_verified'] != 0 ) echo '<script> $("#clientStatus").val(1);</script>';
			
			if(isset($operation['vis_action']) && $operation['vis_action'] != "")
                 { 
                     if( $operation['vis_form'] == 'vis_client_form')
                     {
                         echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_area",{"vis_module":"client", "vis_operation":"view_clients"});</script>'; 
                     }
                     else
                     {
                      echo '<script>  $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");
                      $("#vis_client_search_details").visExecution("load_operations",{"vis_module":"client", "vis_operation":"client_search", "vis_form":"vis_client_search_form"}); 
                      </script>';  
                     }
               }
               else
               {
			$vis_spam = $vis_spam_checked = $spam_html ='';
			if($operation['vis_spam']==1){
				$vis_spam = '0';
				$vis_spam_checked = 'checked="checked"';
				if($client_details['is_banned']!=1)
				echo '<script>$("#spam_label").attr("style", "display: block ");$("#ban_label").attr("style", "display: none !important");$("#approve_label").attr("style", "display: none !important");</script>';
				else
				echo '<script>$("#approve_label").attr("style", "display: none !important");</script>';
			}
			else
			{
				$vis_spam = '1';	
				echo '<script>$("#spam_label").attr("style", "display: none !important");</script>';
				if($client_details['is_banned']!=1 && $operation['vis_spam']==0)
				{
					if($client_details['is_approved']==1)	
					{
						 echo '<script>$("#approve_label").attr("class", "label vh-green-bg inline ");$("#approve_label").attr("style", "display: block ");$("#approve_label").html("'.$vis_language['TXTAPPROVED'].'");</script>';
					}
					else
					{
						echo '<script>$("#approve_label").attr("class", "label label-default inline");$("#approve_label").attr("style", "display: block ");$("#approve_label").html("'.$vis_language['TXTPENDING'].'");</script>';
					}
				}
			}	
			echo $spam_html = '<label class="col-xs-10 col-md-6 m-l-n control-label">'.$res.'</label><div class="col-sm-2"><label class="switch"><input type="checkbox" onchange="$(\'#spam_status\').visExecution(\'load_area\', {\'vis_module\':\'client\', \'vis_operation\':\'toggle_spam\', \'vis_client_id\':'.$operation['vis_client_id'].', \'vis_static_area\':\'vis_client_delete_form\', \'vis_spam\':'.$vis_spam.'});"'.$vis_spam_checked.' /><span></span></label></div>';
            /*echo $spam_html.'<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$message.'");</script>';*/
		}
        }
        $client_status=($operation['vis_spam']==1)?'Spam':'Not spam';
        $name=((isset($client_details['first_name']) && $client_details['first_name']!='') || (isset($client_details['last_name']) && $client_details['last_name']!=''))?$client_details['first_name']." ".$client_details['last_name']:"";
		$email=(isset($client_details['email']) && $client_details['email']!='')? "( ".$client_details['email']." ) ":"";
        $activity_log=$setting_object->add_staff_activity_log("Changed client ".$name.$email." status as ".$client_status, VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		$database->database_disconnect();     
        return true;
    }
        
    function get_merge_clients($operation) {
        global $vis_language;
        $action='merge_clients';
        $title=$vis_language['TKTMRGCLIENT'];
        include(VIS_PATH_TEMPLATE.'client_merge.php');
        return true;
    }

    function get_merge_contact($operation) {
        global $vis_language;
        $action='merge_contact';
        $title=$vis_language['TKTMRGCONTACT'];
        include(VIS_PATH_TEMPLATE.'client_merge.php');
        return true;
    }

    function merge_contact($operation) {
        global $vis_language;
        $data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		
		/*if(isset($data['vis_email']) || trim($data['vis_email'])!='')
		{
			list($message, $flag) = check_email_validation($data['vis_email']);
			if(!$flag) {
				 list($message, $flag1) = check_email_validation($data['vis_tempemail']);
				 if($flag1) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCANNOTMERGEOTHERCLIENTEMAIL'].'"); </script>';
            }else
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGEMAILMISS'].'"); </script>';
            return false;
			} 
		}	*/
		if(isset($data['vis_email']) && $data['vis_email']=='')
		{
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINVALIDCLIENTFORMERGE'].'"); </script>';
            return false;
		}
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $ret=$object->merge_contact($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); $("#vis_client_merge_area").dialog("close"); </script>';
            return false;
        }
        else {
        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_client_merge_area").visExecution("load_area",{"vis_module":"client", "vis_operation":"display_short_summary", "vis_client_id": "'.$data['vis_client_id'].'"});javascript:active_contact_tab();$(".modal").modal("toggle");</script>';
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
		$activity_log=$setting_object->add_staff_activity_log("Client ".$data['vis_email']." merged as contact with client ".$data['vis_client_email'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		}
		$database->database_disconnect();
        return true;
    }

    function merge_clients($operation) {
        global $vis_language;
        $data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		/*if(isset($data['vis_email']) || trim($data['vis_email'])!='')
		{
			list($message, $flag) = check_email_validation($data['vis_email']);
			if(!$flag) {
				 list($message, $flag1) = check_email_validation($data['vis_tempemail']);
				 if($flag1) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCANNOTMERGEOTHERCLIENTEMAIL'].'"); </script>';
            }else
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGEMAILMISS'].'"); </script>';
            return false;
			} 
		}*/
		if(isset($data['vis_email']) && $data['vis_email']=='')
		{
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINVALIDCLIENTFORMERGE'].'"); </script>';
            return false;
		}
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $ret=$object->merge_clients($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        
        $clientFilter=VIS_TABLE_PREFIX.'client.client_id='.$data['vis_client_id'];
        list($client_info, $client_details)=$object->get_client($clientFilter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0];
        $client_details['vis_client_name'] = $client_details['first_name'].' '.$client_details['last_name'];
        
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); $("#vis_client_merge_area").dialog("close"); </script>';
            return false;
        }
        else {
        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_client_merge_area").visExecution("load_area",{"vis_module":"client", "vis_operation":"display_short_summary", "vis_client_id": "'.$data['vis_client_id'].'"});$(".modal").modal("toggle");$("#vis_workpanel_div").visExecution("load_module","'.$client_details['vis_client_name'].'/'.$client_details['client_id'].'",{"vis_module":"client","vis_operation":"client_details", "vis_client_id":"'.$data['vis_client_id'].'"});</script>';
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
		$activity_log=$setting_object->add_staff_activity_log("Client ".$data['vis_email']." merged with client ".$data['vis_client_email'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
		}
		$database->database_disconnect();
        return true;
    }
   
    function client_search($operation)
    {
    	  global $vis_language;
    	  $database = new class_database();
    	 
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');

          $database=new class_database();
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
          $clientobject=new class_client($database);
          $domain_list=$this->get_domain_access_list();
          $client_group=$clientobject->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
          $object=new class_ticket($database);
          $filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
          $domain_details=$object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
          if((isset($operation['vis_action']) && $operation['vis_action']!='') || (isset($operation['vis_submit']) && $operation['vis_submit']!='')) 
          {
                if(isset($operation['vis_submit']) && $operation['vis_submit']!='') $data=$operation;
                else $data=parse_form_array($operation['vis_action']);
             
                if(count($data)==0) 
                {
                      include(VIS_PATH_TEMPLATE.'client_search.php');
                      return true;
                }
                $operation['view_title']=$vis_language['TXTCLIENTS'];
                $this->view_clients($operation);
          }
          else
          {
         	    include(VIS_PATH_TEMPLATE.'client_search.php');
          }
          return true;
    }
    function additional_info($operation)
    {	
        global $vis_language;
		if( VIS_LOCATION_AREA == 1 && ( IS_LOGIN != 1) ){
             include(VIS_PATH_TEMPLATE.'header.php');
             echo '<script> $("#vis_submit_err_div").show(); $("#vis_submit_err_div").html("'.$vis_language['MSGACCSSDND'].'");</script>';
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
        }
        $database = new class_database();     
        $breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTPROFILE']] = '?/client/view-profile';
		$breadcrumb[$vis_language['TXTADDITIONALFIELDS']] = '';
		$vis_title = $header_title=$vis_language['TXTPROFILE'];
        
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        
        $ticket=new class_ticket($database);
        $ticket_module = new ticket();
        $location=array(0=>0, 1=>0);
        $filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;       
        $departments=$ticket->get_department($filter, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];    
        $custom_field_var = $ticket_module->edit_custom_fields($location, VIS_CLIENT_ID, $departmentid, $database);
        $database->database_disconnect();
        
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'custom_registration.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        if($operation['flag']==false) return '0';
		return '1';
    
        
    }

   function display_short_summary($operation){
		$client_id=$operation['vis_client_id'];
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $sett_obj=new class_setting($database);
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$client_id;
        list($client_info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0];
								$domain_id=$client_details['domain_id'];
        $filter=VIS_TABLE_PREFIX.'client.client_id='.$client_details['parent_id'];
        $parent_details=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$parent_details=$parent_details[1][0];
		$filter='module_id=3 and title="vis_additional_emails" and domain_id='.$domain_id;
		$setting_details=$sett_obj->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$email=$object->display_email($client_id,'', VIS_LOCATION_AREA);
		list($dpinf, $domain)=$sett_obj->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.domain_id='.$domain_id, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	if($domain!=-1) $domain=$domain[0];
        include(VIS_PATH_TEMPLATE.'client_short_summary.php');
        return true;
	}
	function update_client_details($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        if(isset($operation['vis_client_id']) && $operation['vis_client_id'] != 0){
            $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
            $client_details = $object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
            $client_details = $client_details[1];
            $filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_parent_client_id'];
            $client_parent_details = $object->get_client($filter,'0,1',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
            $client_parent_details = $client_parent_details[1];
        }
        $domain_list=$this->get_domain_access_list();
        $client_group=$object->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
          $client_setting=$object->get_subclient_setting($operation['vis_client_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
          
          $twofactorsetting=$object->get_setting('title="vis_two_factor_auth" ', VIS_STAFF_ID, VIS_LOCATION_AREA);
		$twoFactorAuth = $twofactorsetting['content'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
        $filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
        $domain_details=$object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        
        
        /*$location[0] = 0;
        include_once(VIS_FUNCTION_PREFIX.'ticket.php');
        $ticket=new ticket();
        $custom_field_var=$ticket->get_custom_fields($location, 0, 0, 0, $database);*/
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'client_new_modal.php');
        return true;
    }
    
    function view_summary($operation) {
        global $vis_language;
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
         }
        
        $breadcrumb['Home'] = 'index.php'; 
        $breadcrumb[$vis_language['TXTPROFILE']] = ''; 
        $vis_title = $vis_language['TXTPROFILE'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $object=new class_client($database);
		if(VIS_VIEW_TICKET==1 && VIS_VIEW_SELFTICKET==0) {
                $parent=(VIS_CLIENT_PARENT==0 || VIS_CLIENT_PARENT==-1)?VIS_CLIENT_ID:VIS_CLIENT_PARENT;
                $client='';
                $client_details=$object->get_client_list('parent_id='.$parent, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
                if($client_details!=-1) for($i=0;$i<count($client_details);$i++) if($client_details[$i]['client_id']!=VIS_CLIENT_ID) $client.=$client_details[$i]['client_id'].',';
                if($client!='') $filter=VIS_TABLE_PREFIX.'ticket.client_id in ('.$client.$parent.')';
                else $filter=VIS_TABLE_PREFIX.'ticket.client_id='.$parent;
            }
        elseif(VIS_VIEW_TICKET==1 && VIS_VIEW_SELFTICKET==1) {
			$parent=(VIS_CLIENT_PARENT==0 || VIS_CLIENT_PARENT==-1)?VIS_CLIENT_ID:VIS_CLIENT_PARENT;
			$tempclient='';
			$client_details=$object->get_client_list('parent_id='.$parent, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
			if($client_details!=-1) for($i=0;$i<count($client_details);$i++) $tempclient.=$client_details[$i]['client_id'].',';
			if($tempclient!='') $filter=VIS_TABLE_PREFIX.'ticket.client_id in ('.$tempclient.$parent.')';
			else $filter=VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		}
		else $filter=VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
        list($client_info, $client)=$object->get_client('client_id='.VIS_CLIENT_ID,'',0,'',0,1);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_object=new class_setting($database);
        list($dminfo, $domain)=$setting_object->get_domain(VIS_TABLE_PREFIX.'domain.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.domain_id = ' .VIS_DOMAIN, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object=new class_ticket($database);
		$filter.=' and is_deleted=0 and draft_id=0 and '.VIS_TABLE_PREFIX.'ticket.client_id!=0 ';
        list($ticket_info, $unsolved_tickets)=$ticket_object->get_ticket($filter.' and status_id!=3 ', '', 0, 'modify_date desc', VIS_CLIENT_ID, VIS_LOCATION_AREA);        
        $unsolved=($unsolved_tickets==-1)?0:count($unsolved_tickets);
        list($ticket_info, $solved_tickets)=$ticket_object->get_ticket($filter.' and status_id=3 ', '', 0, 'modify_date desc', VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $solved=($solved_tickets==-1)?0:count($solved_tickets);
        
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
        $ticket=new class_ticket($database);
        $ticket_module = new ticket();
        $location=array(0=>0, 1=>0);
        $filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;       
        $departments=$ticket->get_department($filter, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];
        $readonly=1;
        $custom_field_var = $ticket_module->edit_custom_fields($location, VIS_CLIENT_ID, $departmentid, $database,$readonly);
		$email=$object->display_email(VIS_CLIENT_ID,'', VIS_LOCATION_AREA);
        if($client!=-1) $client=$client[0];
        else return false;
        if($client['parent_id']!=0 && VIS_ACCESS_BILLING==1){
            list($parent_client_info, $parent_client)=$object->get_client('client_id='.$client['parent_id'],'',0,'',0,1);
            $client['credit']=$parent_client[0]['credit'];
            $client['balance']=$parent_client[0]['balance'];
        }
         $filter='module_id=3 and title="vis_additional_emails" and domain_id='.VIS_DOMAIN;
        $setting_details=$setting_object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();

        $header_title=$vis_language['TXTPROFILE'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'summary.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }
    function approved_client($operation) {
        global $vis_language;
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $operation['view_title']=$vis_language['TXTAPPROVED'];
        return $this->client_display($operation, '', '', ' (is_banned=0 and is_approved=1) and '.VIS_TABLE_PREFIX.'client.parent_id=0 ', $page, $sort_by);
    }
    function banned_client($operation) {
        global $vis_language;
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $operation['view_title']=$vis_language['TXTBANNED'];
        return $this->client_display($operation, '', '', ' is_banned=1 and '.VIS_TABLE_PREFIX.'client.parent_id=0 ', $page, $sort_by);
    }
    function get_survey($operation) {
        global $vis_language; 
		$survey_id = $operation['survey_id'];
      	include_once(VIS_FUNCTION_PREFIX.'ticket.php');  
        $database=new class_database();
        $ticket=new ticket(); 	
        $location[0] = 4;
        $location[1] = $survey_id;
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client = new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $ticket_object = new class_ticket($database);
        $custIds = $client->get_custom_fields_id($survey_id);
        $idArr = array();
        foreach($custIds as $id) {
            foreach($id as $key => $val) {
                $idArr[] = $val;
            }
        }
        $checkExists = $ticket_object->get_customfield_data($idArr[0], VIS_CLIENT_ID, 4);
        if($checkExists == -1) {
            $custom_field_var=$ticket->get_custom_fields($location, 0, '' , 0, $database);
        }
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTSURVEY']] = ''; 
		$vis_title = $header_title = $vis_language['TXTSURVEY'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'client_survey.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
	}
	function add_client_survey($operation) {
    	global $vis_language;
		if(VIS_LOCATION_AREA == 1 && ($operation['field'.$operation['custom_field_id'].'']=='' && isset($operation['custom_field_id']))){
            $operation['operation']='get_survey';
            $operation['survey_id']=$operation['vis_survey'];
            $operation['servay_error_msg'] = 'true';
            $this->get_survey($operation);
            return false;
        } else {
            $database=new class_database();
            include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'client.php');
            $client=new class_client($database);
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
            $ticket_object=new class_ticket($database);
            $survey_id = $operation['vis_survey'];
            $custIds = $client->get_custom_fields_id($survey_id);
            $idArr = array();
            foreach($custIds as $id) {
                foreach($id as $key => $val) {
                    $idArr[] = $val;
                }
            }
            $checkExists = $ticket_object->get_customfield_data($idArr[0], VIS_CLIENT_ID, 4);
            if($checkExists == -1) {
                $ret = $client->save_client_survey($operation,VIS_LOCATION_AREA);
                $checkExists = 2;
            }
            $surveyAccess = 1;
			$operation = array();
			$header_title = $vis_language['TXTSURVEY'];
            include(VIS_PATH_TEMPLATE.'header.php');
			include(VIS_PATH_TEMPLATE.'client_survey.php');
			include(VIS_PATH_TEMPLATE.'footer.php');
			return true;
        }
	}
    function import_clients($operation)
    {
         global $vis_language;
         $database = new class_database(); 
         include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
         include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
         $object_ticket=new class_ticket($database);
         $object=new class_client($database);
         $domain_list=$this->get_domain_access_list();
         $client_group=$object->get_client_group(' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1', VIS_STAFF_ID, VIS_LOCATION_AREA);
         $filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
         $domain_details=$object_ticket->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
         
         include(VIS_PATH_TEMPLATE.'client_import.php');         
    }
    function import_client_list($operation){
          global $vis_language;
          $database = new class_database(); 
          $data=parse_form_array($operation['vis_action']);  
		  include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		  $setting_object=new class_setting($database);		  
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');  
          $object=new class_client($database);
          $arr_attachment = explode(',',$data['vis_import_attachments']);
          $filter = 'parent_type=4 and attachment_id='.$arr_attachment[0] ;
          $attach = $object->get_attachment($filter, VIS_STAFF_ID, VIS_LOCATION_AREA) ;
        
          if($attach!=-1){
             for($i=0;$i<count($attach);$i++){ 
                     $file_name =VIS_PATH_FILES.$attach[$i]['content'];
                     $file_handle = fopen($file_name, "r");
                     $first_row = fgetcsv($file_handle, 1000, ";");
					 $header_arr=explode(",",$first_row[0]);
					 $count=1;
                     while (!feof($file_handle) ){
						 
                          $line_of_text1 = fgetcsv($file_handle);
                          $line_of_text = array_combine($header_arr, $line_of_text1);
                          if(trim($line_of_text['EMAIL']) != "" ){ 
								if(trim($line_of_text['FIRST NAME']) == ""){
									$explode=explode('@', $line_of_text['EMAIL']);
									$data['vis_firstname'] = $explode[0];
								}else{
									$data['vis_firstname'] = $line_of_text['FIRST NAME'];
								}
								$data['vis_lastname'] = $line_of_text['LAST NAME'];
                              
								if(trim($line_of_text['USER NAME']) == ""){
									$data['vis_username'] = $line_of_text['EMAIL'];
								}else{
									$data['vis_username'] = $line_of_text['USER NAME'];
								}					  
							  $data['vis_password'] = generate_random('alfanum', 8);		
                              $data['vis_email'] = $line_of_text['EMAIL'];
                              $data['vis_contact_number'] = $line_of_text['CONTACT NUMBER'];
                              $data['vis_caddress'] = $line_of_text['TEMPORARY ADDRESS'];
                              $data['vis_paddress'] = $line_of_text['PERMANENT ADDRESS'];
                              $data['vis_city'] = $line_of_text['CITY'];
                              $data['vis_state'] = $line_of_text['STATE'];
                              $data['vis_zipcode'] = $line_of_text['ZIP CODE'];
                              $data['vis_country'] = $line_of_text['COUNTRY'];
                              $data['vis_website'] = $line_of_text['WEBSITE'];
                              $data['vis_fax'] = $line_of_text['FAX'];
                              $data['vis_office_name'] = $line_of_text['COMPANY NAME'];
                              $data['vis_office_email'] = $line_of_text['COMPANY EMAIL'];
                              $data['vis_office_address'] = $line_of_text['COMPANY ADDRESS'];
                              $data['vis_office_fax'] = $line_of_text['COMPANY FAX'];
                              $data['vis_office_website'] = $line_of_text['COMPANY WEBSITE'];
                              $data['vis_job_title'] = $line_of_text['JOB TITLE'];
                              $data['vis_gender'] = $line_of_text['GENDER'];
							  $data['vis_department'] = $line_of_text['DEPARTMENT'];
                              $data['vis_netmeeting'] = $line_of_text['NETMEETINGID'];
                              $data['vis_twitter_id'] = $line_of_text['TWITTER ID'];
                              $data['vis_facebook_id'] = $line_of_text['FACEBOOK ID'];
                              $data['vis_otherim'] = $line_of_text['OTHERIM'];
                              $data['vis_is_spam'] = $line_of_text['IS SPAM'];
                              $data['vis_is_banned'] = $line_of_text['IS BANNED'];
                              $data['vis_is_approved'] = $line_of_text['IS APPROVED'];
                              $data['vis_credit'] = $line_of_text['CREDIT'];
                              $data['vis_balance'] = $line_of_text['BALANCE'];
                              $lang_name="'".$line_of_text['LANGUAGE']."'";
                              $data['vis_gender']=(strtolower($data['vis_gender'])=='male')?0:1;
                              list($language_info, $language_details)=$setting_object->get_language(VIS_TABLE_PREFIX.'language.language_name='.$lang_name, '', 0, 0, VIS_STAFF_ID, VIS_LOCATION_AREA);
                              $data['vis_language'] = $language_details[0]['language_id'];
                              if($line_of_text['PARENT EMAIL']!='' || $line_of_text['PARENT EMAIL']!=NULL){
								  $parent_email="'".$line_of_text['PARENT EMAIL']."'";
								  $check_parent=$object->get_client('email='.$parent_email, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
								  $data['vis_parent_id']=($check_parent[0]['total']==1)?$check_parent[1][0]['client_id']:0;   
							  }
                              $client=$object->get_client('email="'.$line_of_text['EMAIL'].'" and '.VIS_TABLE_PREFIX.'client.domain_id="'.$data['vis_domain'].'"', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
                              if($client[0]['total'] < 1){ 
								$activity_log=$setting_object->add_staff_activity_log("Imported new client ".$data['vis_firstname']." ".$data['vis_lastname'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA);
								$res = $object->add_client($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
                            }
                          }   
						$count++;		
                     }
              }
             echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTIMPORT'].'",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
         }
         else echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';        
   }
   function clear_client_img($operation){
        global $vis_language;
        $database = new class_database(); 
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $filter='client_id='.$operation['vis_client_id'];
        $client_details = $object->get_client($filter,'',1,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
        $client_details=$client_details[1][0];
        if(IS_LOGIN==0 || ($operation['vis_client_id']!=VIS_CLIENT_ID && $client_details['parent_id']!=VIS_CLIENT_ID && VIS_LOCATION_AREA==1)){
            echo json_encode( array('vis_success' => 'false') );
            return false;
        }
        $res=$object->clear_client_img($operation['vis_client_id'],VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        echo json_encode( array('vis_success' => (($res)?'true':'false')) );
        return true;
   }
   function linkedin_auth(){
		session_start();
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=19 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);		
	    $config['base_url']             =   $row3[0]['db_password'].'/index.php?/client/linkedin_auth';
		$config['callback_url']         =   $row3[0]['db_password'].'/index.php?/client/linkedin_login';
		$config['linkedin_access']      =   $row3[0]['host_name'];
		$config['linkedin_secret']      =   $row3[0]['user_name'];
	
		include_once(VIS_PATH_PLUGINS.'linkedin/linkedin.php');
	
		# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
		$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
		//$linkedin->debug = true;
	
		# Now we retrieve a request token. It will be set as $linkedin->request_token
		$linkedin->getRequestToken();
		$_SESSION['requestToken'] = serialize($linkedin->request_token);
	  
		# With a request token in hand, we can generate an authorization URL, which we'll direct the user to
		//echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "\n\n";
		header("Location: " . $linkedin->generateAuthorizeUrl());
	}
	function linkedin_login(){
		global $request_data,$operation_hnd;
	   // session_start();
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=19 and domain_id='.VIS_DOMAIN;
        $row3=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);		
	    $config['base_url']             =   $row3[0]['db_password'].'/index.php?/client/linkedin_auth';
		$config['callback_url']         =   $row3[0]['db_password'].'/index.php?/client/linkedin_login';
		$config['linkedin_access']      =   $row3[0]['host_name'];
		$config['linkedin_secret']      =   $row3[0]['user_name'];
	
		include_once(VIS_PATH_PLUGINS.'linkedin/linkedin.php');	   
		
		# First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
		$linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['callback_url'] );
		//$linkedin->debug = true;
	
	   if (isset($_REQUEST['oauth_verifier'])){
			$_SESSION['oauth_verifier']     = $_REQUEST['oauth_verifier'];
	
			$linkedin->request_token    =   unserialize($_SESSION['requestToken']);
			$linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
			$linkedin->getAccessToken($_REQUEST['oauth_verifier']);
	
			$_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
			header("Location: " . $config['callback_url']);
			exit;
	   }
	   else{
			$linkedin->request_token    =   unserialize($_SESSION['requestToken']);
			$linkedin->oauth_verifier   =   $_SESSION['oauth_verifier'];
			$linkedin->access_token     =   unserialize($_SESSION['oauth_access_token']);
	   }
	    # You now have a $linkedin->access_token and can make calls on behalf of the current member
		$_SESSION['oauth_verifier'];
		$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,email-address,headline,picture-url)");
		//die;
		$linked_in = new SimpleXMLElement($xml_response);
		//print_r($linked_in);
		function _xml2array ( $linked_in, $out = array () ){
		foreach ( (array) $linked_in as $index => $node )
				$out[$index] = ( is_object ( $node ) ) ? _xml2array ( $node ) : $node;
			return $out;
		}
		$linked_in=_xml2array($linked_in);
		//echo $linked_in['email-address'];die;
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		$request_data['vis_email'] = $linked_in['email-address'];
		$request_data['vis_firstname'] = $linked_in['first-name'];
		$request_data['vis_lastname'] = $linked_in['last-name'];
		$request_data['vis_username'] = $linked_in['first-name'].' '.$linked_in['last-name'];
		$request_data['vis_module'] = 'home';
		$request_data['vis_is_approved'] = '1';
		$request_data['vis_group'] = '1';
		$request_data['vis_domain'] = VIS_DOMAIN;
		$request_data['vis_department'] = '0';
		$request_data['vis_login_as'] = '6';
		$filter1= "email='".$request_data['vis_email']."'";
		list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);				
		if($row_count==-1){
			$register = $object->add_client($request_data,'0','1');	
			$filter = "email='".$request_data['vis_email']."'";	
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);			
			$operation_hnd->is_login();		
		} else {
			$filter = "email='".$request_data['vis_email']."'";
			$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);		
			$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);		
			$operation_hnd->is_login();
		}
		
	}
    function department_access($operation){
        global $vis_language;
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();     
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'subsetting.php');
		$sub_object=new class_subsetting($database);
        list($department_info,$department_details)=$sub_object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'department.domain_id='.$operation['vis_domain_id'],'',0,0,VIS_STAFF_ID, VIS_LOCATION_AREA,1);
        $filter='client_id='.$operation['vis_client_id'];
        list($info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $client_details=$client_details[0];
        $department_access=explode(',',$client_details['department_access']);
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'client_departments.php');
        return true;
    }
    
    function update_client_department($operation){
        global $vis_language;
        $data=parse_form_array($operation['vis_action']);
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once( VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php' );
        $setting = new class_setting($database);
        $ret=$object->update_client_department($data, VIS_CLIENT_ID, VIS_LOCATION_AREA); 
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
			$filter='client_id='.$data['vis_client_id'];
            list($info, $client_details)=$object->get_client($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_details_list").visExecution("load_operations",{"vis_module":"client", "vis_action":"details", "vis_operation":"department_access", "vis_client_id":"'.$data['vis_client_id'].'","vis_domain_id":"'.$operation['vis_domain_id'].'"});</script>';
			$name=((isset($client_details[0]['first_name']) && $client_details[0]['first_name']!='') || (isset($client_details[0]['last_name']) && $client_details[0]['last_name']!=''))?$client_details[0]['first_name']." ".$client_details[0]['last_name']:"";
			$email=(isset($client_details[0]['email']) && $client_details[0]['email']!='')? "( ".$client_details[0]['email']." ) ":"";
			$setting->add_staff_activity_log("Changed department access of client ".$name.$email, VIS_STAFF_ID,1,VIS_LOCATION_AREA);
		}
        $database->database_disconnect();
        return true;
    }
    
    function verify_email($operation){
        global $vis_language,$operation_hnd;
        $spliter=(strstr($_SERVER['QUERY_STRING'], '/'))?'/':',';
        $action=explode($spliter, $_SERVER['QUERY_STRING']);
        $breadcrumb['Home'] = 'index.php'; 
        $breadcrumb[$vis_language['TXTVERIFYEMAIL']] = ''; 
        $vis_title = $vis_language['TXTVERIFYEMAIL'];
        $header_title=$vis_language['TXTVERIFYEMAIL'];
        if(isset($action[3]) && $action[3]!='') {
            $client_email=convert_uudecode (base64_decode($action[3]));
            $database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
            $object=new class_client($database);
            $res=$object->verify_email($client_email, VIS_CLIENT_ID, VIS_LOCATION_AREA);
            if($res == 1)
            {
				$client_details = $object->get_client('email="'.$client_email.'" and '.VIS_TABLE_PREFIX.'client.domain_id = "'.VIS_DOMAIN.'"', '', '', '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
				$client_details = $client_details[1][0];
			    $operation['domain_id'] = VIS_DOMAIN;
			    $operation['is_verified'] = $res;
				$operation['vis_client_id'] = $client_details['client_id'];
				$operation['vis_password']=password_decrypt($client_details['authentication'],VIS_PASSWORD_KEY);
				$operation['authentication']=$client_details['authentication'];		
				$operation['vis_is_approved'] = $client_details['is_approved'];
				$operation['vis_firstname'] = $client_details['first_name'];
				$operation['vis_lastname'] = $client_details['last_name'];
				$operation['vis_username'] = $client_details['user_name'];
				$operation['vis_email'] =$client_details['email'];
				$operation['vis_operation'] ='two_factor_auth';
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
                $mobject=new class_mail($database);
                $operation_hnd->operation_flow($operation, VIS_STAFF_ID, VIS_LOCATION_AREA, $database);
			}
			$database->database_disconnect();
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'client_verify_email.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
            return true;
        }
    }
    
    function reset_password($operation){
	    global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $spliter=(strstr($_SERVER['QUERY_STRING'], '/'))?'/':',';
        $action=explode($spliter, $_SERVER['QUERY_STRING']);
        $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if(isset($action[3]) && $action[3]!='') {
            $client_email=base64_decode($action[3]);
            $passwd_auth=$action[4];
        }else{
            $client_email=$operation['vis_email'];
            $passwd_auth=$operation['vis_passwd_auth'];
        }
        $breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTRESETPASSWORD']] = ''; 
		$vis_title = $header_title = $vis_language['TXTRESETPASSWORD'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'client_reset_password.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }
    
    function set_forgetpassword($operation){ 
		if(VIS_LOCATION_AREA==2) {
            $operation=parse_form_array($operation['vis_action']);
        }
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
		$filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		$security_password_details=$object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$security_password=($security_password_details==-1)?50:$security_password_details[0]['content'];
		if($operation['vis_newpassword']=='') {
			$operation['message']='<div class="vis_error_message">'.$vis_language['MSGREQFLDPSWD'].'</div>';
			if(VIS_LOCATION_AREA==2){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGREQFLDPSWD'].'"); </script>';
				return false;
			}else{ return $this->reset_password($operation); }
		}
		if($operation['password_strength_per']<$security_password) {
            $operation['message']='<div class="vis_error_message">'.$vis_language['TXTPASSWORDNOTALLOWED'].'</div>';
			if(VIS_LOCATION_AREA==2){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['TXTPASSWORDNOTALLOWED'].'"); </script>';
				return false;
			}else{ return $this->reset_password($operation); }
        }
        if($operation['vis_newpassword']!=$operation['vis_confirm']) {
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGPASSMISSMTCH'].'</div>';
			if(VIS_LOCATION_AREA==2){
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGPASSMISSMTCH'].'"); </script>';
				return false;
			}else{ return $this->reset_password($operation); }
        }
	    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $filter=' email="'.$operation['vis_email'].'"';
		if(VIS_LOCATION_AREA!= 2){
			$filter .= ' and password_auth='.convert_uudecode (base64_decode($operation['vis_passwd_auth']));
		}		
        list($clientsinfo,$clients)=$object->get_client($filter, '', '', '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
        if($clients==-1 || $clients==''){
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGRESETPASSWORDLINKEXPIRED'].'</div>';
            return $this->reset_password($operation);
        }else{
            $res=$object->set_forgetpassword($operation, VIS_CLIENT_ID, VIS_LOCATION_AREA);
            $database->database_disconnect();
            if($res){
               	if(VIS_LOCATION_AREA==2){ 
					echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $(".modal").modal("toggle");</script>';
					return true;
				}else { $_SESSION['successmessage']=$vis_language['MSGSUCCOPRT'];}
            }
            header('Location: index.php?/home/login');
        }
    }
    
    function new_note($operation){
        $message_info['client_id']=(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id']))?$operation['vis_client_id']:0;
        $onlyclientnote=1;
        include(VIS_PATH_TEMPLATE.'ticket_note.php');
		return true;
    }
    
    function reset_password_call_client($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $password=generate_random('alfanum', 8);
        $object->set_password_call_client($password,$operation['vis_client_id'],VIS_CLIENT_ID, VIS_LOCATION_AREA);
        include(VIS_PATH_TEMPLATE.'reset_password_call_client.php');
        return true;
    }
	function magento($operation){
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $database=new class_database();
        $object=new class_setting($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff_object=new class_staff($database);
        $staff_access=$staff_object->get_staff('staff_id='.VIS_STAFF_ID,VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_department=$staff_access[0]['department_access'];
         if($staff_department==-2)
        {
			$filter = VIS_TABLE_PREFIX.'staff_group.group_id = '.VIS_TABLE_PREFIX.'staff.group_id and '.VIS_TABLE_PREFIX.'staff_group.group_id='.$staff_access[0]['group_id'].' and '.VIS_TABLE_PREFIX.'staff.staff_id='.VIS_STAFF_ID;
			list($staffgroup_info, $staff_group)=$object->get_staffgroup_ofstaff($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			$staff_department_array=explode(',', $staffgroup_info['department_access']);
		}
		else
		{
			$staff_department_array=explode(',', $staff_department);
		}
		$dept=array();
		$domain_access=array();
		for($i=0;$i<count($staff_department_array); $i++)
		{
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
			$ticket_object=new class_ticket($database);
			if($staff_department_array[$i]!=0)
			{
				$department=$ticket_object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'department.department_id='.$staff_department_array[$i], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				$dept[]=$department[0];
			}
			else
			{
				$department=$ticket_object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 ', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
				$dept[]=$department[0];
			}
		}
		for($i=0;$i<count($dept); $i++)
		{
			if($dept[$i]!='') $domain_access[]=$dept[$i]['domain_id'];
		}
		$domain_access=array_unique($domain_access);
		$domain_access=implode(',', $domain_access);
		if(substr($domain_access, -1)==',')
		$domain_access=substr($domain_access,0,-1);
		$filter=VIS_TABLE_PREFIX.'domain.is_enabled=1 AND '.VIS_TABLE_PREFIX.'domain.domain_id in ('.$domain_access.')';
		list($dpinf, $department)=$object->get_domain($filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		$magento_filter='integration_type=31 AND is_enabled=1 AND ('.VIS_TABLE_PREFIX.'integration.domain_id in ('.$domain_access.'))';
        list($count, $magento_details)=$object->get_integration_setting($magento_filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		include(VIS_PATH_TEMPLATE.'magento.php');
	}
	function get_magento_client($operation){
		global $vis_language;
        $database=new class_database();
		$data= isset($operation['vis_action']) && $operation['vis_action']!='' ? parse_form_array($operation['vis_action']) : $operation;
		
		if(!isset($data['vis_search_by'])){
			echo json_encode( array('status' => 'error', 'msg'=> $vis_language['MSGINFOMISS']));
			return false;
		}
		else{
			if($data['vis_search_by']=='email'){				
				if(isset($data['vis_search_string']) || trim($data['vis_search_string'])!='')
				{
					list($message, $flag) = check_email_validation($data['vis_search_string']);
					if(!$flag) {
						echo json_encode( array('status' => 'error', 'msg'=> $message));
					   return false;
					} 
				}	
			}else if($data['vis_search_by']=='name'){
				if(!isset($data['vis_search_string']) || trim($data['vis_search_string'])=='') {
					echo json_encode( array('status' => 'error', 'msg'=> $vis_language['MSGINFOMISS']));
					return false;
				}
			}else{
				echo json_encode( array('status' => 'error', 'msg'=> $vis_language['MSGINFOMISS']));
				return false;
			}
		}
		if(!isset($data['vis_domain']) || !is_numeric($data['vis_domain'])){
			echo json_encode( array('status' => 'error', 'msg'=> $vis_language['MSGERROPRT']));
			return false;
		}
		
		$magento_result = array();
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='integration_type=31 AND is_enabled=1 AND domain_id='.$data['vis_domain'];
        list($magento_info, $magento_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(count($magento_details)>0 && $magento_details!=-1){
			foreach($magento_details as $magento){
				$magento_client =array();
				$client = new SoapClient($magento['billing_url'].'/api/soap/?wsdl');
				$session_magento = $client->login($magento['user_name'], $magento['api_key']);
				if($data['vis_search_by']=='email'){
					$filter_array = array(array('email' => $data['vis_search_string']));
				}else if($data['vis_search_by']=='name'){
					$explode = explode(' ', $data['vis_search_string']);
					$filter_arr['firstname'] = $explode[0];
					if(isset($explode[1]))$filter_arr['lastname'] = $explode[1];
					$filter_array = array($filter_arr);
				}
				$client_list = $client->call($session_magento, 'customer.list', $filter_array);
				
				if(count($client_list)>0){
					foreach($client_list as $client_info){
						$magento_client[]=$client_info; 
					}
					if(count($magento_client)>0){
						$all_client['integration_details']=$magento;
						$all_client['client']=$magento_client;
						$all_magento_client[] = $all_client;
					}
				} 
				$client->endSession($session_magento);
			}
		}
		ob_start();
		include(VIS_PATH_TEMPLATE.'magento_list.php');
		$magento_list = ob_get_clean();
		ob_end_flush();
		echo json_encode( array('status' => 'success', 'msg'=> '',  'list'=> $magento_list));
	}
	function magento_client($operation){
		global $vis_language;
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		if(!isset($operation['integration_id']) || !isset($operation['customer_id'])){
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';  
			return false;
		}
		//print_r($operation);
		$filter='integration_id='.$operation['integration_id'];
        list($magento_info, $magento_details)=$object->get_integration_setting($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(count($magento_details)>0 && $magento_details!=-1){
			$magento = $magento_details[0];
			$client = new SoapClient($magento['billing_url'].'/api/soap/?wsdl');
			$session_magento = $client->login($magento['user_name'], $magento['api_key']);
			$client_info = $client->call($session_magento, 'customer.info', $operation['customer_id']);
			$order_list = $client->call($session_magento, 'order.list', array(array('customer_id' => $operation['customer_id'])));
			//echo "<pre>";print_r($client_info);print_r($order_list);
			$client->endSession($session_magento);
		}
		include(VIS_PATH_TEMPLATE.'magento_orders.php');
        return true;
		
	}
	function client_status_details($operation)
	{
		global $vis_language;
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		$filter= VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
		list($contact_info, $contact_details)=$object->get_client($filter, '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		if($contact_details!=-1)
		{
		    if($contact_details[0]['is_banned'] == 1 )echo 3;
		    else if($contact_details[0]['is_spam'] == 1) echo 4;
		    else if($contact_details[0]['is_approved'] == 0) echo 2;
			else if($contact_details[0]['is_verified'] == 0) echo 5;
			else if($contact_details[0]['is_approved'] != 0 || $contact_details[0]['is_banned'] != 1 || $contact_details[0]['is_spam'] != 1 || $contact_details[0]['is_verified'] != 0 ) echo 1 ;
		}
		else
		{
			echo 0;
		}
	}
	
	function get_ci_list($operation){
		global $vis_language;
        $filter='';
		$view_only=0;		
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'cmdb.php');
        $cmdb_object=new class_cmdb($database);
        if(VIS_LOCATION_AREA==2)
		{
			$filter =VIS_TABLE_PREFIX."client.client_id=".$operation['vis_client_id'];
			$assets_details=$cmdb_object->get_assets($filter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
			include(VIS_PATH_TEMPLATE.'cmdb_assets_list.php');
		}
		else if(VIS_LOCATION_AREA==1 && IS_LOGIN)
		{
			$filter =VIS_TABLE_PREFIX."client.client_id=".VIS_CLIENT_ID;
			$assets_details=$cmdb_object->get_assets($filter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA,1);
			$header_title=$vis_language['TXTASSETS'];
            $vis_title=$vis_language['TXTASSETS'];	
			$breadcrumb['Home'] = 'index.php'; 
			 $vis_count =  $assets_list_count;
			$breadcrumb[$vis_language['TXTASSETS']] = '?/client/get_ci_list';
            if(!defined('IS_WHMCS')) include(VIS_PATH_TEMPLATE.'header.php');
            else echo '<link href="'.VIS_PORTAL_URL.VIS_PATH_STYLE.'vis_style.css" rel="stylesheet" />';
			 include(VIS_PATH_TEMPLATE.'cmdb_assets_list.php');
            if(!defined('IS_WHMCS')) include(VIS_PATH_TEMPLATE.'footer.php');
			
		}
		else {
			$assets_details=-1;
            if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
            if(!defined('IS_WHMCS')) include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'login.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
            break;
            }            
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGACCSSDND'].'</div>';
        }
		$database->database_disconnect();
		return true;
	}
	function get_domain_access_list(){
		global $vis_language;
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
		$tkt_object=new class_ticket($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $staff_object=new class_staff($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
        $staff_access=$staff_object->get_staff('staff_id='.VIS_STAFF_ID,VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff_department=$staff_access[0]['department_access'];
         if($staff_department==-2)
        {
			$tempfilter = VIS_TABLE_PREFIX.'staff_group.group_id = '.VIS_TABLE_PREFIX.'staff.group_id and '.VIS_TABLE_PREFIX.'staff_group.group_id='.$staff_access[0]['group_id'].' and '.VIS_TABLE_PREFIX.'staff.staff_id='.VIS_STAFF_ID;
			list($staffgroup_info, $staff_group)=$setting_object->get_staffgroup_ofstaff($tempfilter, VIS_STAFF_ID, VIS_LOCATION_AREA);
			if($staffgroup_info['department_access']==0)
			$staff_department_array=$staffgroup_info['department_access'];
			else
			$staff_department_array=explode(',', $staffgroup_info['department_access']);
		}
		else
		{
			$staff_department_array=explode(',', $staff_department);
		}
		if($staff_department_array[0]!=0)
		{
			$dept=$domain_list=array();
			for($k=0; $k<count($staff_department_array); $k++){
				$department_details=$tkt_object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'department.department_id='.$staff_department_array[$k],VIS_STAFF_ID, VIS_LOCATION_AREA);
				$dept[]=$department_details[0];
			}
			for($d=0;$d<count($dept);$d++){
				$domain_list[]=$dept[$d]['domain_id'];
			}
			$domain_list=array_unique($domain_list);
			$domain_list=implode(',', $domain_list);
			if(substr($domain_list, -1)==',') $domain_list=substr($domain_list,0,-1);
			
		}
		else{
			$department_details=$tkt_object->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 ',VIS_STAFF_ID, VIS_LOCATION_AREA);
			for($d=0;$d<count($department_details);$d++){
				$domain_list[]=$department_details[$d]['domain_id'];
			}
			$domain_list=array_unique($domain_list);
			$domain_list=implode(',', $domain_list);
			if(substr($domain_list, -1)==',') $domain_list=substr($domain_list,0,-1);
		}
		$domain_list = trim($domain_list, ',');
		return $domain_list;
	}
	function list_contents($operation) {
		 global $vis_language;
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		$domain_list=$this->get_domain_access_list();
		$filter=' (domain_id in ('.$domain_list.') or domain_id=0 ) and is_enable=1';
        $group=$object->get_client_group($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,'');
        $flag=$operation['vis_action'];
        include_once(VIS_PATH_TEMPLATE.'client_properties.php');
        return true;
    }
     function update_properties($operation) {
		$list_chk=array();
        global $vis_language;
        if(isset($operation['vis_ticket_id']) && is_numeric($operation['vis_ticket_id'])) {
            $property=$operation['vis_action'];
            $list_chk[]=$operation['vis_ticket_id'];
        }
        else {
            $options=parse_form_array($operation['vis_action']);
            foreach($options as $data=>$value) if(preg_match('/vis_global_mass_chk_[0-9]/', $data)) $list_chk[]=$value;
        }
        if(VIS_LOCATION_AREA==1) $user=VIS_CLIENT_ID;
		else  if(VIS_LOCATION_AREA==2)  $user=VIS_STAFF_ID;
		if(isset($operation['vis_update_id']) && is_numeric($operation['vis_update_id'])) {
			$database=new class_database();
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
			$client_object=new class_client($database);
			$ret=$client_object->set_client_property($operation, $list_chk, $user, VIS_LOCATION_AREA);
			$database->database_disconnect();
			if($ret)
			{
				echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTCLIENTS'].'",{"vis_module":"client", "vis_operation":"view_clients"});</script>';
			}
			else{
				 echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGMOVETOORG'].'");</script>';
                    return false;
			}
		 }
	 }
	  function send_custom_email($operation){
		 $data =$operation;
		 global $vis_language;
		 $database=new class_database();
   		  include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
		  $mobject=new class_mail($database);
		  
   		  include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		  $obj_client=new class_client($database);
		  list($client_info, $client_details)=$obj_client->get_client(' client_id='.$data['vis_client_id'], '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
		  if($client_details!=-1){$data['vis_email']=$client_details[0]['email'];
			  $data['vis_to_email']=$client_details[0]['email'];}
		  $data['vis_is_approved']= 1;
		  $ret=true;
			  switch($data['vis_template_id']) {
				  case '1':
					$template_name = 'add_client'; 
				  break;
				  case '5':
					$template_name = 'send_password';
				  break;
				  case '6':
					$template_name = 'client_approval';
				  break;
				  case '16':
					$template_name = 'two_factor_auth';
				  break;
				  default:
					$ret=false;
				  break;
				  
			  }
		  if($data['vis_template_id']>28){
		  	  $template_name = 'custom_mail';
			  $data['template_id']=$data['vis_template_id']; 
			  $ret=true;
		  }
		  if($ret!=false){
		  	$ret=$mobject->email_notify($data, $template_name, VIS_STAFF_ID, VIS_LOCATION_AREA);
		  }
		  if($ret){
			  echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");</script>';
		  }
		  else{
			   echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'");</script>';
				  return false;
		  }
		  $database->database_disconnect();
	 }
	 function open_custom_email($operation){
		 global $vis_language;
		 $data = $operation;
		 $database=new class_database();
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
         $sett_obj=new class_setting($database);
			list($info, $etemplate)=$sett_obj->get_etemplate('is_enabled=1 and language=1 and (etemplate_id>24 or etemplate_id=1 or etemplate_id=5 or etemplate_id=6 or etemplate_id=16)', '', 0, 'etemplate_name', VIS_STAFF_ID, VIS_LOCATION_AREA);
	  	 $database->database_disconnect();
         include(VIS_PATH_TEMPLATE.'send_custom_email.php');
         return true; 
	 }
	function get_client_details($operation)// API Function for get salt for client auto login 
    {
		$database=new class_database();
        global $vis_language;
        $filter='';
        if(isset($operation['vis_email']) && $operation['vis_email']!='') $filter=' email = "'.$operation['vis_email'].'"';
        if(isset($operation['vis_domain']) && $operation['vis_domain']!='') $filter.=' and domain ="'.$operation['vis_domain'].'"';
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        $limit=(isset($operation['vis_limit']) && $operation['vis_limit']!='')?$operation['vis_limit']:'';
        list($client_info, $client_details)=$object->get_client($filter, $limit, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        $client_info['client_count']=(!$client_details || $client_details==-1)?0:count($client_details);
        $data=(isset($operation['vis_details_req']) && $operation['vis_details_req']==1)?array('details'=>$client_info, 'data'=> $client_details):$client_details;
        if($client_details==-1) echo $vis_language['TXTNOMATCHFOUND'];
        else echo get_xml($data[0]['salt'], 'Salt');
        return true;
	}
	function open_custom_reset_pass($operation){ //echo '<pre>'; print_r($operation); echo '</pre>';
		 global $vis_language;
		 $client_details = $data = $operation;
		 $database=new class_database();
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
         $object=new class_setting($database);
		 $filter=VIS_TABLE_PREFIX.'settings.title="vis_security_password"';
		 $security_password_details=$object->get_settings($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		 $security_password=($security_password_details==-1)?50:$security_password_details[0]['content'];
			//list($info, $etemplate)=$object->get_etemplate('is_enabled=1 and language=1 and (etemplate_id>24 or etemplate_id=1 or etemplate_id=5 or etemplate_id=6 or etemplate_id=16)', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
	  	 $database->database_disconnect();
         include(VIS_PATH_TEMPLATE.'client_reset_pass_popup.php');
         return true; 
	 }
	 function get_groupname($operation){
		$database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        if($operation['vis_domain']!='')
		{	
			if(isset($operation['vis_add_client_m']) && $operation['vis_add_client_m']==1){
				$filter=  '('.VIS_TABLE_PREFIX.'client_group.group_name like "%'.$operation['vis_data'].'%" and '.VIS_TABLE_PREFIX.'client_group.domain_id='.$operation['vis_domain'].') or '.VIS_TABLE_PREFIX.'client_group.domain_id=0';
			}else{
				$filter=  VIS_TABLE_PREFIX.'client_group.group_name like "%'.$operation['vis_data'].'%" and '.VIS_TABLE_PREFIX.'client_group.domain_id='.$operation['vis_domain'];
			}
		}else
		{
			$filter= VIS_TABLE_PREFIX.'client_group.group_name like "%'.$operation['vis_data'].'%" or  FIND_IN_SET("'.strtolower($operation['vis_data']).'" , REPLACE( domain_list, "", "" ) ) ';
		}
        $report=$object->get_groupname($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$database->database_disconnect();
		if($report==-1){ if(VIS_LOCATION_AREA==0) echo 'No data found'; else echo '{}'; return false;}
		$count=count($report);
        $result='[';
        if($report!=-1) for($i=0;$i<$count;$i++) {
            $result.='{"id":"'.$report[$i]['group_id'].'","group_name":"'.$report[$i]['group_name'].'"},';
        }
        $result=substr($result, 0, -1);
        if($result=='') return false;
        $result.=']';
        echo $result; 
        return true;
	}
	function update_organization($operation){
		$parts = explode("/", $operation['vis_action']);
		global $vis_language;
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
        }
        $header_title = $vis_language['TXTORGANIZATION'];
        $vis_title = $vis_language['TXTORGANIZATION'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $object=new class_client($database);
        $client=$object->get_organization_data('group_id='.$parts[3],'',0,'',0,1);
      
        $header_title=$vis_language['TXTORGANIZATION'];
		$breadcrumb['Home'] = 'index.php'; 
        $breadcrumb[$vis_language['TXTORGANIZATION']] = '?/client/view-organization/'.$parts[3].''; 
        $breadcrumb[$client[0]['group_name']] = '';
         /////////////////////////
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
		$ticket=new ticket();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');    
		$ticket_obj=new class_ticket($database);
		$location[0] = 12;
		$filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;                   
        $departments=$ticket_obj->get_department($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $departmentid = $departments[0]['department_id'];
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$setting_obj=new class_setting($database);
        
		$filt= ' location=12 and  is_enabled= 1 ';
        $custom_check = $setting_obj->get_custom_group_list($filt, VIS_STAFF_ID, VIS_LOCATION_AREA);
		$location=array(0=>12, 1=>0);
		 if($custom_check[0]['visible_to']==1 or $custom_check[0]['visible_to']==0) 
		{
			//$custom_field_var = $ticket->edit_custom_fields($location, $parts[3], 0, $database,0,0,1);
			$custom_field_var = $ticket->get_custom_fields($location, 0, $departmentid, $parts[3], $database);
		}
		/////////////////
		  $database->database_disconnect();

        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'edit_client_group.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
	}
	function update_organization_details($operation) {
        global $vis_language;
		$data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		$flag=true;
		$operation['vis_action'] = '#/'.$data['vis_module'].'/'.$data['vis_operation'].'/'.$data['vis_group_id'].'';
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		/*if($data['vis_name']==''){
			 $operation['message']='<div class="vis_message">Title missing</div>';
			 $flag=false; 
		}*/
		if($flag){
			list($message, $flag) = check_email_validation($data['vis_email']);
			if(trim($data['vis_email'])!='' && !$flag)
			{
				$operation['message']='<div class="vis_message">'.$message.'</div>';
				$operation['error']=0;
			}
			else{
				$ret=$object->update_client_organization($data,'', VIS_LOCATION_AREA);
				$res='';
				if(isset($data['vis_customfields']) || $data['vis_customfields']==1){
					include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php'); 
					$object_ticket = new ticket();
					$data['vis_department']=$data['vis_department_id'];
					//////////echo "Updating  organization custom fields";
					$res=$object_ticket->update_customfields($data);
				}
				if(!$ret) {
					$operation['message']='<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
					return false;
				}else if($res!=1 && $res['status']==''){
					$operation['message']='<div class="vis_message">'.$res['vis_err_msg'].'</div>';
					$operation['error']=0;
				}else{
					$operation['message']='<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>';
					$operation['success']=1;
				}
			}
		}
		$this->update_organization($operation);
        $database->database_disconnect();
        return true;
    }
	function add_email($operation){
		global $vis_language;
		$flag=true;
		$data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		if($data['vis_additional_email']==''){
			 echo "blank";
			 return false;
		}
		if(isset($data['vis_additional_email']) && trim($data['vis_additional_email'])!='')
		{
			list($message, $flag) = check_email_validation($data['vis_additional_email']);
			if(!$flag) {
				echo "err";
				return false;
			} 
		}
		if(isset($operation['vis_domain']) && is_numeric($operation['vis_domain'])) $data['vis_domain']=$operation['vis_domain'];
		if(VIS_LOCATION_AREA==1) $data['vis_domain']=VIS_DOMAIN; 
		$vis_domain =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : $data['vis_domain'];
		$data['vis_domain'] = $vis_domain;
		$result=$object->add_email($data,'', VIS_LOCATION_AREA);
		if($result==-1){         //value insert
		 $id=$data['vis_client_id'];
			$filter=' additional_email="'.$data['vis_additional_email'].'" and 	is_verified=0 and client_id='.$id;
			$resdata=$object->get_client_addtion_email($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
		if($resdata!=-1){
																$data['vis_verify_email']=$resdata[0]['email_id'];
																include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
                $mobject=new class_mail($database);
                $data['vis_module']='client';
                $data['is_verified']= 0;
				$data['domain_id']=$data['vis_domain'];												
				$data['vis_static_area']=1;
                $ret=$mobject->email_notify($data, 'two_factor_auth', VIS_STAFF_ID, VIS_LOCATION_AREA);
				
			}
			$list=$object->display_email($id,'', VIS_LOCATION_AREA);
			$count=count($list);
			if($list!=-1) for($i=0;$i<$count;$i++) {
				$title='';$class='';$style='';
				if($list[$i]['is_verified']!=1){
					$title=$vis_language['TXTRESEND'];
					$style="color: #428bca;";
					$style1="text-decoration:line-through;";
				}else{
				 $title="";
				 $style="color: #428bca;"; 
				 $class="not-active";
					$style1="";
				}
				$str.='<p><span style="'.$style1.'">'.$list[$i]['additional_email'].'</span> <a href="javascript:void(0)" value="'.$list[$i]['email_id'].'" onClick="removedata('.$list[$i]['email_id'].');"><i class="vh vh-cross-slim"></i></a> <a href="javascript:void(0)" value="'.$list[$i]['email_id'].'" onClick="emailVerify('.$list[$i]['email_id'].');" style="'.$style.'" class="'.$class.'">'.$title.'</a></p>';
			}
			if($str=='') return false;
			echo $str;
		    $database->database_disconnect();
		}else{                  //already exist
			echo "repeat";
		}
		
	}
	function remove_email($operation){
		global $vis_language;
		$data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		$database=new class_database();
		include_once( VIS_PATH_CLASS . VIS_CLASS_PREFIX . 'setting.php' );
        $setting = new class_setting($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
								// get detail
								$name='';$email='';$additionalemail='';
								$filter='email_id='.$data['vis_remove_email'];
								$client_additional_details =$object->get_client_addtion_email($filter,VIS_STAFF_ID,VIS_LOCATION_AREA);
			if($client_additional_details!=-1)
			{
				$additionalemail=$client_additional_details[0]['additional_email'];
				$filter = VIS_TABLE_PREFIX.'client.client_id='.$client_additional_details[0]['client_id'];
   list($clientinfo,$client_details) =$object->get_client($filter,'0,1','','',VIS_STAFF_ID,VIS_LOCATION_AREA);
		
			if($client_details!=-1){
						$name=$client_details[0]['first_name'].' '.$client_details[0]['last_name'];
						$email=$client_details[0]['email'];
			}
			}
			
        $result=$object->email_remove($data,'', VIS_LOCATION_AREA);
		if($result==1){
			//add to log
			if(VIS_LOCATION_AREA!=1){
			$setting->add_staff_activity_log("Delete additional email (".$additionalemail.") of client ".$name.' ('.$email.')', VIS_STAFF_ID,1,VIS_LOCATION_AREA);
			}
			
			$id=$data['vis_client_id'];
			$list=$object->display_email($id,'', VIS_LOCATION_AREA);
			$count=count($list);
			if($list!=-1) for($i=0;$i<$count;$i++) {
				$title='';$class='';$style='';
				if($list[$i]['is_verified']!=1){
					$title=$vis_language['TXTRESEND'];
					$style="color: #428bca;"; 
					$style1="text-decoration:line-through;";
				}else{
				     $title="";
				     $style="color: #428bca;"; 
				     $class="not-active";	
									$style1="";
				}
				$str.='<p><span style="'.$style1.'">'.$list[$i]['additional_email'].'</span>&nbsp;&nbsp;<input type="hidden" id="vis_client_id" name="vis_client_id" value="'.$id.'"> <a href="javascript:void(0)" value="'.$list[$i]['email_id'].'" onClick="removedata('.$list[$i]['email_id'].');"><i class="vh vh-cross-slim"></i></a>&nbsp;<a href="javascript:void(0)" value="'.$list[$i]['email_id'].'" onClick="emailVerify('.$list[$i]['email_id'].');" style="'.$style.'" class="'.$class.'">'.$title.'</a></p>';
			}
			if($str=='') return false;
			echo $str;
		    $database->database_disconnect();
		}
		
	}
	function check_verify_email($operation){
		global $vis_language;
        $data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
        $database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $setting_object=new class_setting($database);
			if($data['vis_static_area']!=''){
                $object->unverify_client_email($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
			}
                include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
                $mobject=new class_mail($database);
                $data['vis_module']='client';
                $data['is_verified']= 0;
                $ret=$mobject->email_notify($data, 'two_factor_auth', VIS_STAFF_ID, VIS_LOCATION_AREA);
		echo $ret;
	   $database->database_disconnect();
	}
	function get_additional_emails($operation) {
        global $vis_language;
        $title=$vis_language['TXTADDITIONALEMAILS'];
        include(VIS_PATH_TEMPLATE.'additional_email.php');
        return true;
    }

	function additional_emails($operation){
		global $vis_language;
		$data=(isset($operation['vis_action']) && $operation['vis_action']!='')?parse_form_array($operation['vis_action']):$operation;
		if(isset($data['vis_additional_email']) || trim($data['vis_additional_email'])!='')
		{
			list($message, $flag) = check_email_validation($data['vis_additional_email']);
			if(!$flag) {
				 list($message, $flag1) = check_email_validation($data['vis_tempemail']);
				 if($flag1) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCANNOTMERGEOTHERCLIENTEMAIL'].'"); </script>';
            }else
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGEMAILMISS'].'"); </script>';
            return false;
			} 
		}
        $database=new class_database();
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		$result=$object->add_email($data,'', VIS_LOCATION_AREA);
		if($result==-1){
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");</script>';
			
		}else{
			 echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "Already exist"); </script>';
		}
		$database->database_disconnect();
        return true;
		
	}
	
	//santosh
	
	function report($operation) {
        global $vis_language; 
        if(VIS_LOCATION_AREA==1 && IS_LOGIN==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
         }       
        if(VIS_LOCATION_AREA==1 && defined('VIS_CLIENT_ID') && VIS_CLIENT_ID!=0 && VIS_CLIENT_ID!=-1) {
            $client_id=VIS_CLIENT_ID;
        }        
        elseif(VIS_LOCATION_AREA==2 && isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) {
            $client_id=$operation['vis_client_id'];
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCLIENDIDMISS'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
	    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_client($database);
        $module_object=new class_setting($database);
		 list($client_info, $clientdetails)=$object->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
		 
		
		 
		  $client=$clientdetails[0]; 
		//if($client['is_manager']==1){
			
		//	$client_res=$object->get_client_list('group_id='.$client['group_id'].' and is_approved='.$client['is_approved'].' and is_spam='.$client['is_spam'].' and is_verified='.$client['is_verified'],VIS_CLIENT_ID,VIS_LOCATION_AREA); 
		//$client_res=$object->get_client_list('group_id='.$client['group_id'].' and is_approved=1 and is_spam=0  ',VIS_CLIENT_ID,VIS_LOCATION_AREA); 
		  
		
		 $module_setting=$module_object->get_module_setting(VIS_LOCATION_AREA);  
		
		
		
		
        $filter=VIS_TABLE_PREFIX.'client.parent_id='.$client_id;
        $staff=new class_staff($database);
        list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);	
        if(VIS_LOCATION_AREA==1) $view_info['page_count']=10;
		$page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;         
   		$limit=($page*$view_info['page_count']).','.$view_info['page_count'];	
        $subaccount_details=$object->get_subaccount($filter, VIS_STAFF_ID, VIS_LOCATION_AREA,$limit);
        $subaccount_info['client_id']=$client_id;
        $subaccount_details_total=$object->get_subaccount($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);	
	   // $subaccount_info['count']=($subaccount_details_total==-1)?0:count($subaccount_details_total);
		//$view_info['page_count']=($subaccount_info['count']==0)?0:ceil($subaccount_info['count']/$view_info['page_count']);		
        $database->database_disconnect();
		$subaccount_info['page']=$page;	
        if(!$subaccount_details) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        $header_title=$vis_language['TXTREPORTS'];
        if(VIS_LOCATION_AREA==1) {
			$breadcrumb['Home'] = 'index.php';  
			$breadcrumb[$vis_language['TXTREPORTS']] = ''; 
			$vis_count = $subaccount_info['count'];
			$vis_title = $vis_language['TXTREPORTS'];
			   if(isset($operation['vis_page']) && is_numeric($operation['vis_page'])){
				if($operation['vis_change']==1){
				include(VIS_PATH_TEMPLATE.'subaccount_report.php');
				 
			   }
			   else {	
				include(VIS_PATH_TEMPLATE.'header.php');
				include_once(VIS_PATH_TEMPLATE.'view_report.php');
				include(VIS_PATH_TEMPLATE.'footer.php');
			   }
			} else {
				include(VIS_PATH_TEMPLATE.'header.php');
				include_once(VIS_PATH_TEMPLATE.'view_report.php');
				include(VIS_PATH_TEMPLATE.'footer.php');
			}
		}
        elseif(VIS_LOCATION_AREA==2) include_once(VIS_PATH_TEMPLATE.'client_subaccounts.php');
        return true;
    }
	
	 function work_report($operation){
		global $vis_language;
		
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTSTATUS']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
        $header_title=$vis_language['TXTSTATUS'];
          
        include(VIS_PATH_TEMPLATE.'header.php');
		$database=new class_database();
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
				$ticket=new class_ticket($database);
				$client_obj=new class_client($database);

		 	list($client_info, $clientdetails)=$client_obj->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA); 
			 
			$client=$clientdetails[0];
			$userid=VIS_CLIENT_ID;
			  
			if($client['department_access']!=0 && $client['parent_id']==0)
				
	 	$filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$client['department_access'].') ';
			elseif($client['parent_id']!=0){
			list($parentclient_info, $parentclient)=$client_obj->get_client('client_id='.$client['parent_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
			if($parentclient[0]['department_access']!=0)
			 $filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$parentclient[0]['department_access'].') ';
			}
			  $filter.=' and '.VIS_TABLE_PREFIX.'domain.domain_id='.$client['domain_id'];
			  
			$departments=$ticket->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA); 
			  $departmentid = $departments[0]['department_id']; 
 
			$type_list=$ticket->get_type_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
			$priority_list=$ticket->get_priorities_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
			$status_list = $ticket->get_status_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
		  
		 /*$database=new class_database();
         include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php'); 
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
         $ticket_object=new class_ticket($database);
		 $filter_condtn = 'is_enabled=1';
         $status_list = $ticket_object->get_status_list($filter_condtn, VIS_STAFF_ID, VIS_LOCATION_AREA);  
           */    
		 
		
		include(VIS_PATH_TEMPLATE.'status.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	} 
	
	
	function ticket_by_status($operation) {
				global $vis_language;
				
			if($operation['vis_duration']=='' && $operation['vis_type']==0){
					$vis_error_msg=$vis_language['TXTSELECTFORGENERATE'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
		 	if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		if($operation['vis_type']==0){
		$vis_error_msg=$vis_language['TXTVALIDSTATUS'];
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		} 
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate).' and status_id='.$operation['vis_type'];
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate).' and status_id='.$operation['vis_type']; 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate).' and status_id='.$operation['vis_type'];
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to.' and status_id='.$operation['vis_type'];  
			 } 
	 

		 
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA); 
		 
		$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_client_status($filter,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
			 
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTTYPE']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTTYPE'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'status_list.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
			
       
		
       
        return true;
    }
	
	
	 
	function priorty_report($operation){
		        global $vis_language;
				
				
				
			
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTPRIORITY']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTPRIORITY'];
         
        include(VIS_PATH_TEMPLATE.'header.php');
		$database=new class_database();
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
				$ticket=new class_ticket($database);
				$client_obj=new class_client($database);

		 	list($client_info, $clientdetails)=$client_obj->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA); 
			 
			$client=$clientdetails[0];
			$userid=VIS_CLIENT_ID;
			  
			if($client['department_access']!=0 && $client['parent_id']==0)
				
			$filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$client['department_access'].') ';
			elseif($client['parent_id']!=0){
			list($parentclient_info, $parentclient)=$client_obj->get_client('client_id='.$client['parent_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
			if($parentclient[0]['department_access']!=0)
			 $filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$parentclient[0]['department_access'].') ';
			}
			  $filter.=' and '.VIS_TABLE_PREFIX.'domain.domain_id='.$client['domain_id'];
			   $departments=$ticket->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA); 
			 
			  $departmentid = $departments[0]['department_id']; 
 
			$ticket_type=$ticket->get_type_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
			$priority_list=$ticket->get_priorities_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
 
		   
	 
        include(VIS_PATH_TEMPLATE.'priority.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	}
	
	function ticket_by_priority($operation)
	{ global $vis_language;
	
	if($operation['vis_duration']=='' && $operation['vis_type']==0){
					$vis_error_msg=$vis_language['TXTSELECTFORGENERATE'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
		if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		if($operation['vis_type']==0){
		$vis_error_msg=$vis_language['TXTVALIDPRIORITY'];
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		}
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate).' and priority_id='.$operation['vis_type'];
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate).' and priority_id='.$operation['vis_type']; 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate).' and priority_id='.$operation['vis_type'];
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to.' and priority_id='.$operation['vis_type'];  
			 } 
	 

			 
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);  
		 
		$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_client_priority($filter,'',VIS_STAFF_ID,VIS_LOCATION_AREA); 
				
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTPRIORITY']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTPRIORITY'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'priority_list.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
		 
        return true;
    }
	
	
	
	
	function owner_report($operation){
		        global $vis_language;
        
		 
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTOWNER']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTOWNER'];
        $parent_id=VIS_CLIENT_ID;
        include(VIS_PATH_TEMPLATE.'header.php');
		
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		 
		  
		 
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
				$ticket=new class_ticket($database);
				$client_obj=new class_client($database);
				$staff_obj=new class_staff($database);

			list($client_info, $client)=$client_obj->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);  
			$client=$client[0];
			$userid=VIS_CLIENT_ID;
			if($client['department_access']!=0 && $client['parent_id']==0)
			$filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$client['department_access'].') ';
			elseif($client['parent_id']!=0){
			list($parentclient_info, $parentclient)=$client_obj->get_client('client_id='.$client['parent_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
		if($parentclient[0]['department_access']!=0)
			$filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$parentclient[0]['department_access'].') ';
			}

			$filter.=' and '.VIS_TABLE_PREFIX.'domain.domain_id='.$client['domain_id'];
		$departments=$ticket->get_department( VIS_STAFF_ID, VIS_LOCATION_AREA, ''); 
		$departmentid = $departments[0]['department_id'];
		$stafflist=$staff_obj->get_staff($userid, VIS_LOCATION_AREA, 'is_enabled=1 ',$departmentid); 
		
        include(VIS_PATH_TEMPLATE.'owner.php'); 
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	}
	
	function ticket_by_owner($operation){
			global $vis_language;
			if($operation['vis_duration']=='' && $operation['vis_type']==0){
					$vis_error_msg=$vis_language['TXTSELECTFORGENERATE'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
		if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		if($operation['vis_type']==0){
		$vis_error_msg=$vis_language['TXTVALIDOWNER'];
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		} 
		 
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate).' and avis_ticket_assign.owner_id='.$operation['vis_type'];
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate).' and avis_ticket_assign.owner_id='.$operation['vis_type']; 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate).' and avis_ticket_assign.owner_id='.$operation['vis_type'];
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to.' and owner_id='.$operation['vis_type'];  
			 } 
	 

		 
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);
		
		 
		//$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_client_owner($filter,'',VIS_STAFF_ID,VIS_LOCATION_AREA); 
				
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTTYPE']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTTYPE'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'owner_list.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
			
       
		
       
        return true;
    }
	function type_report($operation){
		        global $vis_language;
        
		 
		 
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTTYPE']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
			 
		 
        $header_title=$vis_language['TXTTYPE'];
        $parent_id=VIS_CLIENT_ID;
        include(VIS_PATH_TEMPLATE.'header.php');
		
	 	$database=new class_database();
		 include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
				$ticket=new class_ticket($database);
				$client_obj=new class_client($database);

		 	list($client_info, $clientdetails)=$client_obj->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA); 
			 
			$client=$clientdetails[0];
			$userid=VIS_CLIENT_ID;
			  
			if($client['department_access']!=0 && $client['parent_id']==0)
				
	 	$filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$client['department_access'].') ';
			elseif($client['parent_id']!=0){
			list($parentclient_info, $parentclient)=$client_obj->get_client('client_id='.$client['parent_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
			if($parentclient[0]['department_access']!=0)
			 $filter.=' and '.VIS_TABLE_PREFIX.'department.department_id in ('.$parentclient[0]['department_access'].') ';
			}
			  $filter.=' and '.VIS_TABLE_PREFIX.'domain.domain_id='.$client['domain_id'];
			  
			$departments=$ticket->get_department(VIS_TABLE_PREFIX.'department.is_enabled=1 and '.VIS_TABLE_PREFIX.'domain.is_enabled=1',VIS_STAFF_ID, VIS_LOCATION_AREA); 
			   $departmentid = $departments[0]['department_id']; 
 
			$type_list=$ticket->get_type_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
			$priority_list=$ticket->get_priorities_list($userid, VIS_LOCATION_AREA, 'is_enabled=1 and (department_access=0 or find_in_set('.$departmentid.', department_access))');
  
		
		 
		 
		include(VIS_PATH_TEMPLATE.'type.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	}
	
	
	function ticket_by_type($operation) {
			global $vis_language;
			if($operation['vis_duration']=='' && $operation['vis_type']==0){
					$vis_error_msg=$vis_language['TXTSELECTFORGENERATE'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
		if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		if($operation['vis_type']==0){
		$vis_error_msg=$vis_language['TXTVALIDTYPE'];
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		}
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate).' and type_id='.$operation['vis_type'];
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate).' and type_id='.$operation['vis_type']; 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate).' and type_id='.$operation['vis_type'];
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to.' and type_id='.$operation['vis_type'];  
			 } 
	 

	 	 
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA); 
		 
		$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_client_type($filter,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
				
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTTYPE']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTTYPE'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'type_list.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
		 
        return true;
    }
	
	function ticket_by_client($operation){
		global $vis_language;
		
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTCLIENTBYTICKET']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
        $header_title=$vis_language['TXTCLIENTBYTICKET'];
          
        include(VIS_PATH_TEMPLATE.'header.php');
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		list($client_info,$clientdetails)=$client_object->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
		 
		  $client=$clientdetails[0]; 
		/*if($client['is_manager']==1){
			
		 
		$client_res=$object->get_client_list('group_id='.$client['group_id'].' and is_approved=1 and is_spam=0  ',VIS_CLIENT_ID,VIS_LOCATION_AREA);  
		}*/
		if($client['is_manager']==1 ){
			if(VIS_SATELLITE_HELPDESK == 1)
			{
				$filter='group_id='.$client['group_id'].' and is_approved=1 and is_spam=0 ';
				
			}else{
				$filter='group_id='.$client['group_id'].' and is_approved=1 and is_spam=0 and avis_client.domain_id='.$client['domain_id'].' and (optionB_domain_id='.$client['optionB_domain_id'].' or optionB_domain_id=0)';
				
			}
				$client_res=$object->get_client_list($filter,VIS_CLIENT_ID,VIS_LOCATION_AREA );   
		}   
	  
		include(VIS_PATH_TEMPLATE.'ticket_client.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	} 
	
	function ticket_by_client1($operation){
			global $vis_language;
			if($operation['vis_duration']=='' && $operation['vis_type']==0){
					$vis_error_msg=$vis_language['TXTSELECTFORGENERATE'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
		if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		if($operation['vis_type']==0){
		$vis_error_msg=$vis_language['TXTVALIDCLIENT']; 
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		}
		//print_r($operation);
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate).' and avis_client.client_id='.$operation['vis_type'];
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate).' and avis_client.client_id='.$operation['vis_type']; 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate).' and avis_client.client_id='.$operation['vis_type'];
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to.' and avis_client.client_id='.$operation['vis_type'];  
			 } 
	  $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA); 
		 
		//$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_ticket_by_client($filter,'',VIS_CLIENT_ID,VIS_LOCATION_AREA);
				
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
	  
	  
	  
	 
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTCLIENT']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTCLIENT'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'ticket_by_client.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
		 
        return true;
    }
	 function organization_by_report($operation){
		global $vis_language;
		
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report'; 
		$breadcrumb[$vis_language['TXTCLIENTBYORGNATION']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
        $header_title=$vis_language['TXTCLIENTBYORGNATION'];
          
        include(VIS_PATH_TEMPLATE.'header.php');
		 
		
		
		include(VIS_PATH_TEMPLATE.'organization_report.php'); 
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
		
	
	} 
	
	function organization_by_report1($operation){
			global $vis_language;
		if($operation['vis_duration']==''){
				$vis_error_msg=$vis_language['TXTVALIDDURATION'];
				$vis_msg='true';
				echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
			}

				if($operation['vis_duration']==4){
			if($operation['vis_from_date']=='' && $operation['vis_to_date']==''){
				$vis_error_msg=$vis_language['TXTVALIDDATE'];
				$vis_msg='true';
			echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
			return false;
				}
				
			}

		/*if($operation['vis_type']==0){
		$vis_error_msg='please select client';
		$vis_msg='true';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg));
		return false;
		}*/
			$currentdate = date('Y-m-d h:i:s');
			  if($operation['vis_duration'] == 1) //if select Last 30 days duration
			 {  
					 $monthDate = strtotime(date('Y-m-d h:i:s', strtotime($currentdate)) . '-1 month')- VIS_TIMEZONE;
					 $filter =  ' avis_ticket.receive_date between '.$monthDate.' AND '.strtotime($currentdate);
			 }    
			 else if($operation['vis_duration'] == 2)//if select Last 7 days duration
			 {
					$weekDate =strtotime("-1 week ")- VIS_TIMEZONE;
				    $filter = 'avis_ticket. receive_date between '.$weekDate.' AND '.strtotime($currentdate); 
			 }
			 else if($operation['vis_duration'] == 3)//if select Last 24 hours duration
			 {
					 $hour=strtotime("-1 day ")- VIS_TIMEZONE;
					  $filter = 'avis_ticket.receive_date between '.$hour.' AND '.strtotime($currentdate);
			 }
			 else if($operation['vis_duration'] == 4)//if select Custom duration
			 {
					$from = strtotime($operation['vis_from_date']) - VIS_TIMEZONE;
					$to = strtotime($operation['vis_to_date']) - VIS_TIMEZONE;
				    $filter =  ' avis_ticket.receive_date between '.$from.' AND '.$to;  
			 } 
	  
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_client($database);
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
		$staff=new class_staff($database);
        
		list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA); 
		
		
		 
        
        
		list($client_info, $clientdetails)=$object->get_client('client_id='.VIS_CLIENT_ID, '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
		 
		$clients=$clientdetails[0]; 
		
		  
		  $filter .= ' and group_id='.$clients['group_id'].' and is_approved=1 ';
		 
		//$filter .= ' and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_CLIENT_ID;
		list($list_info,$client) = $object->get_organization_report($filter,'',VIS_STAFF_ID,VIS_LOCATION_AREA);
				
		
		 
		$view_info['page_count']=($list_info['total']==0)?0:ceil($list_info['total']/$view_info['page_count']);
		
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTREPORTS']] = '?/client/report/type_report'; 
		$breadcrumb[$vis_language['TXTCLIENTBYORGNATION']] = ''; 
		$vis_title = $vis_language['TXTREPORTS'];
		
        $header_title=$vis_language['TXTCLIENTBYORGNATION'];

			

			ob_start();
				include(VIS_PATH_TEMPLATE.'orginazation_list.php');
				$type_list = ob_get_clean();
				ob_end_flush();
			$vis_msg='false';
		echo json_encode( array('vis_error_msg'=>$vis_error_msg,'vis_msg'=>$vis_msg,'report_list'=>$type_list));
		 
        return true;
	}
	
	// created by np
	function organizations_details($operation) {
		
        global $vis_language;
        if(defined('VIS_CLIENT_ID') && is_numeric(VIS_CLIENT_ID)) {
            $client_id=VIS_CLIENT_ID;
        }
        else {
            if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
                $organization_id=$operation['vis_organization_id'];
            }
            else {
                $spliter=(strstr($operation['vis_action'], '/'))?'/':',';
                $action=explode($spliter, $operation['vis_action']);
                if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) {
                    $organization_id=$operation['vis_organization_id']=$action[4];
                }
                else {
                    $this->error=$vis_language['MSGCLIENDIDMISS'];
                    return false;
                }
            }
        }
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $database=new class_database();
        
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object=new class_client($database);
        $ticket_object=new class_ticket($database);
       
									
        
        $group=$client_object->get_client_group('group_id='.$organization_id, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $org_details=$group[0];
								
								$ticketfilter=' '.VIS_TABLE_PREFIX.'ticket.ticket_id IN(select distinct ticket_id from '.VIS_TABLE_PREFIX.'ticket, '.VIS_TABLE_PREFIX.'client, '.VIS_TABLE_PREFIX.'client_group where '.VIS_TABLE_PREFIX.'client.group_id='.VIS_TABLE_PREFIX.'client_group.group_id and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_TABLE_PREFIX.'client.client_id and '.VIS_TABLE_PREFIX.'client_group.group_id='.$org_details['group_id'].' and '.VIS_TABLE_PREFIX.'ticket.is_deleted=0 and '.VIS_TABLE_PREFIX.'ticket.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket.status_id!=3 and '.VIS_TABLE_PREFIX.'client.parent_id=0)';
			list($ticket_info,$ticket_details)=$ticket_object->get_ticket_listing_count($ticketfilter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$ticketcount=$ticket_info['total'];
			$filter=VIS_TABLE_PREFIX.'client.parent_id=0 and '.VIS_TABLE_PREFIX.'client_group.group_id='.$org_details['group_id'];
			$client_info=$client_object->get_client_count($filter, '','', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$clientcount=$client_info['total'];
			
		$domain_name=$ticket_object->get_domain_name($org_details['domain_id']);		
		$filter=VIS_TABLE_PREFIX.'client.group_id='.$org_details['group_id'].' and '.VIS_TABLE_PREFIX.'client.is_manager=1 and is_spam=0 and 	is_banned=0 and is_approved=1 and is_verified=1';			
			list($clientinfo,$clentmanager)=$client_object->get_client($filter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);				
			
			$filter=' '.VIS_TABLE_PREFIX.'ticket.client_id IN(select distinct client_id from '.VIS_TABLE_PREFIX.'client, '.VIS_TABLE_PREFIX.'client_group where '.VIS_TABLE_PREFIX.'client.group_id='.VIS_TABLE_PREFIX.'client_group.group_id  and '.VIS_TABLE_PREFIX.'client_group.group_id='.$operation['vis_organization_id'].')';
            $quick_view=$ticket_object->get_filter_quick_counts($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);	
								
        $database->database_disconnect();
      
        if(VIS_LOCATION_AREA==2)
        {
              if($org_details==-1 || $org_details==''){
                 echo '<section class="wrapper fullFrame b-light"><div class="fullMiddle">'.$vis_language['MSGACCSSDND'].'</div></section>';
                 return false;
              }
			  include(VIS_PATH_TEMPLATE.'organizations_view.php');
			 
		}
      
        return true;
  }
		
		function get_orgshort_summary($operation) {
		
        global $vis_language;
        if(defined('VIS_CLIENT_ID') && is_numeric(VIS_CLIENT_ID)) {
            $client_id=VIS_CLIENT_ID;
        }
        else {
            if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
                $organization_id=$operation['vis_organization_id'];
            }
            else {
                $spliter=(strstr($operation['vis_action'], '/'))?'/':',';
                $action=explode($spliter, $operation['vis_action']);
                if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) {
                    $organization_id=$operation['vis_organization_id']=$action[4];
                }
                else {
                    $this->error=$vis_language['MSGCLIENDIDMISS'];
                    return false;
                }
            }
        }
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $database=new class_database();
        
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $client_object=new class_client($database);
        $ticket_object=new class_ticket($database);
       
									
        
        $group=$client_object->get_client_group('group_id='.$organization_id, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $org_details=$group[0];
			
		$domain_name=$ticket_object->get_domain_name($org_details['domain_id']);		
		$filter=VIS_TABLE_PREFIX.'client.group_id='.$org_details['group_id'].' and '.VIS_TABLE_PREFIX.'client.is_manager=1 and is_spam=0 and 	is_banned=0 and is_approved=1 and is_verified=1';			
			list($clientinfo,$clentmanager)=$client_object->get_client($filter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);				
			
			/*	$ticketfilter=' '.VIS_TABLE_PREFIX.'ticket.ticket_id IN(select distinct ticket_id from '.VIS_TABLE_PREFIX.'ticket, '.VIS_TABLE_PREFIX.'client, '.VIS_TABLE_PREFIX.'client_group where '.VIS_TABLE_PREFIX.'client.group_id='.VIS_TABLE_PREFIX.'client_group.group_id and '.VIS_TABLE_PREFIX.'ticket.client_id='.VIS_TABLE_PREFIX.'client.client_id and '.VIS_TABLE_PREFIX.'client_group.group_id='.$organization_id.' and '.VIS_TABLE_PREFIX.'ticket.is_deleted=0 and '.VIS_TABLE_PREFIX.'ticket.draft_id=0 and '.VIS_TABLE_PREFIX.'ticket.status_id!=3 and '.VIS_TABLE_PREFIX.'client.parent_id=0)';
			list($ticket_info,$ticket_details)=$ticket_object->get_ticket_listing_count($ticketfilter, '', '', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$ticketcount=$ticket_info['total'];
			$filter=VIS_TABLE_PREFIX.'client.parent_id=0 and '.VIS_TABLE_PREFIX.'client_group.group_id='.$organization_id;
			$client_info=$client_object->get_client_count($filter, '','', '', VIS_STAFF_ID, VIS_LOCATION_AREA);
			$clientcount=$client_info['total'];*/
								
        $database->database_disconnect();
      
        if(VIS_LOCATION_AREA==2)
        {
              if($org_details==-1 || $org_details==''){
                 echo '<section class="wrapper fullFrame b-light"><div class="fullMiddle">'.$vis_language['MSGACCSSDND'].'</div></section>';
                 return false;
              }
			  include(VIS_PATH_TEMPLATE.'org_short_summary.php');
			 
		}
      
        return true;
  }
		
		function get_orgsummary($operation) {
        global $vis_language;
        if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
            $database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
												include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
            $object=new class_client($database);
            $ticket_object=new class_ticket($database);
           $filter=' '.VIS_TABLE_PREFIX.'ticket.client_id IN(select distinct client_id from '.VIS_TABLE_PREFIX.'client, '.VIS_TABLE_PREFIX.'client_group where '.VIS_TABLE_PREFIX.'client.group_id='.VIS_TABLE_PREFIX.'client_group.group_id  and '.VIS_TABLE_PREFIX.'client_group.group_id='.$operation['vis_organization_id'].')';
            $quick_view=$ticket_object->get_filter_quick_counts($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);	
            if(!$quick_view) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
           
             include_once(VIS_PATH_TEMPLATE.'organization_summary.php');
            $database->database_disconnect();
            return true;
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGCLIENDIDMISS'].'"); </script>';
            return false;
        }
    }
				
				function get_organizationdetails($operation) {
        global $vis_language;
        if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
            $database=new class_database();
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
																	$ticket_object=new class_ticket($database);
																	$filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
																	$domain_details=$ticket_object->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
																	include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
																	$object=new class_client($database);
																	$group=$object->get_client_group('group_id='.$operation['vis_organization_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
																	$group=$group[0];
																	
									// ======== Newly Added Code Starts here =========
									include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
									$ticket=new ticket();
									$location[0] = 12;
									$filter=VIS_TABLE_PREFIX.'domain.domain_id='.VIS_DOMAIN;                   
									$departments=$ticket_object->get_department($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
									$departmentid = $departments[0]['department_id'];
									$parent_id = $operation['vis_organization_id'];
									$custom_field_var=$ticket->get_custom_fields($location, 0, $departmentid, $parent_id, $database);
									// ======== Newly Added Code Ends Here ===========
									
																	$database->database_disconnect();
            if(!$group) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
            include_once(VIS_PATH_TEMPLATE.'organization_edit.php');
            return true;
        }
        else {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGORGIDMISS'].'"); </script>';
            return false;
        }
    }
				
				function getorganization_ticket($operation)
				{
					global $vis_language;
					if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
                    $page=0;
                    $sort_by='modify_date desc';
                    $limit='';
                    $database=new class_database();
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
                    $ticket_object=new class_ticket($database);
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
                    $client_object=new class_client($database);
                    $client='';
                        $client_details=$client_object->get_client_list('group_id='.$operation['vis_organization_id'], '', 0, '', VIS_CLIENT_ID, VIS_LOCATION_AREA);
                        if($client_details!=-1) for($i=0;$i<count($client_details);$i++) if($client_details[$i]['client_id']!=VIS_CLIENT_ID) $client.=$client_details[$i]['client_id'].',';
                    $client=substr($client,0,-1);
																				
                    $filter=VIS_TABLE_PREFIX.'ticket.client_id in ('.$client.')';
                    $filter.=' and is_deleted=0 and '.VIS_TABLE_PREFIX.'ticket.is_spam=0 and draft_id=0';
                    $staff=new class_staff($database);
																				list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);
                    $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;     
																					$limit=($page*$view_info['page_count']).','.$view_info['page_count'];	
																				
                    list($ticket_info, $ticket_details)=$ticket_object->get_ticket($filter, $limit, $page, $sort_by, VIS_STAFF_ID, VIS_LOCATION_AREA);
                    if(!$ticket_details || !$view_details) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                        return false;
                    }
                    
                        $view_info['page_count']=($ticket_info['total']==0)?0:ceil($ticket_info['total']/$view_info['page_count']);
                        $ticket_info['ticket_count']=(!$ticket_details || $ticket_details==-1)?0:count($ticket_details);
                        if($operation['vis_ticket']==1)
                        $ticket_info['page']=$page;
                        else
                        $ticket_info['page']=$page*$view_info['page_count'];
                        $view_info['view_count']=count($view_details);
                        if(isset($operation['vis_ticket_id']) && is_numeric($operation['vis_ticket_id'])) $ticket_id=$operation['vis_ticket_id'];
                        else $ticket_id=0;
                        foreach($view_details as $v){
							if($ticket_details!=-1){			
								for($i=0;$i<count($ticket_details);$i++){
									switch($v['tab_name']){
										case 'link': 	
											$link_data=array('vis_ticket_id'=>$ticket_details[$i]['ticket_id'], 'case'=>1);	
											$link = $ticket_object->get_ticket_link($link_data, VIS_STAFF_ID, VIS_LOCATION_AREA);
											$ticket_details[$i]['link_present']=($link == -1)?0:1;
											break;
										case 'label':	
											$label_details[$i] = $ticket_object->get_ticket_labels($ticket_details[$i]['ticket_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
											break;
										default: // this case will execute at last
											$ticket_lock[$i] = $ticket_object->get_ticket_lock($ticket_details[$i]['ticket_id'], 'staff_id!='.VIS_STAFF_ID, VIS_STAFF_ID, VIS_LOCATION_AREA);
									}
								}
							}
						}
                        include_once(VIS_PATH_TEMPLATE.'ticket_list.php');
                    
                    $database->database_disconnect();
                    return true;
                }
                else {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGORGIDMISS'].'"); </script>';
                    return false;
                }
				}
				
				function getorganization_client($operation)
				{
					global $vis_language;
					if(isset($operation['vis_organization_id']) && is_numeric($operation['vis_organization_id'])) {
                    $page=0;
                    $sort_by='';
                    $limit='';
                    $database=new class_database();
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
                    include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
                    $client_object=new class_client($database);
                    $staff=new class_staff($database);
																				$ticket_object=new class_ticket($database);
																				                     
                    
                    
																				list($view_info, $view_details)=$staff->get_ticket_view(VIS_STAFF_ID, VIS_LOCATION_AREA);
                    $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;     
																					$limit=($page*$view_info['page_count']).','.$view_info['page_count'];	
																					$filter=VIS_TABLE_PREFIX.'client.group_id='.$operation['vis_organization_id'];
                    list($client_info, $client_details)=$client_object->get_client($filter, $limit,$page, $sort_by, VIS_STAFF_ID, VIS_LOCATION_AREA,$listflag); 
																			
                    if(!$client_details || !$client_details) {
                        echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                        return false;
                    }
                    
                        $view_info['page_count']=($client_info['total']==0)?0:ceil($client_info['total']/$view_info['page_count']);
                        $client_info['client_count']=(!$client_details || $client_details==-1)?0:count($client_details);
                        if($operation['vis_client']==1)
                        $client_info['page']=$page;
                        else
                        $client_info['page']=$page*$view_info['page_count'];
                        $view_info['view_count']=count($view_details);
                        if(isset($operation['vis_client_id']) && is_numeric($operation['vis_client_id'])) $ticket_id=$operation['vis_client_id'];
                        else $client_id=0;
                        
																								$operation['vis_action']='history';
                        include_once(VIS_PATH_TEMPLATE.'client_list.php');
                    
                    $database->database_disconnect();
                    return true;
                }
                else {
                    echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGORGIDMISS'].'"); </script>';
                    return false;
                }
				}
		function spam_client($operation) {
        global $vis_language;
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $sort_by=(isset($operation['vis_sort_by']))?$operation['vis_sort_by']:'';
        $operation['view_title']=$vis_language['TXTSPAM'];
        return $this->client_display($operation, '', '', ' is_spam=1 and '.VIS_TABLE_PREFIX.'client.parent_id=0 ', $page, $sort_by);
    }
	
	function windows_login($operation){	//echo '***'; print_r($operation);	
	   global $request_data,$operation_hnd;
	   $request_data=$operation;		
		$database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $filter='login_type=26 and domain_id='.VIS_DOMAIN;
        //$row5=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);			
		
		if (isset($operation['userName']) && $operation['userName']!='') {
			
			$user_fbid	= $fbuser;
			$user_email = $operation['userName'];
			$profile_name = explode(' ',$operation['profile_name']);
			$fname = $profile_name[0];
			$lname = (isset($profile_name[1])&& $profile_name[1]!='')?$profile_name[1]:'';
			
			$database=new class_database();
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
			$object=new class_client($database);
			$request_data['vis_email'] = $user_email;
			$request_data['vis_firstname'] = $fname;
			$request_data['vis_lastname'] = $lname;
			$request_data['vis_username'] = $user_email;
			$request_data['vis_module'] = 'home';
			$request_data['vis_is_approved'] = '1';
			$request_data['vis_group'] = '1';
			$request_data['vis_domain'] = VIS_DOMAIN;
			$request_data['vis_department'] = '0';
			$request_data['vis_login_as'] = '13';
			$filter1= "email='".$request_data['vis_email']."' and ".VIS_TABLE_PREFIX."client.domain_id=".VIS_DOMAIN ;
			list($ss, $row_count)=$object->get_client($filter1, '','', '', '', VIS_LOCATION_AREA);
						
			if($row_count==-1){
				$register = $object->add_client($request_data,'0','1');	
				$filter = "email='".$request_data['vis_email']."' and ".VIS_TABLE_PREFIX."client.domain_id=".VIS_DOMAIN ;		
				$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);				
				$request_data['vis_client_id'] =$user_details[1][0]['client_id'];
				$request_data['vis_operation'] = 'add_client';					
				$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
				$request_data['authentication']=$user_details[1][0]['authentication'];					
				$operation_hnd->check_automation($request_data);
				$operation_hnd->operation_flow($request_data, $request_data['vis_client_id'], VIS_LOCATION_AREA, $database);
				$request_data['vis_operation'] = 'windows_login';			
				$operation_hnd->is_login();
			} else {
				$filter = "email='".$request_data['vis_email']."' and ".VIS_TABLE_PREFIX."client.domain_id=".VIS_DOMAIN ;
				$user_details=$object->get_client($filter, '','', '', '', VIS_LOCATION_AREA);
				$request_data['vis_password']=password_decrypt($user_details[1][0]['authentication'],VIS_PASSWORD_KEY);
				$request_data['vis_operation'] = 'windows_login';
				$operation_hnd->is_login();
			}			
		}
	}
	function windows_success($operation){
		 include_once(VIS_PATH_TEMPLATE.'windows_success.php');
	}
	function import_orgnizations($operation){
         global $vis_language;
         $database = new class_database(); 
         include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
         $object_ticket=new class_ticket($database);
         $domain_list=$this->get_domain_access_list();
         $filter =VIS_TABLE_PREFIX.'domain.is_enabled=1';
         $domain_details=$object_ticket->get_domain($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
        include(VIS_PATH_TEMPLATE.'organization_import.php');         
    }
	function import_organization_list($operation){
		  global $vis_language;
          $database = new class_database(); 
          $data=parse_form_array($operation['vis_action']); 
		  include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		  $setting_object=new class_setting($database);		  
          include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');  
          $object=new class_client($database);
          $arr_attachment = explode(',',$data['vis_import_attachments']);
          $filter = 'parent_type=5 and attachment_id='.$arr_attachment[0] ;
          $attach = $object->get_attachment($filter, VIS_STAFF_ID, VIS_LOCATION_AREA) ;
        
          if($attach!=-1){
             for($i=0;$i<count($attach);$i++){ 
                     $file_name =VIS_PATH_FILES.$attach[$i]['content'];
                     $file_handle = fopen($file_name, "r");
                     $first_row = fgetcsv($file_handle, 1000, ";");
					 $header_arr=explode(",",$first_row[0]);
					 $count=1;
                     while (!feof($file_handle) ){						 
                          $line_of_text1 = fgetcsv($file_handle);
                          $line_of_text = array_combine($header_arr, $line_of_text1);
                          if(trim($line_of_text['GROUP NAME']) != "" ){ 
							  $data['vis_name'] 		= $line_of_text['GROUP NAME'];
                              $data['vis_domain_list'] 	= $line_of_text['DOMAIN LIST'];
          					  $data['vis_details'] 		= $line_of_text['DESCRIPTION'];
							  $data['vis_email'] 		= $line_of_text['OFFICE EMAIL'];
							  $data['vis_website'] 		= $line_of_text['OFFICE WEBSITE'];
							  $data['vis_fax'] 			= $line_of_text['OFFICE FAX'];
                              $data['vis_address'] 		= $line_of_text['OFFICE ADDRESS'];
                              $data['vis_city'] 		= $line_of_text['OFFICE CITY'];
                              $data['vis_country'] 		= $line_of_text['OFFICE COUNTRY'];
                              $data['vis_state']		= $line_of_text['OFFICE STATE'];
                              $data['vis_mob'] 			= $line_of_text['OFFICE MOBILE'];
                              $data['vis_phone'] 		= $line_of_text['OFFICE PHONE'];
                              $data['vis_zipcode'] 		= $line_of_text['ZIP CODE'];
                              $data['vis_credit'] 		= $line_of_text['CREDIT'];
                              $data['vis_debit'] 		= $line_of_text['DEBIT']; 
							  $data['vis_status'] 		= $line_of_text['STATUS']; 	
							  $client=$object->get_client_group_view('group_name="'.$line_of_text['GROUP NAME'].'" and '.VIS_TABLE_PREFIX.'client_group.domain_id="'.$data['vis_domain'].'"', '', 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);							  
							 	if($client[0]['total'] < 1){ 
									$activity_log=$setting_object->add_staff_activity_log("Imported new Orgnization  ".$data['vis_name'], VIS_STAFF_ID ,1,VIS_LOCATION_AREA); 
									$res = $object->add_client_group($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
								} 
									
                          }   
						$count++;		
                     }
              }
			 echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'");$("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTIMPORT'].'",{"vis_module":"client", "vis_operation":"view_client_group"});</script>';
         }
         else echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';        
   }	
   function fb_callback($operation){
		$database=new class_database();
		global $vis_language;
		ob_start();
		session_start();


		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$object=new class_setting($database);
		$filter='login_type=16 and domain_id='.VIS_DOMAIN;
		$rowfb=$object->get_single_login_details($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);		
		
		$appid 	  = $rowfb[0]['host_name'];
		$appsecret  = $rowfb[0]['user_name'];
		$callback_url  = $rowfb[0]['db_password'];
			
		
		require_once VIS_PATH_PLUGINS.'fb_login/fbConfig.php';	
		
		if(isset($accessToken)){
			if(isset($_SESSION['facebook_access_token'])){
				$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
			}else{
				// Put short-lived access token in session
				$_SESSION['facebook_access_token'] = (string) $accessToken;
				
				// OAuth 2.0 client handler helps to manage access tokens
				$oAuth2Client = $fb->getOAuth2Client();
				
				// Exchanges a short-lived access token for a long-lived one
				$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
				$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
				
				// Set default access token to be used in script
				$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
			}
			
			// Redirect the user back to the same page if url has "code" parameter in query string
			
			if(isset($_GET['code'])){ 
				header('Location: ./index.php?/');
				
			}
		
			// Getting user facebook profile info
			try {
				$profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
				$fbUserProfile = $profileRequest->getGraphNode()->asArray();		
				
				$fbUserData = array(
					'oauth_provider'=> 'facebook',
					'oauth_uid' 	=> $fbUserProfile['id'],
					'first_name' 	=> $fbUserProfile['first_name'],
					'last_name' 	=> $fbUserProfile['last_name'],
					'email' 		=> $fbUserProfile['email'],
					'gender' 		=> $fbUserProfile['gender'],
					'locale' 		=> $fbUserProfile['locale'],
					'picture' 		=> $fbUserProfile['picture']['url'],
					'link' 			=> $fbUserProfile['link']
				);
				//print_r($fbUserData);die;
					$_SESSION['fbUserData']=$fbUserData;
				$operation['fbUserData']=$fbUserData;
				include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'client.php'); 
					$object_client = new client();
					$operation = $fbUserData;
					$object_client->fb_login($operation); 
					header('Location: ./index.php?/');
				//$_SESSION['fbUserData']=$fbUserData;
				 
			} catch(FacebookResponseException $e) { 
				echo 'Graph returned an error: ' . $e->getMessage();
				session_destroy();
				// Redirect user back to app login page
				header('Location: ./index.php?/');
				exit;
			} catch(FacebookSDKException $e) { 
				echo 'Facebook SDK returned an error: ' . $e->getMessage();
				exit;
			}
			
				// Initialize User class
				$user = new User();
			
				// Insert or update user data to the database
				$fbUserData = array(
					'oauth_provider'=> 'facebook',
					'oauth_uid' 	=> $fbUserProfile['id'],
					'first_name' 	=> $fbUserProfile['first_name'],
					'last_name' 	=> $fbUserProfile['last_name'],
					'email' 		=> $fbUserProfile['email'],
					'gender' 		=> $fbUserProfile['gender'],
					'locale' 		=> $fbUserProfile['locale'],
					'picture' 		=> $fbUserProfile['picture']['url'],
					'link' 			=> $fbUserProfile['link']
				);
				//print_r($fbUserData);die;
					$_SESSION['fbUserData']=$fbUserData;
					//header("Location: index.php?/client/fb_login");
		}
		unset($_SESSION['facebook_access_token']);
		session_destroy();	
	}
}
?>
