cf7_adb.class.php 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php
class CF7AdvanceDB{
	
	function __construct() {
		add_action( 'admin_menu', array($this,'renderGUI') );
		add_action( 'wpcf7_before_send_mail', array( $this, 'beforeSendEmail' ));
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueueLibs' ) );
		
		//ajax
		add_action( 'wp_ajax_delete_cf7_data', array($this, 'cf7AdbAjaxController') );
		add_action( 'wp_ajax_export_cf7_data', array($this, 'cf7AdbExportController') );
		add_action('admin_menu', array($this, 'cf7AdbExportController'));

		//admin notice
		if( get_option( 'cf7-adb-data-show-notif', false ) ){
			add_action( 'admin_notices', array( $this, 'dashboard_notices' ) ); 
		}
		
	}
	
	function renderGUI(){
		add_submenu_page( 'wpcf7','Contact Form Advanced Database','Contact Form Advanced Database', 'manage_options', 'cf7-adb', array($this,'renderBackend') );
	}
	
	function renderBackend(){
		
		require_once('display/cf7-db-view.php');

		//hide notification
		update_option( 'cf7-adb-data-show-notif', 0 );
		
	}
	

	function beforeSendEmail( $cf7 ){
			
		$submission = WPCF7_Submission::get_instance();
		if ( $submission ) {
			$data = $submission->get_posted_data();
			$dataArr = array_merge($data,array('date-de-reception' => current_time( 'mysql' )));
			add_post_meta($data['_wpcf7'],'cf7-adb-data',$dataArr);	
			$unread_messages = get_post_meta( $data['_wpcf7'], 'cf7-adb-data-unread', true );
			update_post_meta( $data['_wpcf7'],'cf7-adb-data-unread', ( intval( $unread_messages ) + 1 ) );
			//status 1= show notification | 2 = hide notofication
			update_option( 'cf7-adb-data-show-notif', 1 );
		}
	}
	
	function enqueueLibs($hook){
			wp_enqueue_style( 'cf7-adb', CF7ADBURL.'/lib/css/style.css' );
			wp_enqueue_script( 'cf7-dataTables', CF7ADBURL.'/lib/js/jquery.dataTables.min.js', array(), '1.10.6', true );
			wp_enqueue_script( 'cf7-script', CF7ADBURL.'/lib/js/cf7-script.js', array(), '1.0.0', true );		
	}
	
	

	function cf7AdbAjaxController() {
	   
	   if(!empty($_POST['data'])){
			foreach($_POST['data'] as $postData){
				delete_post_meta($postData['id'],$postData['key'],maybe_unserialize(base64_decode($postData['val'])));
			}
			echo "success";
	   }else{
		echo "error";
	   }
		
	   die();
	}
	
	function cf7AdbExportController() {
	  // filename for download\
		$hook = add_submenu_page(null, '', '', 'administrator', 'cf7-adb-export-xls', function(){});
		add_action('load-' . $hook, function() {
			$id= $_GET['id'];
			$filename = "contact_form_advanced_database_" . date('Ymd') . ".csv";
			header("Content-Disposition: attachment; filename=\"$filename\"");
			header("Content-Type: application/vnd.ms-excel");
			
			require_once('display/export.php');
			
        exit;
		});
		
	}

	function dashboard_notices() {
	        echo"<div class='updated cfdb-notif'> <p>New Message Received. Please Click <a href='admin.php?page=cf7-adb'>Here</a> to view new messages.</p></div>"; 
	}
	


					
}



?>