Added lnApp files

This commit is contained in:
Deon George
2011-01-17 16:33:55 +11:00
parent a43ad2b2c5
commit d03f675646
44 changed files with 2264 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for rendering HTML body blocks (left, center, right).
*
* It will provide a header, body and footer.
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
* @uses Style
*/
class lnApp_Block extends HTMLRender {
protected static $_data = array();
protected static $_spacer = '<table><tr class="spacer"><td>&nbsp;</td></tr></table>';
protected static $_required_keys = array('body');
/**
* Add a block to be rendered
*
* @param array Block attributes
*/
public static function add($block,$prepend=FALSE) {
parent::add($block);
// Detect any style sheets.
if (! empty($block['style']) && is_array($block['style']))
foreach ($block['style'] as $data=>$media)
Style::add(array(
'type'=>'file',
'data'=>$data,
'media'=>$media,
));
}
/**
* Return an instance of this class
*
* @return Block
*/
public static function factory() {
return new Block;
}
/**
* Render this block
*
* @see HTMLRender::render()
*/
protected function render() {
$output = '';
$styles = array();
$i = 0;
foreach (static::$_data as $value) {
if ($i++)
$output .= static::$_spacer;
$output .= '<table class="block" border="0">';
if (! empty($value['title']))
$output .= sprintf('<tr class="title"><td>%s</td></tr>',$value['title']);
if (! empty($value['subtitle']))
$output .= sprintf('<tr class="subtitle"><td>%s</td></tr>',$value['subtitle']);
$output .= sprintf('<tr class="body"><td>%s</td></tr>',$value['body']);
if (! empty($value['footer']))
$output .= sprintf('<tr class="footer"><td>%s</td></tr>',$value['footer']);
$output .= '</table>';
}
return $output;
}
}
?>

View File

@@ -0,0 +1,64 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for rendering a breadcrumb menu.
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_Breadcrumb extends HTMLRender {
protected static $_data = array();
protected static $_spacer = ' &raquo; ';
protected static $_required_keys = array('body');
/**
* Set the breadcrumb path
*
* @param array Block attributes
*/
public static function set($path) {
if (is_string($path))
static::$_data['path'] = explode('/',$path);
else
static::$_data['path'] = $path;
}
/**
* Enable a friendly name to be used for a path
*/
public static function name($path,$name) {
static::$_data['name'][$path] = $name;
}
/**
* Return an instance of this class
*
* @return Breadcrumb
*/
public static function factory() {
return new Breadcrumb;
}
/**
* Render this Breadcrumb
*/
protected function render() {
$output = HTML::anchor('/',_('Home'));
$data = empty(static::$_data['path']) ? explode('/',Request::instance()->uri) : static::$_data['path'];
foreach ($data as $k => $v) {
$output .= static::$_spacer;
$p = join('/',array_slice($data,0,$k+1));
$output .= HTML::anchor($p,empty(static::$_data['name'][$p]) ? ucfirst($v) : static::$_data['name'][$p]);
}
return $output;
}
}
?>

View File

@@ -0,0 +1,129 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends the core Kohana class by adding some core application
* specific functions, and configuration.
*
* @package lnApp
* @subpackage Core
* @category Overrides
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Config extends Kohana {
/**
* Find a list of all database enabled modules
*
* @uses cache
*/
public static function appmodules() {
$cacheable = TRUE;
if (array_key_exists('cache',Kohana::modules())) {
$cache = Cache::instance(static::cachetype());
if ($cacheable AND $cache->get('modules'))
return $cache->get('modules');
} else
$cache = '';
$modules = array();
$module_table = 'module';
if (class_exists('Model_'.ucfirst($module_table))) {
$mo = ORM::factory($module_table)->where('status','=',1)->find_all()->as_array();
foreach ($mo as $o)
$modules[$o->name] = MODPATH.$o->name;
}
if ($cache)
$cache->set('modules',$modules);
return $modules;
}
/**
* 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'));
return $site['site'];
}
/**
* 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()];
}
/**
* 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()];
}
public static function sitename() {
return Kohana::config('config.site_name');
}
public static function logo() {
$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 our caching mechanism
*/
public static function cachetype() {
return is_null(Kohana::config('config.cache_type')) ? 'file' : Kohana::config('config.cache_type');
}
/**
* Show a date using a site configured format
*/
public static function date($date) {
return date(Kohana::config('config.date_format'),$date);
}
/**
* 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('config.email_admin_only');
if (is_null($config) OR ! is_array($config) OR empty($config[$template]))
return FALSE;
else
return $config[$template];
}
}
?>

View File

@@ -0,0 +1,45 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for all image icons shown on the page header.
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_HeadImages extends HTMLRender {
protected static $_data = array();
protected static $_spacer = '&nbsp;';
protected static $_required_keys = array('url','img');
/**
* Return an instance of this class
*
* @return HeadImage
*/
public static function factory() {
return new HeadImages;
}
/**
* Render this Header Image
*
* @see HTMLRender::render()
*/
protected function render() {
$output = '';
$mediapath = Route::get(static::$_media_path);
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);
$output .= static::$_spacer;
}
return $output;
}
}
?>

