Location: PHPKode > projects > Mocovie web framework > webs/common/controls/dirlist.php
<?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
 */

/**
 * Lists all elements in a specified directory.
 *
 * Your options are to set {@link $restrictions} and {@link $obligations} as regular expressions.
 *
 * Plus you can define from which {@link $mustBeTypeOf type} your element must be of.
 *
 * @package mocovi
 * @subpackage controls
 */
class dirlist_control extends Control
{
	protected $filesystem;

	/**
	 * Defines the childname for XML. Later this name will be needed to parse
	 * it with XSL, so be sure to be conventional.
	 */
	protected $childname = 'element';

	protected $defaultOptions = array
		( 'directory'			=> '/' // Root directory
		, 'lastModifiedFormat'	=> 'c' // PHP date() format
		);

	/**
	 * Patterns that must appear in the elements.
	 */
	protected $obligations = array
	(
		// Predefined regex obligations can be set here like '/\.jpg$/' (which will only show files with .jpg at the end)
	);

	/**
	 * Patterns that must NOT appear in the elements.
	 */
	protected $restrictions = array
	(
		// Predefined regex restrictions can be set here like '/^\./' (which will diesallow dots at the beginning to be displayed)
	);

	/**
	 * All <param>s will be saved here.
	 *
	 * This is an associative array with pattern $name => $value.
	 * Everything inside is from user-input.
	 *
	 * @var array Contains the parameters for this class
	 */
	protected $params = array();


	/**
	 * @override
	 */
	protected function _beforeRun()
	{
		$this->filesystem = $GLOBALS['filesystem'];
		$this->options['directory'] = ($this->getOption('directory') === '.' ? $GLOBALS['page'] : rtrim($this->getOption('directory'), '/')).'/';
		$this->setRules();
		return true;
	}

	/**
	 * @override
	 */
	protected function program()
	{
		$this->readElementsIn($this->node, $this->options['directory']);
	}

	protected function readElementsIn(DomNode &$container, /* XPath */ $dir)
	{
		foreach($this->filesystem->getList($dir) as $element)
		{
			$elementName = $element->getAttribute('name');
			if($this->isValid($elementName))
				$container->appendChild($this->createElement($dir, $elementName));
		}
	}

	protected function createElement($dir, $name)
	{
		$current = $this->dom->createElement($this->childname);
		$current->setAttribute('path', preg_replace('#\/{2,}#', '', $dir));
		$current->setAttribute('name', $name);
		if($lastmodified = $this->filesystem->getLastModified($dir.$name.'/'))
			$current->setAttribute('lastModified', date($this->getOption('lastModifiedFormat'), strtotime($lastmodified)));
		if($priority = $this->filesystem->getPriority($dir.$name.'/'))
			$current->setAttribute('priority', $priority);
		if($elementAlias = Translator::translateToken($this->filesystem->getAlias($dir.$name.'/'))->nodeValue)
			$current->setAttribute('alias', $elementAlias);
		return $current;
	}

	/**
	 * Sets rules which elements are being read
	 *
	 * All rules are obligations (pattern that must contained), restrictions
	 * (pattern that mustn't contained) and type (must be type of)
	 *
	 * @return void;
	 * @access protected
	 */
	protected function setRules()
	{
		$rules = $this->getParams();
		if(isset($rules['obligation']))
			$this->obligations = array_merge($this->obligations, $rules['obligation']);
		if(isset($rules['restriction']))
			$this->restrictions = array_merge($this->restrictions, $rules['restriction']);
	}

	/**
	 * Combines {@see isObligatory()} and {@see isRestricted()}
	 *
	 * @param string Value to be checked
	 * @return boolean Returns wether the the string is valid
	 * @see isObligatory()
	 * @see isRestricted()
	 */
	protected function isValid($string)
	{
		return (bool)($this->isObligatory($string) & !$this->isRestricted($string));
	}

	/**
	 * Checks all obligatory patterns that must match with the elements
	 *
	 * @param $string string
	 */
	protected function isObligatory($string)
	{
		if(empty($this->obligations))
			return true;
		foreach($this->obligations as $rule)
			if(!empty($rule) & !preg_match($rule, $string))
				return false;
		return true;
	}

	/**
	 * Checks all restriction patterns that must not match with the elements
	 *
	 * @param $string string
	 */
	protected function isRestricted($string)
	{
		if(empty($this->restrictions))
			return false;
		foreach($this->restrictions as $rule)
			if(empty($rule) || preg_match($rule, $string))
				return true;
		return false;
	}

	/**
	 * Merges path and name to get an URI
	 */
	protected function getURIFromElement(&$element)
	{
		return (string) $element->getAttribute('path') . $element->getAttribute('name') . '/';
	}

	/**
	 * Read parameters and get them as associative array
	 */
	protected function getParams()
	{
		foreach($this->controls as $control)
			$this->params[$control->getOption('name')][] = $control->getOption('value');
		return (array) $this->params;
	}
}
Return current item: Mocovie web framework