<?php
/**
 * Additional template functionalities
 *
 * @author Mirko Zinn <mail@derzinn.de>
 * @package Minimalism template
 **/

error_reporting(0);
if (!defined('DOKU_INC')) die();

function min_crumbs($str) {
	switch ($str) {
		case 'trace':
			tpl_breadcrumbs();
			break;
		case 'youarehere':
			tpl_youarehere();
			break;
		default:
			echo '&nbsp;';
	}
}

function min_links($str) {
	if (strlen($str) > 0) {
		$get = array('(c)', '%YEAR%');
		$put = array('&copy;', date('Y'));
		$str = str_replace($get, $put, $str);
		preg_match_all('/\[\[([\w\d\s.:~\/\|äöü]+)\]\]/ui', $str, $items);
		if (count($items[1]) > 0) {
			for ($i = 0, $c = count($items[1]); $i < $c; $i++) {
				$items[2][$i] = min_link_builder($items[1][$i]);
			}
			$str = str_replace($items[0], $items[2], $str);
		}
		echo $str;
	}
}

function min_link_builder($str) {
	global $ID, $conf;
	ob_start();
	if (trim(strlen($str)) > 0) {
		$item = stristr($str, '|') !== false ? explode('|', $str) : array(null, $str);
		$link = $item[0] !== null ? trim(preg_replace('/^:?/', '', $item[0])) : $str;
		$title = trim($item[1]);
		$extern = !stristr($item[0], 'http') ? false : true;
		if (strlen($link) > 0 && strlen($title) > 0) {
			if ($extern) {
				if (trim(strlen($conf['target']['extern'])) > 0) {
					$target = ' target="'.$conf['target']['extern'].'"';
				} else {
					$target = null;
				}
				tpl_link($link, $title, 'title="'.$link.'"'.$target);
			} else {
				if (trim(strlen($conf['target']['wiki'])) > 0) {
					$target = ' target="'.$conf['target']['wiki'].'"';
				} else {
					$target = null;
				}
				$style = $link == $ID ? ' class="active"' : null;
				tpl_link(wl($link), $title, 'title="'.$link.'"'.$target.$style);
			}
		}
	}
	$str = ob_get_contents();
	ob_end_clean();
	return $str;
}

?>