View File

@@ -0,0 +1,94 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is the base used for common static methods that are used
* for rendering.
*
* @package lnApp
* @subpackage Page
* @category Abstract/Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_HTMLRender {
protected static $_media_path = 'default/media';
protected static $_required_keys = array();
protected static $_unique_vals = array();
public function __construct() {
if (! isset(static::$_data))
throw new Kohana_Exception(':class is missing important static variables',array(':class'=>get_called_class()));
}
/**
* Add an item to be rendered
*
* @param array Item to be added
*/
public static function add($item,$prepend=FALSE) {
foreach (static::$_required_keys as $key)
if (! isset($item[$key]))
throw new Kohana_Exception('Missing key :key for image',array(':key'=>$key));
// Check for unique keys
if (static::$_unique_vals)
foreach (static::$_unique_vals as $v=>$u)
foreach (static::$_data as $d)
if (isset($d[$u]) && $d['data'] == $item['data'])
return;
if ($prepend)
array_unshift(static::$_data,$item);
else
array_push(static::$_data,$item);
}
/**
* Set the space used between rendering output
*/
public static function setSpacer($spacer) {
static::$_spacer = $spacer;
}
/**
* Set the Kohana Media Path, used to determine where to find additional
* HTML content required for rendering.
*/
public static function setMediaPath($path) {
static::$_media_path = $path;
}
/**
* Factory instance method must be declared by the child class
*/
public static function factory() {
throw new Kohana_Exception(':class is calling :method, when it should have its own method',
array(':class'=>get_called_class(),':method'=>__METHOD__));
}
/**
* Return the HTML to render the header images
*/
public function __toString() {
try {
return static::render();
}
// Display the exception message
catch (Exception $e) {
Kohana::exception_handler($e);
return '';
}
}
/**
* Rendering must be declared by the child class
*/
protected function render() {
throw new Kohana_Exception(':class is calling :method, when it should have its own method',
array(':class'=>get_called_class(),':method'=>__METHOD__));
}
}
?>

View File

@@ -0,0 +1,34 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is class is for all HTML page attributes.
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_Meta {
private $_data = array();
private $_array_keys = array();
public function __get($key) {
if (in_array($key,$this->_array_keys) && empty($this->_data[$key]))
return array();
if (empty($this->_data[$key]))
return null;
else
return $this->_data[$key];
}
public function __set($key,$value) {
if (in_array($key,$this->_array_keys) && ! is_array($value))
throw new Kohana_Exception('Key :key must be an array',array(':key'=>$key));
$this->_data[$key] = $value;
}
}
?>

View File

@@ -0,0 +1,59 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for rendering HTML script tags
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_Script extends HTMLRender {
protected static $_data = array();
protected static $_spacer = "\n";
protected static $_required_keys = array('type','data');
protected static $_unique_vals = array('file'=>'type');
/**
* Return an instance of this class
*
* @return Script
*/
public static function factory() {
return new Script;
}
/**
* Render the script tag
*
* @see HTMLRender::render()
*/
protected function render() {
$foutput = $soutput = '';
$mediapath = Route::get(static::$_media_path);
$i = $j = 0;
foreach (static::$_data as $value) {
switch ($value['type']) {
case 'file':
$foutput .= HTML::script($mediapath->uri(array('file'=>$value['data'])));
if ($i++)
$foutput .= static::$_spacer;
break;
case 'stdin':
$soutput .= sprintf("<script type=\"text/javascript\">//<![CDATA[\n%s\n//]]></script>",$value['data']);
if ($j++)
$soutput .= static::$_spacer;
break;
default:
throw new Kohana_Exception('Unknown style type :type',array(':type'=>$value['type']));
}
}
return $foutput.static::$_spacer.$soutput;
}
}
?>

