<?php
/**
 * @version		1.0
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2008 Ternaria Informatica. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Custom meta tags
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 		1.5
 */
class plgContentCustommetatags extends JPlugin
{

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param object $subject The object to observe
	 * @param object $params  The object that holds the plugin parameters
	 * @since 1.5
	 */
	function plgContentExample( &$subject, $params )
	{
		parent::__construct( $subject, $params );
	}

	/**
	 * Example before display content method
	 *
	 * Method is called by the view and the results are imploded and displayed in a placeholder
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 * @return	string
	 */
	function onAfterDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;
		
		/**
		 * List the SEO entities:
		 * + HTML title
		 * + meta description
		 * + meta keywords
		 * + canonical URL
		 * + meta generator
		 * that's it I don't think we need too much
		 * */
		
		// get a reference of the current document
		
		$document = &JFactory::getDocument();
		
		// replace the HTML title
		
		preg_match('/{metatitle:(.*?)}/', $article->text, $metatitle);
		
		if (array_key_exists(0, $metatitle)) {
			
			// clean article's text
			$article->text = str_replace($metatitle[0], '', $article->text);
			
			// clean our string
			$htmltitle = preg_replace('/{metatitle:(.*?)}/', "$1", $metatitle[0]);
			
			// put our string as HTML title
			$document->setTitle($htmltitle);
			$document->setMetaData( 'title', $htmltitle);
			
		}
		
		// replace the meta description
		
		preg_match('/{metadesc:(.*?)}/', $article->text, $metadesc);
		
		if (array_key_exists(0, $metadesc)) {
			
			// clean article's text
			$article->text = str_replace($metadesc[0], '', $article->text);
			
			// clean our string
			$htmldesc = preg_replace('/{metadesc:(.*?)}/', "$1", $metadesc[0]);
			
			// put our string as meta decsription
			$document->setMetaData('description', $htmldesc);
			
		}
		
		// replace the meta keywords
		
		preg_match('/{metakeys:(.*?)}/', $article->text, $metakeys);
		
		if (array_key_exists(0, $metakeys)) {
			
			// clean article's text
			$article->text = str_replace($metakeys[0], '', $article->text);
			
			// clean our string
			$htmlkeys = preg_replace('/{metakeys:(.*?)}/', "$1", $metakeys[0]);
			
			// put our string as meta keywords
			$document->setMetaData('keywords', $htmlkeys);
			
		}
		
		// insert canonical URL
		
		preg_match('/{metacanonical:(.*?)}/', $article->text, $metacanonical);
		
		if (array_key_exists(0, $metacanonical)) {
			
			// clean article's text
			$article->text = str_replace($metacanonical[0], '', $article->text);
			
			// clean our string
			$htmlcanonical = preg_replace('/{metacanonical:(.*?)}/', "$1", $metacanonical[0]);
			
			// put our string as meta keywords
			$document->setMetaData('canonical', $htmlcanonical);
			
		}
		
		// insert the meta generator
		// insert the default generator
		
		$defaultGenerator = $this->params->get( 'defaultgenerator' );
		$generator = $this->params->get( 'generator' );
		
		if ($defaultGenerator == 2){
			$generator = $document->getMetaData('generator');
		}
		elseif ($defaultGenerator == 0){
			$generator = '';
		}
		
		// or insert a specific generator
		
		preg_match('/{metagenerator:(.*?)}/', $article->text, $metagenerator);
		
		if (array_key_exists(0, $metagenerator)) {
			
			// clean article's text
			$article->text = str_replace($metagenerator[0], '', $article->text);
			
			// clean our string
			$htmlgenerator = preg_replace('/{metagenerator:(.*?)}/', "$1", $metagenerator[0]);
			
			// put our string as meta keywords
			$document->setMetaData('generator', $htmlgenerator);
			
		}
		
		
		
		if (!$htmlgenerator){
			$document->setMetaData('generator', $generator);
		}
		
		
		
	}

}
