<?php

/*************************************************************************/
/* SPAGHETTILEARNING - E-Learning System                                 */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2002 by Claudio Erba (webmaster@spaghettilearning.com)  */
/* & Fabio Pirovano (gishell@tiscali.it) http://www.spaghettilearning.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.        */
/*************************************************************************/

if(isset($_SESSION['sesCmsAdmUser']) && isset($_SESSION['sesCmsAdmLevel'])) {

function textarea() {
	//access control
	global $prefixCms;
	funAdminAccess('OP');

	include('core/class/class.typeone.php');

	addTitleArea('textarea');

	//finding textarea names
	$reTextarea = mysql_query("
	SELECT idCommon, nameTextarea
	FROM ".$prefixCms."_form_textarea
	WHERE language = '".getCmsAdmLang()."'
	ORDER BY nameTextarea");



	echo '<div class="stdBlock">';

	$tableGroup = new typeOne(0);

	$tableGroup->OpenTable(_TEXTAREALIST);

	$modAcc = funAdminAccess('MOD', true);
	$remAcc = funAdminAccess('REM', true);

	$head = array(_FIELD);
	$headType = array('');

	if($modAcc) {
		$head[] = '<img src="'.getPathImage().'standard/mod.gif" alt="'._MODIFY.'" title="'._MODIFYG.'" />';
		$headType[] = 'img';
	}
	if($remAcc) {
		$head[] = '<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVE.'" title="'._REMOVEG.'" />';
		$headType[] = 'img';
	}

	$tableGroup->WriteHeader(
		$head,
		$headType
		);

	while(list($idCommon, $nameTextarea) = mysql_fetch_row($reTextarea))  {

		$data = array($nameTextarea);

		if($idCommon != '0') {
			if($modAcc) $data[] = '<a href="admin.php?modulename=textarea&amp;op=newinfotextarea&amp;idCommon='.$idCommon.'">
				<img src="'.getPathImage().'standard/mod.gif" alt="'._MODIFIYN.'" /></a>';
			if($remAcc) $data[] = '<a href="admin.php?modulename=textarea&amp;op=deltextarea&amp;idCommon='.$idCommon.'">
				<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVE.'" /></a>';
		}
		else {
			if($modAcc) $data[] = '&nbsp;';
			if($remAcc) $data[] = '&nbsp;';
		}

		$tableGroup->WriteRow($data);
	}
	if(funAdminAccess('NEW', true)) {
		$tableGroup->WriteAddRow('<a href="admin.php?modulename=textarea&amp;op=newtextarea" title="'._ADDELEM.'">
								<img src="'.getPathImage().'standard/add.gif" alt="'._ADD.'" /> '._ADDELEMENT.'</a>');
	}
	$tableGroup->CloseTable();

	//list of avvailable language
	$language = get_lang_list();
	echo '<div class="navLang">'
		.'<b>'._LANGAVVAILABLE.' :</b> ';
	while(list(, $lang) = each($language)) {
		echo ' [ '.ucfirst($lang).' ] ';
	}
	echo '</div>';
	echo '</div>';
}

function newtextarea() {
	//access control
	funAdminAccess('NEW');

	//title of the area
	addTitleArea('textarea');

	$language = get_lang_list();

	echo '<div class="stdBlock">'
		.'<b>'._HOWTO.'</b><br /><br />';
	echo '<form method="post" action="admin.php?modulename=textarea&amp;op=instextarea">';

		while(list(, $lang) = each($language)) {
			echo '<div class="lineInput"><div class="words">'.ucfirst($lang).'</div>'
				.'<input class="inputText" type="text" name="nameTextarea['.$lang.']" maxlength="100" />';
			if(getCmsDefaultAdmLang() == $lang) echo ' <b>'._MUSTINS.'</b>';
			echo '</div>';
		}
		echo '<br /><br />'
		.'<div class="lineInput"><input type="submit" value="'._INSERT.'" /></div>'
		.'</form>';

	echo '</div>';
}

function instextarea() {
	//REQUIRES: at least one name passed in array nameCateg
	//EFFECTS : insert a category
	//MODIFIES: $prefixCms_groupcategory

	//access control
	funAdminAccess('NEW');
	global $prefixCms;

	addTitleArea('textarea');

	if($_POST['nameTextarea'][getCmsDefaultAdmLang()] == '') errorCommunication(_BLANKMUST);

	$first = true;
	$idCommon = 0;
	while(list($lang, $name) = each($_POST['nameTextarea'])) {

		//if is the first insert
		if($first) {

			//if the name is not empty
			if($name != '') {

				//the Textarea exists ?
				if(mysql_num_rows(mysql_query("SELECT idTextarea FROM ".$prefixCms."_form_textarea WHERE language = '$lang' AND nameTextarea = '$name'"))) {

					//the Textarea exists !
					errorCommunication(_ALREDYINSERT);
					return;
				}

				//insert Textarea
				mysql_query("INSERT INTO ".$prefixCms."_form_textarea SET language = '$lang', nameTextarea = '$name'");

				//find primary key insert id
				list($idCommon) = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));
				if(!$idCommon) {

					//value of primary key not found
					errorCommunication(_CRITICALERREXIT);
					return;
				}

				//setting idCommon equals to idCategory for the first insert
				if(!mysql_query("UPDATE ".$prefixCms."_form_textarea SET idCommon = idTextarea WHERE idTextarea = '$idCommon'")) {

					//setting of idCommon fails => category removed
					mysql_query("DELETE FROM ".$prefixCms."_form_textarea WHERE idTextarea = '$idCommon'");
					errorCommunication(_CRITICALERREXIT);
					return;
				}
				//the first element insert was succesful!
				$first = false;
			}
		}
		else {
			if($name != '') {
				mysql_query("
				INSERT INTO ".$prefixCms."_form_textarea
				SET language = '$lang',
					nameTextarea = '$name',
					idCommon = '$idCommon'");
			}
		}
	}
	Header("Location:admin.php?modulename=textarea&op=textarea");
}

function newinfotextarea() {
	//access control
	global $prefixCms;
	funAdminAccess('MOD');

	//title of the area
	addTitleArea('textarea');

	$languages = giveLanguageList();

	//finding category names
	$reTextarea = mysql_query("
	SELECT language, nameTextarea
	FROM ".$prefixCms."_form_textarea
	WHERE idCommon = '".$_GET['idCommon']."'");

	while(list($lang, $nameTextarea) = mysql_fetch_row($reTextarea))  {
		$nameFree[$lang] = $nameTextarea;
	}

	echo '<div class="stdBlock">'
		.'<b>'._NOEMPTY.'</b><br /><br />'
		.'<form method="post" action="admin.php?modulename=textarea&amp;op=uptextarea">'
		.'<input type="hidden" name="idCommon" value="'.$_GET['idCommon'].'" />';

	while(list(, $lang) = each($languages)) {
		echo '<div class="lineInput"><div class="words">'.ucfirst($lang).'</div>'
			.'<input class="inputText" type="text" name="nameFree['.$lang.']" maxlength="100" value="'.$nameFree[$lang].'" />'
			.' ( '.$nameFree[$lang].' )'
			.'</div>';
	}
	echo '<br /><br />'
		.'<div class="lineInput"><input type="submit" value="'._UPDATE.'" /></div>'
		.'</form>';

	echo '<form method="post" action="admin.php?modulename=textarea&amp;op=textarea">'
		.'<div class="lineInput"><input type="submit" value="'._BACK.'" /></div>'
		.'</form>';

	echo '</div>';
}

function uptextarea() {
	//REQUIRES: at least one name passed in array nameFree[]
	//EFFECTS : modify a textarea
	//MODIFIES: $prefixCms_form_textarea

	//access control
	funAdminAccess('MOD');
	global $prefixCms;

	addTitleArea('textarea');
	$first = true;
	$idCommon = 0;
	while(list($lang, $name) = each($_POST['nameFree'])) {

		//if the name is not empty
		if($name == '') $name = _NOTITLE;

		//the textarea exists ?
		if(mysql_num_rows(mysql_query("
			SELECT idTextarea
			FROM ".$prefixCms."_form_textarea
			WHERE language = '$lang' AND idCommon='".$_POST['idCommon']."'"))) {

				//update existing category
				mysql_query("
				UPDATE ".$prefixCms."_form_textarea
				SET nameTextarea = '$name'
				WHERE language = '$lang' AND idCommon = '".$_POST['idCommon']."'");
		}
		else {
			//insert inexistentcategory
			mysql_query("
			INSERT INTO ".$prefixCms."_form_textarea
			SET language = '$lang',
				nameTextarea = '$name',
				idCommon = '".$_POST['idCommon']."'");
		}
	}
	Header("Location:admin.php?modulename=textarea&op=newinfotextarea&idCommon=".$_POST['idCommon']);
}

function dependenceFree($idC) {
	//EFFECTS : if exists at least one group related with this field return a list of they, else return an empty array
	global $prefixCms;

	$reGroup  = mysql_query("
	SELECT t1.nameGroup
	FROM ".$prefixCms."_group AS t1 , ".$prefixCms."_groupfield AS t2
	WHERE t1.idCommon = t2.idGroup AND (
	( t2.idResource = '$idC' AND t2.type='textarea' AND t1.language = '".getCmsAdmLang()."')
		OR
	( t2.idResource = '$idC' AND t2.type='textarea' AND t1.idCommon = '0'))");
	echo mysql_error();
	if(!mysql_num_rows($reGroup)) return array();

	while(list($name) = mysql_fetch_row($reGroup)) $nameG[] = $name;
	return $nameG;
}

function remtextarea() {
	//access control
	global $prefixCms;
	funAdminAccess('REM');

	addTitleArea('textarea');

	//finding textarea names
	$reTextarea = mysql_query("
	SELECT idCommon, nameTextarea
	FROM ".$prefixCms."_form_textarea
	WHERE language = '".getCmsAdmLang()."'
	ORDER BY nameTextarea");

	echo '<div class="stdBlock">'
		.'<div class="title">'._TEXTAREALIST.'</div><br />'
		.'<div class="line"></div>';

	if(isset($_GET['idCommon'])) {
		$related = dependenceFree($_GET['idCommon']);
		if(count($related)) {
			errorCommunication(_USED);
			echo '<div class="userBlock">';
			while(list(,$nameG) = each($related))
				echo ' [ '.$nameG.' ] ';
			echo '</div><br />';
		}
		else {
			echo '<div class="confirmBlock">'._REMOVETHISONE.' '
				.'<a href="admin.php?modulename=textarea&amp;op=deltextarea&amp;idCommon='.$_GET['idCommon'].'"> ['._YES.' ]</a>'
				.'</div>';
		}
	}
	$i = 0;
	while(list($idCommon, $nameFree) = mysql_fetch_row($reTextarea))  {
		if($idCommon == $_GET['idCommon']) {
			echo '<div class="lineSelected">'
				.'<img src="'.getPathImage().'standard/sel.gif" alt="'._REMOVESEl.'" />'
				.$nameFree
				.'</div>';
		}
		else {
			echo '<div class="line'.($i++%2 == 0 ? 'col' : '').'">'
				.'<a href="admin.php?modulename=textarea&amp;op=remtextarea&amp;idCommon='.$idCommon.'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVEN.'" /></a>'
				.$nameFree
				.'</div>';
		}
	}

	//list of avvailable language
	$language = get_lang_list();
	echo '<div class="navLang">'
		.'<b>'._LANGAVVAILABLE.' :</b> ';
	while(list(, $lang) = each($language)) {
		echo ' [ '.ucfirst($lang).' ] ';
	}
	echo '</div>';
	echo '</div>';
}

function deltextarea() {
	//REQUIRES: idCommon passed as $_GET
	//EFFECTS : delete a category
	//MODIFIES: $prefixCms_groupcategory

	//access control
	funAdminAccess('REM');
	global $prefixCms;

	addTitleArea('group');

	$related = dependenceFree($_GET['idCommon']);
	if(count($related)) {
		errorCommunication(_USED);
		echo '<div class="stdBlock">';
		while(list(,$nameG) = each($related))
			echo ' [ '.$nameG.' ] ';
		echo '</div><br />';
		return;
	}

	if($_GET['confirm'] == 1) {
		//if idCommon is passed
		if(isset($_GET['idCommon'])) {

			if(!mysql_query("DELETE FROM ".$prefixCms."_form_textarea WHERE idCommon = '".$_GET['idCommon']."'")) {

				//error occurred while removing
				errorCommunication(_REMOVEERR);
				return;
			}
		}
		else {
			//idCommon doesn't exists
			errorCommunication(_NOSELECT);
			return;
		}
		Header("Location:admin.php?modulename=textarea&op=remtextarea");
	}
	else {

		echo '<div class="stdBlock">';

		if(isset($_GET['idCommon'])) {

			echo '<div class="remRequest">';

			echo _REMOVETHISONE.'<br /><br />'
				._GROUPNAME.' : <span class="redFont">'.$groupName.'</span><br /><br />'

				.'[ <a href="admin.php?modulename=textarea&amp;op=deltextarea&amp;idCommon='.$_GET['idCommon'].'&amp;confirm=1" '
				.'title="'._YESGTIT.'">'._YES.'</a> | '

				.'<a href="admin.php?modulename=textarea&amp;op=textarea" '
				.'title="'._NOGITIT.'">'._NO.'</a> ]';

			echo '</div>';

		echo '</div>';
		}
	}
}

switch($op) {
	case "textarea" : {
		textarea();
	};break;

	case "newtextarea" : {
		newtextarea();
	};break;
	case "instextarea" : {
		instextarea();
	};break;

	case "newinfotextarea" : {
		newinfotextarea();
	};break;
	case "uptextarea" : {
		uptextarea();
	};break;

	case "deltextarea" : {
		deltextarea();
	};break;
}

}

?>