<?php

/*************************************************************************/
/* DOCEBO CMS - Content Management System                                */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2005 by Giovanni Derks <virtualdarkness[AT]gmail-com>   */
/* http://www.docebocms.org                                              */
/*                                                                       */
/* 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.        */
/*************************************************************************/



function chatBlockEdit(& $out, & $lang, & $form, $block_id, $sub_id) {

	$opt=loadBlockOption($block_id);


	$out->add(getBlockTitleField($form, $lang, $block_id));

 	$out->add(block_css_list($form, $lang, $opt["css"]));

 	$pubdate=(isset($opt["pubdate"]) ? $opt["pubdate"] : "");
	$expdate=(isset($opt["expdate"]) ? $opt["expdate"] : "");
 	$out->add(show_pubexp_table($form, $lang, $pubdate, $expdate));


	$chat_type=array();
	$chat_type["htmlframechat"]=$lang->def("_CHAT_HTMLFRAMECHAT");
	$chat_type["htmlwachat"]=$lang->def("_CHAT_HTMLWACHAT");

	if (isset($opt["chat_type"]))
		$sel=$opt["chat_type"];
	else
		$sel=0;

	$out->add($form->getDropdown( $lang->def("_CHAT_TYPE").":", "chat_type", "chat_type", $chat_type , $sel));

	require_once($GLOBALS["where_scs"]."/lib/lib.room.php");
	$sel =(isset($opt["default_room"]) ? (int)$opt["default_room"] : FALSE);
	$room_list =getRoomList(FALSE, array("course"));
	$out->add($form->getDropdown( $lang->def("_DEFAULT_ROOM").":", "default_room", "default_room", $room_list , $sel));

	// $chk =((isset($opt["use_room"])) && ($opt["use_room"] == 0) ? FALSE : TRUE);
	// $out->add($form->getCheckBox($lang->def("_USE_ROOMS"), "use_room", "use_room", "1", $chk));

	$out->add(getBlindNavDescField($form, $lang, $opt));
	$out->add(getGMonitoringField($form, $lang, $opt));

}



function chatBlockSave($block_id, $sub_id) {

	saveBlockTitle($block_id);

	saveParam($block_id, "css", (int)$_POST["css"]);
	saveParam($block_id, "chat_type", $_POST["chat_type"]);
	// saveParam($block_id, "use_room", (int)$_POST["use_room"]);
	saveParam($block_id, "default_room", (int)$_POST["default_room"]);

	save_pubexp_info($block_id);

	saveBlindNavDesc($block_id);
	saveGMonitoring($block_id);

}


function chatBlockAdd($block_id, $sub_id) {

	saveParam($block_id, "css", 1);
	saveParam($block_id, "chat_type", "htmlframechat");

}




// OLD / TEMP:

//require_once($GLOBALS["where_cms"]."/admin/modules/manpage/manBlocks.php");

function chat_options( $block ) {
	global $prefixCms;

	$backurl = $block->getBackurl();
	echo '<form method="POST" action="'.page_url(array()).'">' //&amp;insert=1
		.'<div class="stdBlock">';
	$block->loadParamAsInput();

	$idBlock=$block->getIdBlock();
	$q=mysql_query("SELECT title FROM ".$prefixCms."_area_block WHERE idBlock='$idBlock';");
	list($title)=mysql_fetch_row($q);

	$opt=loadBlockOption($idBlock); //--debug-//print_r($opt);
	echo "<br />";

	echo("<br /><b>"._BLOCK_TITLE.":</b>\n");
	echo("<input type=\"text\" id=\"title\" name=\"title\" size=\"25\" value=\"".$title."\" /><br />\n");

	echo("<br />\n");
	$db_group=db_block_groups($idBlock);
	sel_block_groups($idBlock, $db_group);

	load_chat_options($idBlock);

	echo("<br /><b>"._BLOCK_STYLE.":</b>\n");
	block_css_list($opt["css"]);

	echo("<br /><br />\n");
	show_pubexp_table($opt["pubdate"], $opt["expdate"]);


	echo '<br /><input class="button" type="submit" id="save" name="save" value="'._CHAT_INSERT.'" />'
		.'</div>'
		.'</form>';

	// back option
	echo '<form method="post" action="'.$backurl['backurl'].'">'
		.'<div class="stdBlock">';
	$block->loadParamAsInput();
	echo '<input class="button" type="submit" value="'._CHAT_BACK.'" />'
		.'</div>'
		.'</form>';

}



