<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentValSimpleCalendar extends JPlugin
{
	function onPrepareContent( &$article, &$params, $page=0)
	{
		global $mainframe;
		
		// A database connection is created
        $db = JFactory::getDBO();		
		$view  = JRequest::getCmd('view');

		if(!$page) {
			$page = 0;
		}		
		
		// get current document
		$document =& JFactory::getDocument();

		// Get Plugin info
		$plugin			=& JPluginHelper::getPlugin('content', 'valsimplecalendar');
		$pluginParams	= new JParameter( $plugin->params );
		
		// simple performance check to determine whether bot should process further
		if ( JString::strpos( $article->text, 'valsimplecalendar' ) === false ) {
			return true;
		}
		
		// process of the logging
		$log_file = fopen('log.trace','w+');
		
		$plugin_block_regex = '/{valsimplecalendar\s+id="([^"]+)?"}/s';
		
		if ( !$pluginParams->get( 'enabled', 1 ) ) {
			$article->text = preg_replace( $plugin_block_regex, '<br />', $article->text );
			return true;
		}		
		
		$plugin_text_matches = array();
		
		preg_match_all( $plugin_block_regex, $article->text, $plugin_text_matches );
		
		// Number of plugins
		$count = count( $plugin_text_matches[0] );
		
		if ( $count ) {
			$document->addScript("plugins/content/valsimplecalendar/js/valsimplecalendar.js");
			
			for ( $i = 0; $i < $count; $i++ ){
				$replaced_text = '';
				$plugin_text = $plugin_text_matches[0][$i];
				
				$replaced_text = '<br/>';
				
				if ($i == 0){
					$replaced_text .= '<form name="date_form_'.$i.'">';
				}				
				
				$template_id = $i + 1;
				
				$replaced_text .= '<input id="valsimplecalendar_'.$template_id.'" type="text" name="date_input_'.$i.'" />';
				
				$replaced_text .= '<script language="JavaScript">';
					$replaced_text .= 'new tcal ({';
					$replaced_text .= '\'formname\': \'date_form_0\',';
					$replaced_text .= '\'controlname\': \'date_input_'.$i.'\'';
					$replaced_text .= '});';
				$replaced_text .= '</script>';
				$replaced_text .= '<div id="tcal">';
				$replaced_text .= '</div>';				
				
				if ($i == ($count - 1)){
					$replaced_text .= '</form>';
				}
				
				fwrite($log_file, $plugin_text."\n");
				fwrite($log_file, $replaced_text."\n");
				
				$article->text = str_replace( $plugin_text, $replaced_text, $article->text );
			}			
		}
		
		if ($csspath = $pluginParams->get( 'csspath' )) {
			$document->addStyleSheet( JURI::base() . $csspath );
		}
		else{
			$document->addStyleSheet( "plugins/content/valsimplecalendar/css/valsimplecalendar.css" );
		}
		
		fclose($log_file);
		
		return true;
	}
	
	function onAfterDisplayTitle( &$article, &$params, $page=0 )
	{
		global $mainframe;

		return '';
	}
	
	function onBeforeDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;

		return '';
	}

	function onAfterDisplayContent( &$article, &$params, $limitstart )
	{
		global $mainframe;

		return '';
	}

	function onBeforeContentSave( &$article, $isNew )
	{
		global $mainframe;

		return true;
	}
	
	function onAfterContentSave( &$article, $isNew )
	{
		global $mainframe;

		return true;
	}	
}