<?php
/**
 * This file implements misc functions to be called from the templates.
 *
 * This file is part of the evoCore framework - {@link http://evocore.net/}
 * See also {@link http://sourceforge.net/projects/evocms/}.
 *
 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}
 * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
 *
 * {@internal License choice
 * - If you have received this file as part of a package, please find the license.txt file in
 *   the same folder or the closest folder above for complete license terms.
 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
 *   then you must choose one of the following licenses before using the file:
 *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
 *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
 * }}
 *
 * {@internal Open Source relicensing agreement:
 * Daniel HAHLER grants Francois PLANQUE the right to license
 * Daniel HAHLER's contributions to this file and the b2evolution project
 * under any OSI approved OSS license (http://www.opensource.org/licenses/).
 * }}
 *
 * @package evocore
 *
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
 * @author blueyed: Daniel HAHLER.
 * @author cafelog (team)
 * @author fplanque: Francois PLANQUE.
 *
 * @version $Id: _template.funcs.php,v 1.7.2.5.2.2 2007/05/02 20:38:27 fplanque Exp $
 */
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );


/**
 * Display a global title matching filter params
 *
 * Outputs the title of the category when you load the page with <code>?cat=</code>
 * Display "Archive Directory" title if it has been requested
 * Display "Last comments" title if these have been requested
 * Display "Statistics" title if these have been requested
 * Display "User profile" title if it has been requested
 *
 * @todo single month: Respect locales datefmt
 * @todo single post: posts do no get proper checking (wether they are in the requested blog or wether their permissions match user rights,
 * thus the title sometimes gets displayed even when it should not. We need to pre-query the ItemList instead!!
 * @todo make it complete with all possible params!
 *
 * @param string prefix to display if a title is generated
 * @param string suffix to display if a title is generated
 * @param string glue to use if multiple title elements are generated
 * @param string format to output, default 'htmlbody'
 * @param boolean display title? (if false: return)
 * @param boolean show the year as link to year's archive (in monthly mode)
 * @param string params for archive link
 * @param boolean do we want to display title for single posts
 * @param default text to display if nothing else
 */
function request_title( $prefix = ' ', $suffix = '', $glue = ' - ', $format = 'htmlbody',
												$display = true, $linktoyeararchive = true, $blogurl = '', $params = '', 
												$disp_single_title = true, $default = '' )
{
	global $cat_modifier, $cat_array, $m, $w, $month, $p, $title, $preview, $ItemCache, $disp, $s, $author;

	$r = array();

	switch( $disp )
	{
		case 'arcdir':
			// We are requesting the archive directory:
			$r[] = T_('Archive Directory');
			break;

		case 'comments':
			// We are requesting the last comments:
			$r[] = T_('Last comments');
			break;

		case 'profile':
			// We are requesting the user profile:
			$r[] = T_('User profile');
			break;

		case 'subs':
			// We are requesting the subscriptions screen:
			$r[] = T_('Subscriptions');
			break;

		case 'msgform':
			// We are requesting the message form:
			$r[] = T_('Send an email message');
			break;

		case 'single':
			// We are displaying a single message:
			if( $preview )
			{	// We are requesting a post preview:
				$r[] = T_('PREVIEW');
			}
			elseif( $disp_single_title )
			{
				if( intval($p) )
				{	// We are requesting a specific post by ID:
					if( $Item = & $ItemCache->get_by_ID( $p, false ) )
					{
						$r[] = $Item->get('title');
					}
				}
				elseif( !empty( $title ) )
				{	// We are requesting a specific post by title:
					if( $Item = & $ItemCache->get_by_urltitle( $title, false ) )
					{
						$r[] = $Item->get('title');
					}
				}
			}
			break;		
	
		default:
			// We are displaying a message list/search results...
			// CATEGORIES:
			if( !empty($cat_array) )
			{ // We have requested specific categories...
				$cat_names = array();
				foreach( $cat_array as $cat_ID )
				{
					if( ($my_cat = get_the_category_by_ID( $cat_ID, false ) ) !== false )
					{ // It is almost never meaningful to die over an invalid cat when generating title
						$cat_names[] = $my_cat['cat_name'];
					}
				}
				if( $cat_modifier == '*' )
				{
					$cat_names_string = implode( ' + ', $cat_names );
				}
				else
				{
					$cat_names_string = implode( ', ', $cat_names );
				}
				if( !empty( $cat_names_string ) )
				{
					if( $cat_modifier == '-' )
					{
						$cat_names_string = T_('All but ').$cat_names_string;
						$r[] = T_('Categories').': '.$cat_names_string;
					}
					else
					{
						if( count($cat_array) > 1 )
							$r[] = T_('Categories').': '.$cat_names_string;
						else
							$r[] = T_('Category').': '.$cat_names_string;
					}
				}
			}


			// TIMEFRAME:
			if( !empty($m) )
			{	// We have asked for a specific timeframe:

				$my_year = substr($m,0,4);
				if( strlen($m) > 4 && isset($month[substr($m,4,2)]) )
				{ // We have requested a month too:
					$my_month = T_($month[substr($m,4,2)]);
				}
				else
				{
					$my_month = '';
				}
				// Requested a day?
				$my_day = substr($m,6,2);

				if( $format == 'htmlbody' && !empty( $my_month ) && $linktoyeararchive )
				{ // display year as link to year's archive
					$my_year = '<a href="' . archive_link( $my_year, '', '', '', false, $blogurl, $params ) . '">' . $my_year . '</a>';
				}

				$arch = T_('Archives for').': '.$my_month.' '.$my_year;

				if( !empty( $my_day ) )
				{	// We also want to display a day
					$arch .= ", $my_day";
				}

				if( !empty($w) && ($w>=0) ) // Note: week # can be 0
				{	// We also want to display a week number
					$arch .= ", week $w";
				}

				$r[] = $arch;
			}


			// KEYWORDS:
			if( !empty($s) )
			{
				$r[] = T_('Keyword(s)').': '.$s;
			}


			// AUTHORS:
			if( !empty($author) )
			{
				$r[] = T_('Author(s)').': '.$author;
			}
	}


	if( ! empty( $r ) )
	{
		$r = implode( $glue, $r );
		$r = $prefix.format_to_output( $r, $format ).$suffix;
	}
	elseif( !empty( $default ) )
	{
		$r = $default;
	}

	if( !empty( $r ) )
	{ // We have something to display:
		if( $display )
			echo $r;
		else
			return $r;
	}

}