function save_options( $block ) {
	global $prefixCms;

	//--debug--// print_r($_POST);

	$backurl = $block->getBackurl();
	$idBlock=$block->getIdBlock();

	if ((int)$_POST["folder_id"] == 0)
		$path="/";
	else {
		$q=mysql_query("SELECT * FROM ".$prefixCms."_media_dir WHERE id='".$_POST["folder_id"]."';");
		$row=mysql_fetch_array($q);

		$path=$row["path"];
	}

	$q=mysql_query("UPDATE ".$prefixCms."_area_block SET title='".$_POST["title"]."' WHERE idBlock='$idBlock';");

	$err="";
	saveParam($idBlock, "css", (int)$_POST["css"]);
	if (check_period($ts_pub, $ts_exp)) {
		saveParam($idBlock, "pubdate", (int)$ts_pub);
		saveParam($idBlock, "expdate", (int)$ts_exp);
	}
	else
		$err=_INVALID_PERIOD;
	save_block_groups($idBlock, $_POST["idGroups"]);
	save_chat_options($idBlock);

	// back option
	echo '<form method="post" action="'.$backurl['backurl'].'">'
		.'<div class="stdBlock">';
	$block->loadParamAsInput();

	if ($err == "") {
		echo("<b>"._CHAT_ALLOK."</b><br />");
		echo '<input class="button" type="submit" value="'._CHAT_BACK.'" />';
	}
	else {
		echo("<b><font color=\"#FF0000\">"._WARN."</font>:</b> $err<br />"._SAVEDREMAIN."<br /><br />\n");
		echo '<input class="button" type="submit" value="'._CHAT_BACK.'" />';
	}


	echo '</div></form>';
}



function load_chat_options($idBlock) {

	$dv_can_admin=load_chat_dbval($idBlock, "can_admin");
	$dv_can_talk=load_chat_dbval($idBlock, "can_talk");
	$dv_can_upload=load_chat_dbval($idBlock, "can_upload");
	$dv_can_pubroom=load_chat_dbval($idBlock, "can_pubroom");
	$dv_can_privroom=load_chat_dbval($idBlock, "can_privroom");

	load_chat_groups(_GRP_CAN_ADMIN, "grp_can_admin", $dv_can_admin);
	load_chat_groups(_GRP_CAN_TALK, "grp_can_talk", $dv_can_talk);
	load_chat_groups(_GRP_CAN_UPLOAD, "grp_can_upload", $dv_can_upload);
	load_chat_groups(_GRP_CAN_PUBROOM, "grp_can_pubroom", $dv_can_pubroom);
	load_chat_groups(_GRP_CAN_PRIVROOM, "grp_can_privroom", $dv_can_privroom);

}



function load_chat_dbval($idBlock, $val_name) {
	global $prefixCms;

	$res=array();

	$qtxt="SELECT idGroup FROM ".$prefixCms."_chat_block_option WHERE idBlock='$idBlock' AND opt_name='$val_name' AND opt_val='1'";
	$q=mysql_query($qtxt);

	if (($q) && (mysql_num_rows($q) > 0)) {
		while($row=mysql_fetch_array($q)) {

			$res[]=$row["idGroup"];

		}
	}

	return (array)$res;
}



function load_chat_groups($title, $label, $db_val) {

	$groups=listGroup();
	echo("<fieldset class=\"option\">");
	echo("<legend>".$title."</legend>\n");
	while(list($idCommon, $nameGroup) = each($groups)) {
		if (in_array($idCommon, $db_val)) $chk=" checked=\"checked\""; else $chk="";
		echo("<label for=\"".$label."[".$idCommon."]\">");
		echo("<input type=\"checkbox\" id=\"".$label."[".$idCommon."]\" name=\"".$label."[".$idCommon."]\" value=\"".$idCommon."\" $chk />\n");
		echo($nameGroup."</label><br />\n");
	}
	echo '</fieldset>'."\n\n";
}




function save_chat_options($idBlock) {
	global $prefixCms;

	$grp_list=array();
	$grp_list["grp_can_admin"]="can_admin";
	$grp_list["grp_can_talk"]="can_talk";
	$grp_list["grp_can_upload"]="can_upload";
	$grp_list["grp_can_pubroom"]="can_pubroom";
	$grp_list["grp_can_privroom"]="can_privroom";


	$qtxt="DELETE FROM ".$prefixCms."_chat_block_option WHERE idBlock='$idBlock'";
	$q=mysql_query($qtxt);


	foreach($grp_list as $key=>$val) {

		foreach($_POST[$key] as $grp_key=>$grp_val) {

			$qtxt ="INSERT INTO ".$prefixCms."_chat_block_option (idBlock, idGroup, opt_name, opt_val) ";
			$qtxt.="VALUES ('$idBlock', '$grp_val', '$val', '1')";
			$q=mysql_query($qtxt);

		}

	}

}



?>