Skip to content
Snippets Groups Projects
Commit 941156ce authored by ALGLAVE Ivan's avatar ALGLAVE Ivan
Browse files

Non tested -> favourites are synchronised with database when connected TODO :...

Non tested -> favourites are synchronised with database when connected TODO : make database connection on every page
parent 6984760c
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,16 @@ ...@@ -8,6 +8,16 @@
<body> <body>
<h1>My Account</h1> <h1>My Account</h1>
<?php
include "scripts/SQL.php";
echo connect('ivanalglave@outlook.fr', '123');
echo '<br>';
echo get_email();
echo '<br>';
echo get_favourites();
?>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -3,18 +3,22 @@ ...@@ -3,18 +3,22 @@
<head> <head>
<title>Drinkpedia</title> <title>Drinkpedia</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head> </head>
<body> <body>
<script src="scripts/DataQuery.js"></script>
<script> <script>
var data = { getFavourites(function(e)
event: 'recipelist', {
value: sessionStorage.getItem('favourites'), var data = {
showScore: 'false', event: 'recipelist',
title: 'Favoris :' value: e,
}; showScore: 'false',
window.top.postMessage(data, [data.event, data.value, data.showScore, data.title]); title: 'Favoris :'
};
window.top.postMessage(data, [data.event, data.value, data.showScore, data.title]);
})
</script> </script>
</body> </body>
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<title>Drinkpedia</title> <title>Drinkpedia</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/RecipeList.css"> <link rel="stylesheet" type="text/css" href="css/RecipeList.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head> </head>
<body> <body>
...@@ -58,6 +59,7 @@ ...@@ -58,6 +59,7 @@
</ul> </ul>
<script src="scripts/DataQuery.js"></script>
<script> <script>
document.addEventListener('keyup', function (e) document.addEventListener('keyup', function (e)
{ {
...@@ -82,24 +84,27 @@ ...@@ -82,24 +84,27 @@
function switchFavourite(img) function switchFavourite(img)
{ {
let new_src = "images/favourite_off.png"; //let new_src = "images/favourite_off.png";
if(!img.classList.contains('favouriteOn')) if(!img.classList.contains('favouriteOn'))
{ {
new_src = "images/favourite_on.png"; new_src = "images/favourite_on.png";
img.classList.add('favouriteOn'); img.classList.add('favouriteOn');
img.classList.remove('favouriteOff'); img.classList.remove('favouriteOff');
let f = sessionStorage.getItem('favourites'); /*let f = sessionStorage.getItem('favourites');
if(f != '') f += '|'; if(f != '') f += '|';
f += img.alt; f += img.alt;
sessionStorage.setItem('favourites', f); sessionStorage.setItem('favourites', f);*/
addFavourite(img.alt, function(e) {
refreshFavourites();
});
} }
else else
{ {
img.classList.remove('favouriteOn'); img.classList.remove('favouriteOn');
img.classList.add('favouriteOff'); img.classList.add('favouriteOff');
let f = sessionStorage.getItem('favourites').split('|'); /*let f = sessionStorage.getItem('favourites').split('|');
let nf = ''; let nf = '';
for(let i = 0; i < f.length; i++) for(let i = 0; i < f.length; i++)
{ {
...@@ -108,17 +113,20 @@ ...@@ -108,17 +113,20 @@
if(i != 0) nf += '|'; if(i != 0) nf += '|';
nf += f[i]; nf += f[i];
} }
} }*/
sessionStorage.setItem('favourites', nf);
removeFavourite(img.alt, function(e){
refreshFavourites();
});
} }
img.src= new_src; //img.src= new_src;
} }
function refreshFavourites() function refreshFavourites()
{ {
let collection = document.getElementsByClassName('toggleFavourite'); let collection = document.getElementsByClassName('toggleFavourite');
let favourites = sessionStorage.getItem('favourites').split('|'); /*let favourites = sessionStorage.getItem('favourites').split('|');
for(let i = 0; i < collection.length; i++) for(let i = 0; i < collection.length; i++)
{ {
let node = collection[i]; let node = collection[i];
...@@ -135,7 +143,28 @@ ...@@ -135,7 +143,28 @@
node.classList.remove('favouriteOn'); node.classList.remove('favouriteOn');
node.src = "images/favourite_off.png"; node.src = "images/favourite_off.png";
} }
} }*/
getFavourites(function(e) {
let favourites = e;
for(let i = 0; i < collection.length; i++)
{
let node = collection[i];
let r_id = node.value;
if(favourites.findIndex(val => val == node.alt) >= 0)
{
node.classList.add('favouriteOn');
node.classList.remove('favouriteOff');
node.src = "images/favourite_on.png";
}
else
{
node.classList.add('favouriteOff');
node.classList.remove('favouriteOn');
node.src = "images/favourite_off.png";
}
}
})
} }
......
...@@ -23,4 +23,126 @@ function getRecipeImage(id, callback) ...@@ -23,4 +23,126 @@ function getRecipeImage(id, callback)
function getAllIngredients(callback) function getAllIngredients(callback)
{ {
query('getAllIngredients', [], callback); query('getAllIngredients', [], callback);
}
function create_user(u_email, u_pwd, callback)
{
query('create_user', [u_email, u_pwd], callback);
}
function connect(u_email, u_pwd, callback)
{
query('connect', [u_email, u_pwd], callback);
}
function is_connected(callback)
{
query('is_connected', [], callback);
}
function get_email(callback)
{
query('get_email', [], callback);
}
function get_favourites(callback)
{
query('get_favourites', [], callback);
}
function set_favourites(favourites, callback)
{
query('set_favourites', [favourites], callback);
}
function set_password(u_newpwd, callback)
{
query('set_password', [u_newpwd], callback);
}
function getFavourites(callback)
{
is_connected(function(e)
{
alert('Connected : ' + (e ? 'true' : 'false'));
get_favourites(function(f)
{
let localFavourites = sessionStorage.getItem('favourites').split('|');
let dbFavourites = f.split('|');
let res = joinCheckDuplicates(localFavourites, dbFavourites);
callback(res);
});
})
}
function joinCheckDuplicates(e1, e2)
{
let e3 = e1;
for(let i = 0; i < e2.length; i++)
{
if(e3.indexOf(e2[i]) < 0) e3.push(e2[i]);
}
return e3;
}
function addFavourite(id, callback)
{
get_favourites(function(f)
{
let localFavourites = sessionStorage.getItem('favourites').split('|');
let dbFavourites = f.split('|');
let res = joinCheckDuplicates(localFavourites, dbFavourites);
let indexOfTest = res.indexOf(id);
if(indexOfTest < 0) res.push(id);
let resultStr = '';
let first = true;
for(let i = 0; i < res.length; i++)
{
if(res[i] != '')
{
if(!first) resultStr += '|';
else first = false;
resultStr += res[i];
}
}
sessionStorage.setItem('favourites', resultStr);
set_favourites(function(g) {
callback(resultStr);
})
});
}
function removeFavourite(id, callback)
{
get_favourites(function(f)
{
let localFavourites = sessionStorage.getItem('favourites').split('|');
let dbFavourites = f.split('|');
let res = joinCheckDuplicates(localFavourites, dbFavourites);
let indexOfToRemove = res.indexOf(id);
if(indexOfToRemove >= 0) res.splice(indexOfToRemove, 1);
let resultStr = '';
let first = true;
for(let i = 0; i < res.length; i++)
{
if(res[i] != '')
{
if(!first) resultStr += '|';
resultStr += res[i];
}
}
sessionStorage.setItem('favourites', resultStr);
set_favourites(function(g) {
callback(resultStr);
})
});
} }
\ No newline at end of file
<?php <?php
include "Functions.php"; include "Functions.php";
include "SQL.php";
$funcName = $_POST['functionname']; $funcName = $_POST['functionname'];
...@@ -26,9 +27,36 @@ ...@@ -26,9 +27,36 @@
{ {
echo implode('|', getAllIngredients()); echo implode('|', getAllIngredients());
} }
else if($funcName == 'create_user')
{
echo create_user($_POST['arguments'][0], $_POST['arguments'][1]);
}
else if($funcName == 'connect')
{
echo connect($_POST['arguments'][0], $_POST['arguments'][1]);
}
else if($funcName == 'is_connected')
{
echo is_connected();
}
else if($funcName == 'get_email')
{
echo get_email();
}
else if($funcName == 'get_favourites')
{
echo get_favourites();
}
else if($funcName == 'set_favourites')
{
echo set_favourites($_POST['arguments'][0]);
}
else if($funcName == 'set_password')
{
echo set_password($_POST['arguments'][0]);
}
else else
{ {
echo 'error - unknown func'; echo 'error - unknown func';
} }
?> ?>
\ No newline at end of file
<?php
$bdd = new PDO('mysql:host=localhost;dbname=drinkpedia;charset=utf8', 'root', '', array(
PDO::ATTR_PERSISTENT => true
));
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$email = '';
function create_user($u_email, $u_pwd)
{
global $bdd;
$bdd->query('INSERT INTO user (email, pwdhash, favourites) VALUES (\'' . $u_email . '\', \'' . $u_pwd . '\', \'1|2|4\')');
}
function connect($u_email, $u_pwd)
{
global $bdd, $email;
$res = $bdd->query('SELECT pwdhash FROM user WHERE email LIKE \'' . $u_email . '\'');
if($res)
{
$res = $res->fetch();
if(count($res) > 0)
{
$fetched_pwd = $res[0];
if($fetched_pwd == $u_pwd)
{
$email = $u_email;
return true;
}
}
}
return false;
}
function is_connected()
{
global $email;
return $email == '' ? false : true;
}
function get_email()
{
global $email;
return $email;
}
function get_favourites()
{
global $bdd, $email;
if($email != '')
{
return $bdd->query('SELECT favourites FROM user WHERE email LIKE \'' . $email . '\'')->fetch()[0];
}
return '';
}
function set_favourites($favourites)
{
global $bdd, $email;
if($email != '')
{
$bdd->query('UPDATE user SET favourites=\'' . $favourites . '\' WHERE email like \'' . $email . '\'');
}
}
function set_password($u_newpwd)
{
global $bdd, $email;
if($email != '')
{
$bdd->query('UPDATE user SET pwdhash=\'' . $u_newpwd . '\' WHERE email like \'' . $email . '\'');
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment