Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/Application.php
<?php

/**
 * Product: Katyshop
 * @version 0.3.2.1
 * @author Catalin Hulea - hide@address.com
 * @copyright Copyright (C) 2007 Catalin Hulea
 * @license GNU General Public License version 3
 * 			You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
 * @link https://sourceforge.net/projects/katyshop
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

require_once(dirname(__FILE__) . "/common/BaseObject.php");

/**
 * Singleton class.
 * Place here all global objects such as database, config... used on the entire site
 *
 */
class Application extends BaseObject
{
	// generic
	var $cfg = array();
	/**
	 * @var Database
	 */
	var $db;
	var $mailAgent;
	var $currentMessage = 0;
	var $currentError = 0;
	var $messages = array();
	var $errors = array();
	/**
	 * @var Visitor
	 */
	var $user;

	//#########################################
	// specific
	var $category;
	var $product;

	/**
	 * @var Order
	 */
	var $shopping_cart;

	/**
	 * This is a singleton class, so the constructor is private
	 * @access private
	 */
	function Application()
	{
	}

	/**
	 * Singleton
	 * @return Application
	 */
	function createInstance()
	{
		global $Application;
		if(!is_a($Application, "Application"))
		{
			$Application = new Application();
			$Application->init();
		}
		return $Application;
	}

	/**
	 * @return User
	 */
	function login($username, $password, $rememberPassword)
	{
		global $Application;
		$user = Visitor::login($username, $password, $rememberPassword);
		$Application->user = $user;
		return $user;
	}

	function logout()
	{
		global $Application;
		$user = Visitor::logout();
		$Application->user = $user;
		$Application->shopping_cart = new Order();
		SessionHandler::clear();
		$Application->commitSession();
		return $user;
	}

	//#######################################
	//# GETTERS								#
	//#######################################
	function getConfig()
	{
		global $Application;
		return $Application->cfg;
	}

	function getConfigValue($key)
	{
		global $Application;
		return @$Application->cfg[$key];
	}

	/**
	 * @return Database
	 */
	function getDb()
	{
		global $Application;
		return $Application->db;
	}

	/**
	 * @return AppMailAgent
	 */
	function getMailAgent()
	{
		global $Application;
		return $Application->mailAgent;
	}

	function getMessages()
	{
		global $Application;
		return $Application->messages;
	}

	function getErrors()
	{
		global $Application;
		return $Application->errors;
	}

	/**
	 * @return Visitor
	 */
	function getUser()
	{
		global $Application;
		return $Application->user;
	}

	/**
	 * @return Category
	 */
	function getCurrentCategory()
	{
		global $Application;
		if(is_a($Application->category, "Category"))
			return $Application->category;
		else
			return $Application->createCategory();
	}

	/**
	 * @return Product
	 */
	function getCurrentProduct()
	{
		global $Application;
		if(is_a($Application->product, "Product"))
			return $Application->product;
		else
			return $Application->createProduct();
	}

	/**
	 * @return Order
	 */
	function getShoppingCart()
	{
		global $Application;
		return $Application->shopping_cart;
	}

	//#######################################
	//# SETTERS								#
	//#######################################

	/**
	 * @param Order $o
	 */
	function setShoppingCart($o)
	{
		global $Application;
		$Application->shopping_cart = new Order();
		$Application->shopping_cart->copyFromObject($o);
	}

	//#######################################
	//# MESSAGES AND ERRORS					#
	//#######################################
	function addError($error)
	{
		global $Application;
		$Application->errors[] = $error;
	}

	function addMessage($message)
	{
		global $Application;
		$Application->messages[] = $message;
	}

	function appendErrors($newErrors)
	{
		global $Application;
		$Application->errors = array_merge($Application->errors, $newErrors);
	}

	function appendMessages($newMessages)
	{
		global $Application;
		$Application->messages = array_merge($Application->messages, $newMessages);
	}

	function hasMessages()
	{
		global $Application;
		return (count($Application->messages) > 0);
	}

	function firstMessage()
	{
		global $Application;
		$Application->currentMessage = 0;
		if(count($Application->messages) > 0)
		{
			return $Application->messages[0];
		}
		else
		{
			return false;
		}
	}

