<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://orbicular.media
 * @since      1.0.0
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 * @author     Bryan Meeks <bryan@orbicular.media>
 */
class Cemetery_Sexton_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Cemetery_Sexton_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Cemetery_Sexton_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		//wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/cemetery-sexton-public.css', array(), $this->version, 'all' );
	    wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/cemetery-sexton-public.css.php', array(), $this->version, 'all' );
	    wp_enqueue_style( 'fontawesome', '//use.fontawesome.com/releases/v5.1.0/css/all.css', array(), $this->version, 'all');
		
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Cemetery_Sexton_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Cemetery_Sexton_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
   
    	wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/cemetery-sexton-public.js', array( 'jquery' ), $this->version, false );
		
		// include the thickbox modal javascript
		wp_enqueue_script('thickbox', null, array('jquery'));
		// include the thickbox modal styles
		wp_enqueue_style('thickbox.css', '/'.WPINC.'/js/thickbox/thickbox.css', null, '1.0');
		
		// include jquery
		wp_enqueue_script('jQuery','https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js');
	}
	
	/**
	 * Search Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_search(){
	    //Display partial html
	    ob_start();
	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-search.php' );
	    return ob_get_clean();
	}
	/**
	 * Quick Search Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_quick_search(){
	    //Display partial html
	    ob_start();
	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-quick-search.php' );
	    return ob_get_clean();
	}
	
	/**
	 * Results Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_results(){
	    //Include tools needed to render page
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-pagination.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-results.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-tools.php' );
	    //get data from the form post submission
	    $getParams = $_GET;
	    if (empty($getParams)){
	        ob_start();
	        echo 'No search parameters entered. Please start a new search. <br>';
	        return ob_get_clean();
	    }else {
    	    //build page
    	    $resultsPage = New Cemetery_Sexton_Results();
    	    //get total results
    	    $tools = New Cemetery_Sexton_tools();
    	    $rstotal = $resultsPage->results_getTotal($getParams);
    	    //get pagination parameters
    	    $pagi = $resultsPage->results_pagination($getParams);
    	    //pagination total number of results
    	    $pagi['rstotal'] = $rstotal;
    	    //pagename slug for results
    	    $pagi['pagename'] = 'results';
    	    //pagination search parameters
    	    $pagi['pageparams'] = $resultsPage->results_getPageParams($getParams);
    	    //if no results, notify, else, print results page
    	    if ($rstotal == 0) {
    	        ob_start();
    	        echo 'No records found. Please try a different search. <br>';
    	        return ob_get_clean();
    	    }else {
    	        //echo 'records found <br>';
    	        //new pagination for display
    	        $thisPagination = New Cemetery_Sexton_Pagination();
    	        $thisPagination->CustomPagination($pagi['pageno'],$pagi['recpage']);
    	        $thisPagination->setTotalRecords($pagi['rstotal']);
    	        //Get the records from the database
    	        $interRow = $resultsPage->results_getRecords($getParams);
    	        // Display partial html
                ob_start();
    	        include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-results.php' );
    	        return ob_get_clean();
    	    }
	    }
	}
	
	/**
	 * Interment Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_interment(){
	    //Include tools needed to render page
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-interment.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-lot.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-tools.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-images.php' );
	    //get data from the form post submission
	    $getParams = $_GET;
	    if (empty($getParams)){
	        ob_start();
	        echo 'No interment number found. Please start a new search. <br>';
	        return ob_get_clean();
	    }else {
	        $interid = $getParams['InterID'];
    	    //get display tools
    	    $tools = New Cemetery_Sexton_tools();
    	    $options = get_option('cemetery-sexton');
    	    // Interment info
    	    $interData = new Cemetery_Sexton_Interment();
    	    // Get interment record
    	    $rsInter = $interData->interment_getInterment($interid);
    	    // Format display name and location
    	    $interName = $rsInter['iFirstName'].' '.$rsInter['iMidName'].' '.$rsInter['iLastName'].' '.$rsInter['iSuffix'];
    	    // Get lot info
    	    $lotData = New Cemetery_Sexton_Lot();
    	    $LotID = $rsInter['LotID'];
    	    $rsLot = $lotData->lot_getLot($LotID);
    	    $lotLocation = $tools->tools_displayLocation($rsLot['Block'],$rsLot['Section'],$rsLot['RowNo'],$rsLot['SpaceNo'],$rsLot['LotNo'],$rsLot['Compass'],$rsLot['Mausoleum'],$rsLot['CryptNo'],$rsLot['Niche']);
    	    // Get interments on same lot
    	    $sameLot = $lotData->lot_getSameLot($LotID);
    	    // Family memorial photos related to the interment
    	    $lotLink = $interData->interment_linkLot($LotID);
    	    // Add modal popup capability
    	    add_thickbox();
    	    // Display partial html
    	    ob_start();
    	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-interment.php' );
    	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-lotmap.php' );
    	    return ob_get_clean();
	   }
	}
	
	/**
	 * Interment Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_lot(){
	    //Include tools needed to render page
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-interment.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-lot.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-tools.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-images.php' );
	    //get data from the form post submission
	    $getParams = $_GET;
	    if (empty($getParams)){
	        ob_start();
	        echo 'No lot number found. Please start a new search. <br>';
	        return ob_get_clean();
	    }else {
    	    $LotID = $getParams['LotID'];
    	    //build page
    	    $lotData = New Cemetery_Sexton_Lot();
    	    //get display tools
    	    $tools = New Cemetery_Sexton_tools();
    	    $options = get_option('cemetery-sexton');
    	    // Format display name and location
    	    $rsLot = $lotData->lot_getLot($LotID);
    	    $lotLocation = $tools->tools_displayLocation($rsLot['Block'],$rsLot['Section'],$rsLot['RowNo'],$rsLot['SpaceNo'],$rsLot['LotNo'],$rsLot['Compass'],$rsLot['Mausoleum'],$rsLot['CryptNo'],$rsLot['Niche']);
    	    // Get interments on same lot
    	    $sameLot = $lotData->lot_getSameLot($LotID);
    	    // Add modal popup capability
    	    add_thickbox();
    	    // Display partial html
    	    ob_start();
    	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-lot.php' );
    	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-lotmap.php' );
    	    return ob_get_clean();
	    }
	}
	
	/**
	 * Map Page Shortcode
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_map(){
	    $options = get_option('cemetery-sexton');
	    ob_start();
	    include_once( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-map.php' );
	    return ob_get_clean();
	}
	
	/**
	 * Process search form for the public-facing side of the site.
	 *
	 * @since    1.1.0
	 */
	public function cemetery_sexton_process_search(){
	    //get data from the form post submission
	    $post =  array_filter($_POST['search']);
	    //redirect to results view with visible url parameters
	    $redirect = esc_url_raw( add_query_arg( $post, get_permalink(get_option('cemetery-sexton')['page_results']) ) );
	    wp_redirect($redirect,301);
	    exit;
	}
	
	/**
	 * Build Custom Page Titles
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_page_title($title_parts) {
	    //include_once tools needed to render page
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-interment.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-lot.php' );
	    include_once( plugin_dir_path( __FILE__ ) . 'class-cemetery-sexton-tools.php' );
	    $slug = get_post_field( 'post_name', get_post() );
	    $getParams = $_GET;
	    if ($slug == 'interment'){
	        if (empty($getParams)){
	            $title_parts['title'] = $title_parts['title'];
	        }else {
	            $interid = $getParams['InterID'];
    	        // Interment info
    	        $interData = new Cemetery_Sexton_Interment();
    	        // Get interment record
    	        $rsInter = $interData->interment_getInterment($interid);
    	        // Format display name and location
    	        $title_parts['title'] = 'Interment: '.$rsInter['iFirstName'].' '.$rsInter['iMidName'].' '.$rsInter['iLastName'].' '.$rsInter['iSuffix'];
	        }
	    }else if ($slug == 'lot'){
	        if (empty($getParams)){
	            $title_parts['title'] = $title_parts['title'];
	        }else {
	            $LotID = $getParams['LotID'];
	            //build page
	            $lotData = New Cemetery_Sexton_Lot();
	            //get display tools
	            $tools = New Cemetery_Sexton_tools();
	            $options = get_option('cemetery-sexton');
	            // Format display name and location
	            $rsLot = $lotData->lot_getLot($LotID);
	            $title_parts['title'] = 'Family Lot: '.$tools->tools_displayLocation($rsLot['Block'],$rsLot['Section'],$rsLot['RowNo'],$rsLot['SpaceNo'],$rsLot['LotNo'],$rsLot['Compass'],$rsLot['Mausoleum'],$rsLot['CryptNo'],$rsLot['Niche']);
	        }
	    }else{
	        $title_parts['title'] = $title_parts['title'];
	    }
	    return $title_parts;
	}
}
