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;
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("

View File

@ -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 <em>%s</em> n'a pas été trouvé, veuillez vérifier votre lien."
[duration]
day = "un jour"

View File

@ -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('<h3>%s</h3>', L::save_missingId);
} elseif (!$db->loadCloud($_POST['fid'])) {
echo sprintf('<h3>%s</h3>', 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('<h3>%s</h3>', L::save_success);
}
echo sprintf('<h3>%s</h3>', L::save_success);
include 'templates/footer.php';