From baa592b81f601143713dd178c58d0a3576ca9adb Mon Sep 17 00:00:00 2001 From: Gregory Trolliet Date: Mon, 16 Nov 2020 21:31:59 +0100 Subject: [PATCH] First working version --- create.php | 44 +++++++++++- css/style.css | 22 +++++- db.php | 155 ++++++++++++++++++++++++++++++++----------- index.php | 13 ++-- save.php | 3 +- templates/footer.php | 2 +- templates/header.php | 4 +- test.db | Bin 20480 -> 16384 bytes 8 files changed, 192 insertions(+), 51 deletions(-) diff --git a/create.php b/create.php index bca5c73..61682c3 100644 --- a/create.php +++ b/create.php @@ -2,6 +2,29 @@ include('templates/header.php'); include('db.php'); + +if (empty($_POST)) { +?> +
+
+ + + + + + + +
+
+isInit()) { echo 'Error db init'; @@ -12,9 +35,25 @@ $db->cleanCloud(); $length = 6; $token = bin2hex(random_bytes($length)); - -while (!$db->createCloud($token)) { +if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) { + $size = $_POST['fsize']; +} else { + $size = 3; +} +if (isset($_POST['ftext'])) { + $text = $_POST['ftext']; +} else { + $text = ''; +} +if (isset($_POST['fduration']) && in_array($_POST['fduration'], DataBase::OPTIONS_DURATION)) { + $duration = $_POST['fduration']; +} else { + $duration = DataBase::DEFAULT_DURATION; +} +$cpt = 0; +while (!$db->createCloud($token, $text, $size, $duration) && $cpt < 10) { $token = bin2hex(random_bytes($length)); + $cpt++; } ?> @@ -33,4 +72,5 @@ while (!$db->createCloud($token)) { '+1 day', + 'week' => '+7 day', + '2week' => '+14 day', + 'month' => '+1 month' + ); + private $db; public function __construct() @@ -28,21 +36,28 @@ class DataBase public function buildTables() { - $stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS clouds(' - . 'id_cloud INTEGER PRIMARY KEY, ' - . 'code TEXT NOT NULL UNIQUE, ' - . 'create_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP);' - ); + $stmt = $this->db->prepare(" + CREATE TABLE IF NOT EXISTS clouds( + id_cloud INTEGER PRIMARY KEY, + code TEXT NOT NULL UNIQUE, + size INTEGER NOT NULL DEFAULT 3, + text TEXT, + delete_t TIMESTAMP DEFAULT + (datetime('now', '" . self::DEFAULT_DURATION . "')) + ); + "); $stmt->execute(); - $stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS words(' - . 'id_word INTEGER PRIMARY KEY, ' - . 'word TEXT NOT NULL, ' - . 'count INT DEFAULT 1, ' - . 'cloud_id INT NOT NULL, ' - . 'FOREIGN KEY (cloud_id) ' - . 'REFERENCES clouds(id_cloud) ON UPDATE CASCADE ' - . 'ON DELETE CASCADE);' - ); + $stmt = $this->db->prepare(" + CREATE TABLE IF NOT EXISTS words( + id_word INTEGER PRIMARY KEY, + word TEXT NOT NULL, + count INT DEFAULT 1, + cloud_id INT NOT NULL, + FOREIGN KEY (cloud_id) + REFERENCES clouds(id_cloud) ON UPDATE CASCADE + ON DELETE CASCADE + ); + "); $stmt->execute(); } @@ -51,7 +66,11 @@ class DataBase if (empty($word)) { return false; } - $stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;'); + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code = :id; + "); $stmt->bindValue(':id', $cloud, PDO::PARAM_STR); $stmt->execute(); $cloudId = null; @@ -60,23 +79,29 @@ class DataBase } else { return false; } - $stmt = $this->db->prepare('SELECT * FROM words ' - . 'WHERE cloud_id = :cid AND word = :w;' - ); + $stmt = $this->db->prepare(" + SELECT * + FROM words + WHERE cloud_id = :cid + AND word = :w; + "); $stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); $stmt->bindValue(':w', $word, PDO::PARAM_STR); $stmt->execute(); if ($data = $stmt->fetch()) { $wordId = $data['id_word']; - $stmt = $this->db->prepare( - 'UPDATE words SET count = count + 1 WHERE id_word = :id;' - ); + $stmt = $this->db->prepare(" + UPDATE words + SET count = count + 1 + WHERE id_word = :id; + "); $stmt->bindValue(':id', $wordId, PDO::PARAM_INT); $stmt->execute(); } else { - $stmt = $this->db->prepare( - 'INSERT INTO words(word, cloud_id) VALUES(:w, :cid);' - ); + $stmt = $this->db->prepare(" + INSERT INTO words(word, cloud_id) + VALUES (:w, :cid); + "); $stmt->bindValue(':w', $word, PDO::PARAM_STR); $stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); $stmt->execute(); @@ -84,10 +109,13 @@ class DataBase } public function getWords(string $id) { - $stmt = $this->db->prepare('SELECT * FROM words ' - . 'JOIN clouds ON words.cloud_id = clouds.id_cloud ' - . 'WHERE code = :id;' - ); + $stmt = $this->db->prepare(" + SELECT * + FROM words + JOIN clouds + ON words.cloud_id = clouds.id_cloud + WHERE code = :id; + "); $stmt->bindValue(':id', $id); $stmt->execute(); $words = []; @@ -105,8 +133,6 @@ class DataBase public function getWordsPercentage(string $id) { $words = $this->getWords($id); - //echo 'test'; - //var_dump($words); $total = 0; foreach ($words as $word) { $total += $word[1]; @@ -121,8 +147,6 @@ class DataBase public function getWordsMax(string $id) { $words = $this->getWords($id); - //echo 'test'; - //var_dump($words); $max = 0; foreach ($words as $word) { $max = max($max, $word[1]); @@ -134,10 +158,29 @@ class DataBase return $words; } - public function createCloud(string $ref) + public function createCloud( + string $ref, + string $text = '', + int $size = null, + string $duration = null) { - $stmt = $this->db->prepare('INSERT INTO clouds(code) VALUES(:code);'); + if (!isset($size)) { + $size = 3; + } + if (!isset($duration)) { + $duration = self::DEFAULT_DURATION; + } elseif (!in_array($duration, self::OPTIONS_DURATION)) { + $duration = self::DEFAULT_DURATION; + } + $duration = date('Y-m-d H:i:s', strtotime($duration)); + $stmt = $this->db->prepare(" + INSERT INTO clouds(code, text, size, delete_t) + VALUES (:code, :text, :size, :duration); + "); $stmt->bindValue(':code', $ref); + $stmt->bindValue(':text', $text); + $stmt->bindValue(':size', $size); + $stmt->bindValue(':duration', $duration, PDO::PARAM_STR); try { $stmt->execute(); } catch (PDOEXception $e) { @@ -146,18 +189,54 @@ class DataBase return true; } + public function getCloudSize(string $ref) + { + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code= :code; + "); + $stmt->bindValue(':code', $ref, PDO::PARAM_STR); + $stmt->execute(); + if ($data = $stmt->fetch()) { + return $data['size']; + } + return null; + } + + public function getCloudText(string $ref) + { + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code= :code; + "); + $stmt->bindValue(':code', $ref, PDO::PARAM_STR); + $stmt->execute(); + if ($data = $stmt->fetch()) { + return $data['text']; + } + return null; + } + public function cleanCloud() { - $stmt = $this->db->prepare('DELETE FROM clouds ' - . "WHERE create_t < (SELECT datetime('now', '-1 week'));" - ); + $stmt = $this->db->prepare(" + DELETE + FROM clouds + WHERE delete_t < CURRENT_TIMESTAMP; + "); $stmt->execute(); return true; } public function isCloud(string $id) { - $stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;'); + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code = :id; + "); $stmt->bindValue(':id', $id, PDO::PARAM_STR); $stmt->execute(); if ($stmt->fetch()) { diff --git a/index.php b/index.php index d0bd21b..199426e 100644 --- a/index.php +++ b/index.php @@ -15,17 +15,18 @@ if (isset($_GET['id'])) { echo 'NO CLOUD'; return; } - $nbWords = 3; + $nbWords = $db->getCloudSize($id); + echo sprintf('

%s

', $db->getCloudText($id)); ?> -
+ - - -'; + echo sprintf('', $name, $i+1); + echo sprintf('', $name, $name); + echo ''; } ?> diff --git a/save.php b/save.php index fe896a0..39a6679 100644 --- a/save.php +++ b/save.php @@ -32,5 +32,6 @@ if (empty($id)) { header('Location: index.php'); die(); } -header('Location: result.php?id=' . $id); +header('Location: index.php'); +//header('Location: result.php?id=' . $id); die(); diff --git a/templates/footer.php b/templates/footer.php index 86cce16..ee48883 100644 --- a/templates/footer.php +++ b/templates/footer.php @@ -1,6 +1,6 @@ - +
diff --git a/test.db b/test.db index 9a136dae8fa78d0bf0557c38228d9138ef9b04d0..11596a3a0e67b778e55f32ecf0b6d2e0b0d252de 100644 GIT binary patch delta 510 zcmZozz}V2hI6+E?L5YEZfdz5YvCdAJ&j zS=hxTB^jF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