	function getNextMessage()
	{
		global $Application;
		if($Application->currentMessage < count($Application->messages))
		{
			$x = $Application->currentMessage;
			$Application->currentMessage++;
			return $Application->messages[$x];
		}
		else
		{
			return false;
		}
	}

	function hasErrors()
	{
		global $Application;
		return (count($Application->errors) > 0);
	}

	function firstError()
	{
		global $Application;
		$Application->currentError = 0;
		if(count($Application->errors) > 0)
		{
			return $Application->errors[0];
		}
		else
		{
			return false;
		}
	}

	function getNextError()
	{
		global $Application;
		if($Application->currentError < count($Application->errors))
		{
			$x = $Application->currentError;
			$Application->currentError++;
			return $Application->errors[$x];
		}
		else
		{
			return false;
		}
	}

	//#######################################
	//# INIT 								#
	//#######################################
	function init()
	{
		ob_start();
		$this->loadConfig();
		$this->loadClasses();

		Logger::request();
		SessionHandler::start();

		$this->fixCompat();
		$this->createDatabase();

		$this->mailAgent = new AppMailAgent();
		$this->createMessagesAndErrors();
		$this->createShoppingCart();
		$this->user = Visitor::getInstance();
	}

	function loadConfig()
	{
		require_once(dirname(dirname(__FILE__)) . "/config/config.php");
		require_once(dirname(dirname(__FILE__)) . "/config/counties.php");
	}

	function loadClasses()
	{
		require_once(COMMON_DIR . "/Tools.php");
		Tools::require_dir_once(COMMON_DIR);
		Tools::require_dir_once(COMMON_DIR . "/phpmailer");
		Tools::require_dir_once(DBLAYER_DIR);
		Tools::require_dir_once(LOGIC_DIR);
		Tools::require_dir_once(TOOLS_DIR);
		require_once(WEB_DIR . "/includes/functions.php");
	}

	function fixCompat()
	{
		$_GET = Compat::stripNice($_GET);
		$_POST = Compat::stripNice($_POST);
		$_COOKIE = Compat::stripNice($_COOKIE);
	}

	function createDatabase()
	{
		$cfgDb = $this->getConfigValue("db");
		$this->db = new Database($cfgDb['host'],$cfgDb['user'],$cfgDb['pass'],$cfgDb['name']);
		$this->db->registerTables();
		$this->db->open() or die("Application could not connect to the database");
	}

	function createMessagesAndErrors()
	{
		$this->messages = SessionHandler::get("Application_messages");
		$this->errors = SessionHandler::get("Application_errors");

		if(!is_array($this->messages))
			$this->messages = array();

		if(!is_array($this->errors))
			$this->errors = array();

		SessionHandler::set("Application_messages", array());
		SessionHandler::set("Application_errors", array());
	}

	/**
	 * @return Category
	 */
	function createCategory()
	{
		$id_category = 0;
		if(!empty($_GET["id_category"]))
		{
			$id_category = $_GET["id_category"];
		}
		elseif (!empty($_GET["id_parent"]))
		{
			$id_category = $_GET["id_parent"];
		}
		elseif (!empty($_GET["id_product"]))
		{
			$p = $this->db->tbProduct->getRecordById($_GET["id_product"]);
			$id_category = $p->id_category;
		}

		$this->category = $this->db->tbCategory->getRecordById($id_category);
		return $this->category;
	}

	function createProduct()
	{
		$this->product = $this->db->tbProduct->getRecordById(@$_GET["id_product"]);
		return $this->product;
	}

	function createShoppingCart()
	{
		$this->shopping_cart = SessionHandler::get("Application_shopping_cart");
		if(!is_a($this->shopping_cart, "Order"))
			$this->shopping_cart = new Order();
	}

	//#######################################
	//# END OF INIT							#
	//#######################################


	//#######################################
	//# COMMIT SESSION						#
	//#######################################

	function commitSession()
	{
		$messages = Application::getMessages();
		$errors = Application::getErrors();
		$shoppingCart = Application::getShoppingCart();
		SessionHandler::set("Application_messages", $messages);
		SessionHandler::set("Application_errors", $errors);
		SessionHandler::set("Application_shopping_cart", $shoppingCart);
	}
}

?>
Return current item: Katyshop