<?php
/*************************************************************************/
/* DOCEBO CORE - Framework                                               */
/* =============================================                         */
/*                                                                       */
/* Copyright (c) 2006 by Giovanni Derks <virtualdarkness[AT]gmail-com>   */
/* http://www.docebo.com                                                 */
/*                                                                       */
/* 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.        */
/*************************************************************************/

/**
 * @package 	admin-library
 * @category 	interaction
 */
 
/**
 * Called in loadHeader method of Block class include .js files if required
 *
 * @return string the code to be include(think to directly use PageWriter->append($string, 'head'))
 **/
function loadHeaderHTMLEditor() {
	$res="";

	if(getAccessibilityStatus() === false)
		$ht = $GLOBALS['framework']['hteditor'];
	else
		$ht = 'accesseditor';

	$relative_path =$GLOBALS['where_framework_relative'];
	$relative_path.=( substr($GLOBALS['where_framework_relative'], -1) == '/' ? '' : '/' );

	switch($ht) {

		case "fckeditor": { // ---------------------------------- Fck Editor --------
			/* $res ='<script type="text/javascript" src="'.$relative_path;
			$res.='addons/fckeditor/docebo_fck.js"></script>'."\n"; */	$res="";
			$res.='<script type="text/javascript" src="'.$relative_path;
			$res.='addons/fckeditor/fckeditor.js"></script>'."\n";
		} break;

		case "xinha": { // -------------------------------------- Xinha -------------

			addAjaxJs();

			$res.="<script type=\"text/javascript\">\n";
			// (preferably absolute) URL (including trailing slash) where Xinha is installed
			$res.="_editor_url  = \"".$relative_path."addons/xinha/\";\n";
			// And the language we need to use in the editor.
			$res.="_editor_lang = \"en\";\n";
			$res.="xinha_editors=new Array();\n";
			$res.="var DoceboSession='".$GLOBALS["platform"]."';\n";
			$res.="</script>\n";
			$res.="<script type=\"text/javascript\" src=\"".$relative_path;
			$res.="addons/xinha/htmlarea.js\"></script>\n";
			$res.="<script type=\"text/javascript\" src=\"".$relative_path;
			$res.="addons/xinha/docebo_config.js\"></script>\n";
		} break;

		case "widgeditor": { // --------------------------------- widgEditor --------
			$res ="<script type=\"text/javascript\"><!--\n";
			$res.="var DoceboSession='".$GLOBALS["platform"]."';\n";
			$res.="--></script>\n";
			$res.='<script type="text/javascript" src="'.$relative_path;
			$res.='addons/widgeditor/scripts/widgEditor.js"></script>'."\n";
			$res.='<link href="'.$relative_path.'addons/widgeditor/css/widgEditor.css" ';
			$res.='rel="stylesheet" type="text/css" />'."\n";
		} break;


		case "xstandard": { // ---------------------------------- xstandard ---------
			$res ="<script type=\"text/javascript\"><!--\n";
			$res.=<<<END

				xstandard_editors=new Array();

				function xstandardEventHandler() {

					var editor='';
					var object='';
					var alternative='';
					for (var x=0; x < xstandard_editors.length; x++) {
						editor=xstandard_editors[x];
						object='xstandard_'+xstandard_editors[x];
						alternative='xstandard_alt_'+xstandard_editors[x];

						try {
							if(typeof(document.getElementById(object).EscapeUnicode) == 'undefined') {
								throw "Error";
							}
							else {
								document.getElementById(object).EscapeUnicode = true;
								document.getElementById(editor).value = document.getElementById(object).value;
							}
						}
						catch(er) {
							document.getElementById(editor).value = document.getElementById(alternative).value;
						}
					}
				}
END;
			$res.="\n";
			$res.="--></script>\n";
		} break;



		case "accesseditor" :
		//using normal textarea
		default : {};break;
	}



	if(isset($GLOBALS['page'])) {
		$GLOBALS['page']->add($res, 'page_head');
	}
	else {
		echo $res;
	}
}


/**
 * Create an instance of HTML Editor.
 *
 * @param string	$formid 		the id of the container form
 * @param string	$textarea_name 	id of textarea to use
 * @param string	$value initial 	content of text area
 *
 * @return string 	html for include the htmleditor
 **/
