<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This is class is for site level configuration
 *
 * @package    lnApp
 * @category   Helpers
 * @author     Deon George
 * @copyright  (c) 2009-2013 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnApp_Site {
	/**
	 * Return our application name
	 */
	public static function Appname() {
		return Kohana::$config->load('config')->appname;
	}

	/**
	 * Show a date using a site configured format
	 */
	public static function Date($date) {
		return (is_null($date) OR ! is_numeric($date)) ? $date : date(Kohana::$config->load('config')->date_format,$date);
	}

	/**
	 * Show a date using a site configured format
	 * @note We need this function here, since we call self:: methods, which need to resolve to the child class.
	 */
	public static function Datetime($date) {
		return $date ? sprintf('%s %s',self::Date($date),self::Time($date)) : NULL;
	}

	/**
	 * Return the site configured id
	 */
	public static function ID($format=FALSE) {
		return $format ? sprintf('%02s',Kohana::$config->load('config')->id) : Kohana::$config->load('config')->id;
	}

	/**
	 * Return the site configured language
	 */
	public static function Language() {
		foreach (Request::factory()->accept_lang() as $k=>$v) {
			if (strlen($k) == 2)
				$k = sprintf('%s_%s',strtolower($k),strtoupper($k));
			else {
				list($k,$v) = preg_split('/[-_]/',$k,2);
				$k = sprintf('%s_%s',strtolower($k),strtoupper($v));
			}

			if ($x=ORM::factory('Language',array('iso'=>$k)) AND $x->loaded())
				return $x;
		}

		return ORM::factory('Language',array('iso'=>Kohana::$config->load('config')->language));
	}

	/**
	 * Return the protocol that the site is using
	 *
	 * @param string URL to be included in the return
	 */
	public static function Protocol($url='') {
		static $secure = NULL;

		if (! $secure AND Request::current())
			$secure = Request::current()->secure() ? 'https://' : 'http://';

		return $secure.($url ? $url : '');
	}

	/**
	 * Return the site theme
	 */
	public static function Theme() {
		return 'theme/'.Kohana::$config->load('config')->theme;
	}

	/**
	 * Show a time using a site configured format
	 */
	public static function Time($date) {
		return date(Kohana::$config->load('config')->time_format,($date ? $date : time()));
	}
}
?>