<?php

/**
 * SQL connect
 *
 * @link       https://orbicular.media
 * @since      1.0.0
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/includes
 */

/**
 * Used to connect to the SQL database using credentials from plugin settings
 *
 *
 * @since      1.0.0
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/includes
 * @author     Bryan Meeks <bryan@orbicular.media>
 */
class Cemetery_Sexton_SQLconnect {

	/**
	 * Short Description. (use period)
	 *
	 * Long Description.
	 *
	 * @since    1.0.0
	 */
	public function cemetery_sexton_connect() {
	    //Get settings from settings table
	    $options = get_option('cemetery-sexton');
	    //Assign options to variables
        $address = $options['sqlserver_address'];
        $port = $options['sqlserver_port'];
        $instance = $options['sqlserver_instance'];
        $database = $options['sqlserver_database'];
        $username = $options['sqlserver_username'];
        $password = $options['sqlserver_password'];
        
        
        //MSSQL database connector using php built-in connector
        //serverName\instanceName, portNumber (1433 by default)
        
        $serverName = $address."\\".$instance.", ".$port; 
        //echo "serverName ".$serverName."<br>";
        $connectionInfo = array( 'Database'=>$database, 'UID'=>$username, 'PWD'=>$password);
        //echo "connectionInfo".print_r($connectionInfo)."<br>"; 
        $connect = sqlsrv_connect( $serverName, $connectionInfo);
        
        if( $connect ) {
            //echo 'Connection Successful <br>';
            //print_r( sqlsrv_client_info($connect));
            //die;
            return $connect;
        } else{
            echo 'Database Connection Error.<br />';
            die( print_r( sqlsrv_errors(), true));
        }
        
        /*
        echo 'plugin_dir_url: '.plugin_dir_url( __FILE__ ).'<br>';
        echo 'plugin_dir_path: '.plugin_dir_path( __FILE__ ).'<br>';
        echo 'get_site_url: '.get_site_url().'<br>';
        echo 'home_url: '.home_url().'<br>';
        //echo 'get_home_path: '.get_home_path().'<br>';
        echo is_page();
        echo basename(get_permalink());
        echo trailingslashit( ABSPATH );
        
        //MSSQL database connector using php built-in connector
        //serverName\instanceName, portNumber (default is port)
        $serverName = "ip\MSSQLSERVER, port";
        $connectionInfo = array( "Database"=>"database", "UID"=>"uid", "PWD"=>"password");
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
        
        if( $conn ) {
            echo "Connection established.<br />";
        }else{
            echo "Connection could not be established.<br />";
            die( print_r( sqlsrv_errors(), true));
        }
        */
        
	}

}