Permalink
Browse files

Saving to database

  • Loading branch information...
vojtasvoboda committed Aug 17, 2014
1 parent d2908da commit bc947808985751e87cbc3fed3a36b3d9b4d78eba
Showing with 119 additions and 1 deletion.
  1. +1 −0 forms/.htaccess
  2. +24 −1 index.php
  3. +1 −0 models/.htaccess
  4. +21 −0 models/Connection.php
  5. +61 −0 models/Reservations.php
  6. +1 −0 sql/.htaccess
  7. +10 −0 sql/reservations.sql
View
@@ -0,0 +1 @@
deny from all
View
@@ -2,6 +2,11 @@
require 'vendor/autoload.php';
require 'forms/RegistrationForm.php';
require 'models/Connection.php';
require 'models/Reservations.php';
// enable debugger
Tracy\Debugger::enable();
?>
<!doctype html>
@@ -15,7 +20,7 @@
<meta name="copyright" content="" />
<meta name="language" content="cs" />
<!-- <script src="js/netteForms.js"></script> -->
<script src="js/netteForms.js"></script>
<style>
th { text-align: right; }
@@ -29,6 +34,24 @@
// create registration form
$form = new RegistrationForm();
// form sent
if ($form->isSubmitted()) {
// reservations
$connection = new Connection();
$reservations = new Reservations($connection);
// if is form valid
if ( $form->isValid() ) {
// then save reservation
$reservations->create($form->getValues());
echo "Saved!";
}
}
// renders the form
echo $form;
View
@@ -0,0 +1 @@
deny from all
View
@@ -0,0 +1,21 @@
<?php
/**
* Class Connection
*/
class Connection extends DibiConnection {
/**
* Return connection to database
*/
public function __construct() {
$options = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'kviff',
);
parent::__construct($options);
}
}
View
@@ -0,0 +1,61 @@
<?php
/**
* Class Reservations
*/
class Reservations {
/** @var \Connection $db */
private $db;
/** @var string $table */
private $table;
/**
* Constructor
*
* @param Connection $connection
*/
public function __construct(Connection $connection) {
$this->db = $connection;
$this->table = 'reservations';
}
/**
* E-mail validation. One e-mail can make only one registration.
*
* @param string $email
*
* @return bool
*/
public function checkEmail($email) {
$exists = $this->db->query('SELECT email FROM ' . $this->table . ' WHERE email = ?', $email)->count();
return $exists;
}
/**
* Term validation.
*
* @param mixed $date
* @param mixed $time
*
* @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;
}
/**
* Create new reservation
*
* @param $data
*/
public function create($data) {
$data['created%sql'] = 'NOW()';
$data['ip'] = $_SERVER['REMOTE_ADDR'];
unset($data['terms']);
$this->db->query('INSERT INTO ' . $this->table, $data);
}
}
View
@@ -0,0 +1 @@
deny from all
View
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS `reservations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`time` time NOT NULL,
`name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
`email` varchar(255) COLLATE utf8_czech_ci NOT NULL,
`created` datetime NOT NULL,
`ip` varchar(255) COLLATE utf8_czech_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;

0 comments on commit bc94780

Please sign in to comment.