Added some default ORM/URL functions and theme/login fixes

This commit is contained in:
Deon George 2014-02-13 14:10:20 +11:00
parent 319b4b29db
commit 7a78a9a7d6
6 changed files with 229 additions and 1 deletions

4
classes/ORM.php Normal file
View File

@ -0,0 +1,4 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
class ORM extends lnApp_ORM {}
?>

4
classes/URL.php Normal file
View File

@ -0,0 +1,4 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
class URL extends lnApp_URL {}
?>

View File

@ -40,7 +40,11 @@ class lnApp_Controller_Login extends Controller_TemplateDefault {
}
}
$oauthlogin = is_null($x=Session::instance()->get_once('login-no-oauth',NULL)) ? TRUE : ! $x;
if (array_key_exists('oauth',Kohana::modules()))
$oauthlogin = is_null($x=Session::instance()->get_once('login-no-oauth',NULL)) ? TRUE : ! $x;
else
$oauthlogin = FALSE;
$output .= View::factory('pages/login')
->set('oauth',$oauthlogin);

View File

@ -35,6 +35,13 @@ abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Templa
*/
protected $secure_actions = array();
public function __construct(Request $request, Response $response) {
if (Config::theme())
$this->template = Config::theme().'/page';
return parent::__construct($request,$response);
}
/**
* Check and see if this controller needs authentication
*

129
classes/lnApp/ORM.php Normal file
View File

@ -0,0 +1,129 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class overrides Kohana's ORM
*
* 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
* @category Modifications
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_ORM extends Kohana_ORM {
protected $_table_names_plural = FALSE;
protected $_model_names_plural = FALSE;
private $_object_formated = array();
private $_formated = FALSE;
// Our filters used to display values in a friendly format
protected $_display_filters = array();
// Whether to show a SystemMessage when a record is saved.
protected $_save_message = FALSE;
/**
* Format fields for display purposes
*
* @param string column name
* @return mixed
*/
private function _format() {
foreach ($this->_display_filters as $column => $formats)
$this->_object_formated[$column] = $this->run_filter($column,$this->__get($column),array($column=>$formats));
$this->_formated = TRUE;
}
/**
* Overrides Kohana cache so that it can be globally disabled.
*/
public function cached($lifetime=NULL) {
return $this->_db->caching($this->_table_name) ? parent::cached($lifetime) : $this;
}
/**
* Return a formated columns, as per the model definition
*/
public function display($column) {
// Trigger a load of the record.
$value = $this->__get($column);
// If some of our fields need to be formated for display purposes.
if (! $this->_formated AND $this->_display_filters)
$this->_format();
if (isset($this->_object_formated[$column]))
return $this->_object_formated[$column];
else
return is_array($value) ? join(', ',$value) : $value;
}
public function display_filters(array $filters) {
$this->_display_filters = Arr::merge($this->_display_filters,$filters);
}
public function dump() {
$result = array();
$result['this'] = $this->object();
foreach ($this->_sub_items as $o)
$result['sub'][] = $o->dump();
return $result;
}
/**
* Return an array of data that can be used in a SELECT statement.
* The ID and VALUE is defined in the model for the select.
*/
public function list_select($blank=FALSE) {
$result = array();
if ($blank)
$result[] = '';
if ($this->_form AND array_intersect(array('id','value'),$this->_form))
foreach ($this->find_all() as $o)
$result[$o->{$this->_form['id']}] = $o->resolve($this->_form['value']);
return $result;
}
/**
* This function is used so that methods can be called via variables
*/
public function resolve($key) {
eval("\$x = \$this->$key;");
return $x;
}
public function save(Validation $validation=NULL) {
parent::save();
if ($this->saved() AND $this->_save_message AND (PHP_SAPI !== 'cli'))
SystemMessage::factory()
->title('Record Updated')
->type('success')
->body(sprintf('Record %s:%s Updated',$this->_table_name,$this->id));
return $this;
}
/**
* We override this function, because we do set our own primary key value
*/
public function values(array $values, array $expected = NULL) {
parent::values($values,$expected);
if (isset($values[$this->_primary_key]))
$this->{$this->_primary_key} = $values[$this->_primary_key];
return $this;
}
}
?>

80
classes/lnApp/URL.php Normal file
View File

@ -0,0 +1,80 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class overrides Kohana's URL
*
* @package lnApp
* @category Modifications
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
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',
);
public static function admin_url() {
return (Request::current() AND (
(Auth::instance()->logged_in() AND ! empty(URL::$method_directory[strtolower(Request::current()->directory())]))
OR in_array(strtolower(Request::current()->controller()),array('login','oauth'))));
}
/**
* Function to reveal the real directory for a URL
*/
public static function dir($dir) {
// Quick check if we can do something here
if (! in_array(strtolower($dir),URL::$method_directory))
return $dir;
// OK, we can, find it.
foreach (URL::$method_directory as $k=>$v)
if (strtolower($dir) == $v)
return ucfirst($k);
// If we get here, we didnt have anything.
return $dir;
}
/**
* Wrapper to provide a URL::site() link based on function
*/
public static function link($dir,$src,$site=FALSE) {
if (! $dir)
return $src;
if (! array_key_exists($dir,URL::$method_directory))
throw new Kohana_Exception('Unknown directory :dir for :src',array(':dir'=>$dir,':src'=>$src));
$x = URL::$method_directory[$dir].'/'.$src;
return $site ? URL::site($x) : $x;
}
public static function navbar() {
$result = array();
foreach (array_reverse(self::$method_directory) as $k=>$v)
switch ($k) {
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');
break;
default: $result[$k] = array('name'=>$k,'icon'=>'icon-question-sign');
}
return $result;
}
}
?>