This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/Config.php

208 lines
5.8 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for access system configuration.
*
* @package OSB
* @subpackage Config
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
2013-01-10 00:53:13 +00:00
class Config extends Kohana_Config {
2011-09-29 07:13:32 +00:00
// Our setup object
public $so;
2012-07-30 05:10:58 +00:00
public static $no_site_id_tables = array('setup','country','currency','language','tax');
2013-01-10 00:53:13 +00:00
protected static $logo = 'img/logo-small.png';
2011-09-29 07:13:32 +00:00
2012-11-09 23:13:57 +00:00
/**
* @compat Restore KH 3.1 functionality
* @var Kohana_Config Singleton static instance
*/
protected static $_instance;
2013-01-10 00:53:13 +00:00
/**
* Some early initialisation
*
* At this point, KH hasnt been fully initialised either, so we cant rely on
* too many KH functions yet.
* NOTE: Kohana doesnt provide a parent construct for the Kohana_Config class.
*/
2012-11-09 23:13:57 +00:00
public function __construct() {
2013-01-10 00:53:13 +00:00
if (defined('PHPUNITTEST'))
$_SERVER['SERVER_NAME'] = PHPUNITTEST;
2012-11-09 23:13:57 +00:00
// We need to know our site here, so that we can subsequently load our enabled modules.
if (Kohana::$is_cli) {
if (! $site = Minion_CLI::options('site'))
// @todo Need to figure out how to make this CLI error nicer.
throw new Minion_Exception_InvalidTask(_('Cant figure out the site, use --site= for CLI'));
else
$_SERVER['SERVER_NAME'] = $site;
}
}
/**
* Get the singleton instance of Config.
*
* $config = Config::instance();
*
* @return Config
* @compat Restore KH 3.1 functionality
*/
public static function instance() {
if (Config::$_instance === NULL)
// Create a new instance
Config::$_instance = new Config;
return Config::$_instance;
}
/** Overloaded methods **/
public static function date($date) {
return date(Config::instance()->loadsite()->so->date_format,($date ? $date : time()));
}
2012-11-09 23:13:57 +00:00
/**
* Show a date using a site configured format
* @note We need this function here, since we call static:: methods, which need to resolve to the child class.
*/
public static function datetime($date) {
return sprintf('%s %s',static::date($date),static::time($date));
}
public static function siteid($format=FALSE) {
return $format ? sprintf('%02s',Config::instance()->loadsite()->so->id) : Config::instance()->loadsite()->so->id;
}
public static function sitemode() {
return Config::instance()->loadsite()->so->status;
}
public static function time($date) {
return date(Config::instance()->loadsite()->so->time_format,($date ? $date : time()));
}
/** Local Methods **/
2011-09-29 07:13:32 +00:00
/**
* Load our site configuration from the DB
*
* We cant do this in __construct(), since Kohana hasn't been fully initialised yet.
*/
public function loadsite() {
// Anti-loop, if we have loaded our record, just return;
if ($this->so AND $this->so->loaded())
return $this;
2012-11-09 23:13:57 +00:00
$this->so = ORM::factory('Setup',array('url'=>URL::base('http')));
2011-09-29 07:13:32 +00:00
if (! $this->so->loaded())
throw new Kohana_Exception(_('Site [:site] not defined in DB?'),array(':site'=>URL::base('http')));
Kohana::$environment = (int)static::sitemode();
2011-09-29 07:13:32 +00:00
return $this;
}
2011-05-02 12:28:17 +00:00
/**
* Find a list of all database enabled modules
*
2011-09-17 10:45:08 +00:00
* Our available modules are defined in the DB (along with method
* security).
2011-05-02 12:28:17 +00:00
*/
public static function modules() {
static $return = array();
2011-05-02 12:28:17 +00:00
if (! count($return))
2012-11-09 23:13:57 +00:00
foreach (ORM::factory('Module')->list_external() as $mo)
$return[$mo->name] = MODPATH.$mo->name;
2011-05-02 12:28:17 +00:00
return $return;
}
2011-05-02 12:28:17 +00:00
public static function moduleexist($module) {
return array_key_exists(strtolower($module),static::modules()) ? TRUE : FALSE;
2011-05-02 12:28:17 +00:00
}
2011-12-30 07:10:02 +00:00
2012-01-29 06:23:24 +00:00
public static function copywrite() {
return '(c) Open Source Billing Development Team';
}
2013-01-10 00:53:13 +00:00
/**
* Return our site name
*/
public static function site() {
return $_SERVER['SERVER_NAME'];
}
public static function sitemodeverbose() {
$modes = array(
Kohana::PRODUCTION=>'Production',
Kohana::STAGING=>'Staging',
Kohana::TESTING=>'Testing',
Kohana::DEVELOPMENT=>'Development',
);
return (! isset($modes[static::sitemode()])) ? 'Unknown' : $modes[static::sitemode()];
}
public static function submode() {
$submode = Kohana::$config->load('config.debug.submode');
return (isset($submode[Request::$client_ip])) ? $submode[Request::$client_ip] : FALSE;
}
public static function sitename() {
return Kohana::$config->load('config')->site_name;
}
// Called in Invoice/Emailing to embed the file.
public static function logo_file() {
list ($path,$suffix) = explode('.',static::$logo);
return ($a=Kohana::find_file(sprintf('media/site/%s',Config::siteid()),$path,$suffix)) ? $a : Kohana::find_file('media',$path,$suffix);
}
public static function logo_uri() {
list ($path,$suffix) = explode('.',static::$logo);
return URL::site(Route::get('default/media')->uri(array('file'=>$path.'.'.$suffix),array('alt'=>static::sitename())),'http');
}
public static function logo() {
return HTML::image(static::logo_uri(),array('class'=>'headlogo','alt'=>_('Logo')));
}
public static function login_uri() {
return ($ao = Auth::instance()->get_user() AND is_object($ao)) ? HTML::anchor('user/account/edit',$ao->name()) : HTML::anchor('login',_('Login'));
}
/**
* Return our caching mechanism
*/
public static function cachetype() {
return is_null(Kohana::$config->load('config')->cache_type) ? 'file' : Kohana::$config->load('config')->cache_type;
}
/**
* See if our emails for the template should be sent to configured admin(s)
*
* @param string template - Template to test for
* @return mixed|array - Email to send test emails to
*/
public static function testmail($template) {
$config = Kohana::$config->load('config')->email_admin_only;
if (is_null($config) OR ! is_array($config) OR empty($config[$template]))
return FALSE;
else
return $config[$template];
}
public static function theme() {
return Kohana::$config->load('config')->theme;
}
2011-05-02 12:28:17 +00:00
}
2010-11-29 22:41:08 +00:00
?>