Load the cloud before any action

This commit is contained in:
Gregory Trolliet 2020-11-25 23:24:43 +01:00
parent 3ff96924fb
commit 76ab05c8c7
3 changed files with 22 additions and 33 deletions

43
db.php
View file

@ -308,38 +308,27 @@ class DataBase
} }
return null; return null;
} }
public function getCloudSize(string $ref) public function getCloudSize()
{ {
$stmt = $this->db->prepare(" if ($this->isCloudSet()) {
SELECT * return $this->cloud['size'];
FROM clouds
WHERE code= :code;
");
$stmt->bindValue(':code', $ref, PDO::PARAM_STR);
$stmt->execute();
if ($data = $stmt->fetch()) {
return $data['size'];
}
return 0;
}
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; return null;
} }
public function countWords(string $cloud) public function getCloudText()
{ {
if ($this->isCloudSet()) {
return $this->cloud['text'];
}
return null;
}
public function countWords()
{
if (!$this->isCloudSet()) {
return null;
}
$stmt = $this->db->prepare(" $stmt = $this->db->prepare("
SELECT count(*) as count SELECT count(*) as count
FROM clouds FROM clouds
@ -347,7 +336,7 @@ class DataBase
ON id_cloud = cloud_id ON id_cloud = cloud_id
WHERE code = :code; WHERE code = :code;
"); ");
$stmt->bindValue(':code', $cloud, PDO::PARAM_STR); $stmt->bindValue(':code', $this->cloud['code'], PDO::PARAM_STR);
$stmt->execute(); $stmt->execute();
if ($data = $stmt->fetch()) { if ($data = $stmt->fetch()) {
return $data['count']; return $data['count'];

View file

@ -4,12 +4,12 @@ include('templates/header.php');
if (isset($_GET['id'])) { if (isset($_GET['id'])) {
$id = $_GET['id']; $id = $_GET['id'];
if (!$db->isCloud($id)) { if (!$db->loadCloud($id)) {
echo L::cloud_noId; echo L::cloud_noId;
return; return;
} }
$nbWords = $db->getCloudSize($id); $nbWords = $db->getCloudSize();
echo sprintf('<p class="cloud_text">%s</p>', $db->getCloudText($id)); echo sprintf('<p class="cloud_text">%s</p>', $db->getCloudText());
?> ?>
<form class="cloud_words" method="post" action="save.php"> <form class="cloud_words" method="post" action="save.php">
<input type="hidden" id="fid" name="fid" value="<?php echo $id;?>"> <input type="hidden" id="fid" name="fid" value="<?php echo $id;?>">

View file

@ -4,14 +4,14 @@ include('templates/header.php');
if (isset($_GET['id'])) { if (isset($_GET['id'])) {
$id = $_GET['id']; $id = $_GET['id'];
if (!$db->isCloud($id)) { if (!$db->loadCloud($id)) {
echo '<p>' . L::cloud_noId . '</p>'; echo '<p>' . L::cloud_noId . '</p>';
$id = null; $id = null;
} }
} }
if (isset($id)) { if ($db->isCloudSet()) {
$nbWords = $db->countWords($id); $nbWords = $db->countWords();
if ($nbWords == 0) { if ($nbWords == 0) {
echo sprintf('<h2>%s</h2>', L::cloud_empty); echo sprintf('<h2>%s</h2>', L::cloud_empty);
} else { } else {