<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule_user_delete extends CI_Controller
{
	public function index()
	{
		$this->load->model('common_model');
		$currentDate = date('Y-m-d');
		//$currentDate = '2019-07-18';
		$getFileData = $this->common_model->getAll('usr_users',array('delete_date'=>$currentDate));
		//echo $this->db->last_query();
		//echo '<pre>';print_r($getFileData);die;
		//mail('testdevloper007@gmail.com','user schedule delete',$this->db->last_query());
		//die;
		if(!empty($getFileData))
		{
			foreach ($getFileData as $value) 
			{
				$userId=$value['id'];
				$userdetails = $this->common_model->getAllRow('feu_userdetails',array('user_id'=>$userId));
				$useremaildata = $this->common_model->getAllRow('usr_users',array('id'=>$userId));
				$userImagesData = $this->common_model->getAll('sys_files',array('user_id'=>$userId));

				if(!empty($userdetails))
				{
					//$to = 'sayan@digitalaptech.com';
					$to = $useremaildata['email'];
					$sub = getAdminLang('Your_account_has_been_deleted',$userdetails['userlanguage']);
					$body= '<p>'.getAdminLang('profile_castleevents_has_been_deleted',$userdetails['userlanguage']).'</p>';
					$body.= '<p>'.getAdminLang('All_content_information_as_well',$userdetails['userlanguage']).'</p>';
					$body.= '<p>'.getAdminLang('We_hope_you_had_a_good_time',$userdetails['userlanguage']).'</p>';
					
					
					$bodys=' <table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
		                    <tr>
		                       <td>
		                          <table cellspacing="0" cellpadding="0" border="0" width="100%">
		                             <tr>
		                                <td align="center" style="padding: 40px; font-family: sans-serif; font-size: 15px; mso-height-rule: exactly; line-height: 20px; color: #b3975b;">
		                                   <h2>Dear <span style="text-transform:capitalize">'.$userdetails['username'].'</span> </h2>
		                                </td>
		                             </tr>
		                             <tr>
		                                <td style="font-family: sans-serif; color: #fff; padding: 0px 40px 40px; font-size: 16px; line-height: 30px;">
		                                   '.$body.'
		                                </td>
		                             </tr>
		                             <tr>
		                             	<td align="center" style="font-family: sans-serif; color: rgb(179, 151, 91); padding: 20px 40px; border-top: 1px solid rgb(79, 79, 79); background: rgb(19, 18, 24) none repeat scroll 0px 0px;">
		                             		'.getAdminLang('Best_regards',$userdetails['userlanguage']).' <br>
		                             		<p style="margin:5px 0 0">'.getAdminLang('Your_team_of_castleevents',$userdetails['userlanguage']).'</p> 
		                             	</td>
		                             </tr>
		                          </table>
		                       </td>
		                    </tr>
		                </table>';
					$this->utility->sendCIMail($to,$sub,$bodys);
				}
				$arr = array(
							'username'=>$userdetails['username'],
							'email'=>$useremaildata['email'],
							'created_date'=>$userdetails['created'],
							'delete_date'=>strtotime($currentDate)
						);
				$inst = $this->common_model->insertData('deleted_users',$arr);
				$comments = $this->common_model->getAll('cmt_comments',array('alias_path'=>'community/profil/'.$userdetails['alias']));
				$this->common_model->deleteData('usr_users',array('id'=>$userId));
				//$this->common_model->deleteData('event_guest',array('username'=>$userdetails['username']));
				$this->common_model->deleteData('feu_userdetails',array('user_id'=>$userId));
				if(count($comments)>0)
				{
					foreach ($comments as $value) {
						$this->common_model->deleteData('cmt_comments',array('id'=>$value['id']));
					}
				}
				$this->common_model->deleteData('feu_addresses',array('user_id'=>$userId));
				$this->common_model->deleteData('feu_auth_requests',array('user_id'=>$userId));
				$this->common_model->deleteData('feu_profilevisits',array('from_user_id'=>$userId));
				$this->common_model->deleteData('feu_profilevisits',array('to_user_id'=>$userId));
				$this->common_model->deleteData('feu_relationships',array('user_id_1'=>$userId));
				$this->common_model->deleteData('feu_relationships',array('user_id_2'=>$userId));
				$this->common_model->deleteData('feu_sexual_preference',array('user_id'=>$userId));
				$this->common_model->deleteData('feu_user_discounts',array('user_id'=>$userId));
				$this->common_model->deleteData('feu_user_steps',array('user_id'=>$userId));
				$this->common_model->deleteData('like_likes',array('user_id'=>$userId));
				$this->common_model->deleteData('like_likes',array('identifier'=>'profile:'.$userId));
				$this->common_model->deleteData('msg_conversations_users',array('user_id'=>$userId));
				$this->common_model->deleteData('msg_messages',array('from_user_id'=>$userId));
				$this->common_model->deleteData('newsletter_couple_lady',array('couple'=>$userId));
				$this->common_model->deleteData('newsletter_couple_lady',array('lady'=>$userId));
				$this->common_model->deleteData('newsletter_couple_lady',array('gentleman'=>$userId));
				$this->common_model->deleteData('notification',array('from_id'=>$userId));
				$this->common_model->deleteData('notification',array('to_id'=>$userId));
				//$this->common_model->deleteData('shop_carts',array('user_id'=>$userId));
				//$this->common_model->deleteData('shop_orders',array('invoiceAddress'=>$useremaildata['email']));
				$this->common_model->deleteData('sys_files',array('user_id'=>$userId));
				$this->common_model->deleteData('ugal_images',array('user_id'=>$userId));
				$this->common_model->deleteData('ugal_images_user_privacy',array('from_user_id'=>$userId));
				$this->common_model->deleteData('ugal_images_user_privacy',array('to_user_id'=>$userId));
				$this->common_model->deleteData('usr_log',array('user_id'=>$userId));
				$this->common_model->deleteData('usr_modules',array('user_id'=>$userId));
				$this->common_model->deleteData('usr_phones',array('user_id'=>$userId));
				$this->common_model->deleteData('usr_recurring_payment',array('user_id'=>$userId));
				$this->common_model->deleteData('usr_roles_users',array('user_id'=>$userId));
				if(count($userImagesData)>0)
				{
					foreach ($userImagesData as $value) {
						$filename = $value['filename'].'.'.$value['extension'];
						
						$filename_blur = $value['filename'].'_blur.'.$value['extension'];
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/80/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/160/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/320/'.$filename);
						
						if($value['temp_name']!='')
						{
							$temp_name = $value['temp_name'].'.'.$value['extension'];
							@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$temp_name);
							@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/80/'.$temp_name);
							@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/160/'.$temp_name);
							@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/320/'.$temp_name);
						}

						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/'.$filename_blur);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/250/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/500/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/750/'.$filename);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/1000/'.$filename);

						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/250/'.$filename_blur);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/500/'.$filename_blur);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/750/'.$filename_blur);
						@unlink(FCPATH.'assets/userfiles/'.$value['path'].'/1000/'.$filename_blur);
					}
				}
				/********Delete from Castleevents German************/
				$subscribeEmail = $useremaildata['email'];
				$apiKey = "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";
				$curl = curl_init();
				curl_setopt_array($curl, array(
				  CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts/search/emails",
				  CURLOPT_RETURNTRANSFER => true,
				  CURLOPT_ENCODING => "",
				  CURLOPT_MAXREDIRS => 10,
				  CURLOPT_TIMEOUT => 30,
				  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				  CURLOPT_CUSTOMREQUEST => "POST",
				  CURLOPT_POSTFIELDS => "{\"emails\":[\"$subscribeEmail\"]}",
				  CURLOPT_HTTPHEADER => array(
				    "authorization: Bearer ".$apiKey,
				    "content-type: application/json"
				  ),
				));

				$response = curl_exec($curl);
				$err = curl_error($curl);

				curl_close($curl);
				if ($err) {
				  //echo 2;die;
				} else {
				  	$outout = json_decode($response, true);
				  	$ids = '';
					if(isset($outout['result'])) {
						if(isset($outout['result'][$subscribeEmail])) {
							if(isset($outout['result'][$subscribeEmail]['contact'])) {
								$ids = $outout['result'][$subscribeEmail]['contact']['id'];
							}
						}
					}
				  /*delete*/
				  	if($ids) {
					  	$curl = curl_init();
						curl_setopt_array($curl, array(
						  CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts?ids=".$ids,
						  CURLOPT_RETURNTRANSFER => true,
						  CURLOPT_ENCODING => "",
						  CURLOPT_MAXREDIRS => 10,
						  CURLOPT_TIMEOUT => 30,
						  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
						  CURLOPT_CUSTOMREQUEST => "DELETE",
						  CURLOPT_POSTFIELDS => "{}",
						  CURLOPT_HTTPHEADER => array(
						    "authorization: Bearer ".$apiKey,
						    "content-type: application/json"
						  ),
						));
						$response = curl_exec($curl);
						$err = curl_error($curl);
						curl_close($curl);
						if ($err) {
						  //echo 2;die;
						} else {
						  //echo 1; exit;
						}
					} else {
						//echo 1; exit;
					}
				}
				/********Delete from Castleevents English************/	
			}
		}
	}
}