Location: PHPKode > projects > Mocovie web framework > webs/common/controls/anchor.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 text control
 */
ControlFactory::load('text');

/**
 * 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, has all its properties and
 * extends them with the "href" attribute.
 *
 * @package mocovi
 * @subpackage controls
 */
class anchor_control extends text_control
{

	protected $externalLinkRegex = '/^(https?|ftp):\/\/.*$/';

	protected $defaultOptions = array
		( 'textToken'	=> ''
		, 'href'		=> ''
		, 'location'	=> ''
		);

	/**
	 * @override
	 */
	protected function program()
	{
		parent::program();
		$this
		->setLinks()
		->setExternals();
		if($this->getOption('href'))
			$this->adoptAttributes('href', 'location');
	}

	/*
		There should be an automatic generation of internal-linking like in wikipedia.
		Therefor you only have to give this control a filenname / uri like "home/imprint"
		or purely "imprint" if there is only one.
	*/
	protected function setLinks()
	{
		$href = $this->getOption('href');
		$replacementTable = array
		(
			array('^([A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,4})$', 'mailto:\\1'), // Parse Mail addresses
			//array('^\/*(.+)$', '/\\1') // Removes trailing forward slash (/)
		);
		foreach($replacementTable as $rule)
			if(preg_match('#'.$rule[0].'#', $href))
			{
				$this->options['href'] = preg_replace('#'.$rule[0].'#', $rule[1], $href);
				break;
			}
		if(!preg_match($this->externalLinkRegex, $href) && $GLOBALS['filesystem']->isFile($href))
			$this->options['href'] = sprintf($GLOBALS['pathformat'], $GLOBALS['basepath'], $GLOBALS['linkprefix'], $href, $GLOBALS['media']);
		return $this;
	}

	protected function setExternals()
	{
		$rules = array($this->externalLinkRegex);
		if(empty($this->options['location']))
			foreach($rules as $rule)
				if(preg_match($rule, $this->getOption('href')))
					$this->options['location'] = 'external';
		return $this;
	}
}
Return current item: Mocovie web framework