<?php
/*  Page Integrator for DotClear
 *  Copyright (C) 2004  Sébastien LECACHEUR
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


//TODO : Replace dirname(__FILE__).'/../../../.. by a prettier method
define('PI_PLUGIN_NAME','pageintegrator');

class dcIntegrator {
	function mappingIniToArray() {
		// Read the configuration file
		$ini_file = dirname(__FILE__).'/'.PI_PLUGIN_NAME.'.ini';
		$objIni = new iniFile($ini_file);
		$objIni->read($ini_file);
		$mapping = dirname(__FILE__).'/'.plugin_mapping_file;

		return parse_ini_file($mapping,TRUE);
	}


	function checkShowMode() {
		global $view,$mode;


		// Test if the 'show' parameter is setted only if the 'home' mode is activated
		if ( $mode == 'home' ) {
			if (dc_url_scan == 'path_info' && !empty($_SERVER['PATH_INFO']) ) {
				$custom_query = substr($_SERVER['PATH_INFO'],1);
			} elseif (dc_url_scan == 'query_string' && !empty($_SERVER['QUERY_STRING']) ) {
				$custom_query = $_SERVER['QUERY_STRING'];
			} else {$custom_query = '';
			}

			if ( strncmp($custom_query,'show/',5)==0 ) {
				$mode='show';
				$view=substr($custom_query,5);
				$mappings = dcIntegrator::mappingIniToArray();
				$GLOBALS['dc_custom_title'] = count($mappings) > 0 ? $mappings[$view]['title'] : '';
			}
		}
	}


	function makeLink($name,$page) {
		$full_url = dc_blog_url;

		if (dc_url_scan == 'path_info' ) {
			if ( $full_url{strlen($full_url)-1} == '/' ) {
				$full_url = $full_url.'show/'.$page;
			} else {
				$full_url = $full_url.'/show/'.$page;
			}

			?><a href="<?php echo $full_url;?>"><?php echo $name;?></a><?php
		} elseif (dc_url_scan == 'query_string' ) {
			if ( $full_url{strlen($full_url)-1} == '/' ) {
				$full_url = substr($full_url,0,strlen($full_url)-1).'?show/'.$page;
			} else {
				$full_url = $full_url.'?show/'.$page;
			}

			?><a href="<?php echo $full_url;?>"><?php echo $name;?></a><?php
		} else {
			?><a href="show/<?php echo $page;?>"><?php echo $name;?></a><?php
		}
	}


	function dcPageTitle($pattern) {
		global $view;


		if (!empty($view)) {
			$mappings = dcIntegrator::mappingIniToArray();
			printf ( $pattern, $mappings[$view]['title']);
		}
	}


	function includePage() {
		global $view;

		// Read the configuration file
		$ini_file = dirname(__FILE__).'/'.PI_PLUGIN_NAME.'.ini';
		$objIni = new iniFile($ini_file);
		$objIni->read($ini_file);

		// Check if the mapping mode is activated
		if (plugin_mapping_active) {
			// Load the mapping file
			$mappings = dcIntegrator::mappingIniToArray();

			// Search the real file name with the mapped name
			if (!empty($mappings[$view])) {
				$pageToInclude = dirname(__FILE__).'/../../../'.plugin_page_dir.'/'.$mappings[$view]['file'];
				if (plugin_page_ext_mask && strlen(plugin_page_ext)!=0) $pageToInclude = $pageToInclude.'.'.plugin_page_ext;
				if ( file_exists($pageToInclude )) {
					clearStatCache();
					include($pageToInclude);
				} else {
					$myPage = $mappings[$view]['file'];
					if (plugin_page_ext_mask && strlen(plugin_page_ext)!=0) $myPage = $myPage.'.'.plugin_page_ext;
					printf(__('\'%s\' not found at %s'),$myPage,$pageToInclude);
				}
			} else {
				printf(__('map not found : \'%s\''),$view);
			}
		} else {
			$pageToInclude = dirname(__FILE__).'/../../../'.plugin_page_dir.'/'.$view;
			if (plugin_page_ext_mask && strlen(plugin_page_ext)!=0) $pageToInclude = $pageToInclude.'.'.plugin_page_ext;
			if ( file_exists($pageToInclude )) {
				clearStatCache();
				include($pageToInclude);
			} else {
				$myPage = $view;
				if (plugin_page_ext_mask && strlen(plugin_page_ext)!=0) $myPage = $myPage.'.'.plugin_page_ext;
				printf(__('\'%s\' not found at %s'),$myPage,$pageToInclude);
			}
		}
	}
}


if (!function_exists('dcCustomTitle')) {
	function dcCustomTitle($s='%s - ') {
		if (!empty($GLOBALS['dc_custom_title'])) {
			printf($s,$GLOBALS['dc_custom_title']);
		}
	}
}


?>