<?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 voicecat() {
	//access control
	global $prefixCms;
	include('core/class/class.typeone.php');
	funAdminAccess('OP');
	
	addTitleArea('voicecat');
	
	//finding category names
	$reCategory = mysql_query("
	SELECT idCommon, nameCategory 
	FROM ".$prefixCms."_groupcategory 
	WHERE language = '".getCmsAdmLang()."'");
	
	echo '<div class="stdBlock">';
	
	$tableVoice = new typeOne(0);
	
	$tableVoice->OpenTable(_CATEGORYLIST);
	
	$newAcc = funAdminAccess('NEW', true);
	$modAcc = funAdminAccess('MOD', true);
	$remAcc = funAdminAccess('REM', true);
	
	$head = array(_CATEGORYNAME,
			'<img src="'.getPathImage().'voicecat/view.gif" alt="'._VISUVOICE.'" />');
	$headType = array('', 'img');
		
	
	if($newAcc) {
		$head[] = '<img src="'.getPathImage().'standard/add.gif" alt="'._ADDVOICE.'" title="'._ADDVOICEG.'" />';
		$headType[] = 'img';
	}

	$tableVoice->WriteHeader(
		$head,
		$headType
		);
	
	while(list($idCommon, $nameCategory) = mysql_fetch_row($reCategory))  {
			
		$data = array(
			$nameCategory, 
			'<a href="admin.php?modulename=voicecat&amp;op=voiceforcat&amp;idCommon='.$idCommon.'">
				<img src="'.getPathImage().'voicecat/view.gif" alt="'._VISUVOICE.'" /></a>'
				);
			
		if($newAcc) {
			$data[] = '<a href="admin.php?modulename=voicecat&amp;op=insertvoicecat&amp;idCommon='.$idCommon.'">
			<img src="'.getPathImage().'standard/add.gif" alt="'._ADDVOICE.'" /></a>';
		}
	
		$tableVoice->WriteRow($data);
	}

	$tableVoice->CloseTable();
	

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

function voiceforcat() {
	//access control
	global $prefixCms;
	include('core/class/class.typeone.php');
	funAdminAccess('OP');
	
	addTitleArea('voicecat');
	
	//finding category names
	$reCategory = mysql_query("
	SELECT idCommonVoice, nameVoice 
	FROM ".$prefixCms."_groupcategory_voice 
	WHERE language = '".getCmsAdmLang()."' AND idCommon='".$_GET['idCommon']."'");
	
	echo '<div class="stdBlock">';
	
	$tableVoice = new typeOne(0);
	
	$tableVoice->OpenTable(_VOICELIST.'<br/><a href="admin.php?modulename=voicecat&op=voicecat">'._BACK.'</a>');
	
	$modAcc = funAdminAccess('MOD', true);
	$remAcc = funAdminAccess('REM', true);
	
	$head = array(_VOICENAME);
	$headType = array('');
		
	
	if($modAcc) {
		$head[] = '<img src="'.getPathImage().'standard/mod.gif" alt="'._MODVOICE.'" title="'._MODVOICEG.'" />';
		$headType[] = 'img';
	}
	if($remAcc) {
		$head[] = '<img src="'.getPathImage().'standard/rem.gif" alt="'._REMVOICE.'" title="'._REMVOICEG.'" />';
		$headType[] = 'img';
	}

	$tableVoice->WriteHeader(
		$head,
		$headType
	);
	
	while(list($idCommon, $nameVoice) = mysql_fetch_row($reCategory))  {
		$data = array($nameVoice);
			
		if($modAcc) {
			$data[] = '<a href="admin.php?modulename=voicecat&amp;op=modifyvoicecat&amp;idCommonVoice='.$idCommon.'&amp;idCommon='.$_GET['idCommon'].'">
			<img src="'.getPathImage().'standard/mod.gif" alt="'._MODVOICE.'" /></a>';
		}
		if($remAcc) {
			$data[] = '<a href="admin.php?modulename=voicecat&amp;op=delvoicecat&amp;idCommonVoice='.$idCommon.'">
			<img src="'.getPathImage().'standard/rem.gif" alt="'._REMVOICE.'" /></a>';
		}
		$tableVoice->WriteRow($data);
	}
	
	if(funAdminAccess('NEW', true)) {
		$tableVoice->WriteAddRow('<a href="admin.php?modulename=voicecat&amp;op=insertvoicecat&amp;idCommon='.$_GET['idCommon'].'" title="'._ADDELEM.'">
								<img src="'.getPathImage().'standard/add.gif" alt="'._ADD.'" /> '._ADDELEMENT.'</a>');
	}
	
	$tableVoice->CloseTable();
	
	//list of avvailable language
	$language = giveLanguageList();
	echo '<div class="navLang">'
		.'<b>'._LANGAVVAILABLE.' :</b> ';
	while(list(, $lang) = each($language)) { 
		echo ' [ '.ucfirst($lang).' ] ';
	}
	echo '</div>';
	echo '</div>';
}

function insertvoicecat() {
	global $prefixCms;
	//access control
	funAdminAccess('NEW');
	
	//title of the area
	addTitleArea('voicecat');
	
	$language = giveLanguageList();
	
	$idCommon = 0;
	if(isset($_GET['idCommon'])) $idCommon = $_GET['idCommon'];
	if(isset($_POST['idCommon'])) $idCommon = $_POST['idCommon'];
	
	list($nameCategory) = mysql_fetch_row(mysql_query("
	SELECT nameCategory 
	FROM ".$prefixCms."_groupcategory 
	WHERE language = '".getCmsAdmLang()."' AND idCommon='".$idCommon."'"));
	
	echo '<div class="stdBlock">'
		.'<div class="title">'._CATEGORYNAME.' : '.$nameCategory.'</div><br />'
		.'</div>';
	
	echo '<div class="stdBlock">'
		.'<b>'._HOWTO.'</b><br /><br />';
	echo '<form method="post" action="admin.php?modulename=voicecat&amp;op=inscategoryvoice">'
		.'<input type="hidden" name="idCommon" value="'.$idCommon.'" />';
		
		while(list(, $lang) = each($language)) { 
			echo '<div class="lineInput"><div class="words">'.ucfirst($lang).'</div>'
				.'<input class="inputText" type="text" name="nameVoice['.$lang.']" maxlength="100" />';
			if(getCmsDefaultAdmLang() == $lang) echo ' <b>'._MUSTINS.'</b>';	
			echo '</div>';
		}
		echo '<div class="lineInput"><input type="submit" value="'._INSERT.'" /></div>'
			.'</form>';
		
		echo '<form method="post" action="admin.php?modulename=voicecat&op=voiceforcat&idCommon='.$idCommon.'">'
			.'<div class="lineInput"><input type="submit" value="'._HAVEDONE.'" /></div>'
			.'</form>';
	
	echo '</div>';
}

function inscategoryvoice() { 
	//REQUIRES: at least one name passed in array nameCateg and idCommon valid
	//EFFECTS : insert a category
	//MODIFIES: $prefixCms_groupcategory
	
	//access control
	addTitleArea('voicecat');
	funAdminAccess('NEW');
	global $prefixCms;
	
	$defLang = getCmsDefaultAdmLang();
	
	if(!isset($_POST['nameVoice'][$defLang]) || ($_POST['nameVoice'][$defLang] == '')) {
		errorCommunication(_NOTINS);
		return;
	}
	
	//the voice alredy exists ?
	if(mysql_num_rows(mysql_query("
	SELECT idVoice 
	FROM ".$prefixCms."_groupcategory_voice 
	WHERE language = '$defLang' AND 
		idCommon = '".$_POST['idCommon']."' AND 
		nameVoice = '".$_POST['nameVoice'][$defLang]."'")))  {
		//the voice alredy exists !
		errorCommunication(_ALREDYINSERT);
		return;
	}
				
	//insert voice
	mysql_query("
	INSERT INTO ".$prefixCms."_groupcategory_voice 
	SET idCommon = '".$_POST['idCommon']."', 
		language = '$defLang', 
		nameVoice = '".$_POST['nameVoice'][$defLang]."'");	
	
	//find primary key id
	list($idCommonVoice) = mysql_fetch_row(mysql_query("
	SELECT idVoice 
	FROM ".$prefixCms."_groupcategory_voice 
	WHERE idCommon = '".$_POST['idCommon']."' AND 
		language = '$defLang' AND 
		nameVoice = '".$_POST['nameVoice'][$defLang]."'"));
	
	//setting idCommonVoice equals to idVoice for the first insert
	if(!mysql_query("UPDATE ".$prefixCms."_groupcategory_voice SET idCommonVoice = idVoice WHERE idVoice = '$idCommonVoice'")) {
		//setting of idCommon fails => category removed
		mysql_query("DELETE FROM ".$prefixCms."_groupcategory_voice WHERE idVoice = '$idCommonVoice'");
		errorCommunication(_CRITICALERREXIT);
		return;
	}
	
	//leave this voice from the array
	unset($_POST['nameVoice'][$defLang]);
	
	//inserts others elements
	while(list($lang, $name) = each($_POST['nameVoice'])) { 
		
		if($name == '') $name = _NOTITLE;

		//insert new voice
		mysql_query("
		INSERT INTO ".$prefixCms."_groupcategory_voice 
		SET idCommon = '".$_POST['idCommon']."', 
			idCommonVoice = '$idCommonVoice', 
			language = '$lang', 
			nameVoice = '$name'");	
	}
	Header("Location:admin.php?modulename=voicecat&op=insertvoicecat&idCommon=".$_POST['idCommon']);
}

function modifyvoicecat() {
	//access control
	global $prefixCms;
	funAdminAccess('MOD');
	
	//title of the area
	addTitleArea('voicecat');

	$languages = giveLanguageList();
	
	//finding category names
	$reCategory = mysql_query("
	SELECT language, nameVoice 
	FROM ".$prefixCms."_groupcategory_voice 
	WHERE idCommonVoice = '".$_GET['idCommonVoice']."'");
	
	while(list($lang, $voice) = mysql_fetch_row($reCategory))  {
		$nameVoice[$lang] = $voice;
	}
	
	echo '<div class="stdBlock">'
		.'<b>'._NOEMPTY.'</b><br /><br />'
		.'<form method="post" action="admin.php?modulename=voicecat&amp;op=updatevoicecat">'
		.'<input type="hidden" name="idCommon" value="'.$_GET['idCommon'].'" />'
		.'<input type="hidden" name="idCommonVoice" value="'.$_GET['idCommonVoice'].'" />';
		
	while(list(, $lang) = each($languages)) { 
		echo '<div class="lineInput"><div class="words">'.ucfirst($lang).'</div>'
			.'<input class="inputText" type="text" name="nameVoice['.$lang.']" maxlength="100" value="'.$nameVoice[$lang].'" />'
			.' ( '.$nameVoice[$lang].' )'
			.'</div>';
	}
	echo '<div class="lineInput"><input type="submit" value="'._UPDATE.'" /></div>'
		.'</form>';
	
	echo '</div>';
}

function updatevoicecat() {
	//access control
	global $prefixCms;
	funAdminAccess('MOD');
	
	while(list($lang, $voice) = each($_POST['nameVoice'])) { 
		if($voice == '') $voice = _NOTITLE;
		
		if(mysql_num_rows(mysql_query("
			SELECT idCommonVoice 
			FROM ".$prefixCms."_groupcategory_voice 
			WHERE language = '$lang' AND idCommonVoice='".$_POST['idCommonVoice']."'"))) {
			
				//updating information
				mysql_query("
				UPDATE ".$prefixCms."_groupcategory_voice 
				SET nameVoice = '$voice' 
				WHERE language = '$lang' AND idCommonVoice = '".$_POST['idCommonVoice']."'");
		}
		else {
			//insert inexistentcategory
				mysql_query("
				INSERT INTO ".$prefixCms."_groupcategory_voice 
				SET idCommon = '".$_POST['idCommon']."', 
					idCommonVoice = '".$_POST['idCommonVoice']."', 
					language = '$lang', 
					nameVoice = '$name'");	
		}
		
	}
	Header("Location:admin.php?modulename=voicecat&op=voiceforcat&idCommon=".$_POST['idCommon']);
}

function dependenceVoice($idC) {
	//EFFECTS : if exists at least one user that select this voice return true else folse
	global $prefixCms;
	
	return mysql_num_rows(mysql_query("
	SELECT t1.idField
	FROM ".$prefixCms."_groupfield AS t1 , ".$prefixCms."_groupuserfield AS t2 
	WHERE t1.type='dropdown' AND  t1.idResource = '$idC' AND t1.idField = t2.idField AND t2.value='$idC'"));
}

function delvoicecat() { 
	//REQUIRES: idCommon passed as $_GET
	//EFFECTS : delete a category
	//MODIFIES: $prefixCms_groupcategory
	
	//access control
	funAdminAccess('REM');
	global $prefixCms;
	
	addTitleArea('voicecat');
	
	if(dependenceVoice($_GET['idCommonVoice'])) {
		errorCommunication(_USED);
		return;
	}
	
	//if idCommon is passed
	if($_GET['confirm'] == 1) {
 	
		if(isset($_GET['idCommonVoice'])) {
			if(!mysql_query("DELETE FROM ".$prefixCms."_groupcategory_voice WHERE idCommonVoice = '".$_GET['idCommonVoice']."'")) {
				
				//error occurred while removing
				errorCommunication(_REMOVEERR);
				return;
			}
		}
		else {
			//idCommon doesn't exists
			errorCommunication(_NOSELECT);
			return;
		}
		Header("Location:admin.php?modulename=voicecat&op=voicecat");
	}
	else {
		echo '<div class="stdBlock">'
			.'<div class="remRequest">';
	
		list($voice, $idCommon) = mysql_fetch_row(mysql_query(
			"SELECT nameVoice, idCommon 
			FROM ".$prefixCms."_groupcategory_voice 
			WHERE language = '".getCmsAdmLang()."' AND idCommonVoice='".$_GET['idCommonVoice']."'"));
			
		echo _REMOVETHISONE.'<br /><br />'
			._VOICENAME.' : <span class="redFont">'.$voice.'</span><br /><br />'
			
			.'[ <a href="admin.php?modulename=voicecat&amp;op=delvoicecat&amp;idCommonVoice='.$_GET['idCommonVoice'].'&amp;confirm=1" '
			.'title="'._YESGTIT.'">'._YES.'</a> | '
			
			.'<a href="admin.php?modulename=voicecat&amp;op=voiceforcat&amp;idCommon='.$idCommon.'" '
			.'title="'._NOGITIT.'">'._NO.'</a> ]';
		
		echo '</div>'
			.'</div>';	
	}
}

switch($op) {
	case "voicecat" : {
		voicecat();
	};break;
	case "voiceforcat" : {
		voiceforcat();
	};break;
	//insert

	case "insertvoicecat" : {
		insertvoicecat();
	};break;
	case "inscategoryvoice" : {
		inscategoryvoice();
	};break;
	
	//modify

	case "modifyvoicecat" : {
		modifyvoicecat();
	};break;
	
	case "updatevoicecat" : {
		updatevoicecat();
	};break;
	
	//remove

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

}

?>