Created
October 4, 2018 06:55
-
-
Save markmathur/e2593152ddfcaaedc8f7353e7ada568e to your computer and use it in GitHub Desktop.
Code in L2 assignment (1dv610) to discuss.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace controller; | |
require_once('./view/RegisterHandling.php'); | |
require_once('./view/LayoutView.php'); | |
require_once('./view/DateTimeView.php'); | |
require_once('./view/LoginView.php'); | |
require_once('./view/LoginHandling.php'); | |
require_once('view/CookieHandler.php'); | |
/** | |
* This is the controller that handles all the incoming http-requests from | |
* the client, except the initial one. | |
*/ | |
class MainController { | |
// public function handleRequestFrClient ($queryStringfrClient) { | |
public function handleRequestFrClient () { | |
$layoutView = new \view\LayoutView(); | |
$loginView = new \view\LoginView(); | |
$dateTimeView = new \view\DateTimeView(); | |
$registerHandling = new \view\RegisterHandling(); // Important object. $message is the information sent back to the user. | |
$loginHandling = new \view\LoginHandling(); | |
$cookieHandler = new \view\CookieHandler(); | |
if ($cookieHandler->checkIfLoggedIn()) { | |
$loginView->message = ''; | |
$layoutView->render(true, $loginView->response(true), $dateTimeView); | |
} elseif (isset($_POST['LoginView::Login'])) { | |
// When client tries to login. | |
if (!$loginHandling->isLoginDataCorrect($loginView)) { | |
$loginView->message = $loginHandling->message; | |
$layoutView->render(false, $loginView->response(), $dateTimeView); | |
} else { | |
$loginHandling->loginUser(); | |
$loginView->message = $loginHandling->message; | |
$layoutView->render(true, $loginView->response(true), $dateTimeView); | |
} | |
} elseif (isset($_POST['RegisterView::Register'])) { | |
// When client submits form on Register page. | |
if (!$registerHandling->isRegistrationDataCorrect()) { | |
// If something is wrong with users input, like too short password. | |
$layoutView->render(false, $registerHandling->generateRegisterHTML(), $dateTimeView); | |
} else { | |
// Successful registration | |
$registerHandling->registerUser(); | |
$loginView->enteredName = $_POST['RegisterView::UserName']; | |
$loginView->message = 'Registered new user.'; | |
$layoutView->render(false, $loginView->response(), $dateTimeView); | |
} | |
} elseif (isset($_GET['register'])) { | |
// When client comes through link on login page. | |
$layoutView->render(false, $registerHandling->generateRegisterHTML(), $dateTimeView); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment