<?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 category() {
	//access control
	global $prefixCms;
	include('core/class/class.typeone.php');
	funAdminAccess('OP');
	
	addTitleArea('category');
	
	//finding category names
	$reCategory = mysql_query("
	SELECT idCommon, nameCategory 
	FROM ".$prefixCms."_groupcategory 
	WHERE language = '".getCmsAdmLang()."'
	ORDER BY nameCategory");
	
	echo '<div class="stdBlock">';
	
	$tableGroup = new typeOne(0);
	
	$tableGroup->OpenTable(_CATEGORYLIST);
	
	$modAcc = funAdminAccess('MOD', true);
	$remAcc = funAdminAccess('REM', true);
	
	$head = array(_FIELDNAME);
	$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, $nameCategory) = mysql_fetch_row($reCategory))  {
		/*echo '<div class="line'.($i++%2 == 0 ? 'col' : '').'">'
			.'<div class="floatRight">'
			//mod link
			.'<a href="admin.php?modulename=category&amp;op=newinfocategory&amp;idCommon='.$idCommon.'">'
			.'<img src="'.getPathImage().'standard/mod.gif" alt="'._MODIFIYN.'" /></a>'
			//rem link
			.'<a href="admin.php?modulename=category&amp;op=remcategory&amp;idCommon='.$idCommon.'">'
			.'<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVEN.'" /></a>'
			.'</div>'
			
			.$nameCategory
			.'</div>';*/
		$data = array($nameCategory);
				
		
		if($modAcc) $data[] = '<a href="admin.php?modulename=category&amp;op=newinfocategory&amp;idCommon='.$idCommon.'">
				<img src="'.getPathImage().'standard/mod.gif" alt="'._MODIFIYN.'" /></a>';
		if($remAcc) $data[] = '<a href="admin.php?modulename=category&amp;op=delcategory&amp;idCommon='.$idCommon.'">
				<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVE.'" /></a>';
		
		$tableGroup->WriteRow($data);
	}
	
	if(funAdminAccess('NEW', true)) {
		$tableGroup->WriteAddRow('<a href="admin.php?modulename=category&amp;op=newcategory" title="'._ADDELEM.'">
								<img src="'.getPathImage().'standard/add.gif" alt="'._ADD.'" /> '._ADDELEMENT.'</a>');
	}
	$tableGroup->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 newcategory() {
	//access control
	funAdminAccess('NEW');
	
	//title of the area
	addTitleArea('category');
	
	$language = giveLanguageList();
	
	echo '<div class="stdBlock">'
		.'<b>'._HOWTO.'</b><br /><br />';
	echo '<form method="post" action="admin.php?modulename=category&amp;op=inscategory">';
		
		while(list(, $lang) = each($language)) { 
			echo '<div class="lineInput"><div class="words">'.ucfirst($lang).'</div>'
				.'<input class="inputText" type="text" name="nameCateg['.$lang.']" maxlength="100" /></div>';
		}
		echo '<div class="lineInput"><input type="submit" value="'._INSERT.'" /></div>'
		.'</form>';
	
	echo '</div>';
}

function inscategory() { 
	//REQUIRES: at least one name passed in array nameCateg
	//EFFECTS : insert a category
	//MODIFIES: $prefixCms_groupcategory
	
	//access control
	funAdminAccess('NEW');
	global $prefixCms;
	
	addTitleArea('category');
	$first = true;
	$idCommon = 0;
	while(list($lang, $name) = each($_POST['nameCateg'])) { 
		
		//if is the first insert
		if($first) {
			
			//if the name is not empty
			if($name != '') {
				
				//the category exists ?
				if(mysql_num_rows(mysql_query("SELECT idCategory FROM ".$prefixCms."_groupcategory WHERE language = '$lang' AND nameCategory = '$name'"))) {
					
					//the category exists !
					errorCommunication(_ALREDYINSERT);
					return;
				}
				
				//insert category
				mysql_query("INSERT INTO ".$prefixCms."_groupcategory SET language = '$lang', nameCategory = '$name'");
				
				//find primary key insert id
				list($idCommon) = mysql_fetch_row(mysql_query("SELECT idCategory FROM ".$prefixCms."_groupcategory WHERE language = '$lang' AND nameCategory = '$name'"));
				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."_groupcategory SET idCommon = idCategory WHERE idCategory = '$idCommon'")) {
					
					//setting of idCommon fails => category removed
					mysql_query("DELETE FROM ".$prefixCms."_groupcategory WHERE idCategory = '$idCommon'");
					errorCommunication(_CRITICALERREXIT);
					return;
				}
				//the first element insert was succesful!
				$first = false;
			}
		}
		else {
			if($name != '') {
				mysql_query("
				INSERT INTO ".$prefixCms."_groupcategory 
				SET language = '$lang',
					nameCategory = '$name',
					idCommon = '$idCommon'");
			}
		}
	}
	Header("Location:admin.php?modulename=category&op=category");
}

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

	$languages = giveLanguageList();
	
	//finding category names
	$reCategory = mysql_query("
	SELECT language, nameCategory 
	FROM ".$prefixCms."_groupcategory 
	WHERE idCommon = '".$_GET['idCommon']."'");
	
	while(list($lang, $nameCategory) = mysql_fetch_row($reCategory))  {
		$nameCat[$lang] = $nameCategory;
	}
	
	echo '<div class="stdBlock">'
		.'<b>'._NOEMPTY.'</b><br /><br />'
		.'<form method="post" action="admin.php?modulename=category&amp;op=upcategory">'
		.'<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="nameCateg['.$lang.']" maxlength="100" value="'.$nameCat[$lang].'" />'
			.' ( '.$nameCat[$lang].' )'
			.'</div>';
	}
	echo '<div class="lineInput"><input type="submit" value="'._UPDATE.'" /></div>'
		.'</form>';
	
	echo '</div>';
}

function upcategory() {
	//REQUIRES: at least one name passed in array nameCateg[]
	//EFFECTS : modify a category
	//MODIFIES: $prefixCms_groupcategory
	
	//access control
	funAdminAccess('MOD');
	global $prefixCms;
	
	addTitleArea('category');
	$first = true;
	$idCommon = 0;
	while(list($lang, $name) = each($_POST['nameCateg'])) { 
		
		//if the name is not empty
		if($name == '') $name = _NOTITLE;
			
			//the category exists ?
		if(mysql_num_rows(mysql_query("
			SELECT idCategory 
			FROM ".$prefixCms."_groupcategory 
			WHERE language = '$lang' AND idCommon='".$_POST['idCommon']."'"))) {
				
				//update existing category
				mysql_query("
				UPDATE ".$prefixCms."_groupcategory 
				SET nameCategory = '$name' 
				WHERE language = '$lang' AND idCommon = '".$_POST['idCommon']."'");
		}
		else {
			//insert inexistentcategory
			mysql_query("
			INSERT INTO ".$prefixCms."_groupcategory 
			SET language = '$lang',
				nameCategory = '$name',
				idCommon = '".$_POST['idCommon']."'");
		}
	}
	Header("Location:admin.php?modulename=category&op=newinfocategory&idCommon=".$_POST['idCommon']);
}

function dependenceDrop($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='dropdown' AND t1.language = '".getCmsAdmLang()."') 
		OR 
	( t2.idResource = '$idC' AND t2.type='dropdown' 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 remcategory() {
	//access control
	global $prefixCms;
	funAdminAccess('REM');
	
	addTitleArea('category');
	
	//finding category names
	$reCategory = mysql_query("
	SELECT idCommon, nameCategory 
	FROM ".$prefixCms."_groupcategory 
	WHERE language = '".getCmsAdmLang()."'
	ORDER BY nameCategory");
	
	echo '<div class="stdBlock">'
		.'<div class="title">'._CATEGORYLIST.'</div><br />'
		.'<div class="line"></div>';
	
	

	$i = 0;
	while(list($idCommon, $nameCategory) = mysql_fetch_row($reCategory))  {
		if($idCommon == $_GET['idCommon']) {
			echo '<div class="lineSelected">'
				.'<img src="'.getPathImage().'standard/sel.gif" alt="'._REMOVESEl.'" />'
				.$nameCategory
				.'</div>';
		}
		else {
			echo '<div class="line'.($i++%2 == 0 ? 'col' : '').'">'
				.'<a href="admin.php?modulename=category&amp;op=remcategory&amp;idCommon='.$idCommon.'">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'._REMOVEN.'" /></a>'
				.$nameCategory
				.'</div>';
		}
	}

	//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 delcategory() { 
	//REQUIRES: idCommon passed as $_GET
	//EFFECTS : delete a category
	//MODIFIES: $prefixCms_groupcategory
	
	//access control
	funAdminAccess('REM');
	global $prefixCms;
	
	addTitleArea('category');
	
	$related = dependenceDrop($_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."_groupcategory_voice WHERE idCommon = '".$_GET['idCommon']."'")) {
				//error occurred while removing
				errorCommunication(_REMOVEERR);
				return;
			}
			elseif(!mysql_query("DELETE FROM ".$prefixCms."_groupcategory WHERE idCommon = '".$_GET['idCommon']."'")) {
				//error occurred while removing
				errorCommunication(_REMOVEERR);
				return;
			}
		}
		else {
			//idCommon doesn't exists
			errorCommunication(_NOSELECT);
			return;
		}
		Header("Location:admin.php?modulename=category&op=category");
	}
	else {
		$reCategory = mysql_query("
		SELECT nameCategory 
		FROM ".$prefixCms."_groupcategory 
		WHERE language = '".getCmsAdmLang()."' AND idCommon = '".$_GET['idCommon']."'
		ORDER BY nameCategory");

		list($nameCategory) = mysql_fetch_row($reCategory);
		echo '<div class="stdBlock">'
			.'<div class="remRequest">';
		
		echo _REMOVETHISONE.'<br /><br />'
			._FIELDNAME.' : <span class="redFont">'.$nameCategory.'</span><br /><br />'
			
			.'[ <a href="admin.php?modulename=category&amp;op=delcategory&amp;idCommon='.$_GET['idCommon'].'&amp;confirm=1" '
			.'title="'._YESGTIT.'">'._YESG.'</a> | '
			
			.'<a href="admin.php?modulename=category&amp;op=category" '
			.'title="'._NOGITIT.'">'._NOG.'</a> ]';
		
		echo '</div>'
			.'</div>';
	}

}

switch($op) {
	case "category" : {
		category();
	};break;
	
	case "newcategory" : {
		newcategory();
	};break;
	case "inscategory" : {
		inscategory();
	};break;
	
	case "newinfocategory" : {
		newinfocategory();
	};break;
	case "upcategory" : {
		upcategory();
	};break;
	
	case "delcategory" : {
		delcategory();
	};break;
}

}

?>