Location: PHPKode > projects > EXtensible Production Suite (XPS) > xpsdev/install/controls/api.php
<?php
/***********************************************************************************************************
*
* <p>Contains system API</p>
*
* @author rennemannt < hide@address.com >
* @TODO: Move all boolean logic to the Controllers...this API file should only be an interface (pass-through)!!
************************************************************************************************************/
Class API
{
	//instance variables
	private $DC; //Database Controller
	private $MC; //Message Controller
	private $SC; //Settings Controller
    public $Smarty; //Smarty object, passed by reference.
    public $message; //Message stores method status messages.
    public $error_count; //Error count stores the number of errors encountered.
    public $template2display; //Smarty template file to be loaded/displayed.
    public $template_displayed; //Smarty template last loaded/displayed.

	/***************************************************
	* <p>constructor for core control.</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >, rennemannt <hide@address.com>
	***************************************************/
	public function __construct($template_displayed)
	{
		require_once( CONTROLS . '/setting_control.php' );
		$this->SC = new Setting_Control();
		require_once( CONTROLS . '/message_control.php' );
		$this->MC = new Message_Control($this->SC);
		require_once( CONTROLS . '/database_control.php' );
		$this->DC = new Database_Control($this->SC, $this->MC);
        require_once(CONFIGS . '/smarty_connect.php');
        $this->Smarty = new Smarty_Connect;
        $this->Smarty->register_object( "API", $this );
        $this->message = '';
        $this->template2display = $template_displayed;
        $this->template_displayed = $template_displayed;
        $this->error_count = 0;
	}

/*********************************************************************
*                          API FUNCTIONS	                         *
*********************************************************************/
/*******************************************************
* <p>set_setting_language</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	boolean true/false.
********************************************************/
public function set_setting_language($params)
{
    if($this->SC->set_setting('language', $params['language']))    {
        //If the update was successful then continue to the next page.
        //$this->message = 'Successfully updated language setting to <b>'.$params['language'].'</b>'; //TODO: add this error message to english.xml.
        //$this->Smarty->assign('error_count', $this->error_count);
        $this->check_file_permissions();
        return true;
    }
    else
    {
        //If there was an error trying to update the config file, then return the error message.
        $this->message = 'ERROR: failed to update settings file.'; //TODO: add this error message to english.xml.
        $this->Smarty->assign('error_count', $this->error_count);
        $this->error_count++;
        $this->template2display = 'choose_language.tpl';
        return false;
    }
}

/*******************************************************
* <p>get_seeting_options</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	list of html setting options
********************************************************/
public function get_setting_options($params)
{
    $options = array();
    $options = $this->SC->get_setting_options($params['id']);
    $this->Smarty->assign('id', $params['id']);
    $this->Smarty->assign('options', $options);

    return $this->Smarty->fetch('_html_options.tpl');
}

/*******************************************************
* <p>get_api_message</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	styled message
********************************************************/
public function get_api_message()
{
    $this->Smarty->assign('message', $this->message);
    $this->Smarty->assign('error_count', $this->error_count);

    $styled_message = $this->Smarty->fetch('_message.tpl');
    return $styled_message;
}

/*******************************************************
* <p>check_file_permissions</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function check_file_permissions()
{
    $results = array();
    $results['TEMPLATES_C']['is_readable'] = is_readable(TEMPLATES_C)-1;
    $results['TEMPLATES_C']['is_writable'] = is_writable(TEMPLATES_C)-1;
    $results['THEMES']['is_readable'] = is_readable(THEMES)-1;
    $results['THEMES']['is_writable'] = is_writable(THEMES)-1;
    $results['MODULES']['is_readable'] = is_readable(MODULES)-1;
    $results['MODULES']['is_writable'] = is_writable(MODULES)-1;
    $results['SETTINGS']['is_readable'] = is_readable(SETTINGS)-1;
    $results['SETTINGS']['is_writable'] = is_writable(SETTINGS)-1;

    $this->Smarty->assign('permissions', $results);
    $this->template2display = 'check_file_permissions.tpl';

    return true;
}

/*******************************************************
* <p>check_php_dependencies</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function check_php_dependencies()
{
    $extensions = array();
    $extensions['PDO'] = extension_loaded('pdo')-1;
    $extensions['PDO_MySQL'] = extension_loaded('pdo_mysql')-1;
    $extensions['PDO_Oracle'] = extension_loaded('pdo_oci')-1;
    $extensions['PDO_Oracle8'] = extension_loaded('pdo_oci8')-1;
    $extensions['PDO_ODBC'] = extension_loaded('pdo_odbc')-1;
    $extensions['PDO_MSSQL'] = extension_loaded('pdo_mssql')-1;
    $extensions['PDO_Firebird'] = extension_loaded('pdo_firebird')-1;
    $extensions['PDO_PostgreSQL'] = extension_loaded('pdo_pgsql')-1;
    $extensions['PDO_SQLite'] = extension_loaded('pdo_sqlite')-1;
    $cfg_file_path = get_cfg_var('cfg_file_path');
    if(!$cfg_file_path) $cfg_file_path = 'not found';

    $this->Smarty->assign('extensions', $extensions);
    $this->Smarty->assign('cfg_file_path', $cfg_file_path);
    $this->template2display = 'check_php_dependencies.tpl';

    return true;
}
/*******************************************************
* <p>get_settings</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	list of settings
********************************************************/
public function get_settings()
{
    return $this->SC->get_settings();
}

/*******************************************************
* <p>edit_settings</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function edit_settings()
{
    $this->Smarty->assign('settings', $this->SC->get_settings());
    $this->template2display = 'edit_settings.tpl';
    return true;
}

/*******************************************************
* <p>show_settings</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function show_settings()
{
    $this->Smarty->assign('settings', $this->SC->get_settings());
    $this->template2display = 'show_settings.tpl';
    return true;
}

/*******************************************************
* <p>finalize_site_config</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function finalize_site_config()
{
    $this->template2display = 'finalize_site_config.tpl';
    return true;
}


/*******************************************************
* <p>update_settings</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	boolean true/false.
********************************************************/
public function update_settings($params)
{
    //Get list of setting labels.
    $settings = array();
    $settings = $this->SC->get_settings();

    foreach($params as $key => $val)
    {
        if(array_key_exists($key, $settings))
        {
            $status = $this->SC->set_setting($key, $val);

            if($status === false)
            {
                $this->message .= 'Failed to update [' . $key . ']<br />';
            }
        }
    }

    if($this->message == '')
    {
        $this->check_db_exists($params);
    }
    else
    {
        $this->template2display = 'edit_settings.tpl';
    }
    $this->Smarty->assign('error_count', $this->error_count);

    return true;
}

/*******************************************************
* <p>test_db_connection</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	api message
********************************************************/
public function test_db_connection($params)
{
    $test_results = array();
    $test_results = $this->DC->test_db_connection($params, true);
    $this->message = $test_results['message'];
    $this->error_count = $test_results['error_count'];

    return $this->get_api_message();
}

/*******************************************************
* <p>Check to see if database exists.</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	api message
********************************************************/
public function check_db_exists($params)
{
    $test_results = array();
    $test_results = $this->DC->test_db_connection($params);
	if(($test_results['error_count'] == 0) and ($params['db_type'] == 'oracle'))
	{
		$db_exists = $this->DC->check_db_exists($params['db_name']);

		if(sizeof($db_exists) == 0)
		{
			$test_results['error_count'] = 1;
		}
	}
		
    $this->message = $test_results['message'];
    $this->error_count = $test_results['error_count'];

    $this->template2display = 'check_db_exists.tpl';
    $this->Smarty->assign('error_count', $this->error_count);
    $this->Smarty->assign('message', $this->message);

    return true;
}

/*******************************************************
* <p>install_database</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	Smarty response
********************************************************/
public function install_database($params)
{
    $this->template2display = 'create_db.tpl';
    return $this->DC->install_database($params, $this->Smarty);
}

/*******************************************************
* <p>drop_database</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	array [$params] array of parameters.
* @return	Smarty response
********************************************************/
public function drop_database($params)
{
    $this->template2display = 'create_db.tpl';
    return $this->DC->drop_database($this->Smarty);
}

/*******************************************************
* <p>Copy /install/configs/settings.xml to /core/configs/settings.xml</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function copy_settings2core()
{
    $status = -1; //false
    if(copy(CONFIGS . '/settings.xml', SETTINGS)) $status = 0; //true

    return $status;
}

/*******************************************************
* <p>Copy /install/configs/settings.xml to /core/configs/settings.xml</p>
*
* @access   public
* @author 	rennemannt <hide@address.com>
* @param	none
* @return	boolean true/false.
********************************************************/
public function complete()
{
    $this->template2display = 'complete.tpl';
    return true;
}

	/*******************************************************
	* <p>retrives a message</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >, rennemannt <hide@address.com>
	* @param	array [$params] array of parameters
	* @return	$message_id
	********************************************************/
	public function get_message($params)
	{
		//rip params
		$message_id = $params['message_id'];

		//do function calls
		return $this->MC->get_message($message_id);
	}

	/*******************************************************
	* <p>retrives a message</p>
	*
	* @access   public
	* @author 	heidtc < hide@address.com >
	* @param	array [$params] array of parameters
	* @return	$label_id
	********************************************************/
	public function get_label($params)
	{
		//rip params
		$label_id = $params['label_id'];

		//do function calls
		return $this->MC->get_label($label_id);
	}

	/*******************************************************
	* <p>alters the value of a setting</p>
	*
	* @access	public
	* @author	Brian Cook < hide@address.com >
	* @param	$params['setting_id'] the id value of the setting in the file
	* @param	$params['value'] the new value of the setting
	* @param	$params['file_location'] the location of the setting file
	********************************************************/
	public function set_setting($params)
	{
		return $this->SC->set_setting($params['setting_id'], $params['value']);
	}
}//end class
?>
Return current item: EXtensible Production Suite (XPS)