Added getting site config from DB

This commit is contained in:
Deon George 2011-09-29 17:13:32 +10:00
parent b6802e4b5d
commit 147d035e46
6 changed files with 77 additions and 40 deletions

View File

@ -1,6 +1,27 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
class Config extends lnApp_Config {
// Our setup object
public $so;
/**
* 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;
$this->so = ORM::factory('setup',array('nonssl_url'=>URL::base('http')));
if (! $this->so->loaded())
throw new Kohana_Exception(_('Site [:site] not defined in DB?'),array(':site'=>URL::base('http')));
return $this;
}
/**
* Find a list of all database enabled modules
*

View File

@ -11,6 +11,18 @@
* @license http://dev.leenooks.net/license.html
*/
class Controller_TemplateDefault extends Controller_lnApp_TemplateDefault {
protected function _headimages() {
// This is where we should be able to change our country
// @todo To implement
$co = Config::instance()->so->country;
HeadImages::add(array(
'img'=>sprintf('img/country/%s.png',strtolower($co->two_code)),
'attrs'=>array('onclick'=>"target='_blank';",'title'=>$co->display('name'))
));
return HeadImages::factory();
}
protected function _left() {
if ($this->template->left)
return $this->template->left;

View File

@ -12,49 +12,43 @@
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Config extends Kohana_Config {
protected static $logo = 'img/logo-small.png';
/**
* 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.
*/
public function __construct() {
if (Kohana::$is_cli) {
if (! $site = CLI::options('site'))
throw new Kohana_Exception(_('Cant figure out the site, use --site= for CLI'));
else
$_SERVER['SERVER_NAME'] = $site['site'];
}
}
/**
* Return our site name
*/
public static function site() {
if (! empty($_SERVER['SERVER_NAME']))
return $_SERVER['SERVER_NAME'];
if (! $site = CLI::options('site'))
throw new Kohana_Exception(_('Cant figure out the site, use --site= for CLI'));
// @todo Need to move this to earlier into the processing stream.
// we can then do away with the test above.
$_SERVER['SERVER_NAME'] = $site['site'];
return $site['site'];
return $_SERVER['SERVER_NAME'];
}
/**
* Work out our site ID for multiehosting
* @todo Change this to query the DB for site number.
*/
public static function siteid() {
$sites = Kohana::config('config.site');
// If we havent been configured for sites
if (is_null($sites) OR ! is_array($sites) OR ! isset($sites[static::site()]))
return 0;
else
return $sites[static::site()];
return Config::instance()->loadsite()->so->id;
}
/**
* Work out our site mode (dev,test,prod)
* @todo Change this to query the DB for mode.
*/
public static function sitemode() {
$sites = Kohana::config('config.site_mode');
// If we havent been configured for sites
if (is_null($sites) OR ! is_array($sites) OR ! isset($sites[static::site()]))
return Kohana::PRODUCTION;
else
return $sites[static::site()];
return Config::instance()->loadsite()->so->status;
}
public static function sitemodeverbose() {
@ -65,24 +59,26 @@ abstract class lnApp_Config extends Kohana_Config {
Kohana::DEVELOPMENT=>'Development',
);
return (! isset($modes[static::sitemode()])) ? 'Unknown' : $modes[static::sitemode()];
return (! isset($modes[static::sitemode()])) ? 'Unknown' : $modes[static::sitemode()];
}
public static function sitename() {
return Kohana::config('config.site_name');
return Config::instance()->loadsite()->so->site_name;
}
// Called in Invoice/Emailing to embed the file.
public static function logo_file() {
// @todo Move the logo filename to a config file
return Kohana::find_file(sprintf('media/%s',Config::siteid()),'img/logo-small','png');
list ($path,$suffix) = explode('.',static::$logo);
return Kohana::find_file(sprintf('media/%s',Config::siteid()),$path,$suffix);
}
public static function logo_uri() {
list ($path,$suffix) = explode('.',static::$logo);
return Route::get('default/media')->uri(array('file'=>$path.'.'.$suffix),array('alt'=>static::sitename()));
}
public static function logo() {
// @todo Move the logo filename to a config file
$mediapath = Route::get('default/media');
$logo = $mediapath->uri(array('file'=>'img/logo-small.png'),array('alt'=>static::sitename()));
return HTML::image($logo,array('class'=>'headlogo','alt'=>_('Logo')));
return HTML::image(static::logo_uri(),array('class'=>'headlogo','alt'=>_('Logo')));
}
/**

View File

@ -13,7 +13,7 @@
class lnApp_HeadImages extends HTMLRender {
protected static $_data = array();
protected static $_spacer = '&nbsp;';
protected static $_required_keys = array('url','img');
protected static $_required_keys = array('img');
/**
* Return an instance of this class
@ -35,7 +35,10 @@ class lnApp_HeadImages extends HTMLRender {
foreach (static::$_data as $value) {
$i = HTML::image($mediapath->uri(array('file'=>$value['img'])),array('alt'=>isset($value['attrs']['title']) ? $value['attrs']['title'] : ''));
$output .= HTML::anchor($value['url'],$i,(isset($value['attrs']) && is_array($value['attrs'])) ? $value['attrs'] : null);
if (isset($value['url']))
$output .= HTML::anchor($value['url'],$i,(isset($value['attrs']) && is_array($value['attrs'])) ? $value['attrs'] : null);
else
$output .= $i;
$output .= static::$_spacer;
}

View File

@ -39,7 +39,9 @@ class ORM extends Kohana_ORM {
// Add our OSB site_id to each SELECT query
final protected function _build($type) {
$this->where($this->_table_name.'.site_id','=',Config::siteid());
// Exclude tables without site ID's
if (! in_array($this->_table_name,array('setup','country','currency','tax')))
$this->where($this->_table_name.'.site_id','=',Config::siteid());
return parent::_build($type);
}

View File

@ -223,7 +223,10 @@ function sqlConditions($db,$Conditions=false,$Tables=false) {
# Add the SITE ID
if (! is_array($Tables) || count($Tables) == 1) {
$where .= sprintf('site_id=%s',DEFAULT_SITE);
if ($Tables == 'setup')
$where .= sprintf('id=%s',DEFAULT_SITE);
else
$where .= sprintf('site_id=%s',DEFAULT_SITE);
} else {
$tbarr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V');