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('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();
if (!$db->isInit()) {
echo 'Error db init';
@ -12,9 +35,25 @@ $db->cleanCloud();
$length = 6;
$token = bin2hex(random_bytes($length));
while (!$db->createCloud($token)) {
if (isset($_POST['fsize']) && is_numeric($_POST['fsize'])) {
$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));
$cpt++;
}
?>
@ -33,4 +72,5 @@ while (!$db->createCloud($token)) {
</div>
</div>
<?php
}
include('templates/footer.php');

View File

@ -41,10 +41,30 @@ a {
/******************* Header *******************/
#navbar {
height: 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 {
height: 400px;
}
.cloud_words label:after {
content: ': ';
}

155
db.php
View File

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

View File

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

View File

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

View File

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

View File

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

BIN
test.db

Binary file not shown.