/*
 * single_month_title(-)
 * arcdir_title(-)
 *
 * @movedTo _obsolete092.php
 */


/**
 * Create a link to archive, using either params or extra path info.
 *
 * @param string year
 * @param string month
 * @param string day
 * @param string week
 * @param boolean show or return
 * @param string link, instead of blogurl
 * @param string GET params for 'file'
 */
function archive_link( $year, $month, $day = '', $week = '', $show = true, $file = '', $params = '' )
{
	global $Settings;

	if( empty($file) )
		$link = get_bloginfo('blogurl');
	else
		$link = $file;

	if( (! $Settings->get('links_extrapath')) || (!empty($params)) )
	{	// We reference by Query: Dirty but explicit permalinks
		$link = url_add_param( $link, $params );
		$link = url_add_param( $link, 'm=' );
		$separator = '';
	}
	else
	{
		$link = trailing_slash( $link ); // there can already be a slash from a siteurl like 'http://example.com/'
		$separator = '/';
	}

	$link .= $year;

	if( !empty( $month ) )
	{
		$link .= $separator.zeroise($month,2);
		if( !empty( $day ) )
		{
			$link .= $separator.zeroise($day,2);
		}
	}
	elseif( $week !== '' )  // Note: week # can be 0 !
	{
		if( ! $Settings->get('links_extrapath') )
		{	// We reference by Query: Dirty but explicit permalinks
			$link = url_add_param( $link, 'w='.$week );
		}
		else
		{
			$link .= '/w'.zeroise($week,2);
		}
	}

	$link .= $separator;

	if( $show )
	{
		echo $link;
	}
	return $link;
}

/**
 * Robots tag
 */
function robots_tag()
{
	global $robots_index, $robots_follow;

	if( is_null($robots_index) && is_null($robots_follow) )
	{
		return;
	}

	$r = '<meta name="robots" content="';

	if( $robots_index === false )
	 $r .= 'NOINDEX';
	else
	 $r .= 'INDEX';

	$r .= ',';

	if( $robots_follow === false )
	 $r .= 'NOFOLLOW';
	else
	 $r .= 'FOLLOW';

	$r .= '" />'."\n";

	echo $r;
}



?>
