Permalink
Browse files

Show only available dates and times

  • Loading branch information...
vojtasvoboda committed Aug 17, 2014
1 parent f18a4fb commit 9a189ec54a5ecae5b5056d9affdf36b0c068c97f
Showing with 113 additions and 23 deletions.
  1. +4 −18 forms/RegistrationForm.php
  2. +9 −5 index.php
  3. +100 −0 models/Reservations.php
View
@@ -8,29 +8,15 @@
*/
class RegistrationForm extends Form {
public function __construct($name = NULL) {
public function __construct($dates, $times) {
parent::__construct($name);
parent::__construct();
// select box for choosing date
$this->addSelect('date', 'Den:',
array(
'2014-07-04' => '4.7.',
'2014-07-05' => '5.7.',
'2014-07-06' => '6.7.',
'2014-07-07' => '7.7.',
'2014-07-08' => '8.7.',
)
)->setRequired('Vyberte prosím datum');
$this->addSelect('date', 'Den:', $dates)->setRequired('Vyberte prosím datum');
// select box for choosing time
$this->addSelect('time', 'Čas:',
array(
'09:00:00' => '9:00',
'12:00:00' => '12:00',
'15:00:00' => '15:00',
)
)->setRequired('Vyberte prosím datum');
$this->addSelect('time', 'Čas:', $times)->setRequired('Vyberte prosím datum');
// inputs for name and e-mail
$this->addText('name', 'Jméno:')->setRequired('Zadejte prosím jméno');
View
@@ -9,16 +9,20 @@
// enable debugger
Tracy\Debugger::enable();
// reservations management
$connection = new Connection();
$reservations = new Reservations($connection);
// získáme volné datumy a časy
$dates = $reservations->getDates();
$times = $reservations->getTimes();
// create registration form
$form = new RegistrationForm();
$form = new RegistrationForm($dates, $times);
// form sent and valid
if ($form->isSuccess()) {
// reservations management
$connection = new Connection();
$reservations = new Reservations($connection);
// form values
$values = $form->getValues();
View
@@ -14,6 +14,30 @@ class Reservations {
/** @var string $table */
private $table;
/**
* Dates
*
* @var array
*/
private $dates = array(
'2014-07-04' => '4.7.',
'2014-07-05' => '5.7.',
'2014-07-06' => '6.7.',
'2014-07-07' => '7.7.',
'2014-07-08' => '8.7.',
);
/**
* Times
*
* @var array
*/
private $times = array(
'09:00:00' => '9:00',
'12:00:00' => '12:00',
'15:00:00' => '15:00',
);
/**
* Constructor
*
@@ -61,4 +85,80 @@ public function create($data) {
$this->db->query('INSERT INTO ' . $this->table, $data);
}
/**
* Returns available dates for reservation
*
* @return array
*/
public function getDates() {
// maximum combinations for one date
$max = $this->getMaxDatesCombination();
// gets saved terms with count of reservations
$result = $this->db->query('SELECT COUNT(id) as pocet, date FROM ' . $this->table . ' WHERE 1 GROUP BY date');
// iterate all rows, but dont modify default dates
$dates = $this->dates;
foreach($result as $r) {
// if some term has maximum count of reservations, remove them
if ( $r->pocet >= $max ) {
$termDate = $r->date;
unset($dates[$termDate->format('Y-m-d')]);
}
}
// return available dates
return $dates;
}
/**
* Returns available times for reservation
*
* @return array
*/
public function getTimes() {
// maximum combinations for one time
$max = $this->getMaxTimesCombination();
// gets saved times with count of reservations
$result = $this->db->query('SELECT COUNT(id) as pocet, time FROM ' . $this->table . ' WHERE 1 GROUP BY time');
// iterate all rows, but dont modify default times
$times = $this->times;
foreach($result as $r) {
// if some time has maximum count of reservations, remove them
if ( $r->pocet >= $max ) {
$termTime = $r->time;
unset($times[$termTime->format('H:i:s')]);
}
}
// returns available times
return $times;
}
/**
* Returns number of combination for one date
*
* @return int
*/
private function getMaxDatesCombination() {
return count($this->times) * self::MAX_PERSON_PER_TERM;
}
/**
* Returns number of combination for one time
*
* @return int
*/
private function getMaxTimesCombination() {
return count($this->dates) * self::MAX_PERSON_PER_TERM;
}
}

0 comments on commit 9a189ec

Please sign in to comment.