First version of the WordsCloud site

This commit is contained in:
Gregory Trolliet 2020-11-15 21:51:28 +01:00
commit 8a6973fcb6
13 changed files with 1656 additions and 0 deletions

9
addWord.php Normal file
View file

@ -0,0 +1,9 @@
<?php
?>
<form method="post" action="index.php">
<label for="fword" class="required">Mot</label>
<input type="text" id="fword" name="fword" placeholder="Mot à entrer" required/>
<input type="hidden" id="fcloud" name="fcloud" value="<?php echo $cloud ?>" >
<input type="submit" value="Réserver"/>
</form>

36
create.php Normal file
View file

@ -0,0 +1,36 @@
<?php
include('templates/header.php');
include('db.php');
$db = new DataBase();
if (!$db->isInit()) {
echo 'Error db init';
return;
}
$db->cleanCloud();
$length = 6;
$token = bin2hex(random_bytes($length));
while (!$db->createCloud($token)) {
$token = bin2hex(random_bytes($length));
}
?>
<div id="cloud_links">
<div id="cloud_links_results" class="cloud_link">
<span class="label">Voici le lien pour visualiser le nuage de mots</span>
<span class="link">
<a href="result.php?id=<?php echo $token ?>">id=<?php echo $token ?></a>
</span>
</div>
<div id="cloud_links_vote" class="cloud_link">
<span class="label">Voici le lien pour participer au nuage</span>
<span class="link">
<a href="index.php?id=<?php echo $token ?>">id=<?php echo $token ?></a>
</span>
</div>
</div>
<?php
include('templates/footer.php');

50
css/style.css Normal file
View file

@ -0,0 +1,50 @@
/******************* Constants *******************/
:root {
--font-color: #EFEFEF;
--font-color-em: #8C8C8C;
--background-color-main: #404040;
--background-color-secondary: #505050;
--avatar-background: #B6B6B6;
--color01: #99CCFF;
--color01_bright: #58af58;
--color02: #ab7026;
--color02_bright: #ea9a36;
--nav-maxwidth: 900px;
--nav-height: 5em;
--body-maxwidth: 900px;
--footer-maxwidth: 900px;
--footer-height: 9em;
--padding-border: 1em;
}
/******************* Page *******************/
* {
box-sizing: border-box;
}
html {
background-color: var(--background-color-main);
font-family: Lato, Helvetica, sans-serif;
font-size: large;
color: var(--font-color);
min-height: 100vh
}
body {
margin: 0;
position: relative;
}
a {
color: var(--font-color);
}
/******************* Header *******************/
#navbar {
height: 2em;
font-size: 2em;
}
#my_canvas {
height: 400px;
}

169
db.php Normal file
View file

@ -0,0 +1,169 @@
<?php
class DataBase
{
private $db;
public function __construct()
{
$this->init();
}
public function init()
{
try {
$this->db = new PDO('sqlite:test.db');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
return null;
}
$stmt = $this->db->prepare('PRAGMA foreign_keys = 1;');
$stmt->execute();
}
public function isInit()
{
return isset($this->db);
}
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->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->execute();
}
public function addWord(string $cloud, string $word)
{
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 {
return false;
}
$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->bindValue(':id', $wordId, PDO::PARAM_INT);
$stmt->execute();
} else {
$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();
}
}
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->bindValue(':id', $id);
$stmt->execute();
$words = [];
$values = [];
while ($data = $stmt->fetch()) {
$words[] = array($data['word'], $data['count']);
$values[] = $data['count'];
$total += $data['count'];
}
array_multisort($values, SORT_DESC, $words);
return $words;
}
public function getWordsPercentage(string $id)
{
$words = $this->getWords($id);
//echo 'test';
//var_dump($words);
$total = 0;
foreach ($words as $word) {
$total += $word[1];
}
foreach ($words as $key => $word) {
$words[$key] = array($word[0], $word[1] / $total);
}
return $words;
}
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]);
}
foreach ($words as $key => $word) {
$words[$key] = array($word[0], $word[1] / $max);
}
return $words;
}
public function createCloud(string $ref)
{
$stmt = $this->db->prepare('INSERT INTO clouds(code) VALUES(:code);');
$stmt->bindValue(':code', $ref);
try {
$stmt->execute();
} catch (PDOEXception $e) {
return false;
}
return true;
}
public function cleanCloud()
{
$stmt = $this->db->prepare('DELETE FROM clouds '
. "WHERE create_t < (SELECT datetime('now', '-1 week'));"
);
$stmt->execute();
return true;
}
public function isCloud(string $id)
{
$stmt = $this->db->prepare('SELECT * FROM clouds WHERE code = :id;');
$stmt->bindValue(':id', $id, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->fetch()) {
return true;
}
return false;
}
}

19
get.php Normal file
View file

@ -0,0 +1,19 @@
<?php
include('db.php');
$db = new DataBase();
if (!$db->isInit()) {
echo 'Error db init';
return;
}
if (!isset($_GET['id'])) {
echo json_encode(false);
}
$id = $_GET['id'];
if ($words = $db->getWordsMax($id)) {
echo json_encode($words);
} else {
echo json_encode('No id');
}

43
index.php Normal file
View file

@ -0,0 +1,43 @@
<?php
include('templates/header.php');
include('db.php');
$db = new DataBase();
if (!$db->isInit()) {
echo 'Error db init';
return;
}
$db->buildTables();
if (isset($_GET['id'])) {
$id = $_GET['id'];
if (!$db->isCloud($id)) {
echo 'NO CLOUD';
return;
}
$nbWords = 3;
?>
<form 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
}
?>
<input type="submit" value="Submit">
</form>
<?php
} else {
?>
<p>Voulez-vous créer un nouveau nuage de mots?</p>
<form method="post" action="create.php">
<input type="submit" value="Créer">
</form>
<?php
}
include('templates/footer.php');
?>

32
js/display.js Normal file
View file

@ -0,0 +1,32 @@
var list = [['foo', 12], ['bar', 6], ['asd', 2], ['bar', 6], ['gert', 10]];
//document.getElementById('my_canvas').innerHTML = 'test';
//WordCloud(document.getElementById('my_canvas'), { list: list } );
console.log(list);
var oReq = new XMLHttpRequest(); // New request object
oReq.onload = function() {
//var weightFactor = function()
// This is where you handle what to do with the response.
// The actual data is found on this.responseText
var list = JSON.parse(this.responseText); // Will alert: 42
console.log(list);
var options = {};
options.list = list;
//options.gridSize = 15;
options.shuffle = true;
options.rotateRatio = 0.5;
options.weightFactor = 50;
options.minSize = 4;
options.drawOutOfBound = true;
options.backgroundColor = 'rgba(255, 255, 255, 0)';
options.color = 'random-light';
//options.origin = [400, 100];
//options.ellipticity = 5;
WordCloud(document.getElementById('my_canvas'), options );
};
var urlParams = new URLSearchParams(window.location.search);
var id = urlParams.get('id');
oReq.open("get", "get.php?id=" + id, true);
oReq.send();

1218
js/wordcloud2.js Normal file

File diff suppressed because it is too large Load diff

15
result.php Normal file
View file

@ -0,0 +1,15 @@
<?php
include('templates/header.php');
?>
<div id="my_canvas">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<?php
$jsWordcloud = true;
include('templates/footer.php');

36
save.php Normal file
View file

@ -0,0 +1,36 @@
<?php
include('db.php');
$db = new DataBase();
if (!$db->isInit()) {
echo 'Error db init';
return;
}
if (empty($_POST)) {
header('Location: index.php');
die();
}
echo '<pre>';
var_dump($_POST);
echo '</pre>';
$id = null;
foreach ($_POST as $name => $value) {
if ($name == 'fid') {
$id = $value;
continue;
}
if (isset($id)) {
$value = trim(strtolower($value));
$db->addWord($id, $value);
}
}
if (empty($id)) {
header('Location: index.php');
die();
}
header('Location: result.php?id=' . $id);
die();

11
templates/footer.php Normal file
View file

@ -0,0 +1,11 @@
</div>
<div id="footer">
WordsCloud -- Gregory Trolliet
</div>
<?php
if (!empty($jsWordcloud)) {
echo '<script type="text/javascript" src="js/display.js"></script>';
}
?>
</body>
</html>

18
templates/header.php Normal file
View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta name="description" content="Webpage description goes here" />
<meta charset="utf-8">
<title>Change_me</title>
<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>
</head>
<body>
<div id="navbar">
<a href="index.php">WordsCloud</a>
</div>
<div class="container">

BIN
test.db Normal file

Binary file not shown.