-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableoperations.js
314 lines (212 loc) · 12.1 KB
/
tableoperations.js
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//DELETE ORDER
$( ".deleteitem" ).click(function() {
console.log("Deleting item...");
var userfromform = $(".usernameorder").val();
console.log("usernameorder: " +userfromform);
var deleteid = "deleteid=" + $(this).attr('id') + "&username=" + readCookie('userjaybus') + '&hash=' + readCookie('hashjaybus') ;
console.log(deleteid);
// console.log("The user is: " userfromform);
$.ajax({
// var deleteid = $(this).attr('id');
// console.log(deleteid);
type: 'post',
url: 'deleteorder.php',
data: deleteid,
success: function (html) {
var $html = html;
console.log(html);
console.log(deleteid);
$( ".result_delete" ).empty();
$( ".result_delete" ).append("<strong>Order Deleted</strong>");
var usardata = "username=" + readCookie('userjaybus') + '&hash=' + readCookie('hashjaybus');
// REDO TABLE TO VIEW RESULTS
$( ".table" ).empty();
$.ajax({
type: 'post',
url: 'table.php',
data: usardata,
success: function (html) {
console.log(html);
var obj = JSON.parse(html);
var table = "<table class='table table-striped'><th>User</th><th>Order no</th><th>Items</th><th>View</th><th>Edit</th><th>Delete</th>";
for(var i in obj)
{
var id = obj[i].id;
var order = obj[i].ordername;
//console.log(order);
var user = obj[i].user;
var items = obj[i].items;
//console.log(user);
table = table + "<tr><td>" + user + "</td>" + "<td>" + order + "</td>" + "<td>" + items + "</td>" + "<td><div class='view'><button type='button' class='btn btn-primary viewitem' id='" + id + "'>View</button></div></td>" + "<td>" + "<div class='edit'><button type='button' class='btn btn-warning edititem' id='" + id + "'>Edit</button></div>" + "</td>" + "<td>" + "<div class='delete'><button type='button' class='btn btn-danger deleteitem' id='" + id + "'>Delete</button></div>" + "</td></tr>";
}
$.getScript( "tableoperations.js", function( data, textStatus, jqxhr ) {
console.log( "Load tableoperations was performed." );
});
$( ".table" ).show();
$( ".table" ).append(table);
$( ".neworder" ).show();
$( ".form_neworder" ).hide();
// clearing results display
$( ".result_edit" ).empty();
$( ".result" ).empty();
$( ".result_creation" ).empty();
}
});
},
error: function () {
$( ".result_delete" ).empty();
$( ".result_delete" ).append("<strong>That was an error</strong>");
}
});
});
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//EDIT ORDER
$( ".edititem" ).click(function() {
console.log("Editing item...");
console.log($(this).attr('id'));
// cleaning any other items from the past
$( ".singleitem" ).remove();
var userfromform = $(".usernameorder").val();
console.log("usernameorder: " + userfromform);
var editid = "editid=" + $(this).attr('id') + "&username=" + readCookie('userjaybus') + '&hash=' + readCookie('hashjaybus') ;
console.log(editid);
$.ajax({
// var deleteid = $(this).attr('id');
// console.log(deleteid);
type: 'post',
url: 'getordertoedit.php',
data: editid,
success: function (html) {
var html = html;
console.log(html);
//console.log(editid);
// Create Form to edit order
var obj = JSON.parse(html);
var form = "<form enctype='multipart/form-data' class='editingorder' style='border:1px solid #ccc;padding:5px;';width:100px; padding: 10px 10px; margin:10px 10px;>";
for(var i in obj)
{
var id = obj[i].id;
console.log(id);
var order = obj[i].ordername;
//console.log(order);
var user = obj[i].user;
var items = obj[i].items;
//console.log(order);
form = form + 'EDITING EXISTING ORDER:<br><input style="display:none;" type="text" value="' + readCookie("hashjaybus") + '" class="hashnameorder" name="hashnameorder" required hidden ><input style="display:none;" type="text" value="' + readCookie("userjaybus") + '" class="usernameorder" name="usernameorder" required hidden >' + '<input type="text" value="' + id + '" class="id" name="id" hidden required >' + 'Order:<br><input type="text" value="' + order + '" class="ordername" name="ordername" required ><br><div class="item">';
if (items.indexOf(",") >= 0){
var array = items.split(',');
}else{
var array = items.split(',');
}
// if (umitem == -1){
// var array[0] = items;
// }else{
// var array = items.split(',');
// }
var arrayLength = array.length;
var items ="";
for (var i = 0; i < arrayLength; i++) {
items = items + '<div class="singleitem">Item:<br><input type="text" value="' + array[i] + '" name="item[]" class="items" required > <span class="canceladditem glyphicon glyphicon-minus"></span></div>';
//Do something
}
form = form + items;
form = form + '</div><div class="additemonedition" style="cursor: pointer"><p>Add an Item</p></div>' +'<br><input type="file" name="fileToUpload" id="fileToUpload"/><br><br><input name="submit" type="submit" value="Save Changes" class="btn btn-primary"> <button type="button" class="btn btn-warning cancelorderedition">Cancel Order Edit</button></form>';
}
console.log(form);
$( ".formtoeditorders" ).empty();
$( ".formtoeditorders" ).append(form);
$.getScript( "editcreationhelper.js", function( data, textStatus, jqxhr ) {
console.log( "Load editcreationhelper was performed." );
});
$.getScript( "canceladditem.js", function( data, textStatus, jqxhr ) {
console.log( "Load canceladditem was performed." );
});
// clearing results display
$( ".result_delete" ).empty();
$( ".result" ).empty();
$( ".result_creation" ).empty();
},
error: function () {
$( ".result_edit" ).empty();
$( ".result_edit" ).append("<strong>That was an error getting the order to be edited</strong>");
}
});
});
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//VIEW Details Operation
$( ".viewitem" ).click(function() {
console.log("viewing details...");
// cleaning any other items from the past
$( ".singleitem" ).remove();
var userfromform = $(".usernameorder").val();
console.log(userfromform);
var viewid = "editid=" + $(this).attr('id') + "&username=" + readCookie('userjaybus') + '&hash=' + readCookie('hashjaybus') ;
console.log(viewid);
$.ajax({
// var deleteid = $(this).attr('id');
// console.log(deleteid);
type: 'post',
url: 'getordertoedit.php',
data: viewid,
success: function (html) {
var html = html;
console.log(html);
console.log(viewid);
// Create Form to edit order
var obj = JSON.parse(html);
console.log("OBJ: " + obj);
var detail = "<div class='' style='margin:10px; padding:10px;float:left;positon:relative'><h1>Details on order</h1><br>";
for(var i in obj)
{
var id = obj[i].id;
var order = obj[i].ordername;
//console.log(order);
var user = obj[i].user;
var items = obj[i].items;
var create = obj[i].timestamp;
var image = obj[i].image;
console.log("image: " + image);
console.log(order);
detail = detail + 'Order id: ' + id + "<br>Created by: " + user + '<br>Date: ' + create +'<br>Order Name: ' + order + "<br><ul>";
var array = items.split(',');
var arrayLength = array.length;
var items ="";
for (var i = 0; i < arrayLength; i++) {
items = items + '<li>' + array[i] + '</li>' ;
//Do something
}
detail = detail + items;
if (image == ""){
detail = detail + '</ul></div><div style="float:left;position:relative;max-height:200px;max-width:200px;margin-top:30px;margin-left: 80px;"></div>';
}
else{
detail = detail + '</ul></div><div style="float:left;position:relative;max-height:200px;max-width:200px;margin-top:40px;margin-left: 80px;"><img style="float:left;position:relative;max-height:200px;max-width:200px;" src="uploads/' + image + '"></img></div>';
}
}
console.log(detail);
$( ".modal-content" ).empty();
$( ".modal-content" ).append(detail);
$('#myModal').modal('show');
// clearing results display
$( ".result_delete" ).empty();
$( ".result_edit" ).empty();
$( ".result" ).empty();
$( ".result_creation" ).empty();
},
error: function () {
console.log("Error");
}
});
});