<?php
/**
 * Filename: _database.php
 * Created by: BlogProfitNetwork.Com <marcus@blogprofitnetwork.com> (https://BlogProfitNetwork.Com)
 * Copyright:2018 All Rights Reserved - BlogProfitNetwork.Com <marcus@blogprofitnetwork.com> (https://BlogProfitNetwork.Com)
 *
 * Skype: blogprofitnetwork
 * Email: marcus@blogprofitnetwork.com
 * GTalk: marcus@blogprofitnetwork.com
 */
defined('_IN_MAIN_MODULE') or die ('ACCESS DENIED');

class funnelVoodooDatabase {
    var $db;
    var $dbPrefix;

    var $tableName;
    var $tableCollation;

    var $tableOptins;
    var $tableOptinsDefaults;

    var $tableAffiliates;
    var $tableAffiliatesDefaults;

    var $tableAdsense;
    var $tableAdsenseDefaults;

    var $tableThankyous;
    var $tableThankyousDefaults;

    var $tableSales;
    var $tableSalesDefaults;

    var $tableRecentComments;
    var $tableRecentCommentsDefaults;

    var $tableRecentPosts;
    var $tableRecentPostsDefaults;

    function __construct() {
        global $wpdb, $funnelVoodooThemes;

        $this->db                           = $wpdb;
        $this->dbPrefix                     = $this->db->prefix;

        $this->tableName                    = $this->dbPrefix .'fv_v3';
        $this->tableCollation               = $this->db->get_charset_collate();

        $this->tableOptins                  = $this->tableName .'_optins';
        $this->tableOptinsDefaults          = array(
        	'enable'                    => FUNNELVOODOO_NO,

        	'title'                     => FUNNELVOODOO_NAME .' Optin',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',

	        'popup' => array(
	        	'enable'        => FUNNELVOODOO_NO,
	            'btn_type'      => 'button', //* button or image
	            'btn'           => 'Click Here To Download',
	            'btn_img'       => '',
	        ),

	        'aweber'    => array(
	        	'enable'    => FUNNELVOODOO_YES,
		        'listname'  => '',
		        'thankyou'  => '',
		        'bullet_1'  => '',
		        'bullet_2'  => '',
		        'bullet_3'  => '',
		        'contents'  => '',
	        ),

	        'privacy'                   => '',

	        'theme' => array(
	        	'id'                    => '',
		        'main_bg'               => '',
		        'main_border_color'     => '',

		        'headline_color'        => '',
		        'headline_bold'         => '',

		        'txtbox_width'          => '',
		        'txtbox_border_color'   => '',
		        'txtbox_fontsize'       => '',

		        'btn_img'               => '', //* yes or no
		        'btn_img_src'           => '',
		        'btn_img_size'          => '',

		        'btn_value'             => '',
		        'btn_bg'                => '',
		        'btn_color'             => '',
		        'btn_border_color'      => '',
		        'btn_bold'              => '',
		        'btn_fontsize'          => '',
	        ),

	        'sidebar'   => array(
	        	'homepage'                  => FUNNELVOODOO_NO,
	            'pages'                     => FUNNELVOODOO_NO,
	            'pagesid'                   => '',
	            'nopagesid'					=> '',
	            'posts'                     => FUNNELVOODOO_NO,
	            'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableAffiliates              = $this->tableName .'_affiliates';
        $this->tableAffiliatesDefaults      = array(
        	'title'                     => FUNNELVOODOO_NAME .' Affiliates',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',

	        'sidebar'   => array(
	        	'homepage'                  => FUNNELVOODOO_NO,
	            'pages'                     => FUNNELVOODOO_NO,
	            'pagesid'                   => '',
	            'nopagesid'					=> '',
	            'posts'                     => FUNNELVOODOO_NO,
	            'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableAdsense                 = $this->tableName .'_adsense';
        $this->tableAdsenseDefaults         = array(
        	'title'                     => FUNNELVOODOO_NAME .' Misc Ads',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',

	        'sidebar'   => array(
				'homepage'                  => FUNNELVOODOO_NO,
				'pages'                     => FUNNELVOODOO_NO,
				'pagesid'                   => '',
				'nopagesid'					=> '',
				'posts'                     => FUNNELVOODOO_NO,
				'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableThankyous               = $this->tableName .'_thankyous';
        $this->tableThankyousDefaults       = array(
        	'title'                     => FUNNELVOODOO_NAME .' Thank You',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',

	        'sidebar'   => array(
				'homepage'                  => FUNNELVOODOO_NO,
				'pages'                     => FUNNELVOODOO_NO,
				'pagesid'                   => '',
				'nopagesid'					=> '',
				'posts'                     => FUNNELVOODOO_NO,
				'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableSales                   = $this->tableName .'_sales';
        $this->tableSalesDefaults           = array(
        	'title'                     => FUNNELVOODOO_NAME .' Sales',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',

	        'sidebar'   => array(
				'homepage'                  => FUNNELVOODOO_NO,
				'pages'                     => FUNNELVOODOO_NO,
				'pagesid'                   => '',
				'nopagesid'					=> '',
				'posts'                     => FUNNELVOODOO_NO,
				'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableRecentComments          = $this->tableName .'_recentcomments';
        $this->tableRecentCommentsDefaults  = array(
        	'title'                     => FUNNELVOODOO_NAME .' Recent Comments',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',
	        'how_many'                  => 0,

	        'sidebar'   => array(
				'homepage'                  => FUNNELVOODOO_NO,
				'pages'                     => FUNNELVOODOO_NO,
				'pagesid'                   => '',
				'nopagesid'					=> '',
				'posts'                     => FUNNELVOODOO_NO,
				'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );


        $this->tableRecentPosts             = $this->tableName .'_recentposts';
        $this->tableRecentPostsDefaults     = array(
        	'title'                     => FUNNELVOODOO_NAME .' Recent Posts',
	        'title_hide'                => FUNNELVOODOO_YES,

	        'contents'                  => '',
	        'how_many'                  => 0,

	        'sidebar'   => array(
				'homepage'                  => FUNNELVOODOO_NO,
				'pages'                     => FUNNELVOODOO_NO,
				'pagesid'                   => '',
				'nopagesid'					=> '',
				'posts'                     => FUNNELVOODOO_NO,
				'postsid'                   => '',
				'nopostsid'					=> ''
	        )
        );
    }

    function execTable($table) {
        include_once (ABSPATH .'/wp-admin/includes/upgrade.php');
        dbDelta($table);
    }

    function install() {

    }

    function uninstall() {

    }



    //* let the fun begin!
	function updateRecentPosts() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$recentposts = get_option($this->tableRecentPosts .'_'. $id);
    		if (is_array($recentposts) || !is_array($recentposts)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],
			        'how_many'  => intval($_POST['howMany']),

			        'sidebar'   => array(
			            'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
			            'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
			            'pagesid'   => trim(strip_tags($_POST['pagesid'])),
			            'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
			            'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
			            'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableRecentPosts .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateRecentComments() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$recentcomments = get_option($this->tableRecentComments .'_'. $id);
    		if (is_array($recentcomments) || !is_array($recentcomments)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],
			        'how_many'  => intval($_POST['howMany']),

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableRecentComments .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateSales() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$sales = get_option($this->tableSales .'_'. $id);
    		if (is_array($sales) || !is_array($sales)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableSales .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateThankyous() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$thankyous = get_option($this->tableThankyous .'_'. $id);
    		if (is_array($thankyous) || !is_array($thankyous)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableThankyous .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateAdsense() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$adsense = get_option($this->tableAdsense .'_'. $id);
    		if (is_array($adsense) || !is_array($adsense)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableAdsense .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateAffiliate() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$affiliate = get_option($this->tableAffiliates .'_'. $id);
    		if (is_array($affiliate) || !is_array($affiliate)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableAffiliates .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

	function updateOptin() {
    	if ($_POST) {

    		$id = intval($_POST['id']);
    		$optin  = get_option($this->tableOptins .'_'. $id);
    		if (is_array($optin) || !is_array($optin)) {
    			$data = array(
		            'enable'    => (trim(strip_tags($_POST['enable'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

		            'title'     => trim(strip_tags($_POST['title'])),
			        'title_hide'=> (trim(strip_tags($_POST['titleHide'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,

			        'contents'  => $_POST['contents'],

			        'popup' => array(
			            'enable'        => (trim(strip_tags($_POST['popup'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
			            'btn_type'      => trim(strip_tags($_POST['popupType'])),
			            'btn'           => trim(strip_tags($_POST['popupBtn'])),
			            'btn_img'       => $_POST['popupImg'],
			        ),

			        'aweber'    => array(
			            'enable'    => (trim(strip_tags($_POST['aweber'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
				        'listname'  => trim(strip_tags($_POST['aweberListname'])),
				        'thankyou'  => trim(strip_tags($_POST['aweberThankyou'])),
				        'bullet_1'  => trim(strip_tags($_POST['aweberBullet1'])),
				        'bullet_2'  => trim(strip_tags($_POST['aweberBullet2'])),
				        'bullet_3'  => trim(strip_tags($_POST['aweberBullet3'])),
				        'contents'  => $_POST['aweberContents'],
			        ),

			        'privacy'                   => $_POST['privacy'],

			        'theme' => array(
			            'id'                    => trim(strip_tags($_POST['theme'])),
				        'main_bg'               => trim(strip_tags($_POST['themeMainBg'])),
				        'main_border_color'     => trim(strip_tags($_POST['themeMainBorderColor'])),

				        'headline_color'        => trim(strip_tags($_POST['themeHeadlineColor'])),
				        'headline_bold'         => (trim(strip_tags($_POST['themeHeadlineBold'])) == 'true') ? 'bold' : 'normal',

				        'txtbox_width'          => trim(strip_tags($_POST['themeTxtboxWidth'])),
				        'txtbox_border_color'   => trim(strip_tags($_POST['themeTxtboxBorderColor'])),
				        'txtbox_fontsize'       => trim(strip_tags($_POST['themeTxtboxFontsize'])),

				        'btn_img'               => trim(strip_tags($_POST['themeBtnImg'])),
				        'btn_img_src'           => trim(strip_tags($_POST['themeBtnImgSrc'])),
				        'btn_img_size'          => intval($_POST['themeBtnImgSize']),

				        'btn_value'             => trim(strip_tags($_POST['themeBtnValue'])),
				        'btn_bg'                => trim(strip_tags($_POST['themeBtnBg'])),
				        'btn_color'             => trim(strip_tags($_POST['themeBtnColor'])),
				        'btn_border_color'      => trim(strip_tags($_POST['themeBtnBorderColor'])),
				        'btn_bold'              => (trim(strip_tags($_POST['themeBtnBold'])) == 'true') ? 'bold' : 'normal',
				        'btn_fontsize'          => trim(strip_tags($_POST['themeBtnFontsize']))
			        ),

			        'sidebar'   => array(
						'homepage'  => (trim(strip_tags($_POST['homepage'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pages'     => (trim(strip_tags($_POST['pages'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'pagesid'   => trim(strip_tags($_POST['pagesid'])),
						'nopagesid'	=> trim(strip_tags($_POST['nopagesid'])),
						'posts'     => (trim(strip_tags($_POST['posts'])) == 'true') ? FUNNELVOODOO_ENABLED : FUNNELVOODOO_DISABLED,
						'postsid'   => trim(strip_tags($_POST['postsid'])),
						'nopostsid'	=> trim(strip_tags($_POST['nopostsid']))
			        )
		        );
			    update_option($this->tableOptins .'_'. $id, $data);
		    }
		    die ('done');

	    }
	    die ('error');
	}

}
