<?php defined('BASEPATH') OR exit('No direct script access allowed');
ini_set('max_execution_time', -1);
class Unsubscribed extends CI_Controller
{
	public function german($offset=0)
	{
		
		$this->load->model('common_model');
		$apiKey = "9a1d414f170b904492f76c4b4aaad59c-us14";
		$this->load->model('common_model');
		$listId = "4b705bab4e";
		$batch_operations = array();
		$url = "https://us14.api.mailchimp.com/3.0/lists/".$listId."/members?status=unsubscribed&&offset=".$offset;
		$operation = array();
		//$request_encoded = json_encode($operation);
	    $curl = curl_init($url);
	    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
	    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
	    //curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encoded); 
		$result = curl_exec($curl);
		//var_dump($result);
		$members = json_decode($result,true);
		if(!empty($members['members']))
		{//print_r($members); 
			foreach ($members['members'] as $key => $value) {
				$userdata = $this->common_model->getAllRow('usr_users',array('email'=>$value['email_address']));
				if(!empty($userdata))
				{
					$status = 0;
					/*if($value['status']=='subscribed')
					{
						$status = 1;
					}*/
					$this->common_model->UpdateData('feu_userdetails',array('mailchimp_list_id'=>$listId,'subscribe'=>$status),array('user_id'=>$userdata['id']));
					//echo $this->db->last_query(); die;
				}
				
			}
			if($members['total_items']>$offset+10)
			{
				$offset = $offset+10;
				$this->german($offset);
			}
		}
		die;
	}
	public function english($offset=0)
	{
		$this->load->model('common_model');
		$apiKey = "9a1d414f170b904492f76c4b4aaad59c-us14";
		$this->load->model('common_model');
		$listId = "a8efb2baef";
		$batch_operations = array();
		$url = "https://us14.api.mailchimp.com/3.0/lists/".$listId."/members?status=unsubscribed&&offset=".$offset;
		$operation = array();
		//$request_encoded = json_encode($operation);
	    $curl = curl_init($url);
	    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
	    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
	    //curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encoded); 
		$result = curl_exec($curl);
		//var_dump($result);
		$members = json_decode($result,true);
		if(!empty($members['members']))
		{//print_r($members); //die;
			foreach ($members['members'] as $key => $value) {
				$userdata = $this->common_model->getAllRow('usr_users',array('email'=>$value['email_address']));
				if(!empty($userdata))
				{
					$status = 0;
					
					$this->common_model->UpdateData('feu_userdetails',array('mailchimp_list_id'=>$listId,'subscribe'=>$status),array('user_id'=>$userdata['id']));
					//echo $this->db->last_query(); die;
				}
				
			}
			if($members['total_items']>$offset+10)
			{
				$offset = $offset+10;
				$this->english($offset);
			}
		}
		die;
	}
	public function spanish($offset=0)
	{
		
		$this->load->model('common_model');
		$apiKey = "9a1d414f170b904492f76c4b4aaad59c-us14";
		$this->load->model('common_model');
		$listId = "8be54dca74";
		$batch_operations = array();
		$url = "https://us14.api.mailchimp.com/3.0/lists/".$listId."/members?status=unsubscribed&&offset=".$offset;
		$operation = array();
		//$request_encoded = json_encode($operation);
	    $curl = curl_init($url);
	    curl_setopt($curl, CURLOPT_USERPWD, 'user:' . $apiKey);    
	    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//raw output
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
	    //curl_setopt($curl, CURLOPT_POSTFIELDS, $request_encoded); 
		$result = curl_exec($curl);
		//var_dump($result);
		$members = json_decode($result,true);
		if(!empty($members['members']))
		{//print_r($members); 
			foreach ($members['members'] as $key => $value) {
				$userdata = $this->common_model->getAllRow('usr_users',array('email'=>$value['email_address']));
				if(!empty($userdata))
				{
					$status = 0;
					/*if($value['status']=='subscribed')
					{
						$status = 1;
					}*/
					$this->common_model->UpdateData('feu_userdetails',array('mailchimp_list_id'=>$listId,'subscribe'=>$status),array('user_id'=>$userdata['id']));
					//echo $this->db->last_query(); die;
				}
				
			}
			if($members['total_items']>$offset+10)
			{
				$offset = $offset+10;
				$this->spanish($offset);
			}
		}
		die;
	}
}