function loadHtmlEditor($id_form, $id, $name, $value, $css_text, $extra_param_for = false, $simple = false) {

	if (!isset($GLOBALS["HTML_EDITOR_SETUP"])) {
		loadHeaderHTMLEditor();
		$GLOBALS["HTML_EDITOR_SETUP"]=TRUE;
	}

	if(getAccessibilityStatus() === false)
		$ht = $GLOBALS['framework']['hteditor'];
	else
		$ht = 'accesseditor';

	$value=htmlspecialchars($value, ENT_COMPAT);

	switch($ht) {
		//using fckeditor
		case "fckeditor" : {
			if ($simple) {
				$toolbar="Docebo_Simple";
			} else {
				$toolbar="Docebo";
			};
			$txt = '<textarea id="'.$id.'" name="'.$name.'" cols="52" rows="7">'."\n"
				.$value."\n"
				.'</textarea>'."\n"
				."<script type=\"text/javascript\">"."\n"
				."<!--\n"
				."		var oFCKeditor = new FCKeditor( '".$id."' ) ;"."\n"
				."		oFCKeditor.BasePath = \"".$GLOBALS['where_framework_relative']
					.( substr($GLOBALS['where_framework_relative'], -1) == '/' ? '' : '/' )."addons/fckeditor/\" ;"."\n"
				."		oFCKeditor.ToolbarSet = \"".$toolbar."\" ;"."\n"
				."		oFCKeditor.Config[\"DoceboSession\"] = \"".$GLOBALS['platform']."\" ;"."\n";
			if(is_array($extra_param_for)) {
				foreach($extra_param_for as $key => $value) {
					$txt .= " oFCKeditor.Config[\"".$key."\"] = \"".$value."\";";
				}
			}
			$txt .= "		oFCKeditor.ReplaceTextarea() ;"."\n"
			."//-->\n"
				."</script>"."\n";
			return $txt;
		};break;

		case "xinha": { // -------------------------------------- Xinha -------------
			$txt ="<script type=\"text/javascript\"><!--\n";
			$txt.="xinha_editors.push('".$name."');\n";
			$txt.="--></script>\n";

			$txt.="<textarea id=\"".$id."\" name=\"".$name."\" cols=\"90\" rows=\"7\">\n";
			$txt.=$value;
			$txt.="</textarea>\n";

			return $txt;
		} break;


		case "widgeditor": { // --------------------------------- widgEditor --------
			$txt ="<textarea id=\"".$id."\" name=\"".$name."\" class=\"widgEditor\" cols=\"52\" rows=\"7\">\n";
			$txt.=$value;
			$txt.="</textarea>\n";

			return $txt;
		} break;


		case "xstandard": { // ---------------------------------- xstandard ---------
			$txt ="<script type=\"text/javascript\"><!--\n";
			$txt.="xstandard_editors.push('".$name."');\n";
			$txt.="--></script>\n";

			$txt.="<input type=\"hidden\" id=\"".$id."\" name=\"".$name."\" value=\"".$value."\" />\n";

			$txt.="<object type=\"application/x-xstandard\" id=\"xstandard_".$id."\" ";
			$txt.="name=\"xstandard_".$name."\" width=\"100%\" height=\"220\">\n";
			$txt.="<param name=\"value\" value=\"".$value."\" />\n";
			$txt.="<textarea id=\"xstandard_alt_".$id."\" name=\"xstandard_alt_".$name."\" cols=\"52\" rows=\"7\">";
			$txt.=$value;
			$txt.="</textarea>\n";
			$txt.="</object>\n\n";

			return $txt;
		} break;


		//using normal textarea
		case "accesseditor" : {

			if(!$css_text || $css_text == 'textarea') $css_text = 'textarea_wh_full';

			$html = '';

			$html .= '<textarea class="'.$css_text.'" id="'.$id.'" name="'.$name.'" cols="52" rows="7">'
				.htmlspecialchars ( $value, ENT_NOQUOTES )
				.'</textarea>';
			return $html;
		};break;
		default : {

			if(!$css_text) $css_text = 'textarea';
			return '<textarea class="'.$css_text.'" id="'.$id.'" name="'.$name.'" cols="52" rows="7">'
				.htmlspecialchars ( $value, ENT_NOQUOTES )
				.'</textarea>';
		};break;
	}
}


function getEditorExtra() {
	$res="";

	if(getAccessibilityStatus() === false)
		$ht = $GLOBALS['framework']['hteditor'];
	else
		$ht = 'accesseditor';

	switch($ht) {

		case "xstandard": { // ---------------------------------- xstandard ---------
			$res="onsubmit=\"xstandardEventHandler();\"";
		} break;

	}

	return $res;
}


function getHTMLEditorList() {
	//EFFECTS: return an array that contain the list of html editor

	$reHt = mysql_query("
	SELECT hteditor, hteditorname
	FROM ".$GLOBALS['prefix_fw']."_hteditor
	ORDER BY hteditorname");
	while(list($hteditor_db, $hteditorname_db) = mysql_fetch_row($reHt)) {
		if(defined($hteditorname_db)) {
			$ht_array[$hteditor_db] = constant($hteditorname_db);
		}
		else {
			$ht_array[$hteditor_db] = strtolower(substr($hteditorname_db, 1));
		}
	}
	mysql_free_result($reHt);
	return $ht_array;
}


?>
