First working version

This commit is contained in:
Gregory Trolliet 2020-11-16 21:31:59 +01:00
parent 8a6973fcb6
commit baa592b81f
8 changed files with 192 additions and 51 deletions

View file

@ -2,6 +2,29 @@
include('templates/header.php'); include('templates/header.php');
include('db.php'); include('db.php');
if (empty($_POST)) {
?>
<div id="cloud_create">
<form method="post" action="create.php">
<label for="ftext">Description du nuage</label>
<input type="text" id="ftext" name="ftext" placeholder="Votre texte ici">
<label for="fduration">Durée de vie du nuage</label>
<select id="fduration" name="fduration">
<?php
foreach (DataBase::OPTIONS_DURATION as $name => $duration) {
echo sprintf('<option valu="%s">%s</option>', $name, $name);
}
?>
</select>
<label for="fsize">Nombre de mots par entrée (entre 1 et 9)</label>
<input type="number" id="fsize" name="fsize" min="1" max="9">
<input type="submit" value="Créer">
</form>
</div>
<?php
} else {
$db = new DataBase(); $db = new DataBase();
if (!$db->isInit()) { if (!$db->isInit()) {
echo 'Error db init'; echo 'Error db init';
@ -12,9 +35,25 @@ $db->cleanCloud();
$length = 6; $length = 6;
$token = bin2hex(random_bytes($length)); $token = bin2hex(random_bytes($length));
if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
while (!$db->createCloud($token)) { $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)); $token = bin2hex(random_bytes($length));
$cpt++;
} }
?> ?>
@ -33,4 +72,5 @@ while (!$db->createCloud($token)) {
</div> </div>
</div> </div>
<?php <?php
}
include('templates/footer.php'); include('templates/footer.php');

View file

@ -41,10 +41,30 @@ a {
/******************* Header *******************/ /******************* Header *******************/
#navbar { #navbar {
height: 2em;
font-size: 2em; font-size: 2em;
margin: 2rem 1rem;
} }
/******************* Footer *******************/
#footer {
width: 100%;
text-align: center;
margin-top: 1em;
}
/******************* Container *******************/
.container {
margin: 0 1em;
}
/******************* Others *******************/
#my_canvas { #my_canvas {
height: 400px; height: 400px;
} }
.cloud_words label:after {
content: ': ';
}

151
db.php
View file

