<?php
$pref='wpf5gdpr-';
$timenow=date('d-M-Y h:iA');
global $wpdb;
$table=$wpdb->prefix."wpfresh5_gdpr_requests";


if(isset($_POST['defrecttext']))
{
	update_option($pref.'drr-message',wpfresh5_gdprEditorText('datarectification'));
}

if(isset($_POST['savedrrsettings']))
{
	if(filter_var($_POST['drremail'],FILTER_VALIDATE_EMAIL))
	{
update_option($pref.'drr-email',$_POST['drremail']);
	}
	else
	{echo "<script>alert('Invalid Email');</script>";}
update_option($pref.'drr-title',$_POST['drrtitle']);
update_option($pref.'drr-message',stripslashes($_POST['drrmessage']));
}
if(isset($_POST['gdprdrremove']))
{
	global $wpdb;
	$table=$wpdb->prefix."wpfresh5_gdpr_requests";
	$wpdb->query($wpdb->prepare("delete from ".$table." where id=%d ",array($_POST['drreqid'])));
}
if(isset($_POST['gdprdrtakeaction']))
{
	$wpcrmailheaders = "MIME-Version: 1.0" . "\r\n";
    $wpcrmailheaders .= "Content-type:text/html;charset=UTF-8" . "\r\n";
	 if(wp_mail($_POST['dremail'],get_option($pref.'drr-title'),get_option($pref.'drr-message'),$wpcrmailheaders))
  {
	  $datables=$wpdb->prefix."wpfresh5_gdpr_requests";
	  $wpdb->query($wpdb->prepare("update ".$datables." set action=%s , actiontime='%s' where id=%d ",array('1',$timenow,$_POST['drreqid'])));
	  echo "<script>alert('Confirmation email successfully sent to ".$to."');</script>";
  }
  else
  {
	   echo "<script>alert('Unable to send email');</script>";
  }
}
?>

  <script type="text/javascript">
  function gdprdrAction(){
			var conf=confirm("Are you sure you want to delete this");
			if(conf==true)
			{return true;}
			else
			{return false;}
			}
  </script>
	<?php
	if(isset($_POST['gdprdrview']))
	{
		if($_POST['drrtype']=='u')
{
	if(email_exists($_POST['dremail']))
	{
	$resultaction="<p align='right'><a href='user-edit.php?user_id=".email_exists($_POST['dremail'])."'><input type='button' class='btn btn-primary mdu' value=''></a></p>";
	}
	else
	{
		$resultaction="<font color='red'><b>User does not exist .</b></font>";
	}
}
if($_POST['drrtype']=='p')
{
	if(email_exists($_POST['dremail']))
	{
	$resultaction=
	"
	<table class='table table-stripped table-hover gdprrqtable'>
	<thead><tr><th>#</th><th>Date</th><th>Post Title</th><th>Action</th></tr><thead>
	<tbody>";
	$args = array(
    'author'        =>email_exists($_POST['dremail']),
    'orderby'       =>  'post_date',
    'order'         =>  'ASC'
    );
	$drposts=get_posts( $args );

	foreach($drposts as $data)
	{
		$resultaction .="<tr><td>".$data->ID."</td><td>".$data->post_date."</td><td>".$data->post_title."</td><td><a href='post.php?post=".$data->ID."&action=edit'><input type='button' class='btn btn-primary mdp' value=''></a></td></tr>";
	}
	$resultaction .="</tbody>
	</table>
	";
	}
}
if($_POST['drrtype']=='c')
{
	$drcomments="<table class='table table-stripped table-hover'><thead><tr><th>#</th><th>Date</th><th>Post Title</th><th>Comment</th><th>Action</th></tr></thead><tbody>";
	$args=array(
	'author_email' =>$_POST['dremail']);
	 $comments=get_comments( $args );
	 foreach($comments as $data)
	 {

		$drcomments .="<tr><td>". $data->comment_ID."</td><td>".$data->comment_date."</td><td>".get_the_title($data->comment_post_ID)."</td><td><p>".$data->comment_content."</p></td><td><a href='comment.php?action=editcomment&c=".$data->comment_ID."'><input type='button' class='btn btn-primary mdc'  value=''></a></td></tr>";

	 }
	 $drcomments .="</tbody></table>";
	 $resultaction=$drcomments;
}




		echo "<br>";
		echo "
		<h4 class='m0'>Requested data for selected Email ".$_POST['dremail']."
		<a href=''><input type='button' class='btn goback' value=''></a></h4>
		";
		$sql="select * from ".$table." where id=".$_POST['drreqid']."";
		$crdata=$wpdb->get_row($sql);
		echo
		"
		<div class='alert alert-warning clrblack'><h4>Request From User <h5>".$crdata->updatedvalue."</h5></div>
		".$resultaction."
		";

	}
	?>

	<?php
	if(! isset($_POST['gdprdrview']))
	{
	?>
<h3>Data Rectification Request Settings - <a href="https://wpfreshstart5.com/support/knowledgebase/right-to-rectification/" target="_blank" style="font-size:0.8em">Support</a></h3>
	 <p>Use this short code <b>[WPFS5GDPR]</b> to display the Data Rectification form in the frontend.</p>


		<label>Admin Email</label>
		<input class="form-control" type="email" name="drremail" value="<?php echo get_option($pref.'drr-email');  ?>">
<br/>

		<label>Mail Subject</label>
		<input type="text" class="form-control" name="drrtitle" value="<?php echo get_option($pref.'drr-title');  ?>">
<br/>
    <label for="">Content:</label>
    <?php wp_editor(get_option($pref.'drr-message'),"drrmessage",$settings = array(
    'editor_height' => 200,
    'textarea_rows' => 20,
	));?>

		<br/>
			<button type="button" class="button button-secondary" id="drect-default-txt" >Use Default Content</button>
			<textarea  id="drect-def" style="display:none">Hello,<br/><br/>
	Please refer to your data rectification request with us. We are pleased to inform you that action was taken on your request and your data has been rectified.<br/><br/>
	Please let us know if you need any further help.<br/><br/><br/>
	Thanks</textarea>
	<!-- <input type="submit" class="button button-secondary" value="Use Default Text"  name="defrecttext"> -->

  	<input type="hidden"  name="savedrrsettings" value="1"/>

<h4 class="m0">Data Rectification Requests List</h4>

<table class="tbl widefat">
	<thead>
		<tr>
		<th>#</th>
		<th>Requested By</th>
		<th>Status</th>
		<th>Services to Rectify</th>
		<th>Request Date</th>
		<th>Action Date</th>
		<th>Options</th>
	    </tr>
	</thead>
	<tbody>
	<?php wpfresh5_showRequestToDataRectification(); ?>
	</tbody>

 </table>

	<?php } ?>