View File

@@ -0,0 +1,54 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for rendering HTML style tags
*
* @package lnApp
* @subpackage Page
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_Style extends HTMLRender {
protected static $_data = array();
protected static $_spacer = "\n";
protected static $_required_keys = array('type','data');
/**
* Return an instance of this class
*
* @return Style
*/
public static function factory() {
return new Style;
}
/**
* Render the style tag
*
* @see HTMLRender::render()
*/
protected function render() {
$output = '';
$mediapath = Route::get(static::$_media_path);
$i = 0;
foreach (static::$_data as $value) {
if ($i++)
$output .= static::$_spacer;
switch ($value['type']) {
case 'file':
$output .= HTML::style($mediapath->uri(array('file'=>$value['data'])),
array('media'=>(! empty($value['media'])) ? $value['media'] : 'screen'),TRUE);
break;
default:
throw new Kohana_Exception('Unknown style type :type',array(':type'=>$value['type']));
}
}
return $output;
}
}
?>

View File

@@ -0,0 +1,129 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is for rendering system information messages.
*
* @package lnApp
* @subpackage SystemMessage
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class lnApp_SystemMessage extends HTMLRender {
protected static $_data = array();
protected static $_spacer = '<table><tr class="spacer"><td>&nbsp;</td></tr></table>';
protected static $_required_keys = array('title','body','type');
/**
* Add a system message to be rendered
*
* @param array System Message attributes
*/
public static function add($msg,$prepend=FALSE) {
if ($msgs = Session::instance()->get('sessionmsgs')) {
static::$_data = $msgs;
}
parent::add($msg);
// Add a gribber popup
Style::add(array(
'type'=>'file',
'data'=>'css/jquery.gritter.css',
'media'=>'screen',
));
Script::add(array(
'type'=>'file',
'data'=>'js/jquery.gritter-1.5.js',
));
Script::add(array(
'type'=>'stdin',
'data'=>sprintf(
'$(document).ready(function() {
$.extend($.gritter.options, {
fade_in_speed: "medium",
fade_out_speed: 2000,
time: "3000",
sticky: false,
});
$.gritter.add({
title: "%s",
text: "%s",
image: "%s",
});});',$msg['title'],$msg['body'],URL::site().static::image($msg['type'],true))));
// Save our messages in our session, so that we get them for redirects
Session::instance()->set('sessionmsgs',static::$_data);
}
/**
* Return an instance of this class
*
* @return SystemMessage
*/
public static function factory() {
return new SystemMessage;
}
/**
* Render an image for the System Message
*/
private static function image($type,$raw=false,$big=false,$alt='') {
$mediapath = Route::get(static::$_media_path);
switch ($type) {
case 'error':
$file = sprintf('img/dialog-error%s.png',$big ? '-big' : '');
break;
case 'info':
$file = sprintf('img/dialog-information%s.png',$big ? '-big' : '');
break;
case 'warning':
$file = sprintf('img/dialog-warning%s.png',$big ? '-big' : '');
break;
case 'debug':
$file = sprintf('img/dialog-question%s.png',$big ? '-big' : '');
break;
default:
throw new Kohana_Exception('Unknown system message type :type',array(':type'=>$value['type']));
}
if ($raw)
return $mediapath->uri(array('file'=>$file));
else
return HTML::image($mediapath->uri(array('file'=>$file)),array('alt'=>$alt ? $alt : '','class'=>'sysicon'));
}
/**
* Render this system message
*
* @see HTMLRender::render()
*/
protected function render() {
$output = '';
$mediapath = Route::get(static::$_media_path);
// Reload our message from the session
if ($msgs = Session::instance()->get('sessionmsgs')) {
Session::instance()->delete('sessionmsgs');
static::$_data = $msgs;
}
$i = 0;
foreach (static::$_data as $value) {
if ($i++)
$output .= static::$_spacer;
$output .= '<table><tr>';
$output .= sprintf('<td class="icon" rowspan="2">%s</td>',static::image($value['type'],false,false,isset($value['alt']) ? $value['alt'] : ''));
$output .= sprintf('<td class="head">%s</td>',$value['title']);
$output .= '</tr><tr>';
$output .= sprintf('<td class="body">%s</td>',$value['body']);
$output .= '</tr></table>';
}
return $output;
}
}
?>