-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
202 lines (186 loc) · 7.71 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
session_start();
if (!isset($_SESSION['UUID']) and !isset($_SESSION['username'])) {
header('Location: ./login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="./includes/css/main.css">
<?php
$UUID = $_SESSION['UUID'];
echo "<script>var authUUID='".$UUID."'; var idsToTitle = {";
include('./includes/db.inc.php');
$us = $_SESSION['username'];
$sort = $_COOKIE['sortBy'];
switch ($sort){
case "newOld":
$r = $conn->prepare("SELECT * FROM `notes` WHERE authorUUID = :uuid ORDER BY `created` DESC");
break;
case "oldNew":
$r = $conn->prepare("SELECT * FROM `notes` WHERE authorUUID = :uuid ORDER BY `created` ASC");
break;
case "az":
$r = $conn->prepare("SELECT * FROM `notes` WHERE authorUUID = :uuid ORDER BY `title` ASC");
break;
case "za":
$r = $conn->prepare("SELECT * FROM `notes` WHERE authorUUID = :uuid ORDER BY `title` DESC");
break;
default:
$r = $conn->prepare("SELECT * FROM `notes` WHERE authorUUID = :uuid");
}
$r->bindValue(':uuid',$UUID);
$r->execute();
if ($r->rowCount() == 0){
echo "};</script>";
$empty = true;
}else{
$i = 0;
while ($row = $r->fetch(PDO::FETCH_ASSOC)){
$i +=1;
if ($i == $r->rowCount()){echo "'".htmlspecialchars($row['title'])."':'".$row['UUID']."'";}else{
echo "'".htmlspecialchars($row['title'])."':'".$row['UUID']."',";
}
}
echo "};</script>";
}
?>
</head>
<body class="mainBody">
<div class="main">
<div id="myDIV" class="header">
<h2 style="margin:5px">My To Do List</h2>
<input type="text" id="myInput" placeholder="Eat Lunch....">
<span onclick="newElement()" class="addBtn">Add</span>
<select onchange="newSort()" class="changeSort" id="changeSort">
<option value="newOld" <?php if ($sort == "newOld"){echo "selected";} ?> >Newest - Oldest</option>
<option value="oldNew" <?php if ($sort == "oldNew"){echo "selected";} ?> >Oldest - Newest</option>
<option value="az" <?php if ($sort == "az"){echo "selected";} ?> >A - Z</option>
<option value="za" <?php if ($sort == "za"){echo "selected";} ?> >Z - A</option>
</select>
<p id="error"></p>
</div>
<ul id="myUL">
<?php
include('./includes/getTodos.php');
?>
</ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/"/g, "&quot;");
str = str.replace(/'/g, "'");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
}
return str;
}
function newSort() {
var x = document.getElementById("changeSort").value;
document.cookie = "sortBy="+x+";";
window.location.reload(false);
}
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
myNodelist[i].appendChild(span);
}
// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
var arr = (div.innerHTML).split("<span");
var uuid = idsToTitle[htmlspecialchars(arr[0].replace("\\",""))];
$.post( "./includes/deleteTodo.php", { "UUID": uuid} )
.done(function(data) {
window.location.reload(false);
});
}
}
// Add a "checked" symbol when clicking on a list item
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
}
}, false);
// Create a new list item when clicking on the "Add" button
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
inputValue = escapeHtml(inputValue);
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
document.getElementById("error").innerHTML = "Error: Please Enter a Value!";
document.getElementById("error").style.marginTop = "10%";
function removeErr() {
setTimeout(function(){
document.getElementById("error").innerHTML = "";
document.getElementById("error").style.marginTop = "0%";
}, 3000);
}
removeErr();
} else {
var keys = Object.keys(idsToTitle);
for (var key in keys){
if (key.toUpperCase() == inputValue.toUpperCase()){
document.getElementById("myInput").value = "";
document.getElementById("error").innerHTML = "Error: You Already Have a Note With That Name!";
document.getElementById("error").style.marginTop = "10%";
function removeErr() {
setTimeout(function(){
document.getElementById("error").innerHTML = "";
document.getElementById("error").style.marginTop = "0%";
}, 3000);
}
var copy = true;
}
}
if (copy == true){
removeErr();
}else{
$.post( "./includes/addTodo.php", {"authUUID" : authUUID, "title" : inputValue} )
.done(function(data) {
window.location.reload(false);
});
}
document.getElementById("myInput").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
}
</script>
</body>
</html>