<?php
/**
* Copyright (C) 2010 Kai Dorschner
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Kai Dorschner <the-hide@address.com>
* @copyright Copyright 2010, Kai Dorschner
* @license http://www.gnu.org/licenses/gpl.html GPLv3
* @package mocovi
* @subpackage controls
*/
/**
* Including the abstract dirlist.
*/
ControlFactory::load('dirlist');
/**
* Image gallery using the {@link image_control}
*
* @package mocovi
* @subpackage controls
*/
class imagegallery_control extends dirlist_control
{
protected $metafileXPath;
protected $defaultOptions = array
( 'source' => '/' // sourcepath where the image can be found
, 'controls' => 'yes' // yes|no - Defines if controlling buttons should be shown
, 'metafile' => 'meta.xml' // meta file that should be searched in the sourcepath
, 'maxelements' => 20
);
/**
* Patterns that must appear in the elements
*/
protected $obligations = array
( '/\.([j|J][p|P][g|G])|([g|G][i|I][f|F])|([t|T][i|I][f|F])|([p|P][n|N][g|G])$/'
);
/**
* Patterns that must NOT appear in the elements
*/
protected $restrictions = array
( '/^\./' // No dots at the beginning
);
/**
* @override
*/
protected function program()
{
$this->options['source'] .= (preg_match('/\/$/', $this->options['source']) ? '' : '/');
$this->setRules();
$this->readElementsIn($this->node, $this->getOption('source'));
if($this->getOption('controls'))
$this->node->setAttribute('controls', $this->getOption('controls'));
}
protected function readElementsIn(DomNode &$container, $dir)
{
$elements = $this->readElements($this->getOption('source'));
$container->setAttribute('elements', count($elements));
$begin = (isset($_GET['position']) && $_GET['position'] < count($elements) && $_GET['position'] >= 0 ? $_GET['position'] : 0);
$maxelements = ($this->getOption('maxelements') + $begin < count($elements) ? ($this->getOption('maxelements') + $begin) : count($elements));
$xpath = new DomXPath($this->dom);
for($i = $begin; $i < $maxelements; $i++)
{
$options = array
( 'source' => preg_replace('#\/{2,}#', '', $dir).$elements[$i]
, 'description' => $elements[$i]
);
$this->adoptMetaInformation($elements[$i], $options);
$image = ControlFactory::createVirtual('image',$options);
$container->appendChild($image->load($this->node)->run());
}
// adds "next" link
if($begin + $this->getOption('maxelements') < count($elements))
{
$next = $this->dom->createElement('next');
$next->setAttribute('startwith', $i);
$container->appendChild($next);
$position = count($elements) - $this->getOption('maxelements');
$container->setAttribute('last', $position);
}
// adds "previous" link
if($begin - 1 >= 0 && $begin < count($elements))
{
$previous = $this->dom->createElement('previous');
$position = $i - ($this->getOption('maxelements') * 2);
if($position < 0) $position = 0;
$previous->setAttribute('startwith', $position);
$container->appendChild($previous);
}
}
/**
* Reads all elements of a physical directory
*/
protected function readElements($dir)
{
$elements = array();
if(is_dir($dir) && $handle = @opendir($dir))
{
while($elementName = readdir($handle))
if($this->isValid($elementName))
$elements[] = $elementName;
closedir($handle);
}
else
$this->error($dir.' could not be found.');
return $elements;
}
protected function metafile()
{
return $this->getOption('source').DIRECTORY_SEPARATOR.$this->getOption('metafile');
}
/**
* Adopt external saved meta informations about an image gallery element.
*/
protected function adoptMetaInformation($file, Array &$array)
{
if(is_file($this->metafile()))
{
if(!is_object($this->metafileXPath))
{
$xml = new DomDocument();
$xml->load($this->metafile());
$this->metafileXPath = new DomXpath($xml);
}
foreach($this->metafileXPath->query('//file[@name = "'.$file.'"]/@*[name() != "name"]') as $attribute)
$array[$attribute->nodeName] = $attribute->nodeValue;
}
}
}