/**
  * Default controller command
  * Shows and handles authorization form
  */
 public function index()
 {
     if ($this->auth->isAuthorized()) {
         $this->redirect('/profile/');
     }
     $form = new Form();
     // Login field
     $loginField = new InputField('login', 'Login');
     $loginField->addValidator(new RequiredValidator());
     // Password field
     $passwordField = new PasswordField('password', 'Password');
     $passwordField->addValidator(new RequiredValidator());
     $form->addField($loginField)->addField($passwordField)->addField(new SubmitField('Login'));
     if (!empty($_POST)) {
         $form->populate($_POST);
         if ($form->validate()->isValid()) {
             try {
                 $this->auth->login($_POST['login'], $_POST['password'], isset($_POST['remember']) ? $_POST['remember'] : false);
                 $this->redirect('/profile/');
             } catch (LoginNotFoundException $e) {
                 $form->getField('login')->setError($e->getMessage());
             } catch (IncorrectPasswordException $e) {
                 $form->getField('password')->setError($e->getMessage());
             } catch (Exception $e) {
                 (new ErrorsController())->error503();
             }
         }
     }
     $formString = $form->render();
     Renderer::getInstance()->render('index', ['form' => $formString]);
 }
 /**
  * Default controller command
  * Shows and handles registration form
  */
 public function index()
 {
     if ($this->auth->isAuthorized()) {
         $this->redirect('/profile/');
     }
     $form = new Form();
     // Login field
     $loginField = new InputField('login', 'Login');
     $loginField->addValidator(new RequiredValidator())->addValidator(new MinLengthValidator(3))->addValidator(new MaxLengthValidator(20));
     // Password field
     $passwordField = new PasswordField('password', 'Password');
     $passwordField->addValidator(new RequiredValidator())->addValidator(new PasswordValidator());
     // Repeat password field
     $repeatPasswordField = new PasswordField('password2', 'Repeat password');
     $repeatPasswordField->addValidator(new RequiredValidator())->addValidator(new EqualToValidator($passwordField));
     // First name field
     $firstNameField = new InputField('first_name', 'First name');
     $firstNameField->addValidator(new RequiredValidator());
     // Last name field
     $lastNameField = new InputField('last_name', 'Last name');
     $lastNameField->addValidator(new RequiredValidator());
     // E-mail field
     $emailField = new EmailField('email', 'E-mail');
     $emailField->addValidator(new EmailValidator());
     // Phone field
     $phoneField = new PhoneField('phone', 'Phone');
     $phoneField->addValidator(new MaxLengthValidator(20));
     // Birth date field
     $birthDateField = new DateField('birth_date', 'Birth date');
     $birthDateField->addValidator(new DateValidator());
     $form->addField($loginField)->addField($passwordField)->addField($repeatPasswordField)->addField($firstNameField)->addField($lastNameField)->addField($emailField)->addField($phoneField)->addField($birthDateField)->addField(new SubmitField('Register'));
     if (!empty($_POST)) {
         $form->populate($_POST);
         if ($form->validate()->isValid()) {
             try {
                 (new ProfileModel())->create($_POST);
                 $this->redirect('/registration/ok/');
             } catch (ModelException $e) {
                 $form->getField('login')->setError($e->getMessage());
             } catch (DatabaseException $e) {
                 (new ErrorsController())->error503();
             }
         }
     }
     $formString = $form->render();
     Renderer::getInstance()->render('registration', ['form' => $formString]);
 }
 /**
  * Default controller command
  * Shows and handles user's profile form
  */
 public function index()
 {
     $this->auth->isAuthorized() ? $userId = $this->auth->getUserId() : $this->goHome();
     $form = new Form();
     // First name field
     $firstNameField = new InputField('first_name', 'First name');
     $firstNameField->addValidator(new RequiredValidator());
     // Last name field
     $lastNameField = new InputField('last_name', 'Last name');
     $lastNameField->addValidator(new RequiredValidator());
     // E-mail field
     $emailField = new EmailField('email', 'E-mail');
     $emailField->addValidator(new EmailValidator());
     // Phone field
     $phoneField = new PhoneField('phone', 'Phone');
     $phoneField->addValidator(new MaxLengthValidator(20));
     // Birth date field
     $birthDateField = new DateField('birth_date', 'Birth date');
     $birthDateField->addValidator(new DateValidator());
     $form->addField($firstNameField)->addField($lastNameField)->addField($emailField)->addField($phoneField)->addField($birthDateField)->addField(new SubmitField('Save'));
     try {
         if (!empty($_POST)) {
             $form->populate($_POST);
             if ($form->validate()->isValid()) {
                 $values = array_merge($_POST, ['id' => $userId]);
                 $this->profileModel->update($values);
                 $this->redirect('/profile/');
             }
         } else {
             $user = $this->profileModel->get($userId);
             if (is_null($user)) {
                 $this->logout();
             }
             $fields = ['first_name' => $user->firstName, 'last_name' => $user->lastName, 'email' => $user->email, 'phone' => $user->phone, 'birth_date' => $user->birthDate];
             $this->profileModel->convertDates($fields, ['birth_date']);
             $form->populate($fields);
         }
         $formString = $form->render();
         Renderer::getInstance()->render('profile', ['form' => $formString]);
     } catch (DatabaseException $e) {
         (new ErrorsController())->error503();
     }
 }