Created
June 6, 2010 04:36
-
-
Save katanacrimson/427298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
*=================================================================== | |
* | |
* Codebite site codebase | |
*------------------------------------------------------------------- | |
* @version 1.0.0 | |
* @category Codebite | |
* @package core | |
* @author Damian Bushong ("Obsidian") | |
* @copyright (c) 2010 - Codebite.net | |
* @license All rights reserved | |
* | |
*=================================================================== | |
* | |
* This code may not be wholly redistributed for any purpose. | |
* | |
*/ | |
if(!defined('CODEBITE')) exit; | |
/** | |
* Codebite - Template class, | |
* Builds up and controls a gigantic template, pretty much. Yeah, important isn't it. | |
* | |
* | |
* @category Codebite | |
* @package core | |
* @author Damian Bushong ("Obsidian") | |
* @license All rights reserved | |
*/ | |
class template | |
{ | |
/** | |
* @var boolean - Denotes whether template mode is enabled. If true, enables the in-template methods for use. | |
*/ | |
public $template_mode = false; | |
/** | |
* @var array - Array of all global template variables | |
*/ | |
protected $data = array(); | |
/** | |
* @var array - Array of all template block objects | |
*/ | |
protected $blocks = array(); | |
/** | |
* @var string - The page data. | |
*/ | |
protected $page; | |
/** | |
* Gets a specified template block object. | |
* @return templateBlock - The template block object we want, or false if no such. | |
*/ | |
public function getBlock($block_key) | |
{ | |
return isset($this->blocks[$block_key]) ? $this->blocks[$block_key] : false; | |
} | |
/** | |
* Creates a template block object or adds to an already existing one. | |
* @param string $block_key - The block's name | |
* @param array $block_data - The data for this block | |
* @return void | |
*/ | |
public function assignBlock($block_key, array $block_data) | |
{ | |
if(!isset($this->blocks[$block_key])) | |
$this->blocks[$block_key] = new templateBlock(); | |
$this->getBlock($block_key)->addRow($block_data); | |
} | |
/** | |
* Edit a specified block. | |
* @param string $block_key - The key of the block to edit. | |
* @param array $block_data - The data to add to the block. | |
* @return void | |
* @throws CodebiteException | |
*/ | |
public function editBlock($block_key, array $block_data) | |
{ | |
if(!isset($this->blocks[$block_key])) | |
throw new CodebiteException('Invalid block key provided', 2001); | |
$this->getBlock($block_key)->alterRow($block_data); | |
} | |
/** | |
* Assign a bunch of template vars at once. | |
* @param array $var_data - Array of variables to set. | |
* @return void | |
*/ | |
public function assignVars(array $var_data) | |
{ | |
array_walk($var_data, array($this, 'assignVar')); | |
} | |
/** | |
* Assigns a specified template var | |
* @param string $var_name - The name of the var to set. | |
* @param mixed $var_value - The value to set the var with. | |
* @return void | |
*/ | |
public function assignVar($var_name, $var_value) | |
{ | |
$this->data[(string) $var_name] = $var_value; | |
} | |
/** | |
* Switch to template mode, and enable the output methods for use. | |
* @return void | |
*/ | |
public function enterTemplateMode() | |
{ | |
foreach($this->blocks as $block) | |
{ | |
$block->resetSlotID(); | |
} | |
$this->template_mode = true; | |
} | |
/* in-template methods */ | |
/** | |
* Template use only; initializes a block for use. | |
* @param string $block_key - The key that denotes the block we want. | |
* @return boolean - True if block exists, false if block does not exist. | |
* @throws CodebiteException | |
*/ | |
public function startBlock($block_key) | |
{ | |
if(!$this->template_mode) | |
return NULL; | |
if(!$this->issetBlock($block_key)) | |
throw new CodebiteException('Invalid block key provided', 2001); | |
$this->getBlock($block_key)->resetSlotID(); | |
} | |
/** | |
* Template use only; fetches the next row for a template block. | |
* @param string $block_key - The key that denotes the block we want. | |
* @return array - Desired template block's next row of values. | |
* @throws CodebiteException | |
*/ | |
public function fetchBlock($block_key) | |
{ | |
if(!$this->template_mode) | |
return NULL; | |
if(!$this->issetBlock($block_key)) | |
throw new CodebiteException('Invalid block key provided', 2001); | |
return $this->getBlock($block_key)->fetchRow(); | |
} | |
/** | |
* Template use only; checks to see if a block exists. | |
* @param string $block_key - The key for the block we want to check. | |
* @return boolean - Does the block exist? | |
*/ | |
public function issetBlock($block_key) | |
{ | |
return isset($this->blocks[$block_key]); | |
} | |
/** | |
* Template use only; fetches a specific template var. | |
* @param string $var_name - The name of the var. | |
* @return mixed - Desired template variable's value. | |
*/ | |
public function fetchVar($var_name) | |
{ | |
if(!$this->template_mode) | |
return NULL; | |
return ($this->issetVar($var_name)) ? $this->data[$var_name] : false; | |
} | |
/** | |
* Template use only; checks to see if a specific template var exists. | |
* @param string $var_name - The name of the var. | |
* @return boolean - Does the var exist? | |
*/ | |
public function issetVar($var_name) | |
{ | |
return isset($this->data[$var_name]); | |
} | |
/** | |
* Alias of template->startBlock() | |
*/ | |
public function sB($b_k) | |
{ | |
return $this->startBlock($b_k); | |
} | |
/** | |
* Alias of template->fetchBlock() | |
*/ | |
public function fB($b_k) | |
{ | |
return $this->fetchBlock($b_k); | |
} | |
/** | |
* Alias of template->issetBlock() | |
*/ | |
public function iB($b_k) | |
{ | |
return $this->issetBlock($b_k); | |
} | |
/** | |
* Alias of template->fetchVar() | |
*/ | |
public function fV($v_n) | |
{ | |
return $this->fetchVar($v_n); | |
} | |
/** | |
* Alias of template->issetVar() | |
*/ | |
public function iV($v_n) | |
{ | |
return $this->issetVar($v_n); | |
} | |
} | |
/** | |
* Codebite - Template block object class, | |
* Acts as the parent object for a template block, containing all iteration methods necessary for things to work decently. | |
* | |
* | |
* @category Codebite | |
* @package core | |
* @author Damian Bushong ("Obsidian") | |
* @license All rights reserved | |
*/ | |
class templateBlock | |
{ | |
/** | |
* @var integer - What row are we working on? | |
* @note We start at -1 because all rows should start at 0...this will be incremented to "0" when we add our first row. | |
*/ | |
protected $i = -1; | |
/** | |
* @var array - Row data storage | |
*/ | |
protected $data = array(); | |
/** | |
* @var boolean - Do we want to increment, or decrement? | |
*/ | |
public $increment = true; | |
/* backend alteration methods */ | |
/** | |
* Increments the current row counter and adds a brand spanking new row. | |
*/ | |
public function addRow(array $data) | |
{ | |
$this->i++; | |
$this->alterRow($data); | |
} | |
/** | |
* Alters the currently selected row, or adds one if there *is* no row. | |
* @var array $data - The data to add to the current row. | |
* @return void | |
*/ | |
public function alterRow(array $data) | |
{ | |
if(!$id) | |
$id = $this->i; | |
if(!isset($this->data[$id]) || !is_array($this->data[$id])) | |
$this->data[$id] = array(); | |
$this->data[$id] = array_merge($this->data[$id], $data); | |
} | |
/** | |
* Drops a row from the block | |
* @param integer $id - The row to block, or the current row if false | |
* @return void | |
*/ | |
public function dropRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
unset($this->data[$id]); | |
} | |
/* shared info methods */ | |
/** | |
* Seek to a specified row | |
* @param integer $row - The row number we want to seek to. | |
* @return void | |
*/ | |
public function seekSlot($row) | |
{ | |
$this->i = (int) $row; | |
} | |
/** | |
* Resets the Slot ID to the first slot, necessary on loading up the block in the template. | |
* @return void | |
*/ | |
public function resetSlotID() | |
{ | |
$this->seekSlot(($this->increment) ? $this->getFirstSlot() - 1 : $this->getLastSlot() + 1); | |
} | |
/** | |
* Get the current slot ID | |
* @return integer - The current slot we're at | |
*/ | |
public function getCurrentSlot() | |
{ | |
return (int) $this->i; | |
} | |
/** | |
* How many slots do we have total? | |
* @return integer - Total number of slots | |
*/ | |
public function getSlotTotal() | |
{ | |
return (int) sizeof($this->data); | |
} | |
/* in-template output methods */ | |
/** | |
* Pull the data from the specified row | |
* @var integer $id - The ID of the row to grab | |
* @return array - The row's data. | |
*/ | |
public function getRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
return $this->data[$id]; | |
} | |
/** | |
* Pull the row data for the next/previous row. | |
* @return array - The next/previous row's data | |
*/ | |
public function fetchRow() | |
{ | |
$this->i = ($this->increment) ? $this->getNextSlot($this->i) : $this->getPrevSlot($this->i); | |
return ($this->i) ? $this->getRow($this->i) : false; | |
} | |
/** | |
* Get the id of the firstmost slot | |
* @return mixed - The ID for the firstmost slot, or false if there are no slots | |
*/ | |
public function getFirstSlot() | |
{ | |
return (!empty($this->data)) ? min(array_keys($this->data)) : false; | |
} | |
/** | |
* Get the id of the lastmost slot | |
* @return mixed - The ID for the lastmost slot, or false if there are no slots | |
*/ | |
public function getLastSlot() | |
{ | |
return (!empty($this->data)) ? max(array_keys($this->data)) : false; | |
} | |
/** | |
* Get the next slot's ID | |
* @param string $id - The slot ID to get the next slot for | |
* @return mixed - Integer of next slot ID if there is one, false if there is not | |
*/ | |
public function getNextSlot($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
// need to use non-logical method of getting next key. | |
// cannot depend on $i++ as we might have deliberate gaps | |
// or might have no more rows afterwards | |
$keys = array_keys($this->data); | |
$flip = array_flip($keys); | |
return isset($keys[$flip[$id] + 1]) ? $keys[$flip[$id] + 1] : false; | |
} | |
/** | |
* Get the previous slot's ID | |
* @param string $id - The slot ID to get the previous slot for | |
* @return mixed - Integer of previous slot ID if there is one, false if there is not | |
*/ | |
public function getPrevSlot($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
// need to use non-logical method of getting previous key. | |
// cannot depend on $i-- as we might have deliberate gaps | |
// or might have no other rows | |
$keys = array_keys($this->data); | |
$flip = array_flip($keys); | |
return isset($keys[$flip[$id] - 1]) ? $keys[$flip[$id] - 1] : false; | |
} | |
/* in-template info methods */ | |
/** | |
* Check if we are on the last row | |
* @param integer $id - The ID to check, if we want to check a different slot | |
* @return boolean - Whether or not we are on the last row | |
*/ | |
public function isLastRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
return $id === max(array_keys($this->data)); | |
} | |
/** | |
* Check if we are on the first row | |
* @param integer $id - The ID to check, if we want to check a different slot | |
* @return boolean - Whether or not we are on the first row | |
*/ | |
public function isFirstRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
return $id === min(array_keys($this->data)); | |
} | |
/** | |
* Check if we are on an odd-numbered row | |
* @param integer $id - The ID to check, if we want to check a different slot | |
* @return boolean - Whether or not we are on an odd-numbered row | |
*/ | |
public function isOddRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
return ($id & 1) ? true : false; | |
} | |
/** | |
* Check if we are on an odd-numbered row | |
* @param integer $id - The ID to check, if we want to check a different slot | |
* @return boolean - Whether or not we are on an odd-numbered row | |
*/ | |
public function isEvenRow($id = false) | |
{ | |
if(!$id) | |
$id = $this->i; | |
return ($id & 1) ? false : true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment