Navigation Menu

Skip to content

Commit

Permalink
Terms validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtasvoboda committed Aug 17, 2014
1 parent d5db58f commit f18a4fb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 3 additions & 1 deletion forms/RegistrationForm.php
Expand Up @@ -34,7 +34,9 @@ public function __construct($name = NULL) {

// inputs for name and e-mail
$this->addText('name', 'Jméno:')->setRequired('Zadejte prosím jméno');
$this->addText('email', 'E-mail:')->setRequired('Zadejte prosím e-mail');
$this->addText('email', 'E-mail:')
->setRequired('Zadejte prosím e-mail')
->addRule(Form::EMAIL, 'E-mail musí být ve formátu jmeno@domena.cz');

// checkbox for contest terms
$checkboxLabel = Html::el();
Expand Down
24 changes: 19 additions & 5 deletions index.php
@@ -1,5 +1,6 @@
<?php

// load all classes
require 'vendor/autoload.php';
require 'forms/RegistrationForm.php';
require 'models/Connection.php';
Expand All @@ -11,15 +12,28 @@
// create registration form
$form = new RegistrationForm();

// form sent
if ($form->isSubmitted()) {
// form sent and valid
if ($form->isSuccess()) {

// reservations
// reservations management
$connection = new Connection();
$reservations = new Reservations($connection);

// if is form valid
if ( $form->isValid() ) {
// form values
$values = $form->getValues();

// check e-mail
if (!$reservations->checkEmail($values->email)) {
$form->addError('Na tento e-mail je již provedena rezervace.');
}

// check date and time
if (!$reservations->isFree($values->date, $values->time)) {
$form->addError('Tato hodina je již obsazena, zkuste vybrat jinou.');
}

// if is form still valid
if ($form->isValid()) {

// then save reservation
$reservations->create($form->getValues());
Expand Down
11 changes: 7 additions & 4 deletions models/Reservations.php
Expand Up @@ -5,6 +5,9 @@
*/
class Reservations {

/** max amout of reservations for one term (one time within one date) */
const MAX_PERSON_PER_TERM = 2;

/** @var \Connection $db */
private $db;

Expand All @@ -29,8 +32,8 @@ public function __construct(Connection $connection) {
* @return bool
*/
public function checkEmail($email) {
$exists = $this->db->query('SELECT email FROM ' . $this->table . ' WHERE email = ?', $email)->count();
return $exists;
$exists = $this->db->query('SELECT id FROM ' . $this->table . ' WHERE email = ?', $email)->count();
return $exists == 0;
}

/**
Expand All @@ -42,8 +45,8 @@ public function checkEmail($email) {
* @return bool
*/
public function isFree($date, $time) {
$exists = $this->db->query('SELECT date FROM ' . $this->table . ' WHERE date = ? AND time = ?', $date, $time)->count();
return $exists;
$count = $this->db->query('SELECT id FROM ' . $this->table . ' WHERE date = ? AND time = ?', $date, $time)->count();
return $count < self::MAX_PERSON_PER_TERM;
}

/**
Expand Down

0 comments on commit f18a4fb

Please sign in to comment.