@ -1,6 +1,14 @@
<?php <?php
class DataBase class DataBase
{ {
const DEFAULT_DURATION = '+7 day';
const OPTIONS_DURATION = array(
'day' => '+1 day',
'week' => '+7 day',
'2week' => '+14 day',
'month' => '+1 month'
);
private $db; private $db;
public function __construct() public function __construct()
@ -28,21 +36,28 @@ class DataBase
public function buildTables() public function buildTables()
{ {
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS clouds(' $stmt = $this->db->prepare("
. 'id_cloud INTEGER PRIMARY KEY, ' CREATE TABLE IF NOT EXISTS clouds(
. 'code TEXT NOT NULL UNIQUE, ' id_cloud INTEGER PRIMARY KEY,
. 'create_t TIMESTAMP DEFAULT CURRENT_TIMESTAMP);' 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->execute();
$stmt = $this->db->prepare('CREATE TABLE IF NOT EXISTS words(' $stmt = $this->db->prepare("
. 'id_word INTEGER PRIMARY KEY, ' CREATE TABLE IF NOT EXISTS words(
. 'word TEXT NOT NULL, ' id_word INTEGER PRIMARY KEY,
. 'count INT DEFAULT 1, ' word TEXT NOT NULL,
. 'cloud_id INT NOT NULL, ' count INT DEFAULT 1,
. 'FOREIGN KEY (cloud_id) ' cloud_id INT NOT NULL,
. 'REFERENCES clouds(id_cloud) ON UPDATE CASCADE ' FOREIGN KEY (cloud_id)
. 'ON DELETE CASCADE);' REFERENCES clouds(id_cloud) ON UPDATE CASCADE
ON DELETE CASCADE
); );
");
$stmt->execute(); $stmt->execute();
} }
@ -51,7 +66,11 @@ class DataBase
if (empty($word)) { if (empty($word)) {
return false; 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->bindValue(':id', $cloud, PDO::PARAM_STR);
$stmt->execute(); $stmt->execute();
$cloudId = null; $cloudId = null;
@ -60,23 +79,29 @@ class DataBase
} else { } else {
return false; return false;
} }
$stmt = $this->db->prepare('SELECT * FROM words ' $stmt = $this->db->prepare("
. 'WHERE cloud_id = :cid AND word = :w;' SELECT *
); FROM words
WHERE cloud_id = :cid
AND word = :w;
");
$stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT); $stmt->bindValue(':cid', $cloudId, PDO::PARAM_INT);
$stmt->bindValue(':w', $word, PDO::PARAM_STR); $stmt->bindValue(':w', $word, PDO::PARAM_STR);
$stmt->execute(); $stmt->execute();
if ($data = $stmt->fetch()) { if ($data = $stmt->fetch()) {
$wordId = $data['id_word']; $wordId = $data['id_word'];
$stmt = $this->db->prepare( $stmt = $this->db->prepare("
'UPDATE words SET count = count + 1 WHERE id_word = :id;' UPDATE words
); SET count = count + 1
WHERE id_word = :id;
");
$stmt->bindValue(':id', $wordId, PDO::PARAM_INT); $stmt->bindValue(':id', $wordId, PDO::PARAM_INT);
$stmt->execute(); $stmt->execute();
} else { } else {
$stmt = $this->db->prepare( $stmt = $this->db->prepare("
'INSERT INTO words(word, cloud_id) VALUES(:w, :cid);' INSERT INTO words(word, cloud_id)
); 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', $cloudId, PDO::PARAM_INT);
$stmt->execute(); $stmt->execute();
@ -84,10 +109,13 @@ class DataBase
} }
public function getWords(string $id) public function getWords(string $id)
{ {
$stmt = $this->db->prepare('SELECT * FROM words ' $stmt = $this->db->prepare("
. 'JOIN clouds ON words.cloud_id = clouds.id_cloud ' SELECT *
. 'WHERE code = :id;' FROM words
); JOIN clouds
ON words.cloud_id = clouds.id_cloud
WHERE code = :id;
");
$stmt->bindValue(':id', $id); $stmt->bindValue(':id', $id);
$stmt->execute(); $stmt->execute();
$words = []; $words = [];
@ -105,8 +133,6 @@ class DataBase
public function getWordsPercentage(string $id) public function getWordsPercentage(string $id)
{ {
$words = $this->getWords($id); $words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$total = 0; $total = 0;
foreach ($words as $word) { foreach ($words as $word) {
$total += $word[1]; $total += $word[1];
@ -121,8 +147,6 @@ class DataBase
public function getWordsMax(string $id) public function getWordsMax(string $id)
{ {
$words = $this->getWords($id); $words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$max = 0; $max = 0;
foreach ($words as $word) { foreach ($words as $word) {
$max = max($max, $word[1]); $max = max($max, $word[1]);
@ -134,10 +158,29 @@ class DataBase
return $words; 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(':code', $ref);
$stmt->bindValue(':text', $text);
$stmt->bindValue(':size', $size);
$stmt->bindValue(':duration', $duration, PDO::PARAM_STR);
try { try {
$stmt->execute(); $stmt->execute();
} catch (PDOEXception $e) { } catch (PDOEXception $e) {
@ -146,18 +189,54 @@ class DataBase
return true; 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() public function cleanCloud()
{ {
$stmt = $this->db->prepare('DELETE FROM clouds ' $stmt = $this->db->prepare("
. "WHERE create_t < (SELECT datetime('now', '-1 week'));" DELETE
); FROM clouds
WHERE delete_t < CURRENT_TIMESTAMP;
");
$stmt->execute(); $stmt->execute();
return true; return true;
} }
public function isCloud(string $id) 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->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute(); $stmt->execute();
if ($stmt->fetch()) { if ($stmt->fetch()) {

View file

@ -15,17 +15,18 @@ if (isset($_GET['id'])) {
echo 'NO CLOUD'; echo 'NO CLOUD';
return; return;
} }
$nbWords = 3; $nbWords = $db->getCloudSize($id);
echo sprintf('<p class="cloud_text">%s</p>', $db->getCloudText($id));
?> ?>
<form 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;?>">
<?php <?php
for ($i=0;$i<$nbWords;$i++) { for ($i=0;$i<$nbWords;$i++) {
$name = 'fword' . $i; $name = 'fword' . $i;
?> echo '<div>';
<label for="<?php echo $name;?>">test</label> echo sprintf('<label for="%s">Mot %d</label>', $name, $i+1);
<input type="text" id="<?php echo $name;?>" name="<?php echo $name;?>"> echo sprintf('<input type="text" id="%s" name="%s">', $name, $name);
<?php echo '</div>';
} }
?> ?>
<input type="submit" value="Submit"> <input type="submit" value="Submit">

View file

@ -32,5 +32,6 @@ if (empty($id)) {
header('Location: index.php'); header('Location: index.php');
die(); die();
} }
header('Location: result.php?id=' . $id); header('Location: index.php');
//header('Location: result.php?id=' . $id);
die(); die();

View file

@ -1,6 +1,6 @@
</div> </div>
<div id="footer"> <div id="footer">
WordsCloud -- Gregory Trolliet SimpleWordsCloud -- Gregory Trolliet
</div> </div>
<?php <?php
if (!empty($jsWordcloud)) { if (!empty($jsWordcloud)) {

View file

@ -8,11 +8,11 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content=""> <meta name="author" content="">
<link rel="stylesheet" href="css/style.css"> <link rel="stylesheet" href="css/style.css">
<script src="wordcloud2.js"></script> <script src="js/wordcloud2.js"></script>
</head> </head>
<body> <body>
<div id="navbar"> <div id="navbar">
<a href="index.php">WordsCloud</a> <a href="index.php">SimpleWordsCloud</a>
</div> </div>
<div class="container"> <div class="container">

BIN
test.db

Binary file not shown.