Permalink
Browse files

Terms validation

  • Loading branch information...
vojtasvoboda committed Aug 17, 2014
1 parent d5db58f commit f18a4fbb7b5db50224114c4ab723e57bb51949ba
Showing with 29 additions and 10 deletions.
  1. +3 −1 forms/RegistrationForm.php
  2. +19 −5 index.php
  3. +7 −4 models/Reservations.php
@@ -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();
View
@@ -1,5 +1,6 @@
<?php
// load all classes
require 'vendor/autoload.php';
require 'forms/RegistrationForm.php';
require 'models/Connection.php';
@@ -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());
View
@@ -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;
@@ -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;
}
/**
@@ -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;
}
/**

0 comments on commit f18a4fb

Please sign in to comment.