<?php
/**
 * @version		$Id: Versioning.php 
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Editor Versioning buton
 *
 * @author Michael Fatica mike@fatica.net
 * @package Editors-xtd
 * @since 1.5
 * @version $Revision$
 */
class plgButtonVersioning extends JPlugin
{
	
	var $params;
	/**
	 * 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 	array  $config  An array that holds the plugin configuration
	 * @since 1.5
	 */
	function plgButtonVersioning(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->params = new JParameter( $config['params'] );	
        $this->loadLanguage( );
	}

	/**
	 * Versioning button
	 * @return array A two element array of ( imageName, textToInsert )
	 */
	function onDisplay($name)
	{
		global $mainframe;
		

	    $lang =& JFactory::getLanguage();
	    $lang->load( 'plg_editors-xtd_versioning', JPATH_SITE );
	    		
		$liveSite = substr_replace(JURI::root(), '', -1, 1);

		$doc 		=& JFactory::getDocument();
		$template 	= $mainframe->getTemplate();
		
		// button is not active in specific content components
		$getContent = $this->_subject->getContent($name);
		
		$present = JText::_('ALREADY EXISTS', true) ;
		
		//fix for fckeditor issue, see
		//http://joomlacode.org/gf/project/joomlafck/tracker/?action=TrackerItemEdit&tracker_item_id=11885		
		if(strtolower($this->_subject->_name) == "fckeditor" || strtolower($this->_subject->_name) == "jck"){
			
			$js = "
				//FCKEditor ". $this->_subject->_name . "
				function insertVersioning(editor,content) {
					try{
					
					
					 	FCKeditorAPI.GetInstance('$name').SetHTML(content);
					}catch(err){
						alert('".JText::_('ERROR_INSERTING')." ' + err);
					}
				}
				";
		}elseif(strpos(strtolower($this->_subject->_name),"wpro") !== false || strpos(strtolower($this->_subject->_name),"wysiwygpro") !== false){
			
			$js = "
				//WPRO:". @$this->_subject->_name . "
				function insertVersioning(editor,content) {
				
					try{
				
						if(WPro.editors['introtext']){
											
							//split content on the <hr id=\"system-readmore\" />
							var _introtext = content.split('<hr id=\"system-readmore\" />')[0];
							var _fulltext = content.split('<hr id=\"system-readmore\" />')[1];
						
							WPro.editors['introtext'].setValue(_introtext);
							WPro.editors['fulltext'].setValue(_fulltext);
						}else{
							WPro.editors['text'].setValue(content);
						}

					}catch(err){
						alert('".JText::_('ERROR_INSERTING')." ' + err);
					}
				}
				";			
			
		}elseif(strpos(strtolower($this->_subject->_name),"jce") !== false){
			
			$js = "
				//JCE:". @$this->_subject->_name . "
				function insertVersioning(text,content) {
				
					try{
						" . @str_replace("'","",$this->_subject->_editor->onSetContent( $name,"content")) ."
					}catch(err){
						alert('".JText::_('ERROR_INSERTING')." ' + err);
					}
				}
				";			
		}else{
			
			$js = "
				//Catch-all:". $this->_subject->_name . "
				function insertVersioning(editor,content) {
				
					try{
						" . @$this->_subject->_editor->onSetContent( $name,"content") ."
					}catch(err){
						alert('".JText::_('ERROR_INSERTING')." ' + err);
					}
				}
				";
		}
		
		
		/*
		TODO: Add support for XHTMLSuite, 
		elseif(strpos($this->_subject->_name,"xhtml") !== false){
		
			$js = "
				//attempting to guess at how xhtmlsuite works.
				function insertVersioning(editor,content) {
					try{
						xs_text.focusEditor();
					 	xs_text.setHTML(document.getElementById('content').value);
					 	xs_text.focusEditor();
					}catch(err){
						alert('".JText::_('ERROR_INSERTING')." ' + err);
					}
				}
				";		
		
		}
		*/

		$doc->addScriptDeclaration($js);
		
		$id = JRequest::getVar('cid',0);
		
		if(is_array($id)){
			$id = $id[0];	
		}
				
		//front-end
		if($id <= 0){
			$id = JRequest::getVar('id',0);			
		}
		
		$id = (int)$id;
		
		$stub =	"index.php";
		
        $link = $stub . '?option=com_versions&tmpl=component&ename='.$name.'&id=' . $id;
      			
		//default height & width of window
        $popup_height = 370;
        $popup_width = 450;
        
		if(@$this->params){
			
			$front_end = @$this->params->get('front_end');
			
			if(@$this->params->get('popup_width') > 0){
				$popup_width = (int)@$this->params->get('popup_width');
			}
			
			if(@$this->params->get('popup_height') > 0){
				$popup_height = (int)@$this->params->get('popup_height');
			}			
			
			
		}
		
		//show the number of staged articles
		$db =& JFactory::getDBO();
		$sql = "SELECT count(id) FROM #__version WHERE content_id = $id  AND (autosaved != '1' or autosaved is null)";
		$db->setQuery( $sql );
		$count = (int)$db->loadResult();
				
		if($count > 0){
			$linktext =  JText::_('VERSIONS') . " ($count)";
		}else{
			$linktext =  JText::_('VERSIONS');
		}		
		
		
		//This is for backwards compatibilty
		if($front_end == "No" && $mainframe->_name != "administrator"){	
			
			$link = "";
        
	        JHTML::_('behavior.modal');
	        $button = new JObject();
	        $button->set('modal', false);
	        $button->set('link', $link);
	        $button->set('text', $linktext);
	        $button->set('name', 'pagebreak');
	        $button->set('options', "");
	        			
		}else{
		        
	        JHTML::_('behavior.modal');
	        $button = new JObject();
	        $button->set('modal', true);
	        $button->set('link', $link);
	        $button->set('text', $linktext);
	        $button->set('name', 'pagebreak');
	        $button->set('options', "{handler: 'iframe',size: {x: $popup_width, y: $popup_height}}");	
			
		}

        return $button;
        
       
	}
}