<?php

/*if(isset($_POST['defrtbftext']))
{
	$pref='wpf5gdpr-';
	update_option($pref.'rtbf-message',wpfresh5_gdprEditorText('rightforget'));
}*/

if(isset($_POST['gdprfmremove']))
{
	global $wpdb;
	$table=$wpdb->prefix."wpfresh5_gdpr_requests";
	$wpdb->query($wpdb->prepare("delete from ".$table." where id=%d ",array($_POST['afmreqid'])));
}
if(isset($_POST['gdprfmtakeaction']))
{
	global $wpdb;
	$table=$wpdb->prefix."wpfresh5_gdpr_requests";
	$timenow=date('d-M-Y h:iA');
	$pref='wpf5gdpr-';
	$wpcrmailheaders = "MIME-Version: 1.0" . "\r\n";
    $wpcrmailheaders .= "Content-type:text/html;charset=UTF-8" . "\r\n";

	/*get_option($pref.'rtbf-email');
get_option($pref.'rtbf-title');
get_option($pref.'rtbf-message');
get_option($pref.'rtbf-reassign');
get_option($pref.'rtbf-reassign-user');*/
$user_info = get_userdata(get_option($pref.'rtbf-reassign'));
$user_info->user_email;

if($_POST['afmvalue']=='c')
{//delete comments
	$args=array(
	'author_email' =>$_POST['afmemail']);
	 $comments=get_comments( $args );
	 foreach($comments as $data)
	 {
		 $comment_id=$data->comment_ID;

		 wp_delete_comment( $comment_id);
	 }

	$wpdb->query($wpdb->prepare("update ".$table." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['afmreqid'])));
}
if($_POST['afmvalue']=='u')
{//delete user meta

	if(email_exists($_POST['afmemail']))
	{

	$metagdprtable=$wpdb->prefix."usermeta";
	$wpdb->query($wpdb->prepare("delete from ".$metagdprtable." where user_id=%d ",array(email_exists($_POST['afmemail']))));
	}

	$deltable=$wpdb->prefix."wpfresh5_gdpr_requests";
	$wpdb->query($wpdb->prepare("update ".$deltable." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['afmreqid'])));
}
if($_POST['afmvalue']=='p')
{
	//delete posts or update author
	if(email_exists($_POST['afmemail']))
	{


		$args = array(
        'author'        =>email_exists($_POST['afmemail']),
        'orderby'       =>  'post_date',
        'order'         =>  'ASC',
        'posts_per_page' => -1
           );
     $posts=get_posts( $args );
   if(get_option($pref.'rtbf-reassign')=='1')
		{//delete post

	 foreach($posts as $data)
	 {
		 wp_delete_post( $data->ID);
	 }
	    }
   if(get_option($pref.'rtbf-reassign')=='0')
		{//update author


			$wpdb->query($wpdb->prepare("update ".$wpdb->prefix."posts set post_author=%d where post_author=%d ",array((int)get_option($pref.'rtbf-reassign-user'),(int)email_exists($_POST['afmemail']))));

		}
	}

	$deltable=$wpdb->prefix."wpfresh5_gdpr_requests";
	$wpdb->query($wpdb->prepare("update ".$deltable." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['afmreqid'])));
}
wp_mail($_POST['afmemail'],get_option($pref.'rtbf-title'),get_option($pref.'rtbf-message'),$wpcrmailheaders);
}
?>

<?php
$pref='wpf5gdpr-';

if(isset($_POST['gdprfmsettingssubmit']))
{
	if(filter_var($_POST['gdprfmnotifemail'], FILTER_VALIDATE_EMAIL))
	{
		update_option($pref.'rtbf-email',$_POST['gdprfmnotifemail']);
	}
// die('f');
    update_option($pref.'rtbf-title',$_POST['gdprfmtitle']);
    update_option($pref.'rtbf-message',stripslashes($_POST['gdprfmmsg']));
    update_option($pref.'rtbf-reassign-user',email_exists($_POST['gdprfmremail1']));
    // var_dump($_POST['gdprfmtitle']);
    // var_dump($_POST['gdprfmmsg']);

    // die();
	/*if(email_exists($_POST['gdprfmremail1']))
	{
	}
	else
	{
		echo "<script>alert('Unable to set reassign email , as user does not exist for this email id');</script>";
	}*/
    update_option($pref.'rtbf-reassign',$_POST['gdprfmdel']);
}

/*get_option($pref.'rtbf-email');
get_option($pref.'rtbf-title');
get_option($pref.'rtbf-message');
get_option($pref.'rtbf-reassign');
get_option($pref.'rtbf-reassign-user');*/
$user_info = get_userdata(get_option($pref.'rtbf-reassign'));
// $user_info->user_email;

//wp editor style
$editorsettings = array(
    'textarea_rows' =>7/*,
    'textarea_name' => 'gdprfmmsg',*/
);
?>

  <script type="text/javascript">
  function gdprfmAction(){
			var conf=confirm("Are you Sure");
			if(conf==true)
			{return true;}
			else
			{return false;}
			}
 function gdprfmActionDel(){
			var conf=confirm("Are you sure you want to delete this");
			if(conf==true)
			{return true;}
			else
			{return false;}
			}
  </script>

<?php
if(isset($_POST['gdprfmview']))
{
	echo "<br>";
		echo "<div class='container-fluid'><h4 class='m0' style='padding:10px;background: linear-gradient(#525863, #444a55);color:white;margin-top:5px;border-radius:2px;padding-left:15px;'>Requested data for selected Email ".$_POST['afmemail']."
		<a href=''><input type='button' class='btn btn-info goback' value=''></a>
		</h4></div>";

	global $wpdb;
	$table=$wpdb->prefix."wpfresh5_gdpr_requests";

	if($_POST['afmvalue']=='c')
	{// view all comments for selected request
		echo "<br><div class='container-fluid'><div class='row'>
		<div class='col-sm-12'>
		<div class='panel panel-primary'><div class='panel-heading'>
		<h4 class='m0'>Comments for:<strong> ".$_POST['afmemail']."</strong></h4></div>
<div class='panel-body'>
	  <div class='table-responsive'>
	  <table class='table table-striped table-hover'>
    <thead>
      <tr>
        <th>Comment ID</th>
        <th>Date</th>
        <th>Post ID</th>
		<th>Content</th>
		<th>Approved</th>
		<th>Type</th>
      </tr>
    </thead>
	<tbody>";

	$args=array(
	'author_email' =>$_POST['afmemail']);
	 $comments=get_comments( $args );
	 foreach($comments as $data)
	 {   echo "<tr>";

		  echo "<td>".$data->comment_ID."</td>
        <td>".$data->comment_date."</td>
        <td>".$data->comment_post_ID."</td>
		<td>".$data->comment_content."</td>
		<td>".$data->comment_approved."</td>
		<td>".$data->comment_type."</td>";
		  echo "</tr>";
	 }


	echo "</tbody>
	</table>
	</div>
	  </div>
	  </div>
	  </div>
	  </div></div>";
	}
	if($_POST['afmvalue']=='u')
	{// view all post meta for selected request
		echo "<br><div class='container-fluid'><div class='row'>
		<div class='col-sm-12'>
		<div class='panel panel-primary'><div class='panel-heading'>
		<h4 class='m0'>All user meta for:<strong> ".$_POST['afmemail']."</strong></h4></div>
<div class='panel-body'>
	  <div class='table-responsive'>

	  <table class='table table-striped table-hover'>
    <thead>
      <tr>
        <th>Meta ID</th>
        <th>User Id</th>
        <th>Meta Key</th>
		<th>Meta Value</th>
      </tr>
    </thead>
	<tbody>";

	if(email_exists($_POST['afmemail']))
	{
	$metagdprtable=$wpdb->prefix."usermeta";

	 $sql="select * from ".$metagdprtable." where user_id=".email_exists($_POST['afmemail'])."";
	   $gdprrecords=$wpdb->get_results($sql);

	 foreach($gdprrecords as $data)
	 {   echo "<tr>";

		  echo "<td>".$data->umeta_id."</td>
        <td>".$data->user_id."</td>
        <td>".$data->meta_key."</td>
		<td>".$data->meta_value."</td>";
		echo "</tr>";
	 }
	}
	echo "</tbody>
	</table>

		</div>
	  </div>
	  </div>
	  </div>
	  </div>
	  </div>";




	}
   if($_POST['afmvalue']=='p')
   {
	  //results of all posts for selected user
	  echo "<br><div class='container-fluid'><div class='row'>
		<div class='col-sm-12'>
		<div class='panel panel-primary'><div class='panel-heading'>
		<h4 class='m0'>Posts for:<strong> ".$_POST['afmemail']."</strong></h4></div>
<div class='panel-body'>
	  <div class='table-responsive'>
	  <table class='table table-striped table-hover'>
    <thead>
      <tr>
        <th>ID</th>
        <th>Date</th>
		<th>Title</th>
		<th>Content</th>
		<th>Modified</th>
		<th>Link</th>
		<th>Status</th>
      </tr>
    </thead>
	<tbody>";

	if(email_exists($_POST['afmemail']))
	{
	$args = array(
        'author'=>email_exists($_POST['afmemail']),
        'orderby' =>  'post_date',
        'order' =>  'ASC',
		'posts_per_page' => -1
           );

   $posts=get_posts( $args );
	 foreach($posts as $data)
	 {   echo "<tr>";

		  echo "<td>".$data->ID."</td>
        <td>".$data->post_date."</td>
        <td>".$data->post_title."</td>
		<td>".$data->post_content."</td>
		<td>".$data->post_modified_gmt."</td>
		<td><a href='".get_permalink( $data->ID )."' target='_BLANK'>".get_permalink( $data->ID )."</a></td>
		<td>".$data->post_status."</td>";
		  echo "</tr>";
	 }
	}

	echo "</tbody>
	</table>

	</div>
	  </div>
	  </div>
	  </div>
	  </div>
	  </div>";


   }
}

?>
	<?php
	if(! isset($_POST['gdprfmview']))
	{
		// echo $pref.'rtbf-message';
		// var_dump(get_option($pref.'rtbf-message'));
		?>


<h3>Right to be Forgotten - <a href="https://wpfreshstart5.com/support/knowledgebase/right-to-be-forgotten/" target="_blank" style="font-size:0.8em">Support</a></h3>
<p>Use this short code <b>[WPFS5GDPR]</b> to display the Right to be Forgotten form in the frontend.</p>
<h4 class="m0">Email Message</h4>
		<label>Admin Email:</label>
		<input class="form-control" type="email" name="gdprfmnotifemail" value="<?php echo get_option($pref.'rtbf-email'); ?>">
<br/>
		<label>Email Subject:</label>
		<input class="form-control" type="text" name="gdprfmtitle" value="<?php echo get_option($pref.'rtbf-title'); ?>">
<br/>
<?php wp_editor( get_option( $pref.'rtbf-message'), 'gdprfmmsg', $editorsettings ); ?>
<br/>
<button type="button" class="button button-secondary" id="rtbg-default-txt" >Use Default Content</button>
<textarea  id="gdprfmmsg-def" style="display:none">Hello,<br/><br/>
Please refer to your request to forget your data on our website.<br/><br/>
This is to report that action has been taken and your data has been forgotten as requested.<br/><br/>
If you have any queries, please let us know.<br/><br/><br/>
Thanks</textarea>
<br/>
<br/>


		<label> <input type="radio" style="margin-top: 2px;" value="0" name="gdprfmdel" <?php if(get_option($pref.'rtbf-reassign')=='0'){echo "checked";} ?>> Reassign to user email :</label>
<?php wp_dropdown_users( array( 'name' => 'gdprfmremail1', 'selected' => get_option($pref.'rtbf-reassign-user') ) ); ?>
<br/>
<label>
		<input type="radio" style="margin-top: 2px;" value="1" name="gdprfmdel" <?php if(get_option($pref.'rtbf-reassign')=='1'){echo "checked";} ?>>
		 Delete user Posts
		</label>
	<br/>
	<br/>
	 <input type="hidden"  value="1" name="gdprfmsettingssubmit" >

<h4 class="m0">Request List for Right to be Forgotten</h4>

<table class="tbl widefat gdprrqtable">
	<thead>
		<tr>
		<th>#</th>
		<th>Requested By</th>
		<th>Status</th>
		<th>Services to Forget</th>
		<th>Request Date</th>
		<th>Mail Date</th>
		<th>Options</th>
	    </tr>
		</thead>
		<tbody><?php echo wpfresh5_showRequestToForget(); ?></tbody>

 </table>
	<?php } ?>