<?php

echo GetBaseJavaFunc();

DisplayHeader(".:".ProgramTitle().":.");

if($_REQUEST['profile_id'])
{
	$profile_array = db_request(db_execute("SELECT PROFILE FROM USER_PROFILES WHERE ID='".$_REQUEST['profile_id']."'"));

//print_r($profile_array);
//die('mmmmm');
//print_r($_REQUEST['profile_id']);

	if($profile_array[1]['PROFILE']!=get_user('PROFILE'))
	{
		$old_profile = $_SYSTEM['User'][1]['PROFILE'];
		$_SYSTEM['User'][1]['PROFILE'] = $profile_array[1]['PROFILE'];
		unset($_SYSTEM['Menu']);
		unset($menu);
		$_SYSTEM['force_include'] = true;
		include 'Menu.php';
		$_SYSTEM['User'][1]['PROFILE'] = $old_profile;
	}
}
else
{
	$_SYSTEM['force_include'] = true;
	include('Menu.php');
}

if($_REQUEST['profile_id'])
{
	$exceptions_array = db_request(db_execute("SELECT PROFILE_ID,MODNAME,VIEWER,EDITOR FROM PROFILE_SETTINGS WHERE PROFILE_ID='".$_REQUEST['profile_id']."'"),array(),array('MODNAME'));
	//$featured_array = db_request(db_execute("SELECT PROFILE_ID,MODNAME FROM AVAILABLE_MODPROG WHERE PROFILE_ID='$_REQUEST[profile_id]'"),array(),array('MODNAME'));
}

if($_REQUEST['servicefunction']=='delete' && AllowEdit())
{
	$profile_array = db_request(db_execute("SELECT TITLE FROM USER_PROFILES WHERE ID='$_REQUEST[profile_id]'"));

	if(Prompt(_('Confirm Delete'),_('Do you want to delete the user profile').' <i>'.$profile_array[1]['TITLE'].'</i>?',_('Users of that profile will retain their permissions as a custom set which can be modified on a per-user basis through the User Permissions program.')))
	{
		db_execute("DELETE FROM USER_PROFILES WHERE ID='".$_REQUEST['profile_id']."'");
		db_execute("DELETE FROM USER_SETTINGS WHERE USERNAME IN (SELECT USERNAME FROM USERS WHERE PROFILE_ID='".$_REQUEST['profile_id']."')");
		db_execute("INSERT INTO USER_SETTINGS (USERNAME,MODNAME,VIEWER,EDITOR) SELECT s.USERNAME,e.MODNAME,e.VIEWER,e.EDITOR FROM USERS s,PROFILE_SETTINGS e WHERE s.PROFILE_ID='$_REQUEST[profile_id]' AND s.PROFILE_ID=e.PROFILE_ID");
		db_execute("DELETE FROM PROFILE_SETTINGS WHERE PROFILE_ID='".$_REQUEST['profile_id']."'");
		unset($_REQUEST['servicefunction']);
		unset($_REQUEST['profile_id']);
	}
}

if($_REQUEST['servicefunction']=='update' && AllowEdit() && !$_REQUEST['new_profile_title'] && $_POST)
{
	if($_REQUEST['title'])
		db_execute("UPDATE USER_PROFILES SET TITLE='".str_replace("\'","''",$_REQUEST['title'])."' WHERE ID='$_REQUEST[profile_id]'");

	$app_categories_array = db_request(db_execute("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES WHERE (ID IN (SELECT CATEGORY_ID FROM STUDENT_FIELDS WHERE FOR_APPLICANTS = 'Y')
                             OR ID IN (SELECT CATEGORY_ID FROM STUDENT_FIELDS_IN_CATS WHERE FIELD_ID IN (SELECT FIELD_ID FROM STUDENT_FIELDS WHERE FOR_APPLICANTS = 'Y'))) OR USED_APPLICANTS = 'Y' ORDER BY SORT_ORDER,TITLE"));				
	foreach($app_categories_array as $category)
	{
		$file = 'mod_apply/Applicants.php&category_id='.$category['ID'];
		$_SYSTEM['Menu']['mod_apply'][$file] = ' &nbsp; &nbsp; &rsaquo; '.$category['TITLE'];
	}
	$categories_array = db_request(db_execute("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE"));
	foreach($categories_array as $category)
	{
		$file = 'mod_students/Students.php&category_id='.$category['ID'];
		$_SYSTEM['Menu']['mod_students'][$file] = ' &nbsp; &nbsp; &rsaquo; '.$category['TITLE'];
	}
	$user_categories_array = db_request(db_execute("SELECT ID,TITLE FROM USER_FIELD_CATEGORIES ORDER BY SORT_ORDER,TITLE"));
	foreach($user_categories_array as $category)
	{
		$file = 'mod_users/Users.php&category_id='.$category['ID'];
		$_SYSTEM['Menu']['mod_users'][$file] = ' &nbsp; &nbsp; &rsaquo; '.$category['TITLE'];
	}					
	$resbase_categories_array = db_request(db_execute("SELECT ID,TITLE FROM RESBASE_CATEGORIES ORDER BY SORT_ORDER,TITLE"));
	foreach($resbase_categories_array as $category)
	{
		$file = 'mod_ssetup/SchInventoryCenter.php&category_id='.$category['ID'];
		$_SYSTEM['Menu']['mod_ssetup'][$file] = ' &nbsp; &nbsp; &rsaquo; '.$category['TITLE'];
	}					

	foreach($_SYSTEM['Menu'] as $modcat=>$values)
	{
		foreach($values as $servicemod=>$title)
		{
//		echo $servicemod;
			if(!is_numeric($servicemod) && strpos($servicemod,'&#8658;'))
			{
				foreach($title as $linkUrl=>$linkName)
				{
					if(!is_array($exceptions_array[$linkUrl]) && ((!$_REQUEST['can_edit'][str_replace('.','_',$linkUrl)] && $profile_array[1]['PROFILE']=='admin') || !$_REQUEST['can_use'][str_replace('.','_',$linkUrl)]))
					{
						db_execute("INSERT INTO PROFILE_SETTINGS (PROFILE_ID,MODNAME) values('".$_REQUEST['profile_id']."','$linkUrl')");
					}
					elseif(is_array($exceptions_array[$linkUrl]) && ($_REQUEST['can_edit'][str_replace('.','_',$linkUrl)] || $profile_array[1]['PROFILE']!='admin') && $_REQUEST['can_use'][str_replace('.','_',$linkUrl)])
						db_execute("DELETE FROM PROFILE_SETTINGS WHERE PROFILE_ID='".$_REQUEST['profile_id']."' AND MODNAME='$linkUrl'");
			
					if((!$_REQUEST['can_edit'][str_replace('.','_',$linkUrl)] && $profile_array[1]['PROFILE']=='admin') || !$_REQUEST['can_use'][str_replace('.','_',$linkUrl)])
					{
						$update = "UPDATE PROFILE_SETTINGS SET ";
						if(!$_REQUEST['can_edit'][str_replace('.','_',$linkUrl)] && $profile_array[1]['PROFILE']=='admin')
							$update .= "EDITOR='N',";
						else
							$update .= "EDITOR=NULL,";
						if(!$_REQUEST['can_use'][str_replace('.','_',$linkUrl)])
							$update .= "VIEWER='N'";
						else
							$update .= "VIEWER=NULL";
					
						$update .= " WHERE PROFILE_ID='$_REQUEST[profile_id]' AND MODNAME='$linkUrl'";
						db_execute($update);
					}
				}
			}
			elseif(!is_numeric($servicemod))
			{
				if(!is_array($exceptions_array[$servicemod]) && ((!$_REQUEST['can_edit'][str_replace('.','_',$servicemod)] && $profile_array[1]['PROFILE']=='admin') || !$_REQUEST['can_use'][str_replace('.','_',$servicemod)]))
				{
					db_execute("INSERT INTO PROFILE_SETTINGS (PROFILE_ID,MODNAME) values('".$_REQUEST['profile_id']."','$servicemod')");
				}
				elseif(is_array($exceptions_array[$servicemod]) && ($_REQUEST['can_edit'][str_replace('.','_',$servicemod)] || $profile_array[1]['PROFILE']!='admin') && $_REQUEST['can_use'][str_replace('.','_',$servicemod)])
					db_execute("DELETE FROM PROFILE_SETTINGS WHERE PROFILE_ID='".$_REQUEST['profile_id']."' AND MODNAME='$servicemod'");
			
				if((!$_REQUEST['can_edit'][str_replace('.','_',$servicemod)] && $profile_array[1]['PROFILE']=='admin') || !$_REQUEST['can_use'][str_replace('.','_',$servicemod)])
				{
						$update = "UPDATE PROFILE_SETTINGS SET ";
						if(!$_REQUEST['can_edit'][str_replace('.','_',$servicemod)] && $profile_array[1]['PROFILE']=='admin')
							$update .= "EDITOR='N',";
						else
							$update .= "EDITOR=NULL,";
						if(!$_REQUEST['can_use'][str_replace('.','_',$servicemod)])
							$update .= "VIEWER='N'";
						else
							$update .= "VIEWER=NULL";
					
						$update .= " WHERE PROFILE_ID='$_REQUEST[profile_id]' AND MODNAME='$servicemod'";
						db_execute($update);
				}
			}
		}
	}
	$exceptions_array = db_request(db_execute("SELECT MODNAME,VIEWER,EDITOR FROM PROFILE_SETTINGS WHERE PROFILE_ID='".$_REQUEST['profile_id']."'"),array(),array('MODNAME'));
	//$featured_array = db_request(db_execute("SELECT PROFILE_ID,MODNAME FROM AVAILABLE_MODPROG WHERE PROFILE_ID='$_REQUEST[profile_id]'"),array(),array('MODNAME'));
	unset($_REQUEST['servicefunction']);
	unset($_REQUEST['can_edit']);
	unset($_REQUEST['can_use']);
	unset($_REQUEST['featured']);
	unset($_SESSION['_REQUEST_vars']['servicefunction']);
	unset($_SESSION['_REQUEST_vars']['can_edit']);
	unset($_SESSION['_REQUEST_vars']['can_use']);
	unset($_SESSION['_REQUEST_vars']['featured']);

	foreach($app_categories_array as $category)
	{
		$file = 'mod_apply/Applicants.php&category_id='.$category['ID'];
		unset($_SYSTEM['Menu']['mod_apply'][$file]);
	}
	foreach($categories_array as $category)
	{
		$file = 'mod_students/Students.php&category_id='.$category['ID'];
		unset($_SYSTEM['Menu']['mod_students'][$file]);
	}					
	foreach($user_categories_array as $category)
	{
		$file = 'mod_users/Users.php&category_id='.$category['ID'];
		unset($_SYSTEM['Menu']['mod_users'][$file]);
	}
	foreach($resbase_categories_array as $category)
	{
		$file = 'mod_ssetup/SchInventoryCenter.php&category_id='.$category['ID'];
		unset($_SYSTEM['Menu']['mod_ssetup'][$file]);
	}
	if($profile_array[1]['PROFILE'] == 'student' || $profile_array[1]['PROFILE'] == 'applicant')
	{
	    unset($_SYSTEM['Menu']['mod_users']);
	    unset($_SYSTEM['Menu']['mod_students']);
	    unset($_SYSTEM['Menu']['mod_apply']);
	    unset($_SYSTEM['Menu']['mod_ssetup']);
	}
	elseif($profile_array[1]['PROFILE'] == 'teacher')
	{
	    unset($_SYSTEM['Menu']['mod_users']);
	    unset($_SYSTEM['Menu']['mod_ssetup']);
	}
	if($profile_array[1]['PROFILE'] == 'parent')
	{
	    unset($_SYSTEM['Menu']['mod_users']);
	    unset($_SYSTEM['Menu']['mod_students']);
	    unset($_SYSTEM['Menu']['mod_ssetup']);
	}

}

if($_REQUEST['new_profile_title'] && AllowEdit())
{
	$id = db_request(db_execute("SELECT ".db_seq_nextval('USER_PROFILES_SEQ')." AS ID".FROM_DUAL));
	$id = $id[1]['ID'];
	unset($exceptions_array);
	db_execute("INSERT INTO USER_PROFILES (ID,TITLE,PROFILE) values('$id','".$_REQUEST['new_profile_title']."','".$_REQUEST['new_profile_type']."')");

	$_REQUEST['profile_id'] = $id;
	$profile_array = db_request(db_execute("SELECT PROFILE FROM USER_PROFILES WHERE ID='".$_REQUEST['profile_id']."'"));
	if($profile_array[1]['PROFILE']!=get_user('PROFILE'))
	{
		$old_profile = $_SYSTEM['User'][1]['PROFILE'];
		$_SYSTEM['User'][1]['PROFILE'] = $profile_array[1]['PROFILE'];
		unset($_SYSTEM['Menu']);
		unset($menu);
		$_SYSTEM['force_include'] = true;
		include 'Menu.php';
		$_SYSTEM['User'][1]['PROFILE'] = $old_profile;
	}
	unset($_REQUEST['new_profile_title']);
	unset($_SESSION['_REQUEST_vars']['new_profile_title']);
	unset($_REQUEST['new_profile_type']);
	unset($_SESSION['_REQUEST_vars']['new_profile_type']);
}

if($_REQUEST['servicefunction']!='delete')
{
	if($_SYSTEM['direction'])
		$arrow_right = 'arrow_left';
	else
		$arrow_right = 'arrow_right';
	echo "<FORM action=modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=update&profile_id=$_REQUEST[profile_id] method=POST>";
	DisplayHeader(_('Select the profile for the user, than select categories of information which can be used for viewing or editing by this profile.'),'<INPUT style="width:100px;" type=submit value='._('Save').'>');
	echo '<BR>';
	echo '<TABLE width=100%><TR><TD valign=top width=200>';
	echo '<TABLE border=0 cellpadding=0 cellspacing=0 width=100%>';
	$profiles_array = db_request(db_execute("SELECT ID,TITLE,PROFILE FROM USER_PROFILES"));
	echo '<TR><TD colspan=3 style="border-width:1; border-style: none none solid none;"><b>'._('List of User Profiles').'</b></TD></TR>';
	if(count($profiles_array))
	{
		foreach($profiles_array as $profile)
		{
			if($profile['ID']==$_REQUEST['profile_id'])
			{
				echo '<TR id=selected_tr onmouseover="" onmouseout="" bgcolor="#'.Preferences('HIGHLIGHT').'" style="color:white;"><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('remove','',"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&profile_id=$profile[ID]",20).'</TD><TD '.$style.' onclick="document.location.href=\'modloader.php?servicemod='.$_REQUEST['servicemod'].'&profile_id='.$profile['ID'].'\';">';
				$this_profile_title = $profile['TITLE'];
			}
			else
				echo '<TR onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="background-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('remove','',"modloader.php?servicemod=$_REQUEST[servicemod]&servicefunction=delete&profile_id=$profile[ID]",20).'</TD><TD'.$style.' onclick="document.location.href=\'modloader.php?servicemod='.$_REQUEST['servicemod'].'&profile_id='.$profile['ID'].'\';">';
			echo '<A style="cursor:pointer;"><small>'.$profile['TITLE'].' &nbsp; </small></A>';
			echo '</TD>';
			echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=images/'.$arrow_right.'.gif></A></TD>';
			echo '</TR>';
			$style = ' style="border-color:black;border-width:1; border-style: dashed none none none;"';
		}
	}
	if(!count($profiles_array) || !$_REQUEST['profile_id'])
		echo '<TR id=selected_tr><TD height=0></TD></TR>';

	echo '<TR id=new_tr onmouseover=\'this.style.backgroundColor="#'.Preferences('HIGHLIGHT').'"; this.style.color="white";\' onmouseout=\'this.style.cssText="background-color:transparent; color:black;";\'><TD width=20 align='.ALIGN_RIGHT.$style.'>'.button('add','','',20).'</TD><TD'.$style.'>';
	echo '<A style="text-decoration:none;cursor:pointer;" onclick=\'document.getElementById("selected_tr").onmouseover="this.style.backgroundColor=\"#'.Preferences('HIGHLIGHT').'\"; this.style.color=\"white\";"; document.getElementById("selected_tr").onmouseout="this.style.cssText=\"background-color:transparent; color:black;\";"; document.getElementById("selected_tr").style.cssText="background-color:transparent; color:black;"; document.getElementById("new_id_div").style.display="block";document.getElementById("new_title_input").focus();document.getElementById("main_div").style.display="none";document.getElementById("new_tr").onmouseover="";document.getElementById("new_tr").onmouseout="";this.onclick="";\'><small> '._('Add a User Profile').' &nbsp;<BR><DIV id=new_id_div style="display:none;">'._('Title').' <INPUT type=text id=new_title_input name=new_profile_title><BR>'._('Type').'<SELECT name=new_profile_type><OPTION value=admin>'._('Administrator').'</OPTION><OPTION value=teacher>'._('Teacher').'</OPTION><OPTION value=parent>'._('Parents').'</OPTION><OPTION value=student>'._('Students').'</OPTION>';
	if($_iModules['mod_apply'] == true)
		echo '<OPTION value=applicant>'._('Applicants').'</OPTION>';
	echo '</SELECT></DIV> </small></A>';
	echo '</TD>';
	echo '<TD'.$style.'><A style="cursor:pointer;"><IMG SRC=images/'.$arrow_right.'.gif></A></TD>';
	echo '</TR>';
	
	echo '</TABLE>';
	echo '</TD><TD width=75%>';
	echo '<DIV id=main_div>';
	if($_REQUEST['profile_id'])
	{
		// PopTable('header') ... PopTable('footer') is copied directly from mod_users/Permissions (except for the poptable title)
		PopTable('header',_('Permissions'),'width=80%');
		echo '<center><h3>'.TextInput($this_profile_title,'title').'</h3></center>';
		echo '<TABLE border=0 valign=top cellspacing=0">';
			
		$count_color=0;
		foreach($_SYSTEM['Menu'] as $modcat=>$values)
		{
			$count_color++;
			if($profile_array[1]['PROFILE']=='admin')
				$prefix = $modcat;
			else
			{
				switch($modcat)
				{
					case 'My_School':
						$prefix = 'mod_ssetup';
					break;
	
					case 'My_Info':
						$prefix = 'mod_users';
					break;
					
					case 'My_Child':
						$prefix = 'mod_students';
					break;
					
					default:
						$prefix = $modcat;
					break;
				}
			}

			$localized_modcat = getModcat($modcat);
			if($count_color%2!=0)
			{
				echo '<TR>';
				echo '<TD width=380 valign=top height=20 align=center style="border-color:gray;border-width:1px;border-style:none solid none none;"><HR style="color:black;">'.DrawPNG($_SYSTEM['icons'][$prefix],'height=20px','').' '.$localized_modcat."<div style='text-align:right;'><small><font color=gray>"._('Viewer')."<INPUT type=checkbox name=controller_can_use_$prefix onclick='checkAll(this.form,this.form.controller_can_use_$prefix.checked,\"can_use[$prefix\");'></font></small>".(($profile_array[1]['PROFILE']=='admin')?" &nbsp;<small><font color=gray>"._('Editor')."<INPUT type=checkbox name=controller_can_edit_$prefix onclick='checkAll(this.form,this.form.controller_can_edit_$prefix.checked,\"can_edit[$prefix\");'></font></small></div>":'');
			}else{
				echo '<TD width=380 valign=top height=20 align=center><HR style="color:black;">'.DrawPNG($_SYSTEM['icons'][$prefix],'height=20px','').' '.$localized_modcat."<div style='text-align:right;'><small><font color=gray>"._('Viewer')."<INPUT type=checkbox name=controller_can_use_$prefix onclick='checkAll(this.form,this.form.controller_can_use_$prefix.checked,\"can_use[$prefix\");'></font></small>".(($profile_array[1]['PROFILE']=='admin')?" &nbsp;<small><font color=gray>"._('Editor')."<INPUT type=checkbox name=controller_can_edit_$prefix onclick='checkAll(this.form,this.form.controller_can_edit_$prefix.checked,\"can_edit[$prefix\");'></font></small></div>":'');
			}
			if(count($values))
			{
				foreach($values as $file=>$title)
				{
					$can_edit = $can_use = 'CHECKED';
					if($exceptions_array[$file][1]['EDITOR']=='N')
						$can_edit = '';
					if($exceptions_array[$file][1]['VIEWER']=='N')
						$can_use = '';
	
					if($bgcolor==Preferences('COLOR'))
						$bgcolor = Preferences('HIGHLIGHT_BACKGROUND');
					else
						$bgcolor = Preferences('COLOR');
					if(!is_numeric($file) && strpos($file,'&#8658;'))
					{
						$stitle = '';
						//обработка названия строки, удаление ненужных символов
						$stitle = str_replace('_',' ',substr($file,0,strpos($file,' &#8658;')));
						$spec_title = str_replace('_',' ',$file);

						echo '<div><table cellspacing=0 width=100%><tr><TD bgcolor=#FFFFFF colspan=3 align=left><BR><small><i>'.$stitle.'</i></small></TD></tr></table></div>';
						foreach($title as $linkUrl=>$linkName)
						{

						    if($exceptions_array[$linkUrl][1]['EDITOR']=='N')
								$can_edit = '';
						    if($exceptions_array[$linkUrl][1]['VIEWER']=='N')
								$can_use = '';

    						echo "<div><table cellspacing=0 width=100%><TR><TD bgcolor=#".$bgcolor."> &nbsp; &nbsp; &rsaquo; $linkName</TD><TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_use[".str_replace('.','_',$linkUrl)."] value=true $can_use></TD>";
						    if($profile_array[1]['PROFILE']=='admin')
								echo "<TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_edit[".str_replace('.','_',$linkUrl)."] value=true $can_edit></TD></TR></table></div>";
							else
								echo "</TR></table></div>";
						    $can_use = $can_edit = 'CHECKED';
						    
						    if($bgcolor==Preferences('COLOR'))
								$bgcolor = Preferences('HIGHLIGHT_BACKGROUND');
						    else
								$bgcolor = Preferences('COLOR');

						}
						                                                                                                                
					}elseif(!is_numeric($file)){
						echo "<div><table cellspacing=0 width=100%><TR><TD bgcolor=#".$bgcolor."> &nbsp; &nbsp;$title</TD><TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_use[".str_replace('.','_',$file)."] value=true $can_use></TD>";
						if($profile_array[1]['PROFILE']=='admin')
							echo "<TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_edit[".str_replace('.','_',$file)."] value=true $can_edit></TD></TR></table></div>";
						else
							echo "</TR></table></div>";
					}
					else
						echo '<div><table cellspacing=0 width=100%><TR><TD bgcolor=#FFFFFF colspan=3 align=left><BR><small><i>'.$title.'</i></small></TD></TR></table></div>';
					if($file=='mod_students/Students.php' || $file=='mod_users/Users.php' || $file=='mod_ssetup/SchInventoryCenter.php' || $file=='mod_apply/Applicants.php')
					{
						if($file=='mod_users/Users.php')
							$categories_table = 'USER_FIELD_CATEGORIES';
						elseif($file=='mod_students/Students.php' || $file=='mod_apply/Applicants.php')
							$categories_table = 'STUDENT_FIELD_CATEGORIES';
						else
							$categories_table = 'RESBASE_CATEGORIES';
				
						$students_program = $file;
						if($file=='mod_apply/Applicants.php')
							$categories_array = db_request(db_execute("SELECT ID,TITLE FROM STUDENT_FIELD_CATEGORIES WHERE (ID IN
				                                 (SELECT CATEGORY_ID FROM STUDENT_FIELDS WHERE FOR_APPLICANTS = 'Y')
                                                                 OR ID IN (SELECT CATEGORY_ID FROM STUDENT_FIELDS_IN_CATS WHERE FIELD_ID IN
                                                                 (SELECT FIELD_ID FROM STUDENT_FIELDS WHERE FOR_APPLICANTS = 'Y'))) OR USED_APPLICANTS = 'Y'
                                                                 ORDER BY SORT_ORDER,TITLE"));				
                                                else
							$categories_array = db_request(db_execute("SELECT ID,TITLE FROM ".$categories_table." ORDER BY SORT_ORDER,TITLE"));
						foreach($categories_array as $category)
						{
							if($bgcolor==Preferences('COLOR'))
								$bgcolor = Preferences('HIGHLIGHT_BACKGROUND');
							else
								$bgcolor = Preferences('COLOR');
							$file = $students_program.'&category_id='.$category['ID'];
							$can_edit = $can_use = 'CHECKED';
							if($exceptions_array[$file][1]['EDITOR']=='N')
								$can_edit = '';
							if($exceptions_array[$file][1]['VIEWER']=='N')
								$can_use = '';

							$title = ' &nbsp; &nbsp; &rsaquo; '.$category['TITLE'];
							echo "<div><table cellspacing=0 width=100%><TR><TD bgcolor=#".$bgcolor."> &nbsp; $title</TD><TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_use[".str_replace('.','_',$file)."] value=true $can_use></TD>";
							if($profile_array[1]['PROFILE']=='admin')
								echo "<TD width=20% align=center bgcolor=#".$bgcolor."><INPUT type=checkbox name=can_edit[".str_replace('.','_',$file)."] value=true $can_edit></TD></TR></table></div>";
							else
								echo "</TR></table></div>";
						}
					}
				}
			}
			echo '</TD>';
			if($count_color%2==0)
				echo '</TR>';
		}
		echo '</TABLE>';
		echo '<TR><TD colspan=5 align=center><INPUT style="width:100px;" type=submit value='._('Save').'></TD></TR>';
		PopTable('footer');
	}
	echo '</DIV>';
	echo '</TD></TR></TABLE>';
	echo '</FORM>';
}
?>