diff --git a/create_picture.php b/create_picture.php new file mode 100644 index 0000000..62266c7 --- /dev/null +++ b/create_picture.php @@ -0,0 +1,147 @@ + + + + + + + + + +
+
+
+
+ +

Upload a photo!

+ + +
+ + + + + +
+ +
+
+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+
+
+ +
+ + +
+

+
+ +
+ +
+ + + + + +
+

+
+ + + + + + + +


+
+
+ + + +
+
+

+
+
+ + + + + + \ No newline at end of file diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..c31a36a --- /dev/null +++ b/css/style.css @@ -0,0 +1,294 @@ +html { + min-height: 100%; + max-width: 100%; + position: relative; + } +h1 { + text-decoration: underline; +} + +.content { + border: 1px solid #444444; + color: #222222; +} + +p.footer { + font-style: italic; + color:white; + + +} + + +footer{ + + background-image: url(../images/issibg.png); + position:absolute; + bottom: 0px; + + width:100%; +} +#logo-lsi { + width: 100px; + height: auto; +} + +#logo-lsi:hover { + cursor: pointer; + filter: blur(4px); +} + +div.title-block{ + text-align: center; + padding-top: 1%; + padding-bottom: 1%; + background-color: #eeeeee; + background-image: url(../images/issibg.png); +} + +body{ + + background-image: url(../images/body-bg.png); + padding-bottom: 70px; + + + +} + + +#title{ + font-size: 300%; + text-decoration: none; + color :white; + font-family: sans-serif; + margin-bottom: 0; +} + +#subtitle{ + font-size:120%; + font-style:italic; + color: white; + font-family:monospace; +} + +.photo-block{ + + text-align:center; + border: 1px solid gray; + border-radius: 5%; +} + +.photo-title { + font-size: 140%; + font-family: Arial; + text-decoration: underline; +} + +.photo-score { + font-size:130%; + font-family: monospace; + +} + +img.photo-image{ + + max-height: 200px; + width:auto; + +} + +.photo-vote-block >img{ + max-height: 40px; + padding-right:.5em; + padding-left:.5em; + cursor:pointer; +} + +div.gallery-container{ + + display:grid; + grid-template-columns: 33% 33% 33%; + +} + + + +#nopadding{ + + padding:2px; +} + +.commontext{ + + color:white; + +} + +.commtextright{ + + text-align:center; +} + +.commtextend{ + + text-align:right; +} + +.userlist{ + + margin-top:20px; + font-size:20px; + +} +.listcenter { + text-align: center; + list-style-position: inside; + margin-right:16px; + padding-left: 0%; + padding-right:0%; + } + + .buttonsize{ + + width:50%; + height: auto; + + } + + #ratebutton{ + + + width:40%; + margin:auto; +} + +.specialcard{ + + + width:75%; + + + +} +.mobileShow {display: none;} +.usernolink{ + + + cursor: default; + text-decoration: none; + color: white; + + +} +.usernolink:hover{ + + + text-decoration:none; + color:white; + +} + +/*.navbar >a{ + color: white; + font-size: 120%; + background-color: #555555; + text-decoration:none; + font-family:monospace; + display: inline-block; + min-width:100px; + + + +}*/ +/* +.form-group>label{ + display:inline-block; + width:200px; + text-align:right; + +} + +.form-group{ + margin-bottom:.5em; +}*/ + +.avatar{ + + max-width: 50%; + border-radius: 50%; + + +} + + +#commoncard{ + + + width:85%; + margin:auto; + margin-bottom:50px; +} + +.usercard{ + background-color: #343a40; + border-style: solid; + border-radius: 1px; + border-color:#343a40; +} +.trendingcard{ + width:50%; + margin:auto; + margin-bottom:50px; + + +} + +.asideul{ + + background-color:#343a40; + border:solid 2px; +} + +.centeritem{ + + + display:flex;align-items: center;justify-content: center +} + +.comment{ + padding-top:15px +} + +@media only screen and (max-width: 1200px) { + + .mobileShow {display: inline;position:fixed;top:45%;z-index: 1;margin-left:10px} + + .trendingcard{ + width:85%; + margin:auto; + margin-bottom:50px; + + + } + + #commoncard{ + + + width:85%; + margin:auto; + margin-bottom:50px; + } + + .specialcard{ + + + + width:85%; + margin:auto; + margin-bottom:50px; + + + + } + +} \ No newline at end of file diff --git a/edittags.php b/edittags.php new file mode 100644 index 0000000..b900ed8 --- /dev/null +++ b/edittags.php @@ -0,0 +1,85 @@ + + + + + + Subir foto + + + + + +
+
+
+
+ +
+
+
+

EditPhoto

+
+
+ +
+ +
+
+
+
+
+ +
+ +
+ +

Existentes:

+
    + +
  • +
    + +
    + + + +
    + + +
  • + + + +
+

Nuevo nombre de la etiqueta

+
+ +
+

O bien borra la seleccionada

+ +

+ + + +
+
+
+ + +
+ +
+ + +
+
+ + + + + + + \ No newline at end of file diff --git a/following.php b/following.php new file mode 100644 index 0000000..f8cb26b --- /dev/null +++ b/following.php @@ -0,0 +1,141 @@ + + + + + + + + + +
+

Hi @loremIpsum

+

Following!



+ + +
+
+ +

+

1.Title

+
+ + + + + +
+ +
@user
+
+
Date: 02/03/2020
+ Description: Lorem Ipsum Dolor Sit Amet + +
+

Score: +30

+ + +
+ + +

Tags:

+
+ + + + +
+
+
+
+ +

2.Title

+
+ + + + + +
+
@user
+
+
Date: 02/03/2020
+ Description: Lorem Ipsum Dolor Sit Amet + +
+

Score: +30

+ + +
+ +

Tags:

+
+ + + + +
+
+
+
+ +

3.Title

+
+ + + + + +
+
@user
+
+ +
Date: 02/03/2020
+ Description: Lorem Ipsum Dolor Sit Amet + +
+

Score: +30

+ + +
+ +

Tags:

+
+ +
+
+
+
+
+
+ + + + + + +
+ + + + + + + + + + + + + + + +
+ + + + + + + + + \ No newline at end of file diff --git a/footer.php b/footer.php new file mode 100644 index 0000000..3ffc8c2 --- /dev/null +++ b/footer.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/generalprofile.php b/generalprofile.php new file mode 100644 index 0000000..ba6e5a1 --- /dev/null +++ b/generalprofile.php @@ -0,0 +1,79 @@ + + + + + + Profile + + + + + +
+
+
+ +
+
+
+

Profile

+
+
+ +
+ +
+ +
+ +
+ +
+ +
User:
+
+
Name:
+
+
Surname:
+
+
Email:
+
+
Phone:
+
+ + +
+ + +
+ +
+ + + + +
+
+ +

+
+ + + + +
+
+ + + + + + + + + \ No newline at end of file diff --git a/header.php b/header.php new file mode 100644 index 0000000..5396028 --- /dev/null +++ b/header.php @@ -0,0 +1,25 @@ +
+ +

PixiArts

+

Lets share Art!

+ +
+ + + + + +
\ No newline at end of file diff --git a/images/body-bg.png b/images/body-bg.png new file mode 100644 index 0000000..a49d724 Binary files /dev/null and b/images/body-bg.png differ diff --git a/images/dislike.png b/images/dislike.png new file mode 100644 index 0000000..273e3fc Binary files /dev/null and b/images/dislike.png differ diff --git a/images/favicon.ico b/images/favicon.ico new file mode 100644 index 0000000..cd863e7 Binary files /dev/null and b/images/favicon.ico differ diff --git a/images/iconaside.png b/images/iconaside.png new file mode 100644 index 0000000..b5f0226 Binary files /dev/null and b/images/iconaside.png differ diff --git a/images/issibg.png b/images/issibg.png new file mode 100644 index 0000000..62f3843 Binary files /dev/null and b/images/issibg.png differ diff --git a/images/like.png b/images/like.png new file mode 100644 index 0000000..26a80cf Binary files /dev/null and b/images/like.png differ diff --git a/images/lsi.png b/images/lsi.png new file mode 100644 index 0000000..029849b Binary files /dev/null and b/images/lsi.png differ diff --git a/imports.php b/imports.php new file mode 100644 index 0000000..f8e93c4 --- /dev/null +++ b/imports.php @@ -0,0 +1,13 @@ + + + + + + + + + + + + diff --git a/index.php b/index.php new file mode 100644 index 0000000..086fc94 --- /dev/null +++ b/index.php @@ -0,0 +1,63 @@ + + + + + + + + + + + + + +
+
+
+
+

+
+ + + + +
+
+ + + + + + + + + + + + + + +
+ + + + + + + + + \ No newline at end of file diff --git a/js/auth.js b/js/auth.js new file mode 100644 index 0000000..cc7c1d4 --- /dev/null +++ b/js/auth.js @@ -0,0 +1,52 @@ +function saveToken(token) { + + return new Promise((resolve, reject) => { + localStorage.setItem("token", token); + localStorage.setItem("tokenTime", new Date().getTime()); + + let userId = getTokenID(token); + localStorage.setItem("userId", userId); + + $.ajax({ + url: "http://localhost:3000/users/" + userId, + success: function(data) { + let name = data.name; + localStorage.setItem("userName", name); + resolve(); + }, + error: reject, + }); + }); + +} + +function isLogged() { + return getToken() !== null; +} + +function logout() { + localStorage.removeItem("token"); + localStorage.removeItem("tokenTime"); + localStorage.removeItem("userId"); + localStorage.removeItem("userName"); +} + +function getToken() { + let token = localStorage.getItem("token"); + // Si no hay token, da un null + + if(token !== null) { + let currentDate = new Date().getTime(); + let tokenDate = localStorage.getItem("tokenTime"); + let diff = currentDate - tokenDate; // en ms + + if(diff > 60*60*1000) { + console.log("The token has expired."); + logout(); + token = null; + } + + } + + return token; +} \ No newline at end of file diff --git a/js/editphoto.js b/js/editphoto.js new file mode 100644 index 0000000..dc576ff --- /dev/null +++ b/js/editphoto.js @@ -0,0 +1,447 @@ +/*const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); +const id2 = urlParams.get('id');*/ + +const id = window.location.search.split('=')[1]; +let tagsAux = []; +let exTags = []; +let tagsServidor = []; +let cntTag = 0; +let cntNewTag = 0; +let oldTags = []; +let tagsId = []; +let newTags = []; +let removedTags = []; +errorCounter = 0; +let bw = []; + +loadServerTags(); + +loadBadWords(); + + + +function loadBadWords(){ + + $.ajax({ + url: "http://localhost:3000/badwords", + success: function(data){ + + for(i of data){ + bw.push(i.word); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){console.log(bw)}); + + +} + +function loadServerTags(){ + + $.ajax({ + url: "http://localhost:3000/tags", + success: processServerTags, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + +function processServerTags(data){ + + + for (tag of data){ + + + tagsServidor.push(tag.tag); + tagsId.push(tag.id); + } + +} +// Función auxiliar para convertir un mensaje de error a un elemento HTML +// Si se usa mucho, sería aconsejable ponerla en un archivo común. +function getError(message) { + return ""; +} + +// Obtenemos los datos de la foto que queremos modificar y los mostramos en el formulario +function loadPhoto() { + axios.get('http://localhost:3000/photos/' + id) + .then(function (response) { + if (response.status === 200) { + showPhoto(response.data); + } + }) + .catch(console.log); +} + +// Función para mostrar los datos de la foto en el formulario cuando se haga la petición +function showPhoto(photo) { + let date = new Date(photo.date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + let date_string = `${day}/${month}/${year}`; + $("#date").text(date_string); + + $("#image").attr("src", photo.url); + $("#description-input").val(photo.description); + + let cnt = 0; + for(tag of photo.tags){ + console.log(tag); + oldTags.push(tag); + let aux = -1; + + for (var z= 0; z ${tagsServidor[aux]}`); + cnt++; + + + } + else{ + + $("li").last().append(`${tagsServidor[aux]}`); + cnt++; + } + $("span.badge").after(" "); + + tagsAux.push(tagsServidor[aux]);} + + } + $("#tags-input").val(photo.tags.join(", ")); + console.log(oldTags); +} + +// Ejecutaremos loadPhoto cuando la página esté lista +$(loadPhoto); + + + + + +$("#photo-form").submit(function (event) { + event.preventDefault(); + $("#errors-container").empty(); + + let description = $("#description-input").val(); + let tags = tagsAux; + + let tags_ = []; + + for( n = 0 ; naux){ + aux = l; + } + } + + aux++; + tagsId.push(aux); + newTags.push(aux); + + let tag = { + tag: w.val(), + + }; + let blbw = true; + for (zk of bw){ + + if (w.val().includes(zk)){ + + blbw = false; + } + } + if(blbw){ + $.ajax({ + + url: "http://localhost:3000/tags", + method: "POST", + data: JSON.stringify(tag), + contentType: "application/json", + success: true, + error: function(){ + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + });} + } + else if (tagsServidor.includes(w.val())&& !(tags.includes(w.val()))){ + tags.push(w.val()); + tags_.push(w.val()); + + } + } + + let aux2 = tags_; + tags = []; + + + for (p of newTags){ + + tags.push(p); + } + + + /*for(var numero = 0; numero +
+ +
+ +
+ +
+ +
+ `; + + $("#new-tags-list").append(html); + + + + + + + + + +} +function createTagBox(){ + cntTag++; + + let html = `
  • + +
    + + + +
    +
    + +
    +
  • `; + + $("#tags-list").append(html); + + for(numero = 0; numero${exTags[numero]}`); + } +} + + + +function processTags(){ + + + $.ajax({ + url: "http://localhost:3000/tags", + success: setTags, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + +function setTags(data){ + + for (tag of data) { + + + + let html =``; + + $("#input-tags-0").append(html); + + exTags.push(tag.tag); + + } + + + + +} + + +function del(s){ + + $(s).parent().parent().parent().remove(); + + + +} +processTags(); diff --git a/js/edittags.js b/js/edittags.js new file mode 100644 index 0000000..4132e14 --- /dev/null +++ b/js/edittags.js @@ -0,0 +1,327 @@ +/*const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); +const id2 = urlParams.get('id');*/ + +const id = window.location.search.split('=')[1]; +let tagsAux = []; +let exTags = []; +let tagsServidor = []; +let cntTag = 0; +let cntNewTag = 0; +let oldTags = []; +let tagsId = []; +let newTags = []; +let removedTags = []; +errorCounter = 0; +let bw = []; + +loadServerTags(); + +loadBadWords(); + + +function patchFotos(id2) { + + + $.ajax({ + url: "http://localhost:3000/photos", + success: function(data){processPatch(data,id2)}, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); + + + +} + +function processPatch(data,id2) { + + for (var photo of data) { + console.log(photo.tags); + + if (photo.tags.includes(id2)) { + + + let n_tags = []; + + for (p of photo.tags) { + + if (p != id2) { + + n_tags.push(p); + } + } + + let tags = n_tags; + let t = { + tags + }; + + fetch('http://localhost:3000/photos/' + photo.id, { + method: "PATCH", + body: JSON.stringify(t), + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + console.log(photo.id); + } else { + $("#errors-container").append(getError(response.statusText)); + } + }).catch(console.log); + + } + } + + window.location.href = "index.php"; +} +function apply() { + + + let new_tag = $("#input-new-name-tag").val(); + console.log(new_tag); + let p = "#input-tags-0"; + + let w = $(p)[0]; + let id2 = -1; + let cnt = 0; + let accept = $("#input-accept")[0].checked; + for (tag2 of tagsServidor) { + + if (tag2 == w[w.selectedIndex].text) { + + id2 = tagsId[cnt]; + } + cnt++; + } + + let tag = new_tag; + let t = { + tag + }; + + if (accept) { + + fetch('http://localhost:3000/tags/' + id2, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + patchFotos(id2); + } else { + console.log("Error al borrar la foto: " + response.statusText); + } + }).catch(function (error) { + console.log("Error al borrar la foto: " + error); + }); + + + + + } + + else { + + if (new_tag != null && $.trim(new_tag).length !== 0 && new_tag != undefined && !(tagsServidor.includes(new_tag))) { + + let palabrota = false; + for (z of bw) { + + + if (new_tag.includes(z)) { + + palabrota = true; + + } + } + + if (palabrota) { + + removeError($("#errors-container")); + + $("#errors-container").append( + getError("¡Cuidado con ese vocabulario!") + ); + } + + else { + + + + fetch('http://localhost:3000/tags/' + id2, { + method: "PATCH", + body: JSON.stringify(t), + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + window.location.href = "index.php"; + } else { + $("#errors-container").append(getError(response.statusText)); + } + }).catch(console.log); + + + } + + } + + + else { + + removeError($("#errors-container")); + + $("#errors-container").append( + getError("¡El contenido a modificar no es correcto!") + ); + + + + } + + + + } + + + + + + +} + +function getError(message) { + return ( + "" + ); +} + + +function removeError(error) { + $(error).empty(); +} + + +function loadBadWords() { + + $.ajax({ + url: "http://localhost:3000/badwords", + success: function (data) { + + for (i of data) { + bw.push(i.word); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { console.log(bw) }); + + +} + +function loadServerTags() { + + $.ajax({ + url: "http://localhost:3000/tags", + success: processServerTags, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + +function processServerTags(data) { + + + for (tag of data) { + + + tagsServidor.push(tag.tag); + tagsId.push(tag.id); + } + +} +// Función auxiliar para convertir un mensaje de error a un elemento HTML +// Si se usa mucho, sería aconsejable ponerla en un archivo común. +function getError(message) { + return ""; +} + +// Obtenemos los datos de la foto que queremos modificar y los mostramos en el formulario + +// Función para mostrar los datos de la foto en el formulario cuando se haga la petición + + + +function processTags() { + + + $.ajax({ + url: "http://localhost:3000/tags", + success: setTags, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + +function setTags(data) { + + for (tag of data) { + + + + let html = ``; + + $("#input-tags-0").append(html); + + exTags.push(tag.tag); + + } + + + + +} + + + +function createNewTagBox() { + cntNewTag++; + let html = `
  • +
    + +
    + +
    + +
    + +
    +
  • `; + + $("#new-tags-list").append(html); + + + +} + + + +function del(s) { + + $(s).parent().parent().parent().remove(); + +} +processTags(); diff --git a/js/generalprofile.js b/js/generalprofile.js new file mode 100644 index 0000000..6b2bd91 --- /dev/null +++ b/js/generalprofile.js @@ -0,0 +1,732 @@ +const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); +const id = urlParams.get('userId'); +const PHOTOS_PER_ROW = 3; +let photoUsId = 0; +let likes = []; +let dislikes = []; +let list_img_id = []; +let list_us = []; +let tags = []; +let tagsId = []; + + +requestUser(); +requestTags(); +//requestLikes(); + +function requestTags(){ + + $.ajax({ + url: "http://localhost:3000/tags", + success: function (data) { + + for (var tag of data) { + + tags.push(tag.tag); + tagsId.push(tag.id); + } + + + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(requestLikes); + +} + +function requestLikes(){ + + $.ajax({ + url: "http://localhost:3000/likes?userId="+localStorage.userId, + success: function(data){ + + for(w of data){ + + + likes.push(w.photoId); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){ + + $.ajax({ + url: "http://localhost:3000/dislikes?userId="+localStorage.userId, + success: function(data2){ + + for (w2 of data2){ + + dislikes.push(w2.photoId); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(requestPhotos); + }); + + + + +} + +function requestUser(){ + + $.ajax({ + url: "http://localhost:3000/users/"+id, + success: loadUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + + +function loadUser(data){ + + $("#user-user").append(data.user); + $("#user-name").append(data.name); + $("#user-surname").append(data.surname); + $("#user-phone").append(data.phone); + $("#user-mail").append(data.email); + + +} + +function requestPhotos(){ + + $.ajax({ + url: "http://localhost:3000/photos?userId="+id, + success: loadPhotos, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(processScore); + + +} + +function loadPhotos(data){ + + + let row = $("div.container-fluid > div.row").last(); + let counter = 0; + + //cambiamos photo Id por photo edit para el test + for (var photo of data) { + + if (photo.public) { + + let but_like = ``; + if (likes.includes(photo.id)) { + but_like = ``; + } + let but_dislike = ``; + + if (dislikes.includes(photo.id)) { + + but_dislike = ``; + } + let punt = 0; + + let html; + let tok = localStorage.userId; + + if (tok != photo.userId) { + html = `
    + +
    +
    ${photo.title}
    + + + +
    + + +
    +
    + +
    +
    +

    + ${photo.description} + +
    +

    +
    + ${but_like} + ${but_dislike} +
    +
    +

    Tags:

    +

    + +

    +
    + +
    Actions:

    + + + + + + + + +
    +
    `; + + + } + + else { + + html = `
    + +
    +
    ${photo.title}
    + + + +
    +

    + +
    + +

    + ${photo.description} + +
    +

    +
    + ${but_like} + ${but_dislike} +
    +
    +

    Tags:

    +

    + +

    +
    + +
    Actions:

    + + + + + + + + +
    +
    `; + + + } + + + let col = $.parseHTML(html); + row.append(col); + + let photo_date = photo.date; + let date = new Date(photo_date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + + + //let date_string = "Fecha: " + day + "/" + month + "/" + year; + let date_string = `Fecha: ${day}/${month}/${year}`; + $("p.date").last().append(date_string); + + + + let p_tags = $("p.card-text").last(); + + for (var tag of photo.tags) { + let aux = 0; + + for (var z = 0; z < tagsId.length; z++) { + console.log(tag); + if (tagsId[z] == tag) { + + aux = z; + console.log(aux); + } + } + if (aux != 0) { + + let tag_span = $("", { + "class": "badge badge-primary", + text: tags[aux], + }); + p_tags.append(tag_span); + } + } + $("span.badge").after(" "); + /* + for (var tag of photo.tags) { + let tag_span = $("", { + "class": "badge badge-primary", + text: tag, + }); + p_tags.append(tag_span); + } + $("span.badge").after(" ");*/ + + counter++; + photoUsId++; + list_img_id.push(photo.id); + list_us.push(photo.userId); + if (counter % PHOTOS_PER_ROW == 0) { + let new_row = $("
    ", { "class": "row" }); + $("div.container-fluid").append(new_row); + row = new_row; + } + + } + + } + + +} + +function storageLikesFromUser(data) { + + + for (var w of data) { + console.log(w); + + likes.push(w.photoId); + } + +} + + +function storageDislikesFromUser(data) { + + + for (var w of data) { + + + dislikes.push(w.photoId); + } + +} + +function like(photoId, zId, but) { + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + + promise.done( + + function () { + if (!(exist)) { + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + promise.done(function () { + + if (existDistict) { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + but.parentNode.children[1].classList.add('btn-danger'); + but.parentNode.children[1].classList.remove('btn-success'); + + + + } + + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + but.classList.add('btn-success'); + but.classList.remove('btn-primary'); + + $.ajax({ + + url: "http://localhost:3000/likes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(function () { + + if (existDistict) { + ProcessOneScore(photoId, zId, +2); + } + else { + + ProcessOneScore(photoId, zId, +1); + } + + + }); + + + }); + + } + + else { + + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(ProcessOneScore(photoId, zId, -1)); + but.classList.remove('btn-success'); + but.classList.add('btn-primary'); + + } + + } + ); +} +function ProcessOneScore(spId, zId, val) { + + let v = "#image-score-" + zId; + let cnt = $(v).text(); + let a = cnt.split(":"); + let va = parseInt(a[1].trim(), 10); + $(v).empty(); + $(v).append("Score : " + (va + val)); + + + + + +} +function dislike(photoId, zId, but) { + + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done( + + function () { + if (!(exist)) { + + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done(function () { + + if (existDistict) { + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + but.parentNode.children[0].classList.add('btn-primary'); + but.parentNode.children[0].classList.remove('btn-success'); + } + but.classList.remove('btn-danger'); + but.classList.add('btn-success'); + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + + $.ajax({ + + url: "http://localhost:3000/dislikes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done( + + function () { + if (existDistict) { + ProcessOneScore(photoId, zId, -2); + } + else { + + ProcessOneScore(photoId, zId, -1); + } + }); + + + }); + + } + + + else { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done(ProcessOneScore(photoId, zId, +1)); + but.classList.remove('btn-success'); + but.classList.add('btn-danger'); + } + + } + ); + +} + + + +function processScore() { + let cnt = 0; + + var a = $.ajax({ + url: "http://localhost:3000/likes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntL = 0; + + + + for (var w of a.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntL++; + + } + } + cntL = cntL++; + let v = "#image-score-" + numero; + $(v).empty(); + $(v).append(cntL); + + + + } + + var z = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntD = 0; + + for (var w of z.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntD = cntD - 1; + + } + } + + + let v = "#image-score-" + numero; + cnt = $(v).text(); + let va = parseInt(cnt, 10); + $(v).empty(); + $(v).append("Score : " + (cntD + va)); + + cnt = 0; + + + } + + + + }); + + + }); + + + + var b = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + }); +} + + +function deletePhoto(id) { + fetch('http://localhost:3000/photos/' + id, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + window.location.href = "index.php"; + } else { + console.log("Error al borrar la foto: " + response.statusText); + } + }).catch(function (error) { + console.log("Error al borrar la foto: " + error); + }); +} diff --git a/js/header.js b/js/header.js new file mode 100644 index 0000000..5497bab --- /dev/null +++ b/js/header.js @@ -0,0 +1,92 @@ +let nav = $("#navbarNav"); +let tok = localStorage.token; + +let username; + +if(tok!=null){ +$.ajax({ + url: "http://localhost:3000/users/" + localStorage.userId, + success: function(data){ + processHeaderUser(data); + username = data.user; + console.log(username); + console.log(data); + + }, + error: console.log, +}); + +} + +if (tok==null){ + + let html = ` + `; + + nav.append(html); +} + +function processHeaderUser(data){ + let html2 = ` + + + `; + +nav.append(html2); + + + +} \ No newline at end of file diff --git a/js/indexgallery.js b/js/indexgallery.js new file mode 100644 index 0000000..53bcb1a --- /dev/null +++ b/js/indexgallery.js @@ -0,0 +1,1002 @@ +"use strict"; +const PHOTOS_PER_ROW = 3; +let photoUsId = 0; +let list_us = []; +let list = []; +let tags = []; +let tagsId = []; +let list_img_id = []; +let likes = []; +let dislikes = []; + + + +function processUsername(data) { + + + + username = data.user; + + +} + +function processPhotos(data) { + let row = $("div.container-fluid > div.row").last(); + let counter = 0; + + //cambiamos photo Id por photo edit para el test + for (var photo of data) { + + + if (photo.public) { + + let html = `
    + +
    +
    ${photo.title}
    + + + +
    +

    + +
    +

    + ${photo.description} + +
    +

    + +
    +

    Tags:

    +

    + +

    + + + + + +
    +
    `; + + let col = $.parseHTML(html); + row.append(col); + + let p_tags = $("p.card-text").last(); + for (var tag of photo.tags) { + let aux = -1; + + for (var z = 0; z < tagsId.length; z++) { + console.log(tag); + if (tagsId[z] == tag) { + + aux = z; + console.log(aux); + } + } + if (aux != -1) { + + let tag_span = $("", { + "class": "badge badge-primary", + text: tags[aux], + }); + p_tags.append(tag_span); + } + } + $("span.badge").after(" "); + + /*for (var tag of photo.tags) { + let tag_span = $("", { + "class": "badge badge-primary", + text: tag, + }); + p_tags.append(tag_span); + } + $("span.badge").after(" ");*/ + + let photo_date = photo.date; + let date = new Date(photo_date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + + + //let date_string = "Fecha: " + day + "/" + month + "/" + year; + let date_string = `Fecha: ${day}/${month}/${year}`; + $("p.date").last().append(date_string); + list_us.push(photo.userId); + + list_img_id.push(photo.id); + + counter++; + photoUsId++; + //list.push(photo.userId); + if (counter % PHOTOS_PER_ROW == 0) { + let new_row = $("
    ", { "class": "row" }); + $("div.container-fluid").append(new_row); + row = new_row; + } + + } + } +} + + + + +function processPhotosLogged(data) { + let row = $("div.container-fluid > div.row").last(); + let counter = 0; + + //cambiamos photo Id por photo edit para el test + for (var photo of data) { + + + if (photo.public) { + + let but_like = ``; + if (likes.includes(photo.id)) { + but_like = ``; + } + let but_dislike = ``; + + if (dislikes.includes(photo.id)) { + + but_dislike = ``; + } + let punt = 0; + + let html; + let tok = localStorage.userId; + + if (tok != photo.userId) { + html = `
    + +
    +
    ${photo.title}
    + + + +
    +

    + +
    +
    + +
    +
    +

    + ${photo.description} + +
    +

    +
    + ${but_like} + ${but_dislike} +
    +
    +

    Tags:

    +

    + +

    +
    + +
    Actions:

    + + + + + + + + +
    +
    `; + + + } + + else { + + html = `
    + +
    +
    ${photo.title}
    + + + +
    +

    + +
    + +

    + ${photo.description} + +
    +

    +
    + ${but_like} + ${but_dislike} +
    +
    +

    Tags:

    +

    + +

    +
    + +
    Actions:

    + + + + + + + + +
    +
    `; + + + + + } + + + + + + + + + + + let col = $.parseHTML(html); + row.append(col); + + let photo_date = photo.date; + let date = new Date(photo_date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + + + //let date_string = "Fecha: " + day + "/" + month + "/" + year; + let date_string = `Fecha: ${day}/${month}/${year}`; + $("p.date").last().append(date_string); + + + + let p_tags = $("p.card-text").last(); + + for (var tag of photo.tags) { + let aux = -1; + + for (var z = 0; z < tagsId.length; z++) { + console.log(tag); + if (tagsId[z] == tag) { + + aux = z; + console.log(aux); + } + } + if (aux != -1) { + + let tag_span = $("", { + "class": "badge badge-primary", + text: tags[aux], + }); + p_tags.append(tag_span); + } + } + $("span.badge").after(" "); + /* + for (var tag of photo.tags) { + let tag_span = $("", { + "class": "badge badge-primary", + text: tag, + }); + p_tags.append(tag_span); + } + $("span.badge").after(" ");*/ + + counter++; + photoUsId++; + list_img_id.push(photo.id); + list_us.push(photo.userId); + if (counter % PHOTOS_PER_ROW == 0) { + let new_row = $("
    ", { "class": "row" }); + $("div.container-fluid").append(new_row); + row = new_row; + } + + } + + + } +} + + + + +function deletePhoto(id) { + + let cnnnt =0; + + $.ajax({ + url: "http://localhost:3000/comments?photoId=" + id, + success: function(data){ + for(var element of data) + cnnnt++; + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){ + + if(cnnnt == 0){ + fetch('http://localhost:3000/photos/' + id, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + window.location.href = "index.php"; + } else { + console.log("Error al borrar la foto: " + response.statusText); + } + }).catch(function (error) { + console.log("Error al borrar la foto: " + error); + }); + + + + } + + else{ + + $("#errors-container").append( + getError("No se puede borrar una foto con comentarios.") + ); + + } + + + + }); + + +} + +function removeError(error) { + $(error).empty(); +} + +function getError(message) { + removeError( $("#errors-container")); + return ( + "" + ); +} +function loadPhotos() { + console.log("Cargando fotos..."); + let tok = localStorage.token + if (tok == null) { + + var a = $.ajax({ + url: "http://localhost:3000/tags", + success: function (data) { + + for (var tag of data) { + + tags.push(tag.tag); + tagsId.push(tag.id); + } + + + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + $.ajax({ + url: "http://localhost:3000/likes?userId=" + localStorage.userId, + success: storageLikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + $.ajax({ + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId, + success: storageDislikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }) + + + }).done(function () { + + + var promise = $.ajax({ + url: "http://localhost:3000/photos?_sort=id&_order=desc", + success: processPhotos, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); + + promise.then(function () { + // enviar(0); + + $.ajax({ + url: "http://localhost:3000/users", + success: processUssers, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + fillUsers(); + + }); + + + }).done(processScore);; + + + }); + + + + }); + + + + + + } + + else { + + var a = $.ajax({ + url: "http://localhost:3000/tags", + success: function (data) { + + for (var tag of data) { + + tags.push(tag.tag); + tagsId.push(tag.id); + } + + + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + $.ajax({ + url: "http://localhost:3000/likes?userId=" + localStorage.userId, + success: storageLikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + $.ajax({ + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId, + success: storageDislikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }) + + + }).done(function () { + + + var promise = $.ajax({ + url: "http://localhost:3000/photos?_sort=id&_order=desc", + success: processPhotosLogged, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); + + promise.then(function () { + // enviar(0); + + $.ajax({ + url: "http://localhost:3000/users", + success: processUssers, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + fillUsers(); + + }); + + + }).done(processScore); + + + + }); + + }); + + + + + } + +} + +function storageLikesFromUser(data) { + + + for (var w of data) { + console.log(w); + + likes.push(w.photoId); + } + +} + + +function storageDislikesFromUser(data) { + + + for (var w of data) { + + + dislikes.push(w.photoId); + } + +} + +function processScore() { + let cnt = 0; + + var a = $.ajax({ + url: "http://localhost:3000/likes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntL = 0; + + + + for (var w of a.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntL++; + + } + } + cntL = cntL++; + let v = "#image-score-" + numero; + $(v).empty(); + $(v).append(cntL); + + + + } + + var z = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntD = 0; + + for (var w of z.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntD = cntD - 1; + + } + } + + + let v = "#image-score-" + numero; + cnt = $(v).text(); + let va = parseInt(cnt, 10); + $(v).empty(); + $(v).append("Score : " + (cntD + va)); + + cnt = 0; + + + } + + + + }); + + + }); + + + + var b = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + }); +} + +function processUssers(data) { + + for (var user of data) { + + list.push(user.user); + + } + + + +} + +function fillUsers() { + + for (var numero = 0; numero < photoUsId; numero++) { + let as = "#userlabel" + numero; + + + let p_user = $(as); + + p_user.append(list[list_us[numero] - 1]); + + + } + + +} + +// Llamar a loadPhotos cuando la página esté lista +$(loadPhotos); +function like(photoId, zId, but) { + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + + promise.done( + + function () { + if (!(exist)) { + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + promise.done(function () { + + if (existDistict) { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + but.parentNode.children[1].classList.add('btn-danger'); + but.parentNode.children[1].classList.remove('btn-success'); + + + + } + + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + but.classList.add('btn-success'); + but.classList.remove('btn-primary'); + + $.ajax({ + + url: "http://localhost:3000/likes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(function () { + + if (existDistict) { + ProcessOneScore(photoId, zId, +2); + } + else { + + ProcessOneScore(photoId, zId, +1); + } + + + }); + + + }); + + } + + else { + + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(ProcessOneScore(photoId, zId, -1)); + but.classList.remove('btn-success'); + but.classList.add('btn-primary'); + + } + + } + ); +} +function ProcessOneScore(spId, zId, val) { + + let v = "#image-score-" + zId; + let cnt = $(v).text(); + let a = cnt.split(":"); + let va = parseInt(a[1].trim(), 10); + $(v).empty(); + $(v).append("Score : " + (va + val)); + + + + + +} +function dislike(photoId, zId, but) { + + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done( + + function () { + if (!(exist)) { + + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done(function () { + + if (existDistict) { + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + but.parentNode.children[0].classList.add('btn-primary'); + but.parentNode.children[0].classList.remove('btn-success'); + } + but.classList.remove('btn-danger'); + but.classList.add('btn-success'); + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + + $.ajax({ + + url: "http://localhost:3000/dislikes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done( + + function () { + if (existDistict) { + ProcessOneScore(photoId, zId, -2); + } + else { + + ProcessOneScore(photoId, zId, -1); + } + }); + + + }); + + } + + + else { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done(ProcessOneScore(photoId, zId, +1)); + but.classList.remove('btn-success'); + but.classList.add('btn-danger'); + } + + } + ); + +} + + + + + + + + + + + diff --git a/js/login.js b/js/login.js new file mode 100644 index 0000000..1afd01c --- /dev/null +++ b/js/login.js @@ -0,0 +1,41 @@ +function validateForm() { + let email = $("#input-email").val(); + let password = $("#input-password").val(); + + // Hay que validar este formulario como cualquier otro!!! + + let login_data = { + email, + password, + }; + + $.ajax({ + url: "http://localhost:3000/login", + method: "POST", + contentType: "application/json", + data: JSON.stringify(login_data), + + success: handleLogin, + error: function(){ + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el login y mostrárselo al usuario + }); + + return false; +} + +function getError(message) { + return ( + "" + ); +} +function handleLogin(data) { + let token = data.accessToken; + saveToken(token).then(function () { + window.location.href = "index.php"; + }); +} diff --git a/js/mypictures.js b/js/mypictures.js new file mode 100644 index 0000000..70d591a --- /dev/null +++ b/js/mypictures.js @@ -0,0 +1,683 @@ +const PHOTOS_PER_ROW = 3; +let photoUsId = 0; +let list_img_id = []; +let likes = []; +let dislikes = []; +let tags = []; +let tagsId = []; +const maxImg = 5; + + +function addNewPictures(){ + + if(maxImg>photoUsId){ + + window.location.href = "create_picture.php"; + + + } + + else{ + + $("#errors-container").append( + getError("Se ha superado el número máximo de fotos, borra 1 para poder añadir nuevas imágenes.") + ); + + } + + + +} + + +function getError(message) { + return ( + "" + ); +} +function deletePhoto(id) { + let cnnnt =0; + + $.ajax({ + url: "http://localhost:3000/comments?photoId=" + id, + success: function(data){ + for(var element of data) + cnnnt++; + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){ + + if(cnnnt == 0){ + fetch('http://localhost:3000/photos/' + id, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function (response) { + if (response.ok) { + window.location.href = "index.php"; + } else { + console.log("Error al borrar la foto: " + response.statusText); + } + }).catch(function (error) { + console.log("Error al borrar la foto: " + error); + }); + + + + } + + else{ + + $("#errors-container").append( + getError("No se puede borrar una foto con comentarios.") + ); + + } + + + + }); +} + + + +function processPhotos(data) { + let row = $("div.container-fluid > div.row").last(); + let counter = 0; + + //cambiamos photo Id por photo edit para el test + for (photo of data) { + console.log(likes); + let but_like = ``; + if (likes.includes(photo.id)) { + but_like = ``; + } + let but_dislike = ``; + + if (dislikes.includes(photo.id)) { + + but_dislike = ``; + } + + + let html = `
    +

    ${photo.title}

    +
    + + + + + +
    + +

    + ${photo.description} + +
    +

    +
    + ${but_like} + ${but_dislike} +
    +
    +

    Tags:

    +

    + +

    +
    + +
    Actions:

    + + + +
    +
    +
    `; + + photoUsId++; + list_img_id.push(photo.id); + + let col = $.parseHTML(html); + row.append(col); + let photo_date = photo.date; + let date = new Date(photo_date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + + + //let date_string = "Fecha: " + day + "/" + month + "/" + year; + let date_string = `Fecha: ${day}/${month}/${year}`; + $("p.date").last().append(date_string); + + let p_tags = $("p.card-text").last(); + + for (tag of photo.tags) { + let aux = -1; + + for (var z = 0; z < tagsId.length; z++) { + console.log(tag); + if (tagsId[z] == tag) { + + aux = z; + console.log(aux); + } + } + if (aux != -1) { + + let tag_span = $("", { + "class": "badge badge-primary", + text: tags[aux], + }); + p_tags.append(tag_span); + } + } + $("span.badge").after(" "); + + counter++; + if (counter % PHOTOS_PER_ROW == 0) { + let new_row = $("
    ", { "class": "row" }); + $("div.container-fluid").append(new_row); + row = new_row; + } + } +} + + + +function loadPhotos() { + console.log("Cargando fotos..."); + //let tok = localStorage.token + // if(tok == null){ + let tok = localStorage.userId; + $.ajax({ + url: "http://localhost:3000/photos?_sort=id&_order=desc&userId=" + tok, + success: processPhotos, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(processScore()); + + + +} + +function processScore() { + let cnt = 0; + + var a = $.ajax({ + url: "http://localhost:3000/likes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntL = 0; + + + + for (var w of a.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntL++; + + } + } + cntL = cntL++; + let v = "#image-score-" + numero; + $(v).empty(); + $(v).append(cntL); + + + + } + + var z = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + for (var numero = 0; numero < photoUsId; numero++) { + + let cntD = 0; + + for (var w of z.responseJSON) { + + if (w.photoId == list_img_id[numero]) { + cntD = cntD - 1; + + } + } + + + let v = "#image-score-" + numero; + cnt = $(v).text(); + let va = parseInt(cnt.trim(), 10); + $(v).empty(); + $(v).append("Score : " + (cntD + va)); + + cnt = 0; + + + } + + + + }); + + + }); + + + + var b = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + }); +} + +// Llamar a loadPhotos cuando la página esté lista + + + +function storageLikesFromUser(data) { + + + for (var w of data) { + console.log(w); + + likes.push(w.photoId); + } + +} + + +function storageDislikesFromUser(data) { + + + for (var w of data) { + + + dislikes.push(w.photoId); + } + +} + +function loadPage() { + + var a = $.ajax({ + url: "http://localhost:3000/tags", + success: function (data) { + + for (var tag of data) { + + tags.push(tag.tag); + tagsId.push(tag.id); + } + + + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); + + a.done(function () { + $.ajax({ + url: "http://localhost:3000/likes?userId=" + localStorage.userId, + success: storageLikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + $.ajax({ + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId, + success: storageDislikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }) + + + }).done(loadPhotos); + }); + + +} + +function ProcessOneScore(spId, zId, val) { + + let v = "#image-score-" + zId; + cnt = $(v).text(); + let a = cnt.split(":"); + let va = parseInt(a[1].trim(), 10); + $(v).empty(); + $(v).append("Score : " + (va + val)); + + + + + +} +function like(photoId, zId, but) { + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + + promise.done( + + function () { + if (!(exist)) { + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + promise.done(function () { + + if (existDistict) { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + but.parentNode.children[1].classList.add('btn-danger'); + but.parentNode.children[1].classList.remove('btn-success'); + + + + } + + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + but.classList.add('btn-success'); + but.classList.remove('btn-primary'); + + $.ajax({ + + url: "http://localhost:3000/likes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(function () { + + if (existDistict) { + ProcessOneScore(photoId, zId, +2); + } + else { + + ProcessOneScore(photoId, zId, +1); + } + + + }); + + + }); + + } + + else { + + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(ProcessOneScore(photoId, zId, -1)); + but.classList.remove('btn-success'); + but.classList.add('btn-primary'); + + } + + } + ); +} + + +function dislike(photoId, zId, but) { + + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done( + + function () { + if (!(exist)) { + + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done(function () { + + if (existDistict) { + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + but.parentNode.children[0].classList.add('btn-primary'); + but.parentNode.children[0].classList.remove('btn-success'); + } + but.classList.remove('btn-danger'); + but.classList.add('btn-success'); + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + + $.ajax({ + + url: "http://localhost:3000/dislikes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done( + + function () { + if (existDistict) { + ProcessOneScore(photoId, zId, -2); + } + else { + + ProcessOneScore(photoId, zId, -1); + } + }); + + + }); + + } + + + else { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done(ProcessOneScore(photoId, zId, +1)); + but.classList.remove('btn-success'); + but.classList.add('btn-danger'); + } + + } + ); + +} + + + + + + + +$(loadPage); diff --git a/js/photoDetail.js b/js/photoDetail.js new file mode 100644 index 0000000..2e89d82 --- /dev/null +++ b/js/photoDetail.js @@ -0,0 +1,735 @@ +/*const queryString = window.location.search; +const urlParams = new URLSearchParams(queryString); +const id = urlParams.get('photoId');*/ +const id = window.location.search.split('=')[1]; +const photoId = id; +let photoUsId = 0; +let list_img_id = []; +let likes = []; +let dislikes = []; +let list = []; +let tags = []; +let tagsId = []; +let photId; + +list_img_id.push(window.location.search.split('=')[1]); +/* +function editPhoto() { + window.location.href = "photo_edit.php?id=" + id; +} + +function deletePhoto() { + fetch('http://localhost:3000/photos/' + id, { + method: "DELETE", + headers: { + 'Authorization': 'Bearer ' + getToken(), + } + }).then(function(response) { + if(response.ok) { + window.location.href = "index.php"; + } else { + console.log("Error al borrar la foto: " + response.statusText); + } + }).catch(function(error){ + console.log("Error al borrar la foto: " + error); + }); + } +*/ +function processPhotoLoad(data) { + $("#image").attr("src", data.url); + $("#image-title").text(data.title); + $("#image-desc").text(data.description); + + loadComments(data.id); + photId = data.id; + let photo_date = data.date; + let date = new Date(photo_date); + + let year = date.getFullYear(); + let month = date.getMonth() + 1; + let day = date.getDate(); + let tok = localStorage.userId; + list_img_id.push(data.id); + let but_like = ``; + if (likes.includes(data.id)) { + but_like = ``; + } + let but_dislike = ``; + console.log( " aaaaaa "+ dislikes ); + if (dislikes.includes(data.id)) { + + but_dislike = ``; + } + + //let date_string = "Fecha: " + day + "/" + month + "/" + year; + let date_string = `Fecha: ${day}/${month}/${year}`; + $("#image-date").text(date_string); + let p_tags = $("p.card-text").last(); + for (tag of data.tags) { + let aux = -1; + + for (var z = 0; z < tagsId.length; z++) { + console.log(tag); + if (tagsId[z] == tag) { + + aux = z; + console.log(aux); + } + } + if (aux != -1) { + + let tag_span = $("", { + "class": "badge badge-primary", + text: tags[aux], + }); + p_tags.append(tag_span); + } + } + $("span.badge").after(" "); + $("#votebutton").append(but_like); + $("#votebutton").append(but_dislike); + let score = data.upvotes - data.downvotes; + let score_text = "Puntuación: " + score; + $("#image-score").text(score_text); + + if (tok == data.userId) { + + $('#follow').remove(); + } +} + +function processUssers(data) { + + for (var user of data) { + + list.push(user.user); + + } + + + +} + +let bw = []; +loadBadWords(); + +function loadBadWords(){ + + $.ajax({ + url: "http://localhost:3000/badwords", + success: function(data){ + + for(i of data){ + bw.push(i.word); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){console.log(bw)}); + + +} + +function addComment(){ + + let comm = $("#input-comment").val(); + + let palabrota = false; + for(z of bw){ + + + if(comm.includes(z)){ + + palabrota = true; + } + } + + if(palabrota){ + + $("#errors-container").append( + getError("Esa boca.") + ); + } + + else{ + + let comment ={ + + userId: localStorage.userId, + comment: comm, + photoId : photId + } + + $.ajax({ + + url: "http://localhost:3000/comments", + method: "POST", + data: JSON.stringify(comment), + contentType: "application/json", + success: function(){ + + location.reload(); + }, + error: function(){ + $("#errors-container").append( + getError("Vaya ha habido un fallo subiendo ese comentario.") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + + } + +} + +function getError(message) { + removeError( $("#errors-container")); + return ( + "" + ); +} + + +function removeError(error) { + $(error).empty(); +} + + +function loadComments(pid){ + + $.ajax({ + url: "http://localhost:3000/users", + success: processUssers, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){ + + + $.ajax({ + url: "http://localhost:3000/comments?photoId=" + pid, + success: function(data){ + + + for(com of data){ + + $("#image-comments").append( + + `
    +
    +
    +
    + +
    ${list[com.userId-1]}
    + + +
    +
    +

    ${com.comment}

    +
    + + + +
    + +

    ` + + + ) + + + } + + + }, + error: console.log, + }); + + + + + }); + + + + + +} + + + +function loadPhoto() { + if (id === null) { + alert("Please provide a photo ID"); + } else { + $.ajax({ + url: "http://localhost:3000/photos/" + id, + success: processPhotoLoad, + error: console.log, + }).done(processScore()); + } +} +function storageLikesFromUser(data) { + + + for (var w of data) { + console.log(w); + + likes.push(w.photoId); + } + +} + + +var a = $.ajax({ + url: "http://localhost:3000/tags", + success: function (data) { + + for (var tag of data) { + + tags.push(tag.tag); + tagsId.push(tag.id); + } + + + + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } +}); + +a.done(function(){$.ajax({ + url: "http://localhost:3000/likes?userId=" + localStorage.userId, + success: storageLikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } +}).done(function () { + + $.ajax({ + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId, + success: storageDislikesFromUser, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }) + + +}).done(loadPhoto);}); +function storageDislikesFromUser(data) { + + + for (var w of data) { + + + dislikes.push(w.photoId); + + } + console.log(dislikes); +} + + // Cargar la foto cuando la página esté lista + +function processScore() { + let cnt = 0; + + var a = $.ajax({ + url: "http://localhost:3000/likes?photoId=" + id, + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + + let cntL = 0; + + + + for (var w of a.responseJSON) { + + + cntL++; + + + } + cntL = cntL++; + let v = "#image-score-0"; + $(v).empty(); + $(v).append(cntL); + + + + + + + var z = $.ajax({ + url: "http://localhost:3000/dislikes?photoId=" + id, + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + let cntD = 0; + + for (var w of z.responseJSON) { + + + cntD = cntD - 1; + + + } + + + let v = "#image-score-0"; + cnt = $(v).text(); + let va = parseInt(cnt, 10); + $(v).empty(); + $(v).append("Score : " + (cntD + va)); + + cnt = 0; + + + + + + + }); + + + }); + + + + var b = $.ajax({ + url: "http://localhost:3000/dislikes", + success: true, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + + }); +} + +// Llamar a loadPhotos cuando la página esté lista + +function like(photoId, zId, but) { + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + + promise.done( + + function () { + if (!(exist)) { + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + promise.done(function () { + + if (existDistict) { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + but.parentNode.children[1].classList.add('btn-danger'); + but.parentNode.children[1].classList.remove('btn-success'); + + + + } + + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + but.classList.add('btn-success'); + but.classList.remove('btn-primary'); + + $.ajax({ + + url: "http://localhost:3000/likes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(function () { + + if (existDistict) { + ProcessOneScore(photoId, zId, +2); + } + else { + + ProcessOneScore(photoId, zId, +1); + } + + + }); + + + }); + + } + + else { + + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).then(ProcessOneScore(photoId, zId, -1)); + but.classList.remove('btn-success'); + but.classList.add('btn-primary'); + + } + + } + ); +} + + +function dislike(photoId, zId, but) { + + + let exist; + let likeId; + let existDistict; + + var promise = $.ajax({ + + url: "http://localhost:3000/dislikes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + exist = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done( + + function () { + if (!(exist)) { + + + var promise = $.ajax({ + + url: "http://localhost:3000/likes?userId=" + localStorage.userId + "&photoId=" + photoId, + method: "GET", + + success: function (data) { + + + + for (var element of data) { + + existDistict = true; + + likeId = element.id; + } + + }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + + + promise.done(function () { + + if (existDistict) { + $.ajax({ + + url: "http://localhost:3000/likes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + but.parentNode.children[0].classList.add('btn-primary'); + but.parentNode.children[0].classList.remove('btn-success'); + } + but.classList.remove('btn-danger'); + but.classList.add('btn-success'); + let vote = { + + photoId: photoId, + userId: localStorage.userId + }; + + $.ajax({ + + url: "http://localhost:3000/dislikes", + method: "POST", + data: JSON.stringify(vote), + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done( + + function () { + if (existDistict) { + ProcessOneScore(photoId, zId, -2); + } + else { + + ProcessOneScore(photoId, zId, -1); + } + }); + + + }); + + } + + + else { + + $.ajax({ + + url: "http://localhost:3000/dislikes/" + likeId, + method: "DELETE", + + contentType: "application/json", + success: function () { }, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done(ProcessOneScore(photoId, zId, +1)); + but.classList.remove('btn-success'); + but.classList.add('btn-danger'); + } + + } + ); + +} + + +function ProcessOneScore(spId, zId, val) { + + let v = "#image-score-" + zId; + cnt = $(v).text(); + let a = cnt.split(":"); + let va = parseInt(a[1].trim(), 10); + $(v).empty(); + $(v).append("Score : " + (va + val)); + + + + + +} \ No newline at end of file diff --git a/js/register.js b/js/register.js new file mode 100644 index 0000000..a6f3820 --- /dev/null +++ b/js/register.js @@ -0,0 +1,156 @@ +let errorCounterBw = 0; +let bw = []; +loadBadWords(); + +function loadBadWords(){ + + $.ajax({ + url: "http://localhost:3000/badwords", + success: function(data){ + + for(i of data){ + bw.push(i.word); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function(){console.log(bw)}); + + +} + + +function validateForm() { + $("#errors-container").empty(); + + let errorCounter = 0; + + console.log("Asacatun"); + let nombre = $("#input-name").val(); + let apellidos = $("#input-surname").val(); + let telefono = $("#input-telephone").val(); + let email = $("#input-email").val(); + let usuario = $("#input-username").val(); + let password1 = $("#input-password1").val(); + let password2 = $("#input-password2").val(); + let accept = $("#input-accept")[0].checked; + console.log(accept); + console.log("Inicio variables"); + if (nombre.trim().length < 3) { + $("#errors-container").append( + getError("El nombre debe tener al menos 3 caracteres de longitud") + ); + errorCounter++; + } + + if (apellidos.trim().length < 6) { + $("#errors-container").append( + getError("Los apellidos deben tener al menos 6 caracteres de longitud") + ); + errorCounter++; + } + + if ( + !new RegExp("^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-s./0-9]*$").test(telefono) + ) { + $("#errors-container").append(getError("El telefono esta mal formateado")); + errorCounter++; + } + + if (password1 != password2) { + $("#errors-container").append( + getError("Las contraseñas deben ser iguales") + ); + errorCounter++; + } + + if (accept){ + + + } + else{ + + errorCounter++; + } + console.log("Trato variables"); + console.log(errorCounter); + + if (errorCounter === 0) { + let user = { + name: nombre, + surname: apellidos, + phone: telefono, + email: email, + password: password1, + user: usuario, + }; + for(z of bw){ + + if(nombre.includes(z)||apellidos.includes(z)||telefono.includes(z)||email.includes(z)||usuario.includes(z)){ + errorCounterBw++; + console.log("palabramala");} + } + if(errorCounterBw==0){ + $.ajax({ + + url: "http://localhost:3000/register", + method: "POST", + data: JSON.stringify(user), + contentType: "application/json", + success: handleRegister, + error: function(data){ + $("#errors-container").append( + getError(data.responseJSON) + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }); + } + else{ + + $("#errors-container").append( + getError("¡Cuidado con ese vocabulario!") + ); + errorCounterBw = 0; + } + + + } + + return false; +} + +function handleRegister(data) { + let token = data.accessToken; + saveToken(token).then(() => { + window.location.href = "index.php"; + }); +} + +/////////////////////////////////////////////////////////////////////////////// + +function getError(message) { + return ( + "" + ); +} + + +function removeError(error) { + $(error).remove(); +} + +$(document).ready(function () { + $("#name").change(function () { + input = $(this); + if (input.val().length < 3) { + input.removeClass("is-valid"); + input.addClass("is-invalid"); + } else { + input.removeClass("is-invalid"); + input.addClass("is-valid"); + } + }); +}); diff --git a/js/scripts.js b/js/scripts.js new file mode 100644 index 0000000..7577ba6 --- /dev/null +++ b/js/scripts.js @@ -0,0 +1,24 @@ +/*let date = new Date(); + +let day = date.getDate(); +let month = date.getMonth() + 1; // Months are 0-based for some reason... +let year = date.getFullYear(); + +let hour = date.getHours(); +let minutes = date.getMinutes(); +let seconds = date.getSeconds(); + +// Add a leading 0 if they're only one digit +hour = hour.toString().padStart(2, "0"); +minutes = minutes.toString().padStart(2, "0"); +seconds = seconds.toString().padStart(2, "0"); +*/ +/* + Print the current date + and a message to report that we're finished +*/ +/* +console.log("Loading finished.") +console.log(`The current time is: ${day}/${month}/${year} ${hour}:${minutes}:${seconds}`);*/ + +// Alternatively, we can use alert() to emit a popup message diff --git a/js/token_decode.js b/js/token_decode.js new file mode 100644 index 0000000..31e7c2a --- /dev/null +++ b/js/token_decode.js @@ -0,0 +1,128 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o> (-2 * bc & 6)) : 0 + ) { + // try to find character in table (0-63, not found => -1) + buffer = chars.indexOf(buffer); + } + return output; +} + + +module.exports = typeof window !== 'undefined' && window.atob && window.atob.bind(window) || polyfill; + +},{}],2:[function(require,module,exports){ +var atob = require('./atob'); + +function b64DecodeUnicode(str) { + return decodeURIComponent(atob(str).replace(/(.)/g, function (m, p) { + var code = p.charCodeAt(0).toString(16).toUpperCase(); + if (code.length < 2) { + code = '0' + code; + } + return '%' + code; + })); +} + +module.exports = function(str) { + var output = str.replace(/-/g, "+").replace(/_/g, "/"); + switch (output.length % 4) { + case 0: + break; + case 2: + output += "=="; + break; + case 3: + output += "="; + break; + default: + throw "Illegal base64url string!"; + } + + try{ + return b64DecodeUnicode(output); + } catch (err) { + return atob(output); + } +}; + +},{"./atob":1}],3:[function(require,module,exports){ +'use strict'; + +var base64_url_decode = require('./base64_url_decode'); + +function InvalidTokenError(message) { + this.message = message; +} + +InvalidTokenError.prototype = new Error(); +InvalidTokenError.prototype.name = 'InvalidTokenError'; + +module.exports = function (token,options) { + if (typeof token !== 'string') { + throw new InvalidTokenError('Invalid token specified'); + } + + options = options || {}; + var pos = options.header === true ? 0 : 1; + try { + return JSON.parse(base64_url_decode(token.split('.')[pos])); + } catch (e) { + throw new InvalidTokenError('Invalid token specified: ' + e.message); + } +}; + +module.exports.InvalidTokenError = InvalidTokenError; + +},{"./base64_url_decode":2}],4:[function(require,module,exports){ +(function (global){ +/* + * + * This is used to build the bundle with browserify. + * + * The bundle is used by people who doesn't use browserify. + * Those who use browserify will install with npm and require the module, + * the package.json file points to index.js. + */ +var jwt_decode = require('./lib/index'); +let getTokenID = token => jwt_decode(token).sub; + +//use amd or just throught to window object. +if (typeof global.window.define == 'function' && global.window.define.amd) { + global.window.define('jwt_decode', function () { return jwt_decode; }); + global.window.define('getTokenID', function () { return getTokenID; }); +} else if (global.window) { + global.window.jwt_decode = jwt_decode; + global.window.getTokenID = getTokenID; +} +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) + +},{"./lib/index":3}]},{},[4]) + diff --git a/js/uploadphoto.js b/js/uploadphoto.js new file mode 100644 index 0000000..686448f --- /dev/null +++ b/js/uploadphoto.js @@ -0,0 +1,453 @@ +function getError(message) { + return ""; +} + +let cntTag = 0; +let cntNewTag = 0; +let photoNumber = 0; +let errorCounter = 0; + +let tagsId = []; +let tagsServidor = []; +let tags = []; +let newTags = []; +loadServerTags(); +const maxImg = 5; +let bw = []; + +loadPhotosNumber(); +loadBadWords(); + +function loadBadWords() { + + $.ajax({ + url: "http://localhost:3000/badwords", + success: function (data) { + + for (i of data) { + bw.push(i.word); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { console.log(bw) }); + + +} + +function loadPhotosNumber() { + + + $.ajax({ + url: "http://localhost:3000/photos?userId=" + localStorage.userId, + success: function (data) { + + for (i of data) { + photoNumber++; + console.log(photoNumber); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + if (maxImg <= photoNumber) { + $("#errors-container").append( + getError("Se ha superado el número máximo de fotos, borra 1 para poder añadir nuevas imágenes.") + ); + } + + }); + + +} +function loadServerTags() { + + $.ajax({ + url: "http://localhost:3000/tags", + success: processServerTags, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }); +} + +function processServerTags(data) { + + + for (tag of data) { + + + tagsServidor.push(tag.tag); + tagsId.push(tag.id); + } + +} + +function getError(message) { + return ( + "" + ); +} + +$("form").submit(function (event) { + + photoNumber = 0; + + $.ajax({ + url: "http://localhost:3000/photos?userId=" + localStorage.userId, + success: function (data) { + + for (i of data) { + photoNumber++; + console.log(photoNumber); + } + }, + error: function (error) { + console.log("Ha ocurrido un error: " + error.toString()); + } + }).done(function () { + + + if (maxImg <= photoNumber) { + $("#errors-container").append( + getError("Se ha superado el número máximo de fotos, borra 1 para poder añadir nuevas imágenes.") + ); + } + + else { + + let tags = []; + + if (maxImg > photoNumber) { + + event.preventDefault(); + $("#errors-container").empty(); + + let url = $("#input-url").val(); + let title = $("#input-title").val(); + + + let description = $("#input-description").val(); + //let tags = $("#input-tags").val().split(",").map(tag => tag.trim()); + + + + for (n = 0; n < cntTag + 1; n++) { + + let p = "#input-tags-" + n; + + let w = $(p)[0]; + console.log(w); + + if (w != undefined && w[w.selectedIndex].text != null && w[w.selectedIndex].text != undefined && !(tags.includes(w[w.selectedIndex].text))) { + + tags.push(w[w.selectedIndex].text); + } + } + + for (n = 0; n < cntNewTag + 1; n++) { + + let p = "#input-new-tags-" + n; + + let w = $(p); + + if (w.val() != null && $.trim(w.val()).length !== 0 && w != undefined && !(tags.includes(w.val())) && !(tagsServidor.includes(w.val()))) { + + tags.push(w.val()); + tagsServidor.push(w.val()); + let aux = 0; + for (l of tagsId) { + + if (l > aux) { + aux = l; + } + } + + aux++; + tagsId.push(aux); + newTags.push(aux); + + + let tag = { + tag: w.val(), + + }; + + if (errorCounter == 0) { + let blbw = true; + for (zk of bw) { + + if (w.val().includes(zk)) { + + blbw = false; + } + } + if (maxImg > photoNumber) { + if (blbw) { + $.ajax({ + + url: "http://localhost:3000/tags", + method: "POST", + data: JSON.stringify(tag), + contentType: "application/json", + success: true, + error: function () { + $("#errors-container").append( + getError("Ha habido un error") + ); + }, // Hay que gestionar este posible error en el registro y mostrárselo al usuario + }).done(function () { + + window.location.href = "mypictures.php"; + }); + } + + } + + else { + + $("#errors-container").append( + getError("Se ha superado el número máximo de fotos, borra 1 para poder añadir nuevas imágenes.") + ); + } + + + } + } + else if (tagsServidor.includes(w.val()) && !(tags.includes(w.val()))) { + tags.push(w.val()); + + } + } + + /*let tagsString =""+tags[0]; + + for(numero = 1; numero${tag.tag}`; + + $("#input-tags-0").append(html); + + tags.push(tag.tag); + + } + + + + +} + +function createNewTagBox() { + cntNewTag++; + let html = `
  • +
    + +
    + +
    + +
    + +
    +
  • `; + + $("#new-tags-list").append(html); + + + + + + + + + +} +function createTagBox() { + cntTag++; + + let html = `
  • + +
    + + + +
    +
    + +
    +
  • `; + + $("#tags-list").append(html); + + for (numero = 0; numero < tags.length; numero++) { + + let input = "#input-tags-" + cntTag; + $(input).append(``); + } +} + +processTags(); + +function del(s) { + + $(s).parent().parent().parent().remove(); + + + +} \ No newline at end of file diff --git a/login.php b/login.php new file mode 100644 index 0000000..9f7d1f7 --- /dev/null +++ b/login.php @@ -0,0 +1,58 @@ + + + + + + + + + +

    Log in!

    +
    +

    Welcome to PixiArts!

    + +
    + + + + + +
    +
    +
    +
    + +
    +
    +


    +
    + +
    + +
    +
    + +


    +
    + +
    + +
    +
    +


    + +


    +
    +
    + +
    +
    +

    + +
    +
    + + + + + \ No newline at end of file diff --git a/mypictures.php b/mypictures.php new file mode 100644 index 0000000..4073b69 --- /dev/null +++ b/mypictures.php @@ -0,0 +1,49 @@ + + + + + + + + + + +
    +
    +
    +
    +
    +

    Hi @loremIpsum

    +

    +

    My pictures!



    + +
    + +
    + + + + + + + + +
    + + + +
    + + + + +
    + + + + + + + + + \ No newline at end of file diff --git a/myprofile.php b/myprofile.php new file mode 100644 index 0000000..44f2daf --- /dev/null +++ b/myprofile.php @@ -0,0 +1,367 @@ + + + + + + + + + + + + + +
    + +
    + +
    +
    +
    +
    + +
    @user4
    + + +
    +
    +

    Likes: 1000
    + Dislikes:1000
    + Pictures: 120 +

    +
    + + + +
    + + +

    My profile!



    + +
    + + +
    + +
    +

    Last updates

    +
    +

    1.Title

    +
    + + + + + +
    + +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + + +
    +

    Tags:

    +
    + +
    +
    +
    + +
    + +
    +

    2.Title

    +
    + + + + + +
    + +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + + +
    +

    Tags:

    +
    + +
    +
    +
    +


    + +
    +
    +

    Top

    +
    +

    1.Title

    +
    + + + + + +
    + +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + + +
    + +

    Tags:

    +
    + + + + +
    +
    +
    +
    + +
    +
    +

    Following

    + +
    + +
    + + +
    @user1
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user1
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user2
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user3
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user4
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user5
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user6
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    + + + +
    +
    + +
    +
    +

    Followers

    +
    + + +
    + + +
    @user1
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user1
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user2
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user3
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user4
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + +
    @user5
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user6
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    + +
    +
    +
    + + + +
    + +
    + + + + + + + + + + + + + + +
    + + + + + + + + + \ No newline at end of file diff --git a/photo_detail.php b/photo_detail.php new file mode 100644 index 0000000..7ff03fa --- /dev/null +++ b/photo_detail.php @@ -0,0 +1,89 @@ + + + + + + Detalles foto + + + + + +
    + +
    +
    +
    + +
    +
    +
    +

    Photo detail

    +
    +
    + +
    + +
    + +
    + +
    + +
    +

    +
    @user
    +
    + +
    +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    +
    +
    +

    Tags:

    +

    + +

    +
    + +
    + + +
    +
    +
    +

    Comments:

    +
    +
    +
    + + +
    +
    + +
    + +
    + + +
    + + + + +
    + + + + + + \ No newline at end of file diff --git a/photo_edit.php b/photo_edit.php new file mode 100644 index 0000000..bd279b2 --- /dev/null +++ b/photo_edit.php @@ -0,0 +1,119 @@ + + + + + + Subir foto + + + + + +
    +
    +
    +

    EditPhoto

    +
    +
    + +
    + +
    +
    +
    +
    +
    + +
    +
    + + +
    +
    + +
    +
    +
    +
    +
    Fecha:
    + Descripción de la imagen: + +
    +

    Foto privada

    + +

    Etiquetas:

    +

    Añade etiquetas existentes, nuevas, o borra las antiguas clickeando en ellas.

    +

    Existentes:

    +
      + +
    • +
      + +
      + + + +
      + +
      + +
      +
      +
    • + + + +
    +
    +

    +
    +

    Nuevas:

    + +
      +
    • +
      + +
      + +
      +
      + +
      + +
      + + +
    • + + +
    +
    +

    +
    +

    Borra las asignadas:

    +
      +
    + +
    +
    +
    + + + + + +
    +
    + + + + + + + \ No newline at end of file diff --git a/register.php b/register.php new file mode 100644 index 0000000..4d8b132 --- /dev/null +++ b/register.php @@ -0,0 +1,133 @@ + + + + + + + + + + +

    Sign up!

    +

    Welcome to PixiArts!

    + +
    + + +
    +
    +
    +
    + + +
    +
    + +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    + +
    + +
    + +
    + +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + +
    +
    + + +
    + +
    + + + + +
    + +
    +


    +
    +
    + + + +
    +
    +

    +
    +
    + + + + + \ No newline at end of file diff --git a/search.php b/search.php new file mode 100644 index 0000000..4ae2783 --- /dev/null +++ b/search.php @@ -0,0 +1,52 @@ + + + + + + + + + +

    Search!

    + + +
    + + + + +
    + +
    +


    +
    + +
    + +
    +
    + +


    +
    + +
    + +
    +
    +


    + +


    +
    +
    + +
    +
    +

    + +
    +
    + + +
    + + \ No newline at end of file diff --git a/trending.php b/trending.php new file mode 100644 index 0000000..f20600a --- /dev/null +++ b/trending.php @@ -0,0 +1,230 @@ + + + + + + + + + + + +
    + +
    +
    +

    Top users

    + +
    + +
    + + +
    @user1
    + + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user1
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user2
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user3
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user4
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user5
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    + + +
    @user6
    + +
    Likes: 2000
    +
    Dislikes: 1234
    + + + +

    +
    +
    +

    Top pictures

    + +
    + +
    + +
    Titulo de la tarjeta
    + + + +
    +
    @user
    + +
    +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + +
    +

    Tags:

    +
    + + + + +
    +
    +
    +
    Titulo de la tarjeta
    + + + +
    +
    @user
    +
    + +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + +
    +

    Tags:

    +
    + + + + +
    +
    +
    +
    Titulo de la tarjeta
    + + + +
    +
    @user
    + +
    +
    Date: 02/03/2020
    + Description: Lorem Ipsum Dolor Sit Amet + +
    +

    Score: +30

    + +
    +

    Tags:

    +
    + + + + +
    +
    +
    + +
    + +
    +
    + + + + + + + + + + + + + + +
    + + + + + + + + + \ No newline at end of file