public function doControl(\permag\model\LoginHandler $lh, \permag\view\PageNavigationView $pageNavView)
 {
     $outputHTML = '';
     $lw = new \permag\view\LoginView();
     $message = '';
     // logged in?
     if ($lh->isLoggedIn()) {
         // clicked logout?
         if ($lw->triedToLogOut()) {
             $lh->doLogout();
             $lw->killCookie();
             $pageNavView->redirectTo($pageNavView->getLoginLink());
         }
         // not logged in?
     } else {
         // login cookie is set
         if ($lw->cookieExists()) {
             // auto login using username/password from cookie
             $userInfo_a = array();
             $userInfo_a = $lw->readLoginCookie();
             if ($lh->doLogin($userInfo_a[0], $userInfo_a[1])) {
                 $message = $lw::USER_LOGGED_IN;
             }
         }
         // clicked login?
         if ($lw->triedToLogIn()) {
             // if username/passw is correct
             if ($lh->doLogin($lw->getUsername(), $lw->getPassword())) {
                 // if remember me is checked, save to cookie
                 if ($lw->rememberMe()) {
                     $lw->saveCookie();
                 }
                 $pageNavView->redirectTo($pageNavView->getHomeLink());
             } else {
                 $message = $lw::LOGIN_ERROR;
             }
         }
     }
     // logged in again?
     if ($lh->isLoggedIn()) {
         $outputHTML .= $lh->getLoggedInUsername();
         $outputHTML .= $lw->doLogoutBox();
     } else {
         $regLink = $pageNavView->getRegisterLink();
         $outputHTML .= $lw->doLoginBox($regLink);
     }
     // get the user message from the view
     $messageHTML = $lw->outputMessage($message);
     return $outputHTML . $messageHTML;
 }
 public function doControl(\permag\model\LoginHandler $lh, \permag\database\Database $db, \permag\view\PageNavigationView $pageNavView)
 {
     $outputHTML = '';
     $message_a = array();
     $regHandler = new \permag\model\RegisterHandler($db);
     $regView = new \permag\view\RegisterView();
     if ($lh->isLoggedIn() == false) {
         $outputHTML .= $regView->doRegisterView();
         // clicked register button
         if ($regView->triedToRegister()) {
             // check empty input
             if ($regView->emptyFieldsExists()) {
                 $message_a[] = \permag\view\RegisterView::EMPTY_FIELDS;
             } else {
                 $validationErrorCount = 0;
                 // validate input
                 if ($regView->validateUsername() == false) {
                     // add error messages
                     $message_a[] = $regView->getValidationError();
                     $validationErrorCount++;
                 }
                 if ($regView->validatePassword() == false) {
                     // add error messages
                     $message_a[] = $regView->getValidationError();
                     $validationErrorCount++;
                 }
                 if ($regView->validateEmail() == false) {
                     // add error messages
                     $message_a[] = $regView->getValidationError();
                     $validationErrorCount++;
                 }
                 if ($validationErrorCount == 0) {
                     // check if username exists
                     if ($regHandler->usernameExists($regView->getUsername()) || $regHandler->usernameIsReserved($regView->getUsername())) {
                         // username exists already, or is reserved, show message
                         $message_a[] = \permag\view\RegisterView::USERNAME_ALREADY_EXISTS;
                     } else {
                         // check password fields match
                         if ($regView->inputPasswordsMatch()) {
                             // register (username, password, email)
                             if ($regHandler->doRegister($regView->getUsername(), $regView->getPassword(), $regView->getEmail())) {
                                 $pageNavView->redirectTo($pageNavView->getLoginLink());
                             }
                         } else {
                             $message_a[] = \permag\view\RegisterView::PASSWORDS_DONT_MATCH;
                         }
                     }
                 }
             }
         }
         $messageHTML = $regView->outputMessage($message_a);
         return $outputHTML . $messageHTML;
     } else {
         $pageNavView->redirectTo($pageNavView->getHomeLink());
     }
 }