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.
lnapp/classes/lnApp/HTMLRender.php

123 lines
2.6 KiB
PHP
Raw Normal View History

2013-04-22 05:50:28 +00:00
<?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
2013-10-08 23:24:11 +00:00
* @category Helpers
2013-04-22 05:50:28 +00:00
* @author Deon George
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_HTMLRender {
2013-05-15 12:21:34 +00:00
protected $_unique_vals = array();
2013-04-25 00:22:36 +00:00
protected $_x = 0;
2013-04-22 05:50:28 +00:00
2013-04-25 00:22:36 +00:00
abstract protected function render();
2013-04-22 05:50:28 +00:00
2013-04-25 00:22:36 +00:00
public function __call($name,$args=NULL) {
if (! in_array($name,$this->_items))
throw new Kohana_Exception('Unknown call to :name',array(':name'=>$name));
2013-04-22 05:50:28 +00:00
2013-04-25 00:22:36 +00:00
$c = $this->_x;
2013-04-22 05:50:28 +00:00
2013-04-25 00:22:36 +00:00
if (! $args)
return static::$_data[$c][$name];
2013-04-22 05:50:28 +00:00
2013-05-15 12:21:34 +00:00
// Strip out unique values
if (in_array($name,$this->_unique_vals))
foreach ($args as $u)
foreach (static::$_data as $d)
if (isset($d[$name]) AND $d[$name] == $u) {
unset(static::$_data[$c]);
return $this;
}
2013-04-25 00:22:36 +00:00
static::$_data[$c][$name] = array_pop($args);
2013-04-22 05:50:28 +00:00
2013-04-25 00:22:36 +00:00
return $this;
2013-04-22 05:50:28 +00:00
}
2013-04-25 00:22:36 +00:00
public function __construct() {
$this->_x = ++static::$_c;
2013-04-22 05:50:28 +00:00
}
/**
* Return the HTML to render the header images
*/
public function __toString() {
2013-04-25 00:22:36 +00:00
if (! static::$_data)
return '';
if (empty(static::$_data[$this->_x]))
$this->record();
2013-04-22 05:50:28 +00:00
try {
2013-04-25 00:22:36 +00:00
return $this->render();
2013-04-22 05:50:28 +00:00
// Display the exception message
2013-05-15 12:21:34 +00:00
} catch (Exception $e) {
2013-04-25 00:22:36 +00:00
return $e->getMessage();
2013-04-22 05:50:28 +00:00
}
}
2013-04-25 00:22:36 +00:00
public static function factory() {
$x = get_called_class();
return new $x;
}
public function record($x=NULL) {
$this->_x = (is_null($x) OR empty(static::$_data[$x])) ? max(array_keys(static::$_data)) : $x;
return $this;
}
/**
* Render all of these items
*/
public function render_all() {
2013-04-25 00:22:36 +00:00
$output = '';
for ($x=0; $x<static::$_c; $x++)
if (isset(static::$_data[$x])) {
$output .= (string)$this->record($x);
unset(static::$_data[$x]);
}
2013-04-25 00:22:36 +00:00
return $output;
}
/**
* Set the space used between rendering output
*/
public static function spacer($spacer) {
static::$_spacer = $spacer;
}
2013-04-22 05:50:28 +00:00
/**
2013-04-25 00:22:36 +00:00
* Add an item to be rendered
*
* @param array Item to be added
* @deprecated
2013-04-22 05:50:28 +00:00
*/
2013-04-25 00:22:36 +00:00
public static function add($item) {
static::$_c = count(static::$_data);
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 (isset(static::$_unique_vals) AND static::$_unique_vals)
2013-04-25 00:22:36 +00:00
foreach (static::$_unique_vals as $v=>$u)
foreach (static::$_data as $d)
if (isset($d[$u]) && $d['data'] == $item['data'])
return;
static::$_data[++static::$_c] = $item;
2013-04-22 05:50:28 +00:00
}
}
?>