Location: PHPKode > projects > MySonar > mysonar-gui-1.0/controller.php
<?php
/**
 * mySonar GUI - Version 1.0
 * mySonar (reachable under mySonar.de) is a platform for Location Based Mobile Networking. 
 * Users can find friends, flirts, partys and places arround by uses their mobile devices.
 * The application has several components. One of the components is the graphical user interface 
 * to access a XML Web Service layer, the services, kernel and database behind. This source file
 * belongs to that component.
 * 
 * Copyright (C) 2008 mySonar GbR, Markus Heussen
 * 
 * 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; only GPLv2.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; 
 * if not, see <http://www.gnu.org/licenses/>.
 */

defined("ACCESS") or die("Direct access to this resource is not allowed.");
session_start();

// set API key if available
if ($_GET["APIKey"]!="") $_SESSION["APIKey"] = $_GET["APIKey"];

require_once("classes-1.0.php");
require_once("mysonar-1.0.php");
require_once("model-1.0.php");
require_once("view-1.0.php");

// initialization
$MySonar = new MySonar(true, $_SESSION["APIKey"]); // standalone = true
$Model = new Model($MySonar);
$View = new View($MySonar);

// device switch (for non-wireless devices route to website)
if ($_GET["WirelessDevice"]=="true") $_SESSION["WirelessDevice"] = true; // mobile device version can be forced by setting param WirelessDevice=true
if (!isset($_SESSION["WirelessDevice"])) { // if wireless device was not forced and not evaluated before then try to evaluate
	$MobileDeviceID = $Model->getMobileDevice($_SERVER["HTTP_USER_AGENT"]); // if != null its a wireless device
	$_SESSION["WirelessDevice"] = ($MobileDeviceID!=null ? true:false);
}
if ($_SESSION["WirelessDevice"]==false) header("Location: ".$MySonar->getProperty("mobile_device_emulator_url")); // if not a mobile device then goto website for mobile device simulator

// user is authenticated
if ($_SESSION["Security"]) {
	
	switch ($_GET["m"]) {
		
		// logout
		case "logout":
			$result = $Model->logout();
			$View->displayLogoutPage();
		break;
		
		// sendPing
		case "sendPing":
			$Query = $_GET["Query"];
			$StartIndex = ($_GET["StartIndex"]!="" ? $_GET["StartIndex"]:1);
			$Limit = ($_GET["Limit"]!="" ? $_GET["Limit"]:9);
			$Profile = $_GET["Profile"];
			$result = $Model->sendPing($Query, $StartIndex, $Limit, $Profile);
			if ($result instanceof SonarErrors) {
				$errors = $result->getErrors();
				if ($errors[0][0]!=433) { // nothing found
					$error = $result;
					$Options = new Options();
					$result = $Model->getTab($Options);
					if ($result instanceof SonarErrors) $result = null;
					else {
						$Statistics = $result[0];
						$Modules = $result[1];
					}
					if ($errors[0][0]==666) { $Profile = "DEFAULT"; } // location missing
					// display homepage with error message
					$View->displayPageHeader();
					$View->displayErrors(false, $error);
					$View->displayHomepage(false, $Query, false, $Modules, $Profile, null, $Statistics);
					$View->displayPageFooter();
				} else {
					$View->displayPing(true, $result, $Query);
				}
			} else $View->displayPing(true, $result, $Query);
		break;
		
		// getMap
		case "getMap":
			$result = $Model->getMap($_GET["Locations"], $_GET["LID"], $_GET["Zoom"], $_GET["StartIndex"], $_GET["Limit"], $_GET["Address"], $_GET["Type"]);
			if ($result instanceof SonarErrors) {
				$error = $result;
				// display error on sonar preferences
				$result = $Model->getSonarPreferences();
				if ($result instanceof SonarErrors) $error = $result; 
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				$View->displaySonarPreferences(false, $result, null);
				$View->displayPageFooter();				
			} else $View->displayMap(true, $_GET["Locations"], $_GET["LID"], $_GET["StartIndex"], $_GET["Limit"], $result);
		break;

		// getProfile
		case "getProfile":
			$result = $Model->getProfile($_GET["Username"], ($_GET["thumbnail"]=="false" ? false:true));
			if ($result instanceof SonarErrors) {
				// display error and return to homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else $View->displayProfile(true, $result, ($_GET["thumbnail"]=="false" ? false:true));
		break;
				
		// setPicture
		case "setPicture":
			if ($_POST["submit"]) {
				$result = $Model->setPicture($_FILES);
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayPictureForm(false);
					$View->displayPageFooter(false);
				} else $View->displayPictureForm(true);
			} else $View->displayPictureForm(true);
		break;

		// changePassword
		case "changePassword":
			if ($_GET["submit"]) {
				$result = $Model->changePassword($_GET["Password"]);
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayPasswordForm(false, $_GET["Password"]);
					$View->displayPageFooter();
				} else {
					$Options = new Options();
					$Options->Tab = "OPTIONS";
					$result = $Model->getTab($Options);
					if ($result instanceof SonarErrors) {
						// display error on homepage
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayHomepage(false, $_SESSION["Location"]->Address);
						$View->displayPageFooter();
					} else $Model->goto("index.php?Msg=Passwort erfolgreich ge&auml;ndert."); {
						$Statistics = $result[0];
						$Module = $result[1];
						$View->displayPageHeader();
						$View->displayMsg(false, "Passwort erfolgreich ge&auml;ndert.");
						$View->displayHomepage(false, $_SESSION["Location"]->Address, false, $Module, "DEFAULT", "OPTIONS", $Statistics); 
						$View->displayPageFooter();
					}
				}
			} else $View->displayPasswordForm(true, $_GET["Password"]);
		break;
		
		//deletePicture
		case "deletePicture":
			if ($_GET["delete"]=="true") {
				$result = $Model->deletePicture();
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayDeletePicture(false);
					$View->displayPageFooter(false);
				} else {
					$result = $Model->getSonarPreferences();
					if ($result instanceof SonarErrors) $error = $result; 
					$View->displayPageHeader();
					if ($error) $View->displayErrors(false, $result);
					else $View->displayMsg(false, "Foto ist jetzt weg.");
					$View->displaySonarPreferences(false, $result, null);
					$View->displayPageFooter();				
				}
			} else $View->displayDeletePicture(true);
		break;
		
		// updateUser
		case "updateUser":
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$_SESSION["UID"] = null;
				// after submitting form
				$User = new User();
				$value = ($_GET["value"]!="" ? $_GET["value"]:"");
				switch ($_GET["Option"]) {
					case "Email": $User->Email = $value; break;
					case "MobilePhoneNumber": $User->MobilePhoneNumber = $value; break;
					case "Name": $User->Name = $value; break;
					case "Sex": $User->Sex = $value; break;
					case "Birth": if ($value!=null) $User->Birth = substr($value,4)."-".substr($value,2,2)."-".substr($value,0,2); break;
					case "Info": $User->Info = ($value=="" ? ' ':$value); break;
					case "CountryCode": $User->CountryCode = $value; break;
					case "Flirt": $User->Flirt = ($value=="true" ? true:false); break;
					case "StatusMessage": $User->StatusMessage = ($value=="" ? ' ':$value); break;
				} 
				$result = $Model->updateUser($User, $_GET["Option"]);
				if ($result instanceof SonarErrors) {
					// display error and return to updateUser
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayUpdateUser(false, $_GET["Option"], $_GET["value"]);
					$View->displayPageFooter();
				} else {
					if ($User->MobilePhoneNumber!="") {
						// show confirmation page
						$View->displayPageHeader();
						$View->displayMsg(false, "Handynr. erfolgreich gespeichert.");
						$View->displayConfirmUser(false);
						$View->displayPageFooter();
					} elseif ($_GET["Option"]=="StatusMessage") {
						// goto homepage
						$Options = new Options();
						$Options->Tab = "OPTIONS";
						$result = $Model->getTab($Options);
						if ($result instanceof SonarErrors) {
							// display error on homepage
							$View->displayPageHeader();
							$View->displayErrors(false, $result);
							$View->displayHomepage(false, $_SESSION["Location"]->Address);
							$View->displayPageFooter();
						} else {
							$Statistics = $result[0];
							$Module = $result[1];
							$View->displayHomepage(true, $_SESSION["Location"]->Address, false, $Module, "DEFAULT", "OPTIONS", $Statistics); 
						}
					} else {
						// display sonar preferences
						$result = $Model->getSonarPreferences();
						if ($result instanceof SonarErrors) {
							// display error and return to homepage
							$View->displayPageHeader();
							$View->displayErrors(false, $result);
							$View->displayHomepage(false, $_SESSION["Location"]->Address);
							$View->displayPageFooter();
						} else $View->displaySonarPreferences(true, $result, null);
					}
				}
			} else {
				$_SESSION["UID"] = rand(100000, 999999);
				$View->displayUpdateUser(true, $_GET["Option"], $_GET["value"]);
			}
		break;
		
		// getSonarPreferences
		case "getSonarPreferences":
			$result = $Model->getSonarPreferences();
			if ($result instanceof SonarErrors) {
				// display error and return to homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else $View->displaySonarPreferences(true, $result, $_GET["Option"]);
		break;

		// doVote
		case "doVote":
			if ($_GET["submit"]) {
				$Vote = new Vote();
				$Vote->PointOfInterest->ID = $_GET["Key"];
				$Vote->Value = $_GET["Value"];
				$result = $Model->doVote($Vote);
				if ($result instanceof SonarErrors) $error = $result;
				$result = $Model->getPointOfInterest($_GET["Key"]);
				if ($result instanceof SonarErrors) $error = $result;
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				else $View->displayMsg(false, "Vielen Dank f&uuml;r deine Bewertung!");
				$View->displayPointOfInterest(false, $result);
				$View->displayPageFooter();
			} else $View->displayVote(true, $_GET["Category"], $_GET["Key"]);
		break;		
		
		// confirmUser
		case "confirmUser":
			if ($_GET["submit"]) {
				$result = $Model->confirmUser($_GET["ActivationCode"]);
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(false, $_SESSION["Location"]->Address);
					$View->displayPageFooter();
				} else {
					$result = $Model->getProfile($_GET["Username"], ($_GET["thumbnail"]=="false" ? false:true));
					if ($result instanceof SonarErrors) {
						// display error and return to homepage
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayHomepage(false, $_SESSION["Location"]->Address);
						$View->displayPageFooter();
					} else {
						$View->displayPageHeader();
						$View->displayMsg(false, "Handynr. ist aktiviert.");
						$View->displayProfile(false, $result, ($_GET["thumbnail"]=="false" ? false:true));
						$View->displayPageFooter();
					}
				}
			} else $View->displayConfirmUser(true, $result, $_GET["Option"]);
		break;		
		
		// updateSonarPreferences
		case "updateSonarPreferences":
			$SonarPreferences = new SonarPreferences();
			$Option = null;
			switch ($_GET["Option"]) {
				case "Range": $SonarPreferences->Range = $_GET["value"]; break;
				case "ObjectType": $SonarPreferences->ObjectType = $_GET["value"]; break;
				case "POITypes": 
					$POITypes = $_GET["POITypes"];
					$POITypes = explode("|", $POITypes);
					if ($_GET["Task"]=="ADD" && !in_array($_GET["value"], $POITypes)) {
						array_push($POITypes, $_GET["value"]);
						$POITypesNew = $POITypes;
					} else {
						// Task = REMOVE
						$POITypesNew = array();
						for($i=0;$i<count($POITypes);$i++) {
							if ($POITypes[$i]!=$_GET["value"]) array_push($POITypesNew, $POITypes[$i]);
						}
					}
					$SonarPreferences->POITypes = implode("|", $POITypesNew); 
					$Option = "POITypes";
				break;
				case "UserSex": $SonarPreferences->UserSex = $_GET["value"]; break;
				case "MinAge": 
					$SonarPreferences->MinAge = ($_GET["value"]!="" ? $_GET["value"]:0);
					switch ($_GET["value"]) {
						case 0: $SonarPreferences->MaxAge = 0; break; 
						case 16: $SonarPreferences->MaxAge = 19; break; 
						case 20: $SonarPreferences->MaxAge = 24; break; 
						case 25: $SonarPreferences->MaxAge = 29; break; 
						case 30: $SonarPreferences->MaxAge = 39; break; 
						case 40: $SonarPreferences->MaxAge = 49; break; 
						case 50: $SonarPreferences->MaxAge = 99; break; 
						default: $SonarPreferences->MinAge = 0; $SonarPreferences->MaxAge = 0; break;
					}
				break;
				case "UserFlirt": $SonarPreferences->UserFlirt = ($_GET["value"]=="true" ? true:false); break;
				case "MapWidth": $SonarPreferences->MapWidth = $_GET["value"]; break;
				case "MapHeight": $SonarPreferences->MapHeight = $_GET["value"]; break;
				case "Visibility": $SonarPreferences->Visibility = $_GET["value"]; break;
				default: unset($SonarPreferences); break;
			} 
			$result = $Model->updateSonarPreferences($SonarPreferences);
			if ($result instanceof SonarErrors) {
				// display error and return to getSonarPreferences
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displaySonarPreferences(false, $result, $_GET["Option"]);
				$View->displayPageFooter(false);
			} else $View->displaySonarPreferences(true, $result, $Option);
		break;
		
		// getStatistics
		case "getStatistics":
			$result = $Model->getStatistics();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter(false);
			} else $View->displayStatistics(true, $result);
		break;
				
		// getLastLocations
		case "getLastLocations":
			$result = $Model->getLastLocations();
			if ($result instanceof SonarErrors) $result = null;
			$View->displayLastLocations(true, $result);
		break;

		// setLocation
		case "setLocation":
			$result = $Model->setLocation(urldecode($_GET["Address"]));
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter(false);
			} else $View->displayHomepage(true, $_SESSION["Location"]->Address);
		break;
		
		// sendMessage
		case "sendMessage":
			$Message = new Message();
			$Message->Recipient = $_GET["Recipient"];
			$Message->Text = $_GET["Text"];
			$Message->Type = $_GET["Type"];
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$_SESSION["UID"] = null;
				$result = $Model->sendMessage($Message);
				if ($result instanceof SonarErrors) {
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayMessageForm(false, $Message);
					$View->displayPageFooter(false);
				} else {
					// show message
					$View->displayPageHeader();
					$View->displayMsg(false, "Erfolgreich verschickt.");
					$View->displayMessage(false, $result);
					$View->displayPageFooter(false);
				}
			} else {
				// show form
				$_SESSION["UID"] = rand(100000, 999999);
				$View->displayMessageForm(true, $Message, $_GET["TextAttachment"]);
			}
		break;
		
		// getInbox
		case "getInbox":
			$result = $Model->getInbox();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayInbox(false, null);
				$View->displayPageFooter(false);
			} else $View->displayInbox(true, $result);
		break;

		// getOutbox
		case "getOutbox":
			$result = $Model->getOutbox();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayOutbox(false, null);
				$View->displayPageFooter(false);
			} else $View->displayOutbox(true, $result);
		break;
				
		// getMessage
		case "getMessage":
			$result = $Model->getMessage($_GET["ID"]);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else $View->displayMessage(true, $result);
		break;		
				
		// deleteMessage
		case "deleteMessage":
			$result = $Model->deleteMessage($_GET["ID"]);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				// show inbox or outbox 
				if ($_GET["goto"]=="Inbox") {
					$result = $Model->getInbox();
					if ($result instanceof SonarErrors) $result = null;
					$View->displayPageHeader();
					$View->displayMsg(false, "Nachricht gel&ouml;scht.");
					$View->displayInbox(false, $result);
					$View->displayPageFooter(false);
				} else {
					$result = $Model->getOutbox();
					if ($result instanceof SonarErrors) {
						// display error on homepage
						$View->displayPageHeader();
						$View->displayOutbox(false, null);
						$View->displayPageFooter(false);
					} else {
						$View->displayPageHeader();
						$View->displayMsg(false, "Nachricht gel&ouml;scht.");
						$View->displayOutbox(false, $result);
						$View->displayPageFooter();
					}
				}
			}
		break;

		// setPointOfInterest
		case "setPointOfInterest":
			$Object = null;
			if ($_GET["Type"]=="PARTY" || $_GET["Type"]=="EVENT") {
				$Object = new Object();
				$Object->Party->ID = $_GET["ID"];
				$Object->Party->Type = $_GET["Type"];
				$Object->Party->Name = $_GET["Name"];
				$Object->Party->Description = $_GET["Description"];
				$Object->Party->LocationName = $_GET["LocationName"];
				if ($_GET["Begin"]!="") $Object->Party->Begin = substr($_GET["Begin"], 4, 4)."-".substr($_GET["Begin"], 2, 2)."-".substr($_GET["Begin"], 0, 2)."T".substr($_GET["Begin"], 9, 2).":".substr($_GET["Begin"], 11, 2).":00";
				else $Object->Party->Begin = date("Y-m-d")."T22:00:00";
				$Object->Party->Private = ($_GET["Private"]=="true" ? true:false);
				$Object->Location->Address = $_GET["Address"];
				$Object->Location->Category = 1; // POI
				$Object->Location->CountryCode = "DE";
			} 
			if ($_GET["Type"]=="RESTAURANT" || 
				$_GET["Type"]=="CLUB" || 
				$_GET["Type"]=="CAFE" || 
				$_GET["Type"]=="BAR" || 
				$_GET["Type"]=="CASHPOINT" || 
				$_GET["Type"]=="CINEMA" || 
				$_GET["Type"]=="GASSTATION" || 
				$_GET["Type"]=="SHOP" || 
				$_GET["Type"]=="SUPERMARKET" || 
				$_GET["Type"]=="BOWLING" || 
				$_GET["Type"]=="BATHS" ||
				$_GET["Type"]=="FASTFOOD" ||
				$_GET["Type"]=="SNACK" ||
				$_GET["Type"]=="DRUGSTORE" ||
				$_GET["Type"]=="HOTSPOT" ||
				$_GET["Type"]=="POST" ||
				$_GET["Type"]=="PARKINGLOT" ||
				$_GET["Type"]=="TAXISTOP" ||
				$_GET["Type"]=="CAMPINGGROUND" ||
				$_GET["Type"]=="YOUTHHOSTEL" ||
				$_GET["Type"]=="GYM" ||
				$_GET["Type"]=="HORSEFARM") {
				$Object = new Object();
				$Object->PointOfInterest->ID = $_GET["ID"];
				$Object->PointOfInterest->Type = $_GET["Type"];
				$Object->PointOfInterest->Name = $_GET["Name"];
				$Object->PointOfInterest->Description = $_GET["Description"];
				$Object->Location->Address = $_GET["Address"];
				$Object->Location->Category = 1; // POI
				$Object->Location->CountryCode = "DE";
			}
			if ($_GET["submit"]) {
				// after submitting form
				$result = $Model->setPointOfInterest($Object);
				if ($result instanceof SonarErrors) {
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayPointOfInterestForm(false, $Object);
					$View->displayPageFooter();
				} else {
					// show POI profile with confirmation message
					$View->displayPageHeader();
					$View->displayPointOfInterest(false, $result);
					$View->displayPageFooter();
				}
			} else {
				// display menu
				$View->displayPointOfInterestForm(true, $Object);
			}
		break;			

		// createGroup
		case "createGroup":
			$Object = null;
			$Group = new Group();
			$Group->ID = $_GET["ID"];
			$Group->Name = $_GET["Name"];
			$Group->Description = $_GET["Description"];
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$result = $Model->createGroup($Group);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayGroupForm(false, $Group);
					$View->displayPageFooter();
				} else {
					$_SESSION["UID"] = NULL;
					$Group = $result;
					$Options = new Options();
					$Options->StartIndex = 1;
					$Options->Limit = 1000;
					$result = $Model->getGroup($Options, $Group);
					if ($result instanceof SonarErrors) {
						// display error on homepage
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayHomepage(false, $_SESSION["Location"]->Address);
						$View->displayPageFooter(false);
					} else {
						$Options = $result[0];
						$Group = $result[1];
						$View->displayGroup(true, $Group, $Options);
					}
				}
			} else {
				$_SESSION["UID"] = rand(100000,999999);
				$View->displayGroupForm(true, $Group);
			}
		break;		

		// joinGroup
		case "joinGroup":
			$Group = new Group();
			$Group->ID = $_GET["ID"];
			$result = $Model->joinGroup($Group);
			if ($result instanceof SonarErrors) $error = $result;
			// get group
			$result = $Model->getGroup(null, $Group);
			$Group = $result[1];
			// display
			$View->displayPageHeader();
			if ($error) $View->displayErrors(false, $error);
			$View->displayGroup(false, $Group);
			$View->displayPageFooter();
		break;			

		// leaveGroup
		case "leaveGroup":
			$Group = new Group();
			$Group->ID = $_GET["ID"];
			$result = $Model->leaveGroup($Group);
			if ($result instanceof SonarErrors) $error = $result;
			// get group
			$result = $Model->getGroup(null, $Group);
			$Group = $result[1];
			// display
			$View->displayPageHeader();
			if ($error) $View->displayErrors(false, $error);
			$View->displayGroup(false, $Group);
			$View->displayPageFooter();
		break;	
				
		// setTopic
		case "setTopic":
			$Group = new Group();
			$Group->ID = $_GET["GroupID"];
			$Group->Name = $_GET["GroupName"];
			$Group->Description = $_GET["GroupDescription"];
			$Article = new Article();
			$Article->GroupID = $_GET["GroupID"];
			$Article->Title = $_GET["Title"];
			$Article->Content = $_GET["Content"];
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$result = $Model->setTopic($Article);
				if ($result instanceof SonarErrors) {
					$error = $result;
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $error);
					$View->displayTopicForm(false, $Article, $Group);
					$View->displayPageFooter();
				} else {
					$_SESSION["UID"] = NULL;
					$result = $Model->getGroup(NULL, $Group);
					$View->displayGroup(true, $result[1]);
				}
			} else {
				// create UID and display form
				$_SESSION["UID"] = rand(100000, 999999);
				$result = $Model->getGroup($Options, $Group);				
				$View->displayTopicForm(true, $Article, $result[1]); // [0] = Options, [1] = Group
			}
		break;			

		// updateTopic
		case "updateTopic":
			$Group = new Group();
			$Group->ID = $_GET["GroupID"];
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->getTopic($Article);
			$Article = $result[0];		
			$Group = $result[1];		
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$Article->Title = $_GET["Title"];
				$Article->Content = $_GET["Content"];
				$result = $Model->updateTopic($Article);
				if ($result instanceof SonarErrors) {
					$error = $result;
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $error);
					$View->displayTopicForm(false, $Article, $Group, true); // [0] = Options, [1] = Group
					$View->displayPageFooter();
				} else {
					$_SESSION["UID"] = NULL;
					$result = $Model->getGroup(NULL, $Group);
					$View->displayGroup(true, $result[1]);
				}
			} else {
				// create UID and display form
				$_SESSION["UID"] = rand(100000, 999999);
				$View->displayTopicForm(true, $Article, $Group, true); // [0] = Options, [1] = Group
			}
		break;			

		// updateArticle
		case "updateArticle":
			if ($_SESSION["Topic"]==NULL) {
				// get topic
				$Topic = new Article();
				$Topic->ID = $_GET["ReferenceID"];
				$result = $Model->getTopic($Topic);
				if ($result instanceof SonarErrors) {
					// TODO: error management
				} else $_SESSION["Topic"] = $result;
			}
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->getArticle($Article);
			$Article = $result[0];
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				// after submitting form
				$Article->Content = $_GET["Content"];
				$result = $Model->updateArticle($Article);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayArticleForm(false, $_SESSION["Topic"], $Article, true); 
					$View->displayPageFooter();
				} else {
					// show topic
					$Topic = new Article();
					$Topic->ID = $_GET["ReferenceID"];
					$result = $Model->getTopic($Topic);
					if ($result instanceof SonarErrors) {
						// TODO: error management
					} else {
						$_SESSION["Topic"] = NULL;
						$View->displayTopic(true, $result);
					}
				}
			} else {
				// create UID and display form
				$_SESSION["UID"] = rand(100000, 999999);
				$View->displayArticleForm(true, $_SESSION["Topic"], $Article, true); 
			}
		break;	
				
		// setArticle
		case "setArticle":
			if ($_SESSION["Topic"]==NULL) {
				// get topic
				$Topic = new Article();
				$Topic->ID = $_GET["ReferenceID"];
				$result = $Model->getTopic($Topic);
				if ($result instanceof SonarErrors) {
					// TODO: error management
				} else $_SESSION["Topic"] = $result;
			}
			$Article = new Article();
			$Article->ReferenceID = $_GET["ReferenceID"];
			$Article->Content = $_GET["Content"];
			if ($_GET["submit"]) {
				if ($_SESSION["UID"]==$_GET["UID"]) {
					$_SESSION["UID"] = null;
					// after submitting form
					$result = $Model->setArticle($Article);
					if ($result instanceof SonarErrors) {
						// display error on homepage
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayArticleForm(false, $_SESSION["Topic"], $Article);
						$View->displayPageFooter();
					} else {
						// show topic
						$Topic = new Article();
						$Topic->ID = $_GET["ReferenceID"];
						$result = $Model->getTopic($Topic);
						if ($result instanceof SonarErrors) {
							// TODO: error management
						} else {
							$_SESSION["Topic"] = NULL;
							$View->displayTopic(true, $result);
						}
					}
				}
			} else {
				// create UID and display form
				$_SESSION["UID"] = rand(100000, 999999);
				$View->displayArticleForm(true, $_SESSION["Topic"], $Article);
			}
		break;		
			
		// deleteArticle
		case "deleteArticle":
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->deleteArticle($Article);
			if ($result instanceof SonarErrors) $error = $result;
			// show topic
			$Topic = new Article();
			$Topic->ID = $_GET["ReferenceID"];
			$result = $Model->getTopic($Topic);
			if ($result instanceof SonarErrors) {
				$Options = new Options();
				$Options->Tab = "GROUPS";
				$result = $Model->getTab($Options);
				if ($result instanceof SonarErrors) {
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(false, $_SESSION["Location"]->Address);
					$View->displayPageFooter();
				} else {
					$Statistics = $result[0];
					$Module = $result[1];
					$View->displayHomepage(true, $_SESSION["Location"]->Address, false, $Module, "DEFAULT", "GROUPS", $Statistics, "Gruppe wurde gel&ouml;scht."); 
				}				
			} else {
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				$View->displayTopic(false, $result);
				$View->displayPageFooter();
			}
		break;	
			
		// deleteTopic
		case "deleteTopic":
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->deleteTopic($Article);
			if ($result instanceof SonarErrors) $error = $result;
			$Group = new Group();
			$Group->ID = $_GET["GroupID"];
			$Options = new Options();
			$Options->StartIndex = 1;
			$Options->Limit = 1000;
			$result = $Model->getGroup($Options, $Group);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$Options = $result[0];
				$Group = $result[1];
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				$View->displayGroup(false, $Group, $Options);
				$View->displayPageFooter();
			}
		break;	

		// deleteGroup
		case "deleteGroup":
			$Group = new Group();
			$Group->ID = $_GET["ID"];
			$result = $Model->deleteGroup($Group);
			if ($result instanceof SonarErrors) $error = $result;
			$Options = new Options();
			$Options->Tab = "GROUPS";
			$result = $Model->getTab($Options);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$Statistics = $result[0];
				$Module = $result[1];
				$View->displayHomepage(true, $_SESSION["Location"]->Address, false, $Module, "DEFAULT", "GROUPS", $Statistics, "Gruppe wurde gel&ouml;scht."); 
			}
		break;	
				
		// updatePointOfInterest
		case "updatePointOfInterest":
			// if submit do update otherwise display form
			if ($_GET["submit"] && $_SESSION["UID"]==$_GET["UID"]) {
				$Object = new Object();
				if ($_GET["Type"]=="PARTY" || $_GET["Type"]=="EVENT") {
					$Object->Party->ID = $_GET["ID"];
					if (isset($_GET["Active"])) $Object->Party->Active = ($_GET["Active"]==1 ? true:false);
					if (isset($_GET["Name"])) $Object->Party->Name = $_GET["Name"];
					if (isset($_GET["Description"])) $Object->Party->Description = $_GET["Description"];
					if (isset($_GET["LocationName"])) $Object->Party->LocationName = $_GET["LocationName"];
					if ($_GET["Begin"]!="") $Object->Party->Begin = substr($_GET["Begin"], 4, 4)."-".substr($_GET["Begin"], 2, 2)."-".substr($_GET["Begin"], 0, 2)."T".substr($_GET["Begin"], 9, 2).":".substr($_GET["Begin"], 11, 2).":00";
//					else $Object->Party->Begin = date("Y-m-d")."T22:00:00";
					if (isset($_GET["Private"])) $Object->Party->Private = ($_GET["Private"]=="true" ? true:false);
					if (isset($_GET["Address"])) {
						$Object->Location->Address = $_GET["Address"];
						$Object->Location->Category = 1; // POI
						$Object->Location->CountryCode = "DE";
					}
				} 
				if ($_GET["Type"]=="RESTAURANT" || 
					$_GET["Type"]=="CLUB" || 
					$_GET["Type"]=="CAFE" || 
					$_GET["Type"]=="BAR" || 
					$_GET["Type"]=="CASHPOINT" || 
					$_GET["Type"]=="CINEMA" || 
					$_GET["Type"]=="GASSTATION" || 
					$_GET["Type"]=="SHOP" || 
					$_GET["Type"]=="SUPERMARKET" || 
					$_GET["Type"]=="BOWLING" || 
					$_GET["Type"]=="BATHS" ||
					$_GET["Type"]=="FASTFOOD" ||
					$_GET["Type"]=="SNACK" ||
					$_GET["Type"]=="DRUGSTORE" ||
					$_GET["Type"]=="HOTSPOT" ||
					$_GET["Type"]=="POST" ||
					$_GET["Type"]=="PARKINGLOT" ||
					$_GET["Type"]=="TAXISTOP" ||
					$_GET["Type"]=="CAMPINGGROUND" ||
					$_GET["Type"]=="YOUTHHOSTEL" ||
					$_GET["Type"]=="GYM" ||
					$_GET["Type"]=="HORSEFARM") {
					$Object->PointOfInterest->ID = $_GET["ID"];
					if (isset($_GET["Active"])) $Object->PointOfInterest->Active = ($_GET["Active"]==1 ? true:false);
					if (isset($_GET["Name"])) $Object->PointOfInterest->Name = $_GET["Name"];
					if (isset($_GET["Description"])) $Object->PointOfInterest->Description = $_GET["Description"];
					if (isset($_GET["Address"])) {
						$Object->Location->Address = $_GET["Address"];
						$Object->Location->Category = 1; // POI
						$Object->Location->CountryCode = "DE";
					}
				}
				$_SESSION["UID"] = null;
				$result = $Model->updatePointOfInterest($Object);
				if ($result instanceof SonarErrors) {
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayPointOfInterestForm(false, $Object, true); // true==update
					$View->displayPageFooter();
				} else $View->displayPointOfInterest(true, $result);
			} else {
				// first get POI by ID
				$result = $Model->getPointOfInterest($_GET["ID"]);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(true, $_SESSION["Location"]->Address, false, NULL, "DEFAULT", "HOME", NULL, $_GET["Msg"]); 
					$View->displayPageFooter();
				} else {
					// get Party or PointOfInterst objects of result
					$Object = $result;
					if ($Object->Party!=NULL) $POI = $Object->Party;
					else $POI = $Object->PointOfInterest;
					// create UID and display form
					$_SESSION["UID"] = rand(100000, 999999);
					$View->displayPointOfInterestForm(true, $Object, true); // true==update
				}
			}
		break;	
				
		// getFriends
		case "getFriends":
			$result = $Model->getFriends($_GET["Username"]);
			if ($result instanceof SonarErrors) $result = null;
			$View->displayFriends(true, $result, $_GET["Username"]);
		break;		

		// getFlirts
		case "getFlirts":
			$result = $Model->getFlirts();
			if ($result instanceof SonarErrors) $result = null;
			$View->displayFlirts(true, $result);
		break;		

		// getFriendshipRequests
		case "getFriendshipRequests":
			$result = $Model->getFriendshipRequests();
			if ($result instanceof SonarErrors) $result = null;
			$View->displayFriendshipRequests(true, $result);
		break;			

		// getPendingPOIs
		case "getPendingPOIs":
			$result = $Model->getPendingPOIs();
			if ($result instanceof SonarErrors) $result = null;
			$View->displayPendingPOIs(true, $result);
		break;	
				
		// setInvitation
		case "setInvitation":
			if ($_GET["OK"]) {
				// after verification
				$User = new User();
				$User->MobilePhoneNumber = $_GET["MobilePhoneNumber"];
				$Message = new Message();
				$Message->Sender = $_GET["Sender"];
				$Message->Recipient = $_GET["Recipient"];
				$Message->Text = $_GET["Text"];
				$result = $Model->setInvitation($User, $Message);
				if ($result instanceof SonarErrors) {
					// display error on homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayInvitationForm(false, $_GET["MobilePhoneNumber"], $_GET["Sender"], $_GET["Recipient"], $_GET["Text"]);
					$View->displayPageFooter(false);
				} else $Model->goto("index.php?Msg=Einladung erfolgreich verschickt.");
			} elseif ($_GET["submit"]) {
				// after submitting form
				$View->displayInvitationConfirmation(true, $_GET["MobilePhoneNumber"], $_GET["Sender"], $_GET["Recipient"], $_GET["Text"]);
			} else $View->displayInvitationForm(true, $_GET["MobilePhoneNumber"], $_GET["Sender"], $_GET["Recipient"], $_GET["Text"]);
		break;		

		// getPointOfInterest
		case "getPointOfInterest":
			$result = $Model->getPointOfInterest($_GET["ID"]);
			if ($result instanceof SonarErrors) $View->displayErrors(true, $result);
			else $View->displayPointOfInterest(true, $result);
		break;			

		// getGroup
		case "getGroup":
			$Group = new Group();
			$Group->ID = $_GET["ID"];
			$Options = new Options();
			$Options->StartIndex = 1;
			$Options->Limit = 1000;
			$result = $Model->getGroup($Options, $Group);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter(false);
			} else {
				$Options = $result[0];
				$Group = $result[1];
				$View->displayGroup(true, $Group, $Options);
			}
		break;		

		// getArticle
		case "getArticle":
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->getArticle($Article);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter(false);
			} else $View->displayArticle(true, $result);
		break;			
		
		// getTopic
		case "getTopic":
			$Article = new Article();
			$Article->ID = $_GET["ID"];
			$result = $Model->getTopic($Article);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();				
			} else { 
				$View->displayTopic(true, $result);
			}
		break;			
		
		// deletePointOfInterest
		case "deletePointOfInterest":
			$result = $Model->deletePointOfInterest($_GET["ID"]);
			if ($result instanceof SonarErrors) $error = $result;
			// look for changes
			$result = $Model->getStatistics();
			if ($result instanceof SonarErrors) $result = null;
			// display error on homepage
			$View->displayPageHeader();
			if ($error)	$View->displayErrors(false, $error);
			else $View->displayMsg(false, "Erfolgreich gel&ouml;scht.");
			$View->displayHomepage(false, $_SESSION["Location"]->Address, false, $result);
			$View->displayPageFooter();
		break;
		
		// setFriendship
		case "setFriendship":
			$User = new User();
			$User->Username = $_GET["Username"];
			$Message = new Message();
			$Message->Text = $_GET["Text"];
			if ($_GET["submit"] && $_GET["UID"]==$_SESSION["UID"]) {
				// after submitting form
				$_SESSION["UID"] = null;
				$result = $Model->setFriendship($User, $Message);
				if ($result instanceof SonarErrors) $error = $result;
				// show profile
				$result = $Model->getProfile($User->Username, true);
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(false, $_SESSION["Location"]->Address);
					$View->displayPageFooter();
				} else {
					$View->displayPageHeader();
					if ($error) $View->displayErrors(false, $error);
					else $View->displayMsg(false, "Anfrage versendet.");
					$View->displayProfile(false, $result, true);
					$View->displayPageFooter();
				}
			} else {
				$_SESSION["UID"] = rand(100000,999999);
				$View->displayFriendshipForm(true, $User, $Message, $_GET["MobilePhoneNumber"]);
			}
		break;	

		// setBestFriend
		case "setBestFriend":
			$User = new User();
			$User->Username = $_GET["Username"];
			$result = $Model->setBestFriend($User);
			if ($result instanceof SonarErrors) $error = $result;
			// display friends page
			$result = $Model->getFriends();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter(false);
			} else {
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				else $View->displayMsg(false, $User->Username." ist jetzt BestFriend.");
				$View->displayFriends(false, $result);
				$View->displayPageFooter(false);
			}
		break;			

		// putEyesOn
		case "putEyesOn":
			$User = new User();
			$User->Username = $_GET["Username"];
			if ($_GET["submit"]) {
				$result = $Model->putEyesOn($User);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayEyesOn(false, $User);
					$View->displayPageFooter();
				} else {
					// display profile
					$result = $Model->getProfile($User->Username, true);
					if ($result instanceof SonarErrors) $error = $result;
					$View->displayPageHeader();
					if ($error) $View->displayErrors(false, $error);
					else $View->displayMsg(false, "Du hast ein Auge drauf geworfen.");
					$View->displayProfile(false, $result, true);
					$View->displayPageFooter();
				}
			} else $View->displayEyesOn(true, $User);
		break;			
		
		// deleteBestFriend
		case "deleteBestFriend":
			$User = new User();
			$User->Username = $_GET["Username"];
			$result = $Model->deleteBestFriend($User);
			if ($result instanceof SonarErrors) $error = $result;
			// display friends page
			$result = $Model->getFriends();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				else $View->displayMsg(false, $User->Username." ist kein BestFriend mehr.");
				$View->displayFriends(false, $result);
				$View->displayPageFooter();
			}
		break;		
		
		// removeEyesOn
		case "removeEyesOn":
			$User = new User();
			$User->Username = $_GET["Username"];
			$result = $Model->removeEyesOn($User);
			if ($result instanceof SonarErrors) $error = $result;
			// display profile
			$result = $Model->getProfile($User->Username, true);
			if ($result instanceof SonarErrors) {
				// display error and return to homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				else $View->displayMsg(false, "Du hast dein Auge zur&uuml;ck.");
				$View->displayProfile(false, $result, true);
				$View->displayPageFooter();
			}
		break;		
			
		// blockEyesOn
		case "blockEyesOn":
			$User = new User();
			$User->Username = $_GET["Username"];
			$result = $Model->blockEyesOn($User);
			if ($result instanceof SonarErrors) $error = $result;
			$result = $Model->getFlirts();
			if ($result instanceof SonarErrors) $result = null;
			$View->displayPageHeader();
			if ($error) $View->displayErrors(false, $error);
			else $View->displayMsg(false, "Flirt wurde abgeblockt.");
			$View->displayFlirts(false, $result);
			$View->displayPageFooter();
		break;	
				
		// deleteFriendship
		case "deleteFriendship":
			$User = new User();
			$User->Username = $_GET["Username"];
			if ($_GET["submit"]) {
				// after submitting form
				$result = $Model->deleteFriendship($User);
				if ($result instanceof SonarErrors) $error = $result;
				// show profile
				$result = $Model->getProfile($User->Username, true);
				if ($result instanceof SonarErrors) {
					// display error and return to homepage
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(false, $_SESSION["Location"]->Address);
					$View->displayPageFooter();
				} else {
					$View->displayPageHeader();
					if ($error) $View->displayErrors(false, $error);
					else $View->displayMsg(false, "Kontakt ist beendet.");
					$View->displayProfile(false, $result, true);
					$View->displayPageFooter();
				}
			} else $View->displayDeleteFriendship(true, $User);
		break;	

		// confirmFriendship
		case "confirmFriendship":
			$User = new User();
			$User->Username = $_GET["Username"];
			$result = $Model->confirmFriendship($User);
			if ($result instanceof SonarErrors) $error = $result;
			// send message to postbox (without error handling)
			$PN = new Message();
			$PN->Recipient = $User->Username;
			$PN->Text = 'Ich habe unsere Kontakt best&auml;tigt. - Diese Nachricht wurde vom System automatisch erzeugt.';
			$result = $Model->sendMessage($PN);
			// show profile
			$result = $Model->getProfile($User->Username, true);
			if ($result instanceof SonarErrors) {
				// display error and return to homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$View->displayPageHeader();
				if ($error) $View->displayErrors(false, $error);
				else $View->displayMsg(false, "Kontakt best&auml;tigt.");
				$View->displayProfile(false, $result, true);
				$View->displayPageFooter();
			}
		break;			

		// deleteCurrentLocation
		case "deleteCurrentLocation":
			// display friends page
			$result = $Model->deleteCurrentLocation();
			if ($result instanceof SonarErrors) {
				// display error and return to getSonarPreferences
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displaySonarPreferences(false, $result, $_GET["Option"]);
				$View->displayPageFooter();
			} else {
				$result = $Model->getSonarPreferences();
				if ($result instanceof SonarErrors) {
					$result = $Model->getStatistics();
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayHomepage(false, $_SESSION["Location"]->Address, false, $result);
					$View->displayPageFooter();					
				} else {
					$View->displayPageHeader();
					$View->displayMsg(false, "Standort gel&ouml;scht.");
					$View->displaySonarPreferences(false, $result);
					$View->displayPageFooter();
				}
			}
		break;	

		// pay
		case "pay":
			// get balance
			$result = $Model->getBalance();
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else $Account = $result;
			// create transaction
			$Transaction = new Transaction();
			$Transaction->Recipient = $_GET["Recipient"];
			$Transaction->Amount = $_GET["Amount"];
			$Transaction->Description = $_GET["Description"];
			if ($_GET["submit"] && $_GET["UID"]==$_SESSION["UID"]) {
				// destroy UID from session
				$_SESSION["UID"] = NULL;
				// after submitting form pay money
				$result = $Model->pay($Transaction);
				if ($result instanceof SonarErrors) {
					// display error
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayPay(false, $Account, $Transaction);
					$View->displayPageFooter();
				} else {
					// $resultset[0] is Account, [1] is Transaction
					$Account = $result;
					$View->displayPageHeader();
					$View->displayMsg(false, "&Uuml;berweisung erfolgreich.");
					$View->displayPayConfirmation(false, $result[0], $result[1]);
					$View->displayPageFooter();
				}
			} else {
				// generate unique key to avoid multiple transactions by reloading page
				$_SESSION["UID"] = rand(100000,900000);
				$View->displayPay(true, $Account, $Transaction);
			}
		break;
		
		// activateCoupon
		case "activateCoupon":
			$Coupon = new Coupon();
			$Coupon->Type = $_GET["Type"];
			$Coupon->Code = $_GET["Code"];
			if ($_GET["submit"]) {
				$result = $Model->activateCoupon($Coupon);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();				
					$View->displayErrors(false, $result);
					$View->displayCouponForm(false, $Coupon);
					$View->displayPageFooter();	
				} else {
					$View->displayPageHeader();				
					$View->displayCouponConfirmation(false, $result);
					$View->displayPageFooter();	
				}
			} else $View->displayCouponForm(true, $Coupon);
		break;

		// getCoupon
		case "getCoupon":
			$Transaction = new Transaction();
			$Transaction->Amount = $_GET["Amount"];
			$Address = new Address();
			$Address->Recipient = $_GET["Recipient"];
			$Address->Street = $_GET["Street"];
			$Address->Location = $_GET["Location"];
			$Coupon = new Coupon();
			$Coupon->Type = $_GET["Type"];
			$Coupon->Transaction = $Transaction;
			$Coupon->Address = $Address;
			if ($_GET["submit"]) {
				$result = $Model->getCoupon($Coupon);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();				
					$View->displayErrors(false, $result);
					$View->displayRequestCouponForm(false, $Coupon);
					$View->displayPageFooter();	
				} else {
					$View->displayPageHeader();				
					$View->displayRequestCouponConfirmation(false, $result, "SONAR_DISBURSEMENT");
					$View->displayPageFooter();	
				}
			} else $View->displayRequestCouponForm(true, $Coupon);
		break;

		// doPhoneCall
		case "doPhoneCall":
			$number = "+49".substr($_GET["MobilePhoneNumber"], 1);
			$View->displayPhoneCall(true, $number); // Statement
		break;			
		
		// getStatement
		case "getStatement":
			$result = $Model->getStatement($_GET["Period"], $_GET["StartDate"]);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else $View->displayStatement(true, $result); // Statement
		break;			
		
		// getPicture
		case "getPicture":
			$thumb = ($_GET["Thumbnail"]=="true" ? true:false);
			$result = $Model->getPicture($thumb);
			$result = $View->displayPicture($result);
		break;

		// client method displayImage
		case "displayImage":
			$View->displayImage($_GET["Filename"]);
		break;
				
		// getHelp
		case "getHelp":
			$result = $Model->getHelp($_GET["key"]);
			$View->displayHelp(true, $result);
		break;
		
		// homepage
		default:
			$Query = $_GET["Query"];
			$Profile = (isset($_GET["Profile"]) ? $_GET["Profile"]:"DEFAULT");
			if ($Profile=="DEFAULT") $Query = $_SESSION["Location"]->Address;
			$Options = new Options();
			$Options->ModuleID = $_GET["ModuleID"];
			$Options->Limit = $_GET["Limit"];
			$Options->Tab = $_GET["Tab"];
			$result = $Model->getTab($Options);
			if ($result instanceof SonarErrors) {
				// display error on homepage
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false, $_SESSION["Location"]->Address);
				$View->displayPageFooter();
			} else {
				$Statistics = $result[0];
				$Module = $result[1];
				$View->displayHomepage(true, $Query, false, $Module, $Profile, $_GET["Tab"], $Statistics, $_GET["Msg"]); 
			}
		break;
	}

} else {
	// user not authenticated
	
	switch ($_GET["m"]) {

		// login
		case "login":
			if (isset($_GET["Username"])) $_SESSION["Username"] = $_GET["Username"];
			if (isset($_GET["Password"])) $_SESSION["Password"] = $_GET["Password"];
			if ($_GET["submit"]) {
				// after submitting form try to log in user
				$result = $Model->login($_GET["Username"], $_GET["Password"]);
				if ($result instanceof SonarErrors) {
					// login failed
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayLoginForm(false, $_GET["Username"], $_GET["Password"]);
					$View->displayPageFooter();
				} else {
					// show whazzup page
					$result = $Model->getStatistics();
					if ($result instanceof SonarErrors) {
						// display error on homepage
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayHomepage(false, $_SESSION["Location"]->Address);
						$View->displayPageFooter(false);
					} else $View->displayStatistics(true, $result);
				}
			} else $View->displayLoginForm(true, $_SESSION["Username"], $_SESSION["Password"]);
		break;

		// getNewPassword
		case "getNewPassword":
			if ($_GET["submit"]) {
				$Birth = substr($_GET["Birth"],4)."-".substr($_GET["Birth"],2,2)."-".substr($_GET["Birth"],0,2);
				$result = $Model->getNewPassword($_GET["Username"], $_GET["MobilePhoneNumber"], $Birth);
				if ($result instanceof SonarErrors) {
					// login failed
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayNewPasswordForm(false, $_GET["Username"], $_GET["MobilePhoneNumber"], $_GET["Birth"]);
					$View->displayPageFooter();
				} else $View->displayLoginForm(true, $_GET["Username"]);
			} else $View->displayNewPasswordForm(true, $_GET["Username"], $_GET["MobilePhoneNumber"], $_GET["Birth"]);
		break;
		
		// checkInvitationCode
		case "checkInvitationCode":
			if ($_GET["submit"]) {
				// in beta phase check invitation code before display registration form
				$result = $Model->checkInvitationCode($_GET["MobilePhoneNumber"], $_GET["InvitationCode"]);
				if ($result instanceof SonarErrors) {
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayCheckInvitationCode(false, $_GET["MobilePhoneNumber"], $_GET["InvitationCode"]);
					$View->displayPageFooter();
				} else {
					$User = $result;
					$User->MobilePhoneNumber = $_GET["MobilePhoneNumber"];
					$View->displayPageHeader();
					$View->displayMsg(false, "Hallo ".$User->Name."!");
					$View->displayConfirmInvitation(false, $User, $_GET["InvitationCode"]);
					$View->displayPageFooter();
				}
			} else $View->displayCheckInvitationCode(true, $_GET["MobilePhoneNumber"], $_GET["InvitationCode"]);
		break;

		// createUser
		case "createUser":
			if ($_SESSION["Code"]=="") $_SESSION["Code"] = $_GET["Code"];
			$User = new User();
			$User->Username = $_GET["Username"];
			$User->Password = $_GET["Password"];
			$User->MobilePhoneNumber = $_GET["MobilePhoneNumber"];
			$User->Birth = $_GET["Birth"];
			$User->Sex = $_GET["Sex"];
			$Page = $_GET["Page"];
			if ($_GET["ok"]) {
				$result = $Model->createUser($User, $_GET["Code"]);
				if ($result instanceof SonarErrors) {
					$User->Birth = $_GET["Birth"];
					// display error on registration form
					$Page = 1;
					$View->displayPageHeader();
					$View->displayErrors(false, $result);
					$View->displayRegistration(false, $User, $Page, $_GET["Code"]);
					$View->displayPageFooter();
				} else { 
					// then login user
					$result = $Model->login($_GET["Username"], $_GET["Password"]);
					if ($result instanceof SonarErrors) {
						// or display error on login form
						$View->displayPageHeader();
						$View->displayErrors(false, $result);
						$View->displayLoginForm(false, $_GET["Username"], $_GET["Password"]);
						$View->displayPageFooter();
					} else {
						// show whazzup page
						$result = $Model->getStatistics();
						if ($result instanceof SonarErrors) {
							// display error on homepage
							$View->displayPageHeader();
							$View->displayErrors(false, $result);
							$View->displayHomepage(false, $_SESSION["Location"]->Address);
							$View->displayPageFooter();
						} else {
							$View->displayPageHeader();
							$View->displayStatistics(false, $result, true);
							$View->displayPageFooter();
						}
					}
				}
			} else $View->displayRegistration(true, $User, $Page, $_SESSION["Code"]);
		break;
		
		// getTermsAndConditions
		case "getTermsAndConditions":
			$result = $Model->getTermsAndConditions();
			if ($result instanceof SonarErrors) {
				// or display error on registration form
				$View->displayPageHeader();
				$View->displayErrors(false, $result);
				$View->displayHomepage(false);
				$View->displayPageFooter();
			} else $View->displayTermsAndConditions(true, $result);
		break;

		// getHelp
		case "getHelp":
			$result = $Model->getHelp($_GET["key"]);
			$View->displayHelp(true, $result);
		break;
			
		// homepage
		default: 
			$View->displayHomepage(true);	
		break;
				
	}
	
}
	
?>
Return current item: MySonar