<?php

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

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

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;
	
	/**
	 * The options name to be used in this plugin
	 *
	 * @since  	1.0.0
	 * @access 	private
	 * @var  	string 		$option_name 	Option name of this plugin
	 */
	private $option_name = 'cemetery_sexton';
	
	/**
	 * 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 this 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 admin area.
	 *
	 * @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-admin.css', array(), $this->version, 'all' );
		
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @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-admin.js', array( 'jquery' ), $this->version, false );

	}
	
	/**
	 * Register the administration menu for this plugin into the WordPress Dashboard menu.
	 *
	 * @since    1.0.0
	 */
	
	public function add_plugin_admin_menu() {
	    
	    /*
	     * Add a settings page for this plugin to the Settings menu.
	     *
	     * NOTE:  Alternative menu locations are available via WordPress administration menu functions.
	     *
	     *        Administration Menus: http://codex.wordpress.org/Administration_Menus
	     *
	     */
	    add_options_page( 
	        'Cemetery Sexton Settings',
	        'Cemetery',
	        'manage_options',
	        $this->plugin_name,
	        array($this, 'display_options_page')
	    );
	}
	
	/**
	 * Register all related settings of this plugin
	 *
	 * @since  1.1.0
	 */
	public function register_setting() {
	    //Display Section
	    add_settings_section(
	        $this->plugin_name . '-display',                        // ID
	        'Page Selections',                                      // Title
	        array( $this, $this->option_name . '_display_cb' ),     // Callback
	        $this->plugin_name. '-display'                          // Page
	        );
	    
	    /**add_settings_field(
	        $this->plugin_name . '-parent-slug',                       // ID
	        'Parent Slug',                                             // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-display',                           // Page
	        $this->plugin_name . '-display',                           // Section
	        array( 'label_for' => $this->plugin_name . '-parent-slug',
	            'ID' => 'parent_slug',
	            'type' => 'text',
	            'section' => 'display'
	        )//Label
	        );*/
	    
	    add_settings_field(
	        $this->plugin_name . '-page-results',                      // ID
	        'Search Results Page',                                     // Title
	        array( $this, $this->option_name . '_setting_page_dropdown' ), // Callback
	        $this->plugin_name . '-display',                           // Page
	        $this->plugin_name . '-display',                           // Section
	        array( 'label_for' => $this->plugin_name . '-page-results',
	            'ID' => 'page_results',
	            'type' => 'text',
	            'section' => 'display'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-page-interment',                      // ID
	        'Interment Page',                                             // Title
	        array( $this, $this->option_name . '_setting_page_dropdown' ), // Callback
	        $this->plugin_name . '-display',                           // Page
	        $this->plugin_name . '-display',                           // Section
	        array( 'label_for' => $this->plugin_name . '-page-interment',
	            'ID' => 'page_interment',
	            'type' => 'text',
	            'section' => 'display'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-page-lot',                      // ID
	        'Family Lot Page',                                             // Title
	        array( $this, $this->option_name . '_setting_page_dropdown' ), // Callback
	        $this->plugin_name . '-display',                           // Page
	        $this->plugin_name . '-display',                           // Section
	        array( 'label_for' => $this->plugin_name . '-page-lot',
	            'ID' => 'page_lot',
	            'type' => 'text',
	            'section' => 'display'
	        )//Label
	        );
	    
	    //Database Section
	    add_settings_section(
	        $this->plugin_name . '-database',                       // ID
	        'SQL Server Connection Settings',                       // Title
	        array( $this, $this->option_name . '_database_cb' ),    // Callback
	        $this->plugin_name. '-database'                         // Page
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-sqlserver-address',                 // ID
	        'Server IP Address',                                       // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_address',
	            'ID' => 'sqlserver_address',
	            'type' => 'text',
	            'section' => 'database'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-sqlserver-port',                    // ID
	        'Server Port',                                             // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_port',
	            'ID' => 'sqlserver_port',
	            'type' => 'text',
	            'section' => 'database'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-sqlserver_instance',                // ID
	        'SQL Instance Name',                                       // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_instance',
	            'ID' => 'sqlserver_instance',
	            'type' => 'text',
	            'section' => 'database'
	        )//Label
	        );
	    add_settings_field(
	        $this->plugin_name . '-sqlserver-username',                // ID
	        'SQL Username',                                            // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_username',
	            'ID' => 'sqlserver_username',
	            'type' => 'text',
	            'section' => 'database'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-sqlserver-password',                // ID
	        'SQL Password',                                            // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_password',
	            'ID' => 'sqlserver_password',
	            'type' => 'password',
	            'section' => 'database'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-sqlserver-database',                // ID
	        'Database Name',                                           // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-database',                          // Page
	        $this->plugin_name . '-database',                          // Section
	        array( 'label_for' => $this->plugin_name . '-database-sqlserver_database',
	            'ID' => 'sqlserver_database',
	            'type' => 'text',
	            'section' => 'database'
	        )//Label
	        );
	    
	    
	    // Photo Section
	    add_settings_section(
	        $this->plugin_name . '-photos',                        // ID
	        'Photo Server Settings',                               // Title
	        array( $this, $this->option_name . '_photos_cb' ),     // Callback
	        $this->plugin_name. '-photos'                          // Page
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-photoserv-inter',                   // ID
	        'Interment Photo Server',                                  // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-photos',                            // Page
	        $this->plugin_name . '-photos',                            // Section
	        array( 'label_for' => $this->plugin_name . '-photos-photoserv_inter',
	            'ID' => 'photoserv_inter',
	            'type' => 'text',
	            'section' => 'photos'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-photoserv-fammon',                  // ID
	        'Family Memorial Photo Server',                            // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-photos',                            // Page
	        $this->plugin_name . '-photos',                            // Section
	        array( 'label_for' => $this->plugin_name . '-photos-photoserv_fammon',
	            'ID' => 'photoserv_fammon',
	            'type' => 'text',
	            'section' => 'photos'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-photoserv-obit',                    // ID
	        'Obituary Photo Server',                                   // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-photos',                            // Page
	        $this->plugin_name . '-photos',                            // Section
	        array( 'label_for' => $this->plugin_name . '-photos-photoserv_obit',
	            'ID' => 'photoserv_obit',
	            'type' => 'text',
	            'section' => 'photos'
	        )//Label
	        );
	    
	    // Map Section
	    add_settings_section(
	        $this->plugin_name . '-map',                        // ID
	        'Google Map Settings',                              // Title
	        array( $this, $this->option_name . '_map_cb' ),     // Callback
	        $this->plugin_name. '-map'                          // Page
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-google-map-key',                    // ID
	        'Google Map API Key',                                      // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-map',                               // Page
	        $this->plugin_name . '-map',                               // Section
	        array( 'label_for' => $this->plugin_name . '-map-google_map_key',
	            'ID' => 'google_map_key',
	            'type' => 'text',
	            'section' => 'map'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-mapoverlay_aerial',                 // ID
	        'Aerial Photo Map Overlay Server',                         // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-map',                               // Page
	        $this->plugin_name . '-map',                               // Section
	        array( 'label_for' => $this->plugin_name . '-map-mapoverlay_aerial',
	            'ID' => 'mapoverlay_aerial',
	            'type' => 'text',
	            'section' => 'map'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-mapoverlay-section',                // ID
	        'Sectional Lot Line Map Overlay Server',                   // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-map',                               // Page
	        $this->plugin_name . '-map',                               // Section
	        array( 'label_for' => $this->plugin_name . '-map-mapoverlay_section',
	            'ID' => 'mapoverlay_section',
	            'type' => 'text',
	            'section' => 'map'
	        )//Label
	        );
	    
	    // Style Section
	    add_settings_section(
	        $this->plugin_name . '-style',                        // ID
	        'Plugin Frontend Style Settings',                     // Title
	        array( $this, $this->option_name . '_style_cb' ),     // Callback
	        $this->plugin_name. '-style'                          // Page
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-style-results-bg-header',           // ID
	        'Interment List Header Background Color',                  // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-style',                             // Page
	        $this->plugin_name . '-style',                             // Section
	        array( 'label_for' => $this->plugin_name . '-style-style_results_bg_header',
	            'ID' => 'style_results_bg_header',
	            'type' => 'text',
	            'section' => 'style'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-style-results-bg-even',             // ID
	        'Interment List Even Background Color',                    // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-style',                             // Page
	        $this->plugin_name . '-style',                             // Section
	        array( 'label_for' => $this->plugin_name . '-style-style_results_bg_even',
	            'ID' => 'style_results_bg_even',
	            'type' => 'text',
	            'section' => 'style'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-style-results-bg-odd',              // ID
	        'Interment List Odd Background Color',                     // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-style',                             // Page
	        $this->plugin_name . '-style',                             // Section
	        array( 'label_for' => $this->plugin_name . '-style-style_results_bg_odd',
	            'ID' => 'style_results_bg_odd',
	            'type' => 'text',
	            'section' => 'style'
	        )//Label
	        );
	    
	    add_settings_field(
	        $this->plugin_name . '-style-results-bg-hover',            // ID
	        'Interment List Hover Background Color',                   // Title
	        array( $this, $this->option_name . '_setting_string_fn' ), // Callback
	        $this->plugin_name . '-style',                             // Page
	        $this->plugin_name . '-style',                             // Section
	        array( 'label_for' => $this->plugin_name . '-style-style_results_bg_hover',
	            'ID' => 'style_results_bg_hover',
	            'type' => 'text',
	            'section' => 'style'
	        )//Label
	        );
	    
	    // Register and sanitize settings
	    register_setting($this->plugin_name, $this->plugin_name, array($this, 'sanitize'));
	    
	}
	
	/**
	 * Render the text for the section callbacks
	 *
	 * @since  1.1.0
	 */
	public function cemetery_sexton_display_cb() {
	    echo '<p>Select the pages you have created for each display page listed above.</p>';
	}
	
	public function cemetery_sexton_database_cb() {
	    echo '<p>The server address is the ip address of your Microsoft SQL server.</p>';
	}
	
	public function cemetery_sexton_photos_cb() {
	    echo '<p>The server address is either the ip address of your url where interment, family memorial, and obituary photos are hosted.</p>';
	}
	
	public function cemetery_sexton_map_cb() {
	    echo '<p>A Google Maps API key is required to show maps on the website. Cemetery provided aerial photo and sectional lot line overlay are visible only when the appropriate server address are supplied.</p>'
	        .'<p>Insert the shortcode <code>[cemetery-sexton-map]</code> to display the cemetery map on any page.</p>';
	}
	
	public function cemetery_sexton_style_cb() {
	    echo '<p>Adjust the colors for the front end display.</p>';
	}
	
	/**
	 * Render the text for input textbox
	 *
	 * @since  1.0.0
	 */
	function cemetery_sexton_setting_string_fn( $args ) {
	    $options = get_option($this->plugin_name);
	    $output = '<input type="'.$args['type'].'" class="regular-text" id="'.$this->plugin_name.'-'.$args['section'].'-'.$args['ID'].'" name="'.$this->plugin_name.'['.$args['ID'].']" size="40"';
	    if(isset($options[$args['ID']])){
	        $output .='value="'.$options[$args['ID']].'"';
	    }
        $output .=' >';
        echo $output;
	}
	
	/**
	 * Render the dropdown to select page
	 *
	 * @since  1.1.0
	 */
	function cemetery_sexton_setting_page_dropdown( $args ) {
	    $options = get_option($this->plugin_name);
	    $pages = get_pages();

	    $output = '<select id="'.$this->plugin_name.'-'.$args['section'].'-'.$args['ID'].'" name="'.$this->plugin_name.'['.$args['ID'].']">';
        $output.= '<option value="0">Select a Page</option>';
    
	    foreach( $pages as $page ) { 
	        if(isset($options[$args['ID']])){
	           $output.='<option value="'.$page->ID.'" '. selected( $options[$args['ID']],$page->ID,false ).'>'. $page->post_title .'</option>';
	        }else{
	           $output.='<option value="'.$page->ID.'">'. $page->post_title .'</option>';
	        }
		};
		
		$output.='</select>';
	    echo $output;
	}
	
	/**
	 * Add settings action link to the plugins page.
	 *
	 * @since    1.0.0
	 */
	public function add_action_links( $links ) {
	    /*
	     *  Documentation : https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name)
	     */
	    $settings_link = array(
	        '<a href="' . admin_url( 'options-general.php?page=' . $this->plugin_name ) . '">' . __('Settings', $this->plugin_name) . '</a>',
	    );
	    return array_merge(  $settings_link, $links );
	    
	}

	
	/**
	 * Render the options page for this plugin.
	 * 
	 * http://localhost/wordpress/wp-admin/options-general.php?page=cemetery-sexton
	 *
	 * @since    1.1.0
	 */
	public function display_options_page() {
	    // Create WP Admin Tabs on-the-fly
	    $tabs = array(
	        'display'  => 'Display',
	        'database' => 'Database',
	        'photos'   => 'Photos',
	        'map'      => 'Map',
	        'style'    => 'Style',
	    );
	    // Get tab and set current
	    if(isset($_GET['tab'])){
	        $current = $_GET['tab'];
	    }else{
	        $current = 'display';
	    }
	    // Build tab navigation
	    $nav = '';
	    $nav .= '<h2 class="nav-tab-wrapper">';
	    foreach( $tabs as $tab => $tabname ) {
	        if ( $current == $tab ) {
	            $class = ' nav-tab-active';
	        } else {
	            $class = '';
	        }
	        $nav .= '<a class="nav-tab' . $class . '" href="?page=' .
	   	        'cemetery-sexton' . '&tab=' . $tab . '">' . $tabname . '</a>';
	    }
	    $nav .= '</h2>';
	    echo '<h1>'.esc_html( get_admin_page_title()).'</h1>';
	    echo '<p>Completely fill out the required information on the Database, Photos, and Map tabs.<br>Finish Plugin setup by following the instructions on the Display tab.</p>';
	    echo $nav;
	    // include page output
	    if ( ! $current )
	        $current = key( $tabs );
	        require_once( 'partials/cemetery-sexton-admin-'.$current.'.php' );
	        $options = get_option($this->plugin_name);
	        //print_r($options);
	}

	/**
	 *
	 * Validate and sanitize settings form inputs
	 * 
	 *@since    1.1.0
	 **/
	public function sanitize($input) {
	    $options = get_option($this->plugin_name);
	    $newInput = array();
	    // Merge input from each page with existing settings array
	    // $var = isset($var) ? $var : "default";
	    // Cleanup display input
	    //$newInput['parent_slug'] = isset($input['parent_slug']) ? sanitize_text_field($input['parent_slug']) : $options['parent_slug'];
	    $newInput['page_results'] = isset($input['page_results']) ? $input['page_results'] : $options['page_results'];
	    $newInput['page_interment'] = isset($input['page_interment']) ? $input['page_interment'] : $options['page_interment'];
	    $newInput['page_lot'] = isset($input['page_lot']) ? $input['page_lot'] : $options['page_lot'];	    
	    // Cleanup database input
	    $newInput['sqlserver_address'] = isset($input['sqlserver_address']) ? sanitize_text_field($input['sqlserver_address']) : $options['sqlserver_address'];
	    $newInput['sqlserver_port'] = isset($input['sqlserver_port']) ? sanitize_text_field($input['sqlserver_port']) : $options['sqlserver_port'];
	    $newInput['sqlserver_instance'] = isset($input['sqlserver_instance']) ? sanitize_text_field($input['sqlserver_instance']) : $options['sqlserver_instance'];
	    $newInput['sqlserver_username'] = isset($input['sqlserver_username']) ? sanitize_text_field($input['sqlserver_username']) : $options['sqlserver_username'];
	    $newInput['sqlserver_password'] = isset($input['sqlserver_password']) ? sanitize_text_field($input['sqlserver_password']) : $options['sqlserver_password'];
	    $newInput['sqlserver_database'] = isset($input['sqlserver_database']) ? sanitize_text_field($input['sqlserver_database']) : $options['sqlserver_database'];
	    // Cleanup photo servers
	    $newInput['photoserv_inter'] = isset($input['photoserv_inter']) ? esc_url($input['photoserv_inter']) : $options['photoserv_inter'];
	    $newInput['photoserv_fammon'] = isset($input['photoserv_fammon']) ? esc_url($input['photoserv_fammon']) : $options['photoserv_fammon'];
	    $newInput['photoserv_obit'] = isset($input['photoserv_obit']) ? esc_url($input['photoserv_obit']) : $options['photoserv_obit'];
	    // Cleanup map options
	    $newInput['google_map_key'] = isset($input['google_map_key']) ? ($input['google_map_key']) : $options['google_map_key'];
	    $newInput['mapoverlay_aerial'] = isset($input['mapoverlay_aerial']) ? esc_url($input['mapoverlay_aerial']) : $options['mapoverlay_aerial'];
	    $newInput['mapoverlay_section'] = isset($input['mapoverlay_section']) ? esc_url($input['mapoverlay_section']) : $options['mapoverlay_section'];
	    // Cleanup style options
	    $newInput['style_results_bg_header'] = isset($input['style_results_bg_header']) ? sanitize_text_field($input['style_results_bg_header']) : $options['style_results_bg_header'];
	    $newInput['style_results_bg_even'] = isset($input['style_results_bg_even']) ? sanitize_text_field($input['style_results_bg_even']) : $options['style_results_bg_even'];
	    $newInput['style_results_bg_odd'] = isset($input['style_results_bg_odd']) ? sanitize_text_field($input['style_results_bg_odd']) : $options['style_results_bg_odd'];
	    $newInput['style_results_bg_hover'] = isset($input['style_results_bg_hover']) ? sanitize_text_field($input['style_results_bg_hover']) : $options['style_results_bg_hover'];
	    return $newInput;
	}
	
	
}
