<?php
include_once '../Debug.php';
class PageGenerator {
const DEFAULT_PAGE_SIZE = 10;
const DEFAULT_ROW_SIZE = 2;
const MIN_PAGE_SIZE = 4;
const MIN_ROW_SIZE = 2;
private static $instance = null;
private $visible;
private $picsPerRow;
private $picsPerPage;
private $numPictures;
private $pictureStack;
private $loadedPictures = array();
private $currentPage;
private $page = 0;
private $skip;
/*
* Constructor, does constructor things.
*/
private function __construct($pictures, $args) {
$this->page = $this->processButtons();
unset($_GET['page']);
$this->pictureStack = $this->extractPictures($pictures);
$this->numPictures = sizeof($this->pictureStack);
$this->extractArgs($args);
}
public static function getInstance($pictures, $args) {
if(self::$instance == null) {
self::$instance = new PageGenerator($pictures, $args);
}
return self::$instance;
}
private function extractPictures($pictures) {
$temp = array();
for($i=0; $i < sizeof($pictures); $i++) {
$temp[$i] = $pictures[$i];
}
return $temp;
}
/*
* Private function used to extract the arguements from the $args parameter in
* the constructor. If no keys match the required name, default values are assigned.
*/
private function extractArgs($args) {
if(is_array($args)) {
foreach($args as $key=>$value) {
if($key == 'picsPerRow') {
$this->picsPerRow = $value;
}
if($key == 'picsPerPage') {
$this->picsPerPage = $value;
}
}
}
if(!isset($this->picsPerPage)){
$this->picsPerPage = PageGenerator::DEFAULT_PAGE_SIZE;
}
if(!isset($this->picsPerRow)){
$this->picsPerRow = PageGenerator::DEFAULT_ROW_SIZE;
}
if($this->picsPerRow > $this->picsPerPage) {
$this->picsPerRow = $this->picsPerPage;
}
}
private function buildButtonPanel() {
?> <div class="buttonDiv">
<form action="" method="get">
<input type="hidden" name="page" value="<?php echo $this->page; ?>" />
<input type="submit" name="back" value="back" />
<input type="submit" name="next" value="next" />
</form>
</div>
<?php
}
private function getPage() {
if(!isset($_GET['page'])) {
return 0;
}
return $_GET['page'];
}
private function processButtons() {
Debug::printArray($_GET);
if(isset($_GET['next'])) {
$_GET['page']++;
}
if(isset($_GET['back'])) {
$_GET['page']--;
}
return $_GET['page'];
}
public function buildPage() {
echo '<div class="photobody">'
.'<table>';
$total = 0;
for($i=0; $i<=$this->picsPerPage; $i++) {
echo (!($total >= $this->picsPerPage) && !($total >= $this->numPictures)) ? '<tr>': '';
for($x=0; $x<$this->picsPerRow; $x++) {
if(!($total >= $this->numPictures) && !($total >= $this->picsPerPage)) {
echo '<td>'
.'<img src="' . $this->getPictureAt($total) . '" />'
.'</td>';
$total++;
}
}
$this->lastIndex = $total;
echo (!($total >= $this->picsPerPage) && !($total >= $this->numPictures)) ? '</tr>': '';
}
echo '</table></div>';
$this->buildButtonPanel();
}
public function getNumberOfPictures() {
return $this->numPictures;
}
public function getPicturesPerPage() {
return $this->picsPerPage;
}
public function getPicturesPerRow() {
return $this->picsPerRow;
}
/*
* Makes sure $num is both set and a numeric value, then
* assigns the integer value of $num to picsPerPage if and only if $num
* is greater than the MIN_PAGE_SIZE.
*/
public function setPicturesPerPage($num) {
if(isset($num) && is_numeric($num)) {
$this->picsPerPage = ($num >= MIN_PAGE_SIZE) ? (int)$num : MIN_PAGE_SIZE;
}
}
/*
* Makes sure $num is both set and a numeric value, then
* assigns the integer value of $num to picsPerRow if and only if $num
* is greater than the MIN_ROW_SIZE.
*/
public function setPicturesPerRow($num) {
if(isset($num) && is_numeric($num)) {
$this->picsPerRow = ($num >= MIN_ROW_SIZE) ? (int)$num : MIN_PAGE_SIZE;
}
}
}
$PageGenerator = PageGenerator::getInstance($pictures, array('picsPerRow'=>5, 'picsPerPage'=>10));
$PageGenerator->buildPage();
echo $PageGenerator->toString();
This post has been edited by giggly kisses: 07 June 2012 - 02:47 PM

New Topic/Question
Reply



MultiQuote




|