<?
if (!defined('IN_BANDTOPIA'))
{
	exit;
}

	if(ADMIN_FULL == "admin" || ADMIN_USER == "user") 
	{
		$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=user&function=userlist">User List</a>';
		/******************************************************/
		/*  User List Page
		/******************************************************/
		if($_GET['function'] == 'userlist' || $_GET['function'] == '') 
		{
			$userObj = new UserObj();
			$userlist = $userObj -> list_users();
			$template -> assign_block_vars("user_list",array());
			if(count($userlist) > 0)
			{
				foreach($userlist as $key => $value)
				{
					$template -> assign_block_vars("user_list.user", array(
						"USER_TABLE_ID" => $value->get_user_table_id(),
						"USERNAME" => $value->get_username(),
						"USER_FULLNAME" => $value->get_user_fullname(),
						"USER_TYPE_ID" => $value->get_user_type_id(),
						"USER_TYPE_TITLE" => $value->get_user_type_title(),
						"INFO_NAME" => $value->get_info_name()));
					
					$userSecyObj = new UserSecyObj();
					$userSecyObj->set_user_table_id($value->get_user_table_id());
					$userSecyList = $userSecyObj->listUserSecy();
					foreach($userSecyList as $key2 => $value2)
					{
						if($key2 == 0)
						{
							$start = "";
						}
						else
						{
							$start = " - ";
						}
						$template -> assign_block_vars("user_list.user.user_secy", array(
							"USER_SECY_ID" => $value2->get_user_secy_id()." boo",
							"SECY_CMPT_NAME" => $value2->get_secy_cmpt_name(),
							"USER_SECY_START" => $start));
					}
				}
			}
			$template -> assign_var_from_handle('USER_ADMIN_PAGE', 'useradminpage');
		}
		/******************************************************/
		/*  User View/Edit Page
		/******************************************************/
		elseif($_GET['function'] == 'userview' || $_GET['function'] == 'useredit' || $_GET['function'] == 'useradd')
		{
			if($_POST['post_form'] == 'true' && ($_POST['useradd'] == 'true' || $_POST['user_table_id'] != ''))
			{
				$userObj = new UserObj();
				$userObj->set_user_table_id($_POST['user_table_id']);
				$userObj->set_username($_POST['username']);
				$userObj->set_password(EncryptPass($_POST['password']));
				$userObj->set_user_fullname($_POST['user_fullname']);
				$userObj->set_user_type_id($_POST['user_type_id']);
				$userObj->set_user_info_id($_POST['user_info_id']);
				$userMsg = $userObj->updateUser();
				
				$secyCmptObj = new SecyCmptObj();
				$secyCmptList = $secyCmptObj->listCmpt();
				foreach($secyCmptList AS $key => $value)
				{
					$userSecyObj = new UserSecyObj();
					$userSecyObj->set_user_table_id($userObj->get_user_table_id());
					$userSecyObj->set_secy_cmpt_id($value->get_secy_cmpt_id());
					$permission = $userSecyObj->getCmptPermission();
					if($_POST['secy_cmpt_id_'.$value->get_secy_cmpt_id()] == $value->get_secy_cmpt_id() && $permission == false)
					{
						$userSecyObj->set_action("insert");
						$userSecyObj->updateCmptPermission();
					}
					elseif($_POST['secy_cmpt_id_'.$value->get_secy_cmpt_id()] != $value->get_secy_cmpt_id() && $permission == true)
					{
						$userSecyObj->set_action("remove");
						$userSecyObj->updateCmptPermission();
					}
				}
				
				$template -> assign_block_vars("user_msg", array(
					"USER_MSG" => $userMsg));
			}
			else
			{
				if($_GET['function'] == 'useradd')
				{
					$template -> assign_block_vars("user_view", array(
						"DISABLED" => " DISABLED"));
					$template -> assign_block_vars("user_view.edit", array());
					$template -> assign_block_vars("user_view.add", array());
					$template -> assign_block_vars("user_pass_script", array());
					$usertypeObj = new UserTypeObj();
					$usertypeList = $usertypeObj->UserTypeList();
					foreach($usertypeList as $key => $value) {
						$template -> assign_block_vars("user_view.edit.user_type", array(
							"USER_TYPE_ID" => $value->get_user_type_id(),
							"USER_TYPE_TITLE" => $value->get_user_type_title()));
					}
					
					$selected = "false";
					
					//  Pull in Artistlist
					$artistObj = new ArtistObj();
					$artistList = $artistObj->list_artists();
					foreach($artistList AS $key => $value)
					{
						$template -> assign_block_vars("artist_list", array(
							"ARTIST_ID" => $value->get_artist_id(),
							"ARTIST_NAME" => $value->get_artist_name(),
							"SELECTED" => $selected));
					}
					
					//  Pull in Stafflist
					$staffObj = new StaffObj();
					$staffList = $staffObj->listStaff();
					foreach($staffList AS $key => $value)
					{
						$template -> assign_block_vars("staff_list", array(
							"STAFF_ID" => $value->get_staff_id(),
							"STAFF_NAME" => $value->get_staff_name(),
							"SELECTED" => $selected));
					}
					
					//  Pull in Agentlist
					$agentObj = new AgentObj();
					$agentList = $agentObj->listAgent();
					foreach($agentList AS $key => $value)
					{
						$template -> assign_block_vars("agent_list", array(
							"AGENT_ID" => $value->get_agent_id(),
							"AGENT_NAME" => $value->get_agent_name(),
							"SELECTED" => $selected));
					}
					
					$secyCmptObj = new SecyCmptObj();
					$secyCmptList = $secyCmptObj->listCmpt();
					foreach($secyCmptList AS $key => $value)
					{
						$template -> assign_block_vars("user_view.secy_cmpt", array(
							"SECY_CMPT_ID" => $value->get_secy_cmpt_id(),
							"SECY_CMPT_NAME" => $value->get_secy_cmpt_name()));
						$template -> assign_block_vars("user_view.secy_cmpt.edit", array());
					}
				}
				elseif($_GET['user_table_id'] == '' && $_POST['user_table_id'] == '')
				{
					header("Location: index.php?action=user&function=userlist");
				}
				else
				{	
					$userObj = new UserObj();
					$userObj->set_user_table_id($_GET['user_table_id']);
					$userObj->getUser();
					if($userObj->get_username() == '')
					{
						$template -> assign_block_vars("page_error",array());
						$template -> assign_block_vars("page_error.error_msg", array(
							"ERROR_MSG" => "No user found with the provided id.  Please try again."));
					}
					else
					{
						$template -> assign_block_vars("user_view", array(
							"USER_TABLE_ID" => $userObj->get_user_table_id(),
							"USERNAME" => $userObj->get_username(),
							"USER_FULLNAME" => $userObj->get_user_fullname(),
							"USER_TYPE_ID" => $userObj->get_user_type_id(),
							"USER_TYPE_TITLE" => $userObj->get_user_type_title(),
							"INFO_NAME" => $userObj->get_info_name()));
							
						//  Pull in Artistlist
						$artistObj = new ArtistObj();
						$artistList = $artistObj->list_artists();
						foreach($artistList AS $key => $value)
						{
							if($userObj->get_user_info_id() == $value->get_artist_id() && $userObj->get_user_type_title() == "Artist")
							{
								$selected = "true";
							}
							else
							{
								$selected = "false";
							}
							$template -> assign_block_vars("artist_list", array(
								"ARTIST_ID" => $value->get_artist_id(),
								"ARTIST_NAME" => $value->get_artist_name(),
								"SELECTED" => $selected));
						}
						
						//  Pull in Stafflist
						$staffObj = new StaffObj();
						$staffList = $staffObj->listStaff();
						foreach($staffList AS $key => $value)
						{
							if($userObj->get_user_info_id() == $value->get_staff_id() && $userObj->get_user_type_title() == "Staff")
							{
								$selected = "true";
							}
							else
							{
								$selected = "false";
							}
							$template -> assign_block_vars("staff_list", array(
								"STAFF_ID" => $value->get_staff_id(),
								"STAFF_NAME" => $value->get_staff_name(),
								"SELECTED" => $selected));
						}
						
						//  Pull in Agentlist
						$agentObj = new AgentObj();
						$agentList = $agentObj->listAgent();
						foreach($agentList AS $key => $value)
						{
							if($userObj->get_user_info_id() == $value->get_agent_id() && $userObj->get_user_type_title() == "Agent")
							{
								$selected = "true";
							}
							else
							{
								$selected = "false";
							}
							$template -> assign_block_vars("agent_list", array(
								"AGENT_ID" => $value->get_agent_id(),
								"AGENT_NAME" => $value->get_agent_name(),
								"SELECTED" => $selected));
						}
							
						$secyCmptObj = new SecyCmptObj();
						$secyCmptList = $secyCmptObj->listCmpt();
						foreach($secyCmptList AS $key => $value)
						{
							$userSecyObj = new UserSecyObj();
							$userSecyObj->set_user_table_id($userObj->get_user_table_id());
							$userSecyObj->set_secy_cmpt_id($value->get_secy_cmpt_id());
							$permission = $userSecyObj->getCmptPermission();
							if($permission != true)
							{
								$checked = "";
								$imgCheck = "<img src='images/gray_x.png'>";
							}
							else
							{
								$checked = " CHECKED";
								$imgCheck = "<img src='images/grn_check.png'>";
							}
							$template -> assign_block_vars("user_view.secy_cmpt", array(
								"SECY_CMPT_ID" => $value->get_secy_cmpt_id(),
								"SECY_CMPT_NAME" => $value->get_secy_cmpt_name(),
								"CHECKED" => $checked,
								"IMGCHECK" => $imgCheck));
							
							if($_GET['function'] == 'useredit') 
							{
								$template -> assign_block_vars("user_view.secy_cmpt.edit", array());
							}
							else
							{
								$template -> assign_block_vars("user_view.secy_cmpt.view", array());
							}
						}
						if($_GET['function'] == 'useredit') 
						{
							$template -> assign_block_vars("user_view.edit", array());
							$usertypeObj = new UserTypeObj();
							$usertypeList = $usertypeObj->UserTypeList();
							foreach($usertypeList as $key => $value) {
								if($value->get_user_type_id() == $userObj->get_user_type_id())
								{
									$selected = " SELECTED";
								}
								else
								{
									$selected = "";
								}
								$template -> assign_block_vars("user_view.edit.user_type", array(
									"USER_TYPE_ID" => $value->get_user_type_id(),
									"USER_TYPE_TITLE" => $value->get_user_type_title(),
									"SELECTED" => $selected));
							}
							$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=user&function=userview&user_table_id='.$_GET['user_table_id'].'">User View</a>';
						}
						else
						{
							$template -> assign_block_vars("user_view.view", array());
						}
					}
				}
			}
			$template -> assign_var_from_handle('USER_ADMIN_PAGE', 'useradminpage');
		}
		/******************************************************/
		/*  User Password Change Page
		/******************************************************/
		elseif($_GET['function'] == 'userpass')
		{
			$nav .= '&nbsp;&gt;&nbsp;<a href="index.php?action=user&function=userview&user_table_id='.$_GET['user_table_id'].'">User View</a>';
			if($_GET['user_table_id'] == '' && $_POST['user_table_id'] == '')
			{
				$template -> assign_block_vars("page_error",array());
				$template -> assign_block_vars("page_error.error_msg", array(
					"ERROR_MSG" => "No user provided.  Please try again."));
			}
			elseif($_POST['post_form'] == 'true' && $_POST['user_table_id'] != '')
			{
				$userObj = new UserObj();
				$userObj->set_user_table_id($_POST['user_table_id']);
				$userObj->set_password(EncryptPass($_POST['password']));
				$userMsg = $userObj->updatePass();
				$template -> assign_block_vars("user_msg", array(
					"USER_MSG" => $userMsg));
			}
			else
			{
				$userObj = new UserObj();
				$userObj->set_user_table_id($_GET['user_table_id']);
				$userObj->getUser();
				if($userObj->get_username() == '')
				{
					$template -> assign_block_vars("page_error",array());
					$template -> assign_block_vars("page_error.error_msg", array(
						"ERROR_MSG" => "No user found with the provided id.  Please try again."));
				}
				else
				{
					$template -> assign_block_vars("user_pass", array(
						"USER_TABLE_ID" => $userObj->get_user_table_id(),
						"USERNAME" => $userObj->get_username(),
						"USER_FULLNAME" => $userObj->get_user_fullname()));
					$template -> assign_block_vars("user_pass_script", array());
				}
			}
			$template -> assign_var_from_handle('USER_ADMIN_PAGE', 'useradminpage');
		}
		/******************************************************/
		/*  User Del(Deactivate)
		/******************************************************/
		elseif($_GET['function'] == 'userdel')
		{
			if($_GET['user_table_id'] == '')
			{
				$template -> assign_block_vars("page_error",array());
				$template -> assign_block_vars("page_error.error_msg", array(
					"ERROR_MSG" => "No user provided.  Please try again."));
			}
			else
			{
				$userObj = new UserObj();
				$userObj->set_user_table_id($_GET['user_table_id']);
				$userMsg = $userObj->delUser();
				$template -> assign_block_vars("user_msg", array(
					"USER_MSG" => $userMsg));
			}
			$template -> assign_var_from_handle('USER_ADMIN_PAGE', 'useradminpage');
		}
	} 
	else
	{
		$template -> assign_block_vars("page_error",array());
		$template -> assign_block_vars("page_error.permission", array());
	}
?>