From fea36cb86748e0a014bb3bad5845a934acffea2e Mon Sep 17 00:00:00 2001 From: Gregory Trolliet Date: Wed, 25 Nov 2020 22:13:59 +0100 Subject: [PATCH] Add load and local save of the cloud --- db.php | 115 ++++++++++++++++++++++++++++++++++++++--------- lang/lang_fr.ini | 2 + save.php | 38 +++++++++------- 3 files changed, 118 insertions(+), 37 deletions(-) diff --git a/db.php b/db.php index fdfe2f9..310b409 100644 --- a/db.php +++ b/db.php @@ -14,12 +14,17 @@ class DataBase const DEFAULT_SIZE = 3; private $db; + private $cloud; public function __construct() { $this->init(); } - + /** + * Initialize the database + * + * Store the PDO object in $db private class variable + */ public function init() { require_once 'dbconfig.php'; @@ -30,15 +35,19 @@ class DataBase $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); } catch (PDOException $e) { echo $e->getMessage(); - return null; } } - + /** + * Check if the database is initialized or not + * @return boolean True if db is inizialized + */ public function isInit() { return isset($this->db); } - + /** + * Build the database tables, only if not existing + */ public function buildTables() { $stmt = $this->db->prepare(" @@ -65,31 +74,35 @@ class DataBase "); $stmt->execute(); } - - public function addWord(string $cloud, string $word) + /** + * Add a word to a cloud + * + * If the cloud is specified, it will be loaded and the word + * will be added. + * If the cloud isn't specified, the already loaded cloud will be used. + * @param string $word Word to add + * @param string|int $cloud Id or code of the cloud + */ + public function addWord(string $word, $cloud = null) { if (empty($word)) { return false; } - $stmt = $this->db->prepare(" - SELECT * - FROM clouds - WHERE code = :id; - "); - $stmt->bindValue(':id', $cloud, PDO::PARAM_STR); - $stmt->execute(); - $cloudId = null; - if ($data = $stmt->fetch()) { - $cloudId = $data['id_cloud']; - } else { + if (isset($cloud)) { + if (!$this->loadCloud($cloud)) { + return false; + } + } + if (!$this->isCloudSet()) { return false; } + $stmt = $this->db->prepare(" SELECT * FROM words WHERE cloud_id = :cid; "); - $stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); + $stmt->bindValue(':cid', $this->cloud['id'], PDO::PARAM_INT); $stmt->execute(); while($data = $stmt->fetch()) { if (areWordsSimilar($data['word'], $word)) { @@ -112,7 +125,7 @@ class DataBase VALUES (:w, :cid); "); $stmt->bindValue(':w', $word, PDO::PARAM_STR); - $stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); + $stmt->bindValue(':cid', $this->cloud['id'], PDO::PARAM_INT); $stmt->execute(); } } @@ -204,7 +217,69 @@ class DataBase } return true; } - + public function loadCloud($ref) + { + if (is_int($ref)) { + return $this->loadCloudById($ref); + } elseif (is_string($ref)) { + return $this->loadCloudByCode($ref); + } + } + public function loadCloudById(int $id) + { + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE id_cloud = :id; + "); + $stmt->bindValue(':id', $id, PDO::PARAM_INT); + $stmt->execute(); + if ($data = $stmt->fetch()) { + $this->cloud = array( + 'id' => $data['id_cloud'], + 'code' => $data['code'], + 'size' => $data['size'], + 'delete_t' => $data['delete_t'], + 'text' => $data['test'], + ); + return true; + } + $this->cloud = null; + return false; + } + public function loadCloudByCode(string $code) + { + $stmt = $this->db->prepare(" + SELECT * + FROM clouds + WHERE code= :code; + "); + $stmt->bindValue(':code', $code, PDO::PARAM_STR); + $stmt->execute(); + if ($data = $stmt->fetch()) { + $this->cloud = array( + 'id' => $data['id_cloud'], + 'code' => $data['code'], + 'size' => $data['size'], + 'delete_t' => $data['delete_t'], + 'text' => $data['test'], + ); + return true; + } + $this->cloud = null; + return false; + } + public function isCloudSet() + { + return isset($this->cloud); + } + public function getCloudId() + { + if ($this->isCloudSet()) { + return $this->cloud['id']; + } + return null; + } public function getCloudSize(string $ref) { $stmt = $this->db->prepare(" diff --git a/lang/lang_fr.ini b/lang/lang_fr.ini index d29e24e..b41be26 100644 --- a/lang/lang_fr.ini +++ b/lang/lang_fr.ini @@ -42,6 +42,8 @@ message = "Vous pouvez entrer ici un ou plusieurs mots, les mots vides ne seront submit = "Enregistrer" word = "Mot %d" success = "Vos mots ont bien été enregistrés, merci de votre participation." +missingId = "Le formulaire est mal formé, veuillez contacter la personne responsable du site." +cloudNotFound = "Le nuage %s n'a pas été trouvé, veuillez vérifier votre lien." [duration] day = "un jour" diff --git a/save.php b/save.php index 00a8818..5a25fff 100644 --- a/save.php +++ b/save.php @@ -7,24 +7,28 @@ if (empty($_POST)) { } include 'templates/header.php'; -$id = null; -$cpt = 0; -$already_used = array(); -foreach ($_POST as $name => $value) { - $cpt; - if ($name == 'fid') { - $id = $value; - continue; - } - if (isset($id) && !in_array($value, $already_used)) { - $already_used[] = $value; - $value = trim(strtolower($value)); - $db->addWord($id, $value); - } - if ($cpt > 9) { - break; +if (!isset($_POST['fid'])) { + echo sprintf('

%s

', L::save_missingId); +} elseif (!$db->loadCloud($_POST['fid'])) { + echo sprintf('

%s

', L::save_cloudNotFound($_POST['fid'])); +} else { + $cpt = 0; + $already_used = array(); + foreach ($_POST as $name => $value) { + if ($name == 'fid') { + continue; + } + $cpt++; + if (!in_array($value, $already_used)) { + $already_used[] = $value; + $value = trim(strtolower($value)); + $db->addWord($value); + } + if ($cpt > 9) { + break; + } } + echo sprintf('

%s

', L::save_success); } -echo sprintf('

%s

', L::save_success); include 'templates/footer.php';