<?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.        */
/*************************************************************************/

if(!defined('IN_DOCEBO')) die('You cannot access this file directly!');

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

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

	$opt=loadBlockOption($block_id);

	include_once($GLOBALS["where_cms"]."/admin/modules/media/media_class.php");

	$tree=createTreeView();

	if( isset($_POST[$tree->_getFolderNameId()]) ) { // Al cambio cartella:
		$folderid = $_POST[$tree->_getFolderNameId()];
	}
	else { // La prima volta che carica la pagina con l'albero:
		unset($tree);
		$tree=createTreeView(true, false, false, $opt["path"]);
		$folderid = $tree->getSelectedFolderId();
	}

	$tree->show_icons=0;

	$folder=$tree->tdb->getFolderById( $tree->getSelectedFolderId() );

	$name=$tree->_getFolderNameId();
	$value=$folderid;
	$out->add($form->getHidden($name, $name, $value));

	$name="folder_id";
	$value=$tree->getSelectedFolderId();
	$out->add($form->getHidden($name, $name, $value));

	//$out->add('<div><b>'.$lang->def("_FOLDER").':</b></div>');
	$out->add($tree->load());


	$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));

	if ((isset($opt["use_comments"])) && ($opt["use_comments"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_USE_COMMENTS"), "use_comments", "use_comments", "1", $chk));

	/* if ((isset($opt["ov_comments"])) && ($opt["ov_comments"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_OVERRIDE_COM"), "ov_comments", "ov_comments", "1", $chk)); */



	if ((isset($opt["recurse"])) && ($opt["recurse"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_MEDIA_RECOURSE"), "recurse", "recurse", "1", $chk));

	$out->add($form->getTextfield($lang->def("_MEDIA_NUMBER").":", "number", "number", 3, $opt["number"]));
	$out->add($form->getTextfield($lang->def("_MEDIA_COLS").":", "cols", "cols", 3, $opt["cols"]));


	if ((isset($opt["showtitle"])) && ($opt["showtitle"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_MEDIA_SHOWTITLE"), "showtitle", "showtitle", "1", $chk));

	if ((isset($opt["showdesc"])) && ($opt["showdesc"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_MEDIA_SHOWDESC"), "showdesc", "showdesc", "1", $chk));

	if ((isset($opt["openinplayer"])) && ($opt["openinplayer"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_MEDIA_OPENINPLAYER"), "openinplayer", "openinplayer", "1", $chk));

	$vt_list=array();
	$vt_list["gallery"]=$lang->def("_VIS_GALLERY");
	$vt_list["slide"]=$lang->def("_VIS_SLIDE");
	$out->add($form->getDropdown($lang->def("_MEDIA_VISTYPE").":", "vistype", "vistype", $vt_list, $opt["vistype"]));

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

/*	if ((isset($opt["show_catlink"])) && ($opt["show_catlink"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_SHOW_CATLINK"), "show_catlink", "show_catlink", "1", $chk));

	if ((isset($opt["show_topiclink"])) && ($opt["show_topiclink"])) $chk=true; else $chk=false;
	$out->add($form->getCheckBox($lang->def("_SHOW_TOPICLINK"), "show_topiclink", "show_topiclink", "1", $chk)); */

}

function mediaBlockSave($block_id, $sub_id) {

	saveBlockTitle($block_id);

	saveParam($block_id, "css", (int)$_POST["css"]);
	saveParam($block_id, "recurse", (int)$_POST["recurse"]);
	saveParam($block_id, "cols", (int)$_POST["cols"]);
	saveParam($block_id, "number", (int)$_POST["number"]);
	saveParam($block_id, "use_comments", (int)$_POST["use_comments"]);
	saveParam($block_id, "vistype", $_POST["vistype"]);
	saveParam($block_id, "showtitle", (int)$_POST["showtitle"]);
	saveParam($block_id, "showdesc", (int)$_POST["showdesc"]);
	saveParam($block_id, "openinplayer", (int)$_POST["openinplayer"]);

	saveBlockPath($block_id, (int)$_POST["folder_id"], "_media_dir");

	save_pubexp_info($block_id);

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

}

function mediaBlockAdd($block_id, $sub_id) {

	saveParam($block_id, "css", 1);
	saveParam($block_id, "path", "/");
	saveParam($block_id, "recurse", 1);
	saveParam($block_id, "showtitle", 1);
	saveParam($block_id, "showdesc", 0);
/*	saveParam($block_id, "show_catlink", 0);
	saveParam($block_id, "show_topiclink", 0); */
	saveParam($block_id, "number", 10);
	saveParam($block_id, "cols", 2);
	saveParam($block_id, "use_comments", 0);
	saveParam($block_id, "vistype", "gallery");
	saveParam($block_id, "openinplayer", 0);

}

// OLD / TEMP:

function media_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);
	$textof=loadTextof($idBlock);
	echo "<br />";

	// ------- mostro albero ----------------------------------------------\

	include_once("admin/modules/media/media_class_sel.php");

	$tree=createTreeView();

	if( isset($_POST[$tree->_getFolderNameId()]) ) { // Al cambio cartella:
		$folderid = $_POST[$tree->_getFolderNameId()];
	}
	else { // La prima volta che carica la pagina con l'albero:
		unset($tree);
		$tree=createTreeView(true, false, false, $opt["path"]);
		$folderid = $tree->getSelectedFolderId();
	}

	$tree->show_icons=0;

	$folder=$tree->tdb->getFolderById( $tree->getSelectedFolderId() );
	//echo '<input type="hidden" value="" name="'._getOpMoveFormId().'" />';
	echo '<input type="hidden" value="'.$folderid.'" name="'.$tree->_getFolderNameId().'" />';
	echo '<input type="hidden" value="'.$tree->getSelectedFolderId().'" name="folder_id" />';
	echo '<div><b>'._FOLDER.':</b></div>';
	$tree->load();
	// --------------------------------------------------------------------/

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

	echo("<br /><b>"._BLOCK_TEXT.":</b>\n");
	loadHTMLEditor('text_write','textof',$textof);

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

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

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

	if ($opt["ov_comments"]) $chk_ov_com=" checked=\"checked\""; else $chk_ov_com="";
	echo("<br /><br />\n<input type=\"checkbox\" id=\"ov_comments\" name=\"ov_comments\" value=\"1\"$chk_ov_com />\n");
	echo("<b>"._OVERRIDE_COM."</b>\n");

	if ($opt["ov_pub"]) $chk_ov_pub=" checked=\"checked\""; else $chk_ov_pub="";
	echo("<br /><br />\n<input type=\"checkbox\" id=\"ov_pub\" name=\"ov_pub\" value=\"1\"$chk_ov_pub />\n");
	echo("<b>"._OVERRIDE_PUB."</b>\n");

	if ($opt["ov_exp"]) $chk_ov_exp=" checked=\"checked\""; else $chk_ov_exp="";
	echo("<br /><br />\n<input type=\"checkbox\" id=\"ov_exp\" name=\"ov_exp\" value=\"1\"$chk_ov_exp />\n");
	echo("<b>"._OVERRIDE_EXP."</b>\n");

	if ($opt["recurse"]) $chk=" checked=\"checked\""; else $chk="";
	echo("<br /><br />\n");
	echo("<input type=\"checkbox\" id=\"recurse\" name=\"recurse\" value=\"1\"$chk />\n");
	echo("<b>"._MEDIA_RECOURSE."</b>\n");

	if ($opt["showtitle"]) $chk=" checked=\"checked\""; else $chk="";
	echo("<br /><br />\n");
	echo("<input type=\"checkbox\" id=\"showtitle\" name=\"showtitle\" value=\"1\"$chk />\n");
	echo("<b>"._MEDIA_SHOWTITLE."</b>\n");

	if ($opt["showdesc"]) $chk=" checked=\"checked\""; else $chk="";
	echo("<br /><br />\n");
	echo("<input type=\"checkbox\" id=\"showdesc\" name=\"showdesc\" value=\"1\"$chk />\n");
	echo("<b>"._MEDIA_SHOWDESC."</b>\n");

	if ($opt["onlypreview"]) $chk=" checked=\"checked\""; else $chk="";
	echo("<br /><br />\n");
	echo("<input type=\"checkbox\" id=\"onlypreview\" name=\"onlypreview\" value=\"1\"$chk />\n");
	echo("<b>"._MEDIA_ONLYPREVIEW."</b>\n");

	if ($opt["onerandom"]) $chk=" checked=\"checked\""; else $chk="";
	echo("<br /><br />\n");
	echo("<input type=\"checkbox\" id=\"onerandom\" name=\"onerandom\" value=\"1\"$chk />\n");
	echo("<b>"._MEDIA_ONERANDOM."</b>\n");

	echo("<br /><br /><b>"._MEDIA_VISTYPE.":</b>\n");
	$dfv=array("gallery", "slide");
	$dfl=array(_VIS_GALLERY, _VIS_SLIDE);
	echo("<select id=\"vistype\" name=\"vistype\">\n");

	foreach ($dfv as $key=>$val) {
		if ($val == $opt["vistype"]) $sel=" selected=\"selected\""; else $sel="";
		echo("<option value=\"$val\"$sel>".$dfl[$key]."</option>\n");
	}
	echo("</select>\n");

	echo("<br /><br /><b>"._MEDIA_NUMBER.":</b>\n");
	echo("<input type=\"text\" id=\"number\" name=\"number\" size=\"3\" value=\"".$opt["number"]."\" />\n");

	echo("<br /><br /><b>"._MEDIA_COLS.":</b>\n");
	echo("<input type=\"text\" id=\"cols\" name=\"cols\" size=\"3\" value=\"".$opt["cols"]."\" /><br />\n");


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

	// back option
	echo '<form method="post" action="'.$backurl['backurl'].'">'
		.'<div class="stdBlock">';
	$block->loadParamAsInput();
	echo '<input class="button" type="submit" value="'._MEDIA_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="";
	saveTextof($idBlock, $_POST['textof']);
	saveParam($idBlock, "showtitle", (int)$_POST["showtitle"]);
	saveParam($idBlock, "showdesc", (int)$_POST["showdesc"]);
	saveParam($idBlock, "vistype", $_POST["vistype"]);
	saveParam($idBlock, "number", (int)$_POST["number"]);
	saveParam($idBlock, "cols", (int)$_POST["cols"]);
	saveParam($idBlock, "onlypreview", (int)$_POST["onlypreview"]);
	saveParam($idBlock, "onerandom", (int)$_POST["onerandom"]);
	saveParam($idBlock, "path", $path);
	saveParam($idBlock, "css", (int)$_POST["css"]);
	saveParam($idBlock, "ov_comments", (int)$_POST["ov_comments"]);
	saveParam($idBlock, "ov_pub", (int)$_POST["ov_pub"]);
	saveParam($idBlock, "ov_exp", (int)$_POST["ov_exp"]);
	saveParam($idBlock, "recurse", $_POST["recurse"]);
	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"]);

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

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


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

?>
