Add load and local save of the cloud

This commit is contained in:
Gregory Trolliet 2020-11-25 22:13:59 +01:00
parent 98b3498de2
commit fea36cb867
3 changed files with 118 additions and 37 deletions

115
db.php
View file

@ -14,12 +14,17 @@ class DataBase
const DEFAULT_SIZE = 3; const DEFAULT_SIZE = 3;
private $db; private $db;
private $cloud;
public function __construct() public function __construct()
{ {
$this->init(); $this->init();
} }
/**
* Initialize the database
*
* Store the PDO object in $db private class variable
*/
public function init() public function init()
{ {
require_once 'dbconfig.php'; require_once 'dbconfig.php';
@ -30,15 +35,19 @@ class DataBase
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) { } catch (PDOException $e) {
echo $e->getMessage(); echo $e->getMessage();
return null;
} }
} }
/**
* Check if the database is initialized or not
* @return boolean True if db is inizialized
*/
public function isInit() public function isInit()
{ {
return isset($this->db); return isset($this->db);
} }
/**
* Build the database tables, only if not existing
*/
public function buildTables() public function buildTables()
{ {
$stmt = $this->db->prepare(" $stmt = $this->db->prepare("
@ -65,31 +74,35 @@ class DataBase
"); ");
$stmt->execute(); $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)) { if (empty($word)) {
return false; return false;
} }
$stmt = $this->db->prepare(" if (isset($cloud)) {
SELECT * if (!$this->loadCloud($cloud)) {
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 {
return false; return false;
} }
}
if (!$this->isCloudSet()) {
return false;
}
$stmt = $this->db->prepare(" $stmt = $this->db->prepare("
SELECT * SELECT *
FROM words FROM words
WHERE cloud_id = :cid; WHERE cloud_id = :cid;
"); ");
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); $stmt->bindValue(':cid', $this->cloud['id'], PDO::PARAM_INT);
$stmt->execute(); $stmt->execute();
while($data = $stmt->fetch()) { while($data = $stmt->fetch()) {
if (areWordsSimilar($data['word'], $word)) { if (areWordsSimilar($data['word'], $word)) {
@ -112,7 +125,7 @@ class DataBase
VALUES (:w, :cid); VALUES (:w, :cid);
"); ");
$stmt->bindValue(':w', $word, PDO::PARAM_STR); $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(); $stmt->execute();
} }
} }
@ -204,7 +217,69 @@ class DataBase
} }
return true; 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) public function getCloudSize(string $ref)
{ {
$stmt = $this->db->prepare(" $stmt = $this->db->prepare("

View file

@ -42,6 +42,8 @@ message = "Vous pouvez entrer ici un ou plusieurs mots, les mots vides ne seront
submit = "Enregistrer" submit = "Enregistrer"
word = "Mot %d" word = "Mot %d"
success = "Vos mots ont bien été enregistrés, merci de votre participation." 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 <em>%s</em> n'a pas été trouvé, veuillez vérifier votre lien."
[duration] [duration]
day = "un jour" day = "un jour"

View file

@ -7,24 +7,28 @@ if (empty($_POST)) {
} }
include 'templates/header.php'; include 'templates/header.php';
$id = null; if (!isset($_POST['fid'])) {
$cpt = 0; echo sprintf('<h3>%s</h3>', L::save_missingId);
$already_used = array(); } elseif (!$db->loadCloud($_POST['fid'])) {
foreach ($_POST as $name => $value) { echo sprintf('<h3>%s</h3>', L::save_cloudNotFound($_POST['fid']));
$cpt; } else {
$cpt = 0;
$already_used = array();
foreach ($_POST as $name => $value) {
if ($name == 'fid') { if ($name == 'fid') {
$id = $value;
continue; continue;
} }
if (isset($id) && !in_array($value, $already_used)) { $cpt++;
if (!in_array($value, $already_used)) {
$already_used[] = $value; $already_used[] = $value;
$value = trim(strtolower($value)); $value = trim(strtolower($value));
$db->addWord($id, $value); $db->addWord($value);
} }
if ($cpt > 9) { if ($cpt > 9) {
break; break;
} }
}
echo sprintf('<h3>%s</h3>', L::save_success);
} }
echo sprintf('<h3>%s</h3>', L::save_success);
include 'templates/footer.php'; include 'templates/footer.php';