<?php

/**
 * <pre>
 * Invision Power Services
 * IP.CCS run template comparison report
 * Last Updated: $Date: 2009-09-16 21:05:09 -0400 (Wed, 16 Sep 2009) $
 * </pre>
 *
 * @author 		$Author: bfarber $
 * @copyright	(c) 2001 - 2009 Invision Power Services, Inc.
 * @license		http://www.invisionpower.com/community/board/license.html
 * @package		IP.Content
 * @link		http://www.invisionpower.com
 * @since		25th Sept 2009
 * @version		$Revision: 85 $
 */

if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit();
}

class admin_ccs_ajax_compare extends ipsAjaxCommand
{
	/**
	 * Shortcut for url
	 *
	 * @access	private
	 * @var		string			URL shortcut
	 */
	private $form_code;
	
	/**
	 * Shortcut for url (javascript)
	 *
	 * @access	private
	 * @var		string			JS URL shortcut
	 */
	private $form_code_js;
	
	/**
	 * HTML library
	 *
	 * @access	public
	 * @var		object
	 */
	public $html;

	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry ) 
	{
		//-----------------------------------------
		// Load HTML
		//-----------------------------------------
		
		$this->html = $this->registry->output->loadTemplate( 'cp_skin_templates' );

		//-----------------------------------------
		// Load Language
		//-----------------------------------------
		
		ipsRegistry::getClass('class_localization')->loadLanguageFile( array( 'admin_lang' ) );

		//-----------------------------------------
		// Grab extra CSS
		//-----------------------------------------
		
		$this->registry->output->addToDocumentHead( 'importcss', $this->settings['skin_app_url'] . 'css/ccs.css' );
		
		//-----------------------------------------
		// Get template data
		//-----------------------------------------
		
		$id			= intval($this->request['id']);
		$template	= $this->DB->buildAndFetch( array( 'select' => '*', 'from' => 'ccs_page_templates', 'where' => 'template_id=' . $id ) );
		
		if( !$template['template_id'] )
		{
			$this->registry->output->showError( $this->lang->words['no_template_compare'], '11CCS1' );
		}
		
		if( !$template['template_database'] )
		{
			$this->registry->output->showError( $this->lang->words['bad_template_compare'], '11CCS2' );
		}
		
		//-----------------------------------------
		// We've confirmed template exists and is
		//	a database template.  Get original now.
		//-----------------------------------------
		
		$content	= file_get_contents( IPSLib::getAppDir('ccs') . '/xml/demosite.xml' );
		$original	= '';
		
		require_once( IPS_KERNEL_PATH . 'classXML.php' );

		$xml = new classXML( IPS_DOC_CHAR_SET );
		$xml->loadXML( $content );
				
		if( $template['template_database'] == 1 )
		{
			foreach( $xml->fetchElements('template') as $dtemplate )
			{
				$_template	= $xml->fetchElementsFromRecord( $dtemplate );

				if( $_template['template_id'] == 4 )
				{
					$original	= $_template['template_content'];
				}
			}
		}
		else if( $template['template_database'] == 2 )
		{
			foreach( $xml->fetchElements('template') as $dtemplate )
			{
				$_template	= $xml->fetchElementsFromRecord( $dtemplate );

				if( $_template['template_id'] == 2 )
				{
					$original	= $_template['template_content'];
				}
			}
		}
		else if( $template['template_database'] == 3 )
		{
			foreach( $xml->fetchElements('template') as $dtemplate )
			{
				$_template	= $xml->fetchElementsFromRecord( $dtemplate );

				if( $_template['template_id'] == 3 )
				{
					$original	= $_template['template_content'];
				}
			}
		}

		//-----------------------------------------
		// Get Diff library
		//-----------------------------------------
		
		require_once( IPS_KERNEL_PATH . 'classDifference.php' );
		$classDifference			= new classDifference();
		$classDifference->method	= 'PHP';
		
		$difference	= $classDifference->getDifferences( $template['template_content'], $original );
		
		$difference	= str_replace( "\n", "<br>", $difference );
		$difference	= str_replace( "&gt;&lt;", "&gt;\n&lt;", $difference );
		$difference	= preg_replace( "#(?<!(\<del|\<ins)) {1}(?!:style)#i", "&nbsp;", $difference );
		$difference	= str_replace( "\t", "&nbsp; &nbsp; ", $difference );
		
		//-----------------------------------------
		// Pass to CP output hander
		//-----------------------------------------
		
		$this->returnHtml( $this->html->viewDiffReport( $template, $difference ) );
	}
}
