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

/**
 * Including the anchor control
 */
ControlFactory::load('anchor');

/**
 * Linking on internal or external content.
 *
 * There is an Option to set the location to "extern", so the template
 * renderer then knows how to build the anchor to get to an external
 * location. With the href Option you define the destination URL in the same
 * way like in simple HTML syntax.
 *
 * This class is inherited from the text control and has all its properties
 *
 * @package mocovi
 * @subpackage controls
 */
class fileanchor_control extends anchor_control
{
	protected $byteDimensions = array
	( 'B'
	, 'KB'
	, 'MB'
	, 'GB'
	, 'TB'
	);

	protected $defaultOptions = array
	( 'text'		=> ''
	, 'textToken'	=> ''
	, 'href'		=> ''
	, 'location'	=> ''
	, 'flow'		=> '' // regulates if the text has been splitted and must stay in logical flow with no break
	);

	protected $filepath;

	/**
	 * @override
	 */
	protected function program()
	{
		$this->filepath = $this->getOption('href');
		$this->options['href'] = preg_replace('#^([http|https|ftp|mail]:\/\/)?(.*)$#', '/\\2', $this->getOption('href'));
		if(strlen($this->getOption('location')) <= 0)
			$this->options['location'] = 'external';
		if(is_file($this->filepath))
			parent::program();
		else
			$this->deleteNode();
	}

	/**
	 * @override
	 */
	protected function getText()
	{
		$size = ' [' . $this->filesize($this->filepath) . ']';
		if($this->getOption('textToken'))
			return new DomText(Translator::translateToken($this->getOption('textToken'))->nodeValue.$size);
		elseif($this->getOption('text'))
			return new DomText($this->getOption('text')->nodeValue.$size);
	}

	protected function filesize($href)
	{
		if(is_file($href))
			return $this->formatSize(filesize($href));
		else
			return '';
	}

	protected function formatSize($bytes, $precision = 2)
	{
		$e = floor(log($bytes) / log(1024)); //exponent
		return round($bytes / pow(1024, $e), $precision).' '.$this->byteDimensions[$e];
	}
}
Return current item: Mocovie web framework