<?php
/*
This file is part of Hotseat Corner.
Hotseat Corner is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hotseat Corner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hotseat Corner. If not, see <http://www.gnu.org/licenses/>.
*/
class HSUtils_Stack implements Iterator {
private $_total;
private $_limit;
private $_name;
private $_stack;
private $_count;
private $_offset = -1;
private $_nexturi;
private $_backuri;
private $_stackpos = 0;
private static $inner_glue = '^';
private static $outer_glue = '|';
private static $props;
private static $loaded = false;
public function __construct ()
{
}
public static function get ($name)
{
if (self::$loaded === false) {
if ($_GET['components']) {
$components = explode(self::$outer_glue, $_GET['components']);
foreach ($components as $component) {
list($id, $offset) = explode(self::$inner_glue, $component);
self::$props[$id] = $offset;
}
self::$loaded = true;
}
}
return (self::$props[$name] > 0)? self::$props[$name] : 0;
}
public static function genArgs ($newkey, $newpos = 0)
{
$inner_glue = self::$inner_glue;
if (self::$props !== null) {
foreach (self::$props as $key => $pos) {
$urlparts[$key] = "{$key}{$inner_glue}{$pos}";
}
}
if ($newpos > 0) {
$urlparts[$newkey] = "{$newkey}{$inner_glue}{$newpos}";
}
else {
unset($urlparts[$newkey]);
}
if ($urlparts !== null) {
return implode(self::$outer_glue, $urlparts);
}
}
public function __get ($key)
{
switch ($key) {
case 'upper':
return (($this->_offset + $this->_limit) > $this->_total)? $this->_total : $this->_offset + $this->_limit;
case 'lower':
return $this->_offset+1;
case 'nexturi':
if (($this->_offset + $this->_limit) >= $this->_total) {
return null;
}
if ($this->_nexturi === null) {
foreach ($_GET as $key=>$val) {
$pairs[$key] = $key.'='.$val;
}
$next = (($this->_offset + $this->_limit) < $this->_total && $this->_limit > 0)? $this->_offset + $this->_limit : 0;
$components = self::genArgs($this->_name, $next);
if ($components != null) {
$pairs['components'] = 'components='.$components;
}
else {
unset($pairs['components']);
}
if ($pairs !== null) {
$argstring = implode('&', $pairs);
}
$this->_nexturi = basename($_SERVER['SCRIPT_URI']).'?'.$argstring;
}
return $this->_nexturi;
case 'backuri':
if ($this->_offset <= 0) {
return null;
}
if ($this->_backuri === null) {
foreach ($_GET as $key=>$val) {
$pairs[$key] = $key.'='.$val;
}
$back = ($this->_offset > 0 && ($this->_offset - $this->_limit) > 0 && $this->_limit > 0)? $this->_offset - $this->_limit : 0;
$components = self::genArgs($this->_name, $back);
if ($components != null) {
$pairs['components'] = 'components='.$components;
}
else {
unset($pairs['components']);
}
if ($pairs !== null) {
$argstring = implode('&', $pairs);
}
$this->_backuri = basename($_SERVER['SCRIPT_URI']).'?'.$argstring;
}
return $this->_backuri;
}
$k = '_'.$key;
return $this->$k;
}
public function __set ($key, $val)
{
switch ($key) {
case 'total':
if ($this->_total === null) {
$this->_total = $val;
}
return;
case 'stack':
if ($this->_stack === null) {
$this->_stack = $val;
$this->_count = count($val);
}
return;
case 'name':
if ($this->_name === null) {
$this->_name = $val;
}
return;
case 'limit':
if ($this->_limit === null) {
$this->_limit = $val;
}
return;
case 'offset':
if ($this->_offset === null) {
$this->_offset = $val;
}
return;
}
}
public function rewind ()
{
$this->_stackpos = 0;
}
public function next()
{
$this->_stackpos++;
}
public function valid ()
{
return ($this->stack[$this->_stackpos] !== null)? true : false;
}
public function current ()
{
return $this->stack[$this->_stackpos];
}
public function key ()
{
return $this->_stackpos;
}
}
?>