Major updates from other projects
This commit is contained in:
4
classes/Site.php
Normal file
4
classes/Site.php
Normal file
@@ -0,0 +1,4 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
class Site extends lnApp_Site {}
|
||||
?>
|
@@ -39,8 +39,8 @@ abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Templa
|
||||
protected $ao;
|
||||
|
||||
public function __construct(Request $request, Response $response) {
|
||||
if (Config::theme())
|
||||
$this->template = Config::theme().'/page';
|
||||
if (Site::Theme())
|
||||
$this->template = Site::Theme().'/page';
|
||||
|
||||
return parent::__construct($request,$response);
|
||||
}
|
||||
@@ -150,7 +150,7 @@ abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Templa
|
||||
|
||||
public function after() {
|
||||
if ($this->auto_render) {
|
||||
$this->template->navbar = $this->template->shownavbar ? View::factory('pages/navbar') : '';
|
||||
$this->template->navbar = $this->template->shownavbar ? View::factory(Site::Theme().'/navbar') : '';
|
||||
|
||||
if (empty($this->template->content))
|
||||
$this->template->content = Block::factory()->render_all();
|
||||
@@ -159,12 +159,6 @@ abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Templa
|
||||
if (isset(URL::$method_directory[strtolower($this->request->directory())]))
|
||||
BreadCrumb::name(URL::$method_directory[strtolower($this->request->directory())],$this->request->directory());
|
||||
|
||||
// Application Title
|
||||
if (class_exists('Model_Module') AND $mo=ORM::factory('Module',array('name'=>Request::current()->controller())) AND $mo->loaded())
|
||||
$this->meta->title = sprintf('%s: %s',Kohana::$config->load('config')->appname,$mo->display('name'));
|
||||
else
|
||||
$this->meta->title = Kohana::$config->load('config')->appname;
|
||||
|
||||
// Description
|
||||
$this->meta->description = sprintf('%s::%s',$this->request->controller(),$this->request->action());
|
||||
|
||||
|
@@ -24,7 +24,7 @@ abstract class lnApp_HTTP_Exception extends Kohana_HTTP_Exception {
|
||||
$output .= '</div>';
|
||||
$output .= '</div></div></div>';
|
||||
|
||||
$view = View::factory(Config::theme().'/page')
|
||||
$view = View::factory(Site::Theme().'/page')
|
||||
->set('meta',new Meta)
|
||||
->set('navbar','')
|
||||
->set('content',$output);
|
||||
|
@@ -43,6 +43,10 @@ class lnApp_Menu {
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function mainnav() {
|
||||
return Kohana::$config->load('mainnav');
|
||||
}
|
||||
|
||||
public static function ul($type,array $result,array $append=NULL,$sub=FALSE,$method=NULL) {
|
||||
$output = $sub ? '<ul class="dropdown-menu">' : '<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">';
|
||||
|
||||
|
@@ -29,14 +29,5 @@ abstract class lnApp_Meta {
|
||||
|
||||
$this->_data[$key] = $value;
|
||||
}
|
||||
|
||||
public function secure() {
|
||||
static $secure = NULL;
|
||||
|
||||
if (! $secure AND Request::current())
|
||||
$secure = Request::current()->secure() ? 'https://' : 'http://';
|
||||
|
||||
return $secure;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* This file contains enhancements for Kohana, that should be considered upstream and maybe havent been yet.
|
||||
* It also contains some functionality for OSB, which cannot be covered in ORM_OSB.
|
||||
*
|
||||
* @package WWZ
|
||||
* @package lnApp
|
||||
* @category Modifications
|
||||
* @author Deon George
|
||||
* @copyright (c) 2014 Deon George
|
||||
@@ -18,12 +18,95 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
private $_object_formated = array();
|
||||
private $_formated = FALSE;
|
||||
|
||||
protected $_created_column = array('column'=>'date_orig','format'=>TRUE);
|
||||
protected $_updated_column = array('column'=>'date_last','format'=>TRUE);
|
||||
|
||||
// Our filters used to display values in a friendly format
|
||||
protected $_display_filters = array();
|
||||
|
||||
// Our attributes used in forms.
|
||||
protected $_form = array();
|
||||
|
||||
// Our attribute blobs that should be compressed
|
||||
protected $_compress_column = array();
|
||||
|
||||
// Our attributes that should be converted to NULL when empty
|
||||
protected $_nullifempty = array();
|
||||
|
||||
// Our attribute values that need to be stored as serialized
|
||||
protected $_serialize_column = array();
|
||||
|
||||
// If we need to load any sub items on loading this model
|
||||
protected $_sub_items = array();
|
||||
protected $_sub_items_load = array();
|
||||
protected $_sub_items_sorted = FALSE;
|
||||
|
||||
// Whether to show a SystemMessage when a record is saved.
|
||||
protected $_save_message = FALSE;
|
||||
|
||||
/**
|
||||
* Auto process some data as it comes from the database
|
||||
* @see parent::__get()
|
||||
*/
|
||||
public function __get($column) {
|
||||
if (array_key_exists($column,$this->_table_columns)) {
|
||||
// If the column is a blob, we'll decode it automatically
|
||||
if (
|
||||
$this->_table_columns[$column]['data_type'] == 'blob'
|
||||
AND in_array($column,$this->_compress_column)
|
||||
AND ! is_null($this->_object[$column])
|
||||
AND ! isset($this->_changed[$column])
|
||||
AND (! isset($this->_table_columns[$column]['auto_convert']) OR ! $this->_table_columns[$column]['auto_convert'])
|
||||
) {
|
||||
|
||||
// In case our blob hasnt been saved as one.
|
||||
try {
|
||||
$this->_object[$column] = $this->_blob($this->_object[$column]);
|
||||
}
|
||||
catch(Exception $e) {
|
||||
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
||||
}
|
||||
|
||||
$this->_table_columns[$column]['auto_convert'] = TRUE;
|
||||
}
|
||||
|
||||
// If the column is a serialized object, we'll unserialize it.
|
||||
if (
|
||||
in_array($column,$this->_serialize_column)
|
||||
AND is_string($this->_object[$column])
|
||||
AND ! is_null($this->_object[$column])
|
||||
AND ! isset($this->_changed[$column])
|
||||
AND (! isset($this->_table_columns[$column]['unserialized']) OR ! $this->_table_columns[$column]['unserialized'])
|
||||
) {
|
||||
|
||||
// In case our object hasnt been saved as serialized.
|
||||
try {
|
||||
$this->_object[$column] = unserialize($this->_object[$column]);
|
||||
}
|
||||
catch(Exception $e) {
|
||||
HTTP_Exception::factory(501,Kohana_Exception::text($e));
|
||||
}
|
||||
|
||||
$this->_table_columns[$column]['unserialized'] = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::__get($column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve and Store DB BLOB data in compressed format.
|
||||
*/
|
||||
private function _blob($data,$set=FALSE) {
|
||||
try {
|
||||
return $set ? gzcompress($this->_serialize($data,$set)) : $this->_serialize(gzuncompress($data));
|
||||
|
||||
// Maybe the data isnt compressed?
|
||||
} catch (Exception $e) {
|
||||
return $this->_serialize($data,$set);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format fields for display purposes
|
||||
*
|
||||
@@ -37,6 +120,57 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
$this->_formated = TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept our object load, so that we can load our subitems
|
||||
*/
|
||||
protected function _load_values(array $values) {
|
||||
parent::_load_values($values);
|
||||
|
||||
$sort = FALSE;
|
||||
if ($this->_loaded AND $this->_sub_items_load AND count($this->_sub_items_load) == 1)
|
||||
foreach ($this->_sub_items_load as $item => $sort)
|
||||
$this->_sub_items = $this->$item->find_all()->as_array();
|
||||
|
||||
if ($sort) {
|
||||
Sort::MAsort($this->_sub_items,$sort);
|
||||
$this->_sub_items_sorted = TRUE;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* If a column is marked to be nullified if it is empty, this is where it is done.
|
||||
*/
|
||||
private function _nullifempty(array $array) {
|
||||
foreach ($array as $k=>$v) {
|
||||
if (is_array($v)) {
|
||||
if (is_null($x=$this->_nullifempty($v)))
|
||||
unset($array[$k]);
|
||||
else
|
||||
$array[$k] = $x;
|
||||
|
||||
} elseif (! $v AND $v !== 0 AND $v !== '0')
|
||||
unset($array[$k]);
|
||||
|
||||
}
|
||||
|
||||
return count($array) ? $array : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try and (un)serialize our data, and if it fails, just return it.
|
||||
*/
|
||||
private function _serialize($data,$set=FALSE) {
|
||||
try {
|
||||
return $set ? serialize($data) : unserialize($data);
|
||||
|
||||
// Maybe the data serialized?
|
||||
} catch (Exception $e) {
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides Kohana cache so that it can be globally disabled.
|
||||
*/
|
||||
@@ -164,6 +298,27 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
}
|
||||
|
||||
public function save(Validation $validation=NULL) {
|
||||
// Find any fields that have changed, and process them.
|
||||
if ($this->_changed)
|
||||
foreach ($this->_changed as $c) {
|
||||
// Any fields that are blobs, and encode them.
|
||||
if (! is_null($this->_object[$c]) AND $this->_table_columns[$c]['data_type'] == 'blob' AND in_array($c,$this->_compress_column)) {
|
||||
$this->_object[$c] = $this->_blob($this->_object[$c],TRUE);
|
||||
|
||||
// We need to reset our auto_convert flag
|
||||
if (isset($this->_table_columns[$c]['auto_convert']))
|
||||
$this->_table_columns[$c]['auto_convert'] = FALSE;
|
||||
|
||||
// Any fields that should be seriailzed, we'll do that.
|
||||
} elseif (is_array($this->_object[$c]) AND in_array($c,$this->_serialize_column)) {
|
||||
$this->_object[$c] = serialize($this->_object[$c]);
|
||||
}
|
||||
|
||||
// Test if the value has still changed
|
||||
if ($this->_original_values AND $this->_object[$c] == $this->_original_values[$c])
|
||||
unset($this->_changed[$c]);
|
||||
}
|
||||
|
||||
parent::save();
|
||||
|
||||
if ($this->saved() AND $this->_save_message AND (PHP_SAPI !== 'cli'))
|
||||
@@ -175,10 +330,26 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function subitems() {
|
||||
return $this->_sub_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the Kohana processing so we can null values if required.
|
||||
* We override this function, because we do set our own primary key value
|
||||
*/
|
||||
public function values(array $values, array $expected = NULL) {
|
||||
foreach ($values as $k=>$v) {
|
||||
// Convert to NULL
|
||||
if (in_array($k,$this->_nullifempty)) {
|
||||
if (is_array($v))
|
||||
$values[$k] = $this->_nullifempty($v);
|
||||
|
||||
elseif (! $v AND $v !== 0 AND $v !== '0')
|
||||
$values[$k] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
parent::values($values,$expected);
|
||||
|
||||
if (isset($values[$this->_primary_key]))
|
||||
@@ -186,5 +357,16 @@ abstract class lnApp_ORM extends Kohana_ORM {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function what_changed() {
|
||||
$result = array();
|
||||
|
||||
foreach ($this->changed() as $k) {
|
||||
$result[$k]['old'] = ($x=Arr::get($this->_original_values,$k)) ? $x : serialize($x);
|
||||
$result[$k]['new'] = ($x=Arr::get($this->_object,$k)) ? $x : serialize($x);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
70
classes/lnApp/Site.php
Normal file
70
classes/lnApp/Site.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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 ! $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 sprintf('%s %s',self::Date($date),self::Time($date));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the site configured language
|
||||
*/
|
||||
public static function Language() {
|
||||
return 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')->date_format,($date ? $date : time()));
|
||||
}
|
||||
}
|
||||
?>
|
@@ -13,8 +13,6 @@ abstract class lnApp_URL extends Kohana_URL {
|
||||
// Our method paths for different functions
|
||||
public static $method_directory = array(
|
||||
'admin'=>'a',
|
||||
'reseller'=>'r',
|
||||
'affiliate'=>'f',
|
||||
'user'=>'u',
|
||||
);
|
||||
|
||||
@@ -64,11 +62,7 @@ abstract class lnApp_URL extends Kohana_URL {
|
||||
case 'admin': $result[$k] = array('name'=>'Administrator','icon'=>'icon-globe');
|
||||
break;
|
||||
|
||||
case 'affiliate':
|
||||
case 'reseller': $result[$k] = array('name'=>'Reseller','icon'=>'icon-th-list');
|
||||
break;
|
||||
|
||||
case 'user': $result[$k] = array('name'=>Auth::instance()->get_user()->name(),'icon'=>'icon-user');
|
||||
case 'user': $result[$k] = array('name'=>class_exists('Auth') ? Auth::instance()->get_user()->name() : 'Guest','icon'=>'icon-user');
|
||||
break;
|
||||
|
||||
default: $result[$k] = array('name'=>$k,'icon'=>'icon-question-sign');
|
||||
|
Reference in New Issue
Block a user