-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
659 lines (585 loc) · 20.5 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
<?php
require 'connection.php';
$database = new Database();
$connection = $database->connect_to_db();
if ($connection == null){
$connection = $database->create_db();
if($connection ==null){
echo 'There error when try to connect to server.';
die;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP TEST - Atef Zoiaidi</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<link rel="stylesheet"
href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/styles/default.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/highlight.min.js"></script>
</head>
<body>
<div class="jumbotron text-center m-0">
<h1>Hotel booking Page</h1>
<p>REST API calls</p>
</div>
<div class="jumbotron m-0" id="add">
<h2>Add</h2>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-12"><small>REST API calls for create hotel item .</small></div>
<div class="col-6">
<h5><span class="badge badge-pill badge-primary">POST</span> <span class="host"></span>Api</h5>
</div>
<div class="col-6">
<button id="post" type="button" class="btn btn-primary float-right">POST</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-12">
<form id="form-add" class="card p-2 mt-2">
<div class="form-row">
<div class="form-group col-md-3">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="rating" placeholder="Rating">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="reputation" placeholder="Reputation">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="price" placeholder="Price">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control"name="availability" placeholder="Availability">
</div>
<div class="form-group col-md-6">
<select class="form-control" name="category">
<option selected value='hotel'>Hotel</option>
<option value='alternative' >alternative</option>
<option value='lodge' >Lodge</option>
<option value='hostel' >Hostel</option>
<option value='resort' >Resort</option>
<option value='guest-house' >Guest-house</option>
</select>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Address">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control" name="city" placeholder="City">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="state" placeholder="State">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" name="zip" placeholder="Zip">
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="country" id="country" placeholder="Country">
</div>
<div class="form-group">
<input type="text" class="form-control" name="image" placeholder="Url image like(https://www.image.com/imge.jpg)">
</div>
</form>
</div>
</div>
</div>
<div class="jumbotron m-0">
<h2>Results</h2>
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-12"><small>REST API calls for get full hotel list.</small></div>
<div class="col-5 ">
<h5 class="pt-2"><span class="badge badge-pill badge-info">GET</span> <span class="host"></span>Api/?page=</h5>
</div>
<div class="col-1 ">
<input type="text" class="form-control" id="page" value='1' >
</div>
<div class="col-6 ">
<button id="get-page" type="button" class="btn btn-info float-right">GET</button>
</div>
<div class="col-12"><small>REST API calls for get single item of hotel .</small></div>
<div class="col-5 ">
<h5 class="pt-2"><span class="badge badge-pill badge-info">GET</span> <span class="host"></span>Api/?pk=</h5>
</div>
<div class="col-1 ">
<input type="text" class="form-control" id="pk-field" value='1' >
</div>
<div class="col-2 ">
<h5 class="float-right pt-2">&field=</h5>
</div>
<div class="col-2 ">
<select id="field" class="form-control" name="category">
<option selected value='name'>name</option>
<option value='rating' >rating</option>
<option value='category' >category</option>
<option value='image' >image</option>
<option value='reputation' >reputation</option>
<option value='price' >price</option>
<option value='availability' >availability</option>
<option value='category' >category</option>
<option value='city' >city</option>
<option value='state' >state</option>
<option value='country' >country</option>
<option value='zip_code' >zip code</option>
<option value='address' >address</option>
</select>
</div>
<div class="col-2 ">
<button id="get-field" type="button" class="btn btn-info float-right">GET</button>
</div>
<div class="col-12"><small>REST API calls for get hotel item.</small></div>
<div class="col-5 mt-2 ">
<h5 class="pt-2"><span class="badge badge-pill badge-info">GET</span> <span class="host"></span>Api/?pk= </h5>
</div>
<div class="col-1 mt-2 ">
<input type="text" class="form-control" id="pk" value='1' >
</div>
<div class="col-6 mt-2">
<button id="get-pk" type="button" class="btn btn-info float-right">GET</button>
</div>
<div class="col-12"><small>REST API calls for hotel booking.</small></div>
<div class="col-5 mt-2 ">
<h5><span class="badge badge-pill badge-primary">POST</span> <span class="host"></span>Booking </h5>
</div>
<div class="col-7 mt-2">
<button id="book" type="button" class="btn btn-primary float-right">POST</button>
</div>
</div>
</div>
</div>
<div class="card mt-2">
<div class="card-body">
<div class="row">
<div class="col-12 " >
<pre><code id="get-json" class="language-json"> </code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="jumbotron m-0" id="update">
<h2>Update</h2>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-12"><small>REST API calls for get hotel item information.</small></div>
<div class="col-5 mt-2 ">
<h5><span class="badge badge-pill badge-info">GET</span> <span class="host"></span>Api/?pk= </h5>
</div>
<div class="col-1 mt-2 ">
<input type="text" class="form-control" id="pk-update" value='1' >
</div>
<div class="col-6 mt-2">
<button id="get-pk-update" type="button" class="btn btn-info float-right">GET</button>
</div>
<div class="col-12"><small>REST API calls for update hotel item information.</small></div>
<div class="col-6 mt-2">
<h5><span class="badge badge-pill badge-primary">POST</span> <span class="host"></span>Api</h5>
</div>
<div class="col-6 mt-2">
<button id="post-update" type="button" class="btn btn-primary float-right">POST</button>
</div>
<div class="col-12"><small>REST API calls for remove hotel item.</small></div>
<div class="col-6 mt-2">
<h5><span class="badge badge-pill badge-primary">DELETE</span> <span class="host"></span>Api</h5>
</div>
<div class="col-6 mt-2">
<button id="delete" type="button" class="btn btn-danger float-right">DELETE</button>
</div>
</div>
</div>
</div>
</div>
<div class="col-12">
<form id="form-update" class="card p-2 mt-2">
<div class="form-row">
<div class="form-group col-md-3">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="rating" placeholder="Rating">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="reputation" placeholder="Reputation">
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control" name="price" placeholder="Price">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control"name="availability" placeholder="Availability">
</div>
<div class="form-group col-md-6">
<select id="category" class="form-control" name="category">
<option selected value=''></option>
<option value='hotel'>Hotel</option>
<option value='alternative' >alternative</option>
<option value='lodge' >Lodge</option>
<option value='hostel' >Hostel</option>
<option value='resort' >Resort</option>
<option value='guest-house' >Guest-house</option>
</select>
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="address" placeholder="Address">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<input type="text" class="form-control" name="city" placeholder="City">
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="state" placeholder="State">
</div>
<div class="form-group col-md-2">
<input type="text" class="form-control" name="zip" placeholder="Zip">
</div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="country" placeholder="Country">
</div>
<div class="form-group">
<input type="text" class="form-control" name="image" placeholder="Url image like(https://www.image.com/imge.jpg)">
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function(){
id_item = 1
host = window.location.href;
$(".host").html(host)
$("#get-page").on("click",function(event){//function for display list of hotel
page = $("#page").val()
if (isNaN(page) || page ==''){
alert(' The page must be an integer');return
}
gethotel(page)
});
$("#get-field").on("click",function(event){//function for display list of hotel
pk = $("#pk-field").val()
field = $("#field").val()
if (isNaN(pk) || pk ==''){
alert(' The page must be an integer');return
}
gethotelfiels(pk,field)
});
$("#get-pk").on("click",function(event){//function for display of hotel item
pk = $("#pk").val()
if (isNaN(pk) || pk ==''){
alert(' The pk must be an integer');return
}
gethotelitem(pk)
});
$("#get-pk-update").on("click",function(event){//function for display of hotel item for update
console.log(event)
id_item = $("#pk-update").val()
if (isNaN(id_item) || id_item ==''){
alert(' The pk must be an integer');return
}
get_hotel_item_forupdate(id_item)
});
$("#post").on("click",function(event){//function for hold submit create form
$("#form-add").submit()
});
$("#book").on("click",function(event){//function for hold submit booking
pk = $("#pk").val()
if (isNaN(pk) || pk ==''){
alert(' The pk must be an integer');return
}
book('pk='+pk)
});
$("#post-update").on("click",function(event){//function for hold submit update form
$("#form-update").submit()
});
$("#delete").on("click",function(event){//function for hold submit update form
id_item = $("#pk-update").val()
if (isNaN(id_item) || id_item ==''){
alert(' The pk must be an integer');return
}
remove('pk='+id_item)
});
$("#form-add").submit(function(event){//function for creat of hotel
var arr=$(this).serialize();
event.preventDefault()
message = get_value(get_post_value())
if(message == null){
save(arr)
}else{
alert(message)
}
});
$("#form-update").submit(function(event){ //function for update hotel informations
var arr=$(this).serialize();
arr += '&id='+id_item
event.preventDefault()
message = get_value(get_update_value())
if(message == null){
save(arr)
}else{
alert(message)
}
});
function get_post_value(){
data ={}
data['name'] = $("#form-add input[name='name']").val()
data['rating'] = $("#form-add input[name='rating']").val()
data['category']= $("#form-add select[name='category']").val()
data['city'] = $("#form-add input[name='city']").val()
data['state'] = $("#form-add input[name='state']").val()
data['country'] = $("#form-add input[name='country']").val()
data['zip_code'] = $("#form-add input[name='zip']").val()
data['address'] = $("#form-add input[name='address']").val()
data['image'] = $("#form-add input[name='image']").val()
data['reputation'] = $("#form-add input[name='reputation']").val()
data['price'] = $("#form-add input[name='price']").val()
data['availability'] = $("#form-add input[name='availability']").val()
return data
}
function get_update_value(){
data ={}
data['name'] = $("#form-update input[name='name']").val()
data['rating'] = $("#form-update input[name='rating']").val()
data['category']= $("#form-update select[name='category']").val()
data['city'] = $("#form-update input[name='city']").val()
data['state'] = $("#form-update input[name='state']").val()
data['country'] = $("#form-update input[name='country']").val()
data['zip_code'] = $("#form-update input[name='zip']").val()
data['address'] = $("#form-update input[name='address']").val()
data['image'] = $("#form-update input[name='image']").val()
data['reputation'] = $("#form-update input[name='reputation']").val()
data['price'] = $("#form-update input[name='price']").val()
data['availability'] = $("#form-update input[name='availability']").val()
return data
}
function get_value(data){
category_array = ['hotel', 'alternative', 'hostel', 'lodge', 'resort', 'guest-house']
if (data.name.length < 10){
return('name should be longer than 10 characters');
}
if (isNaN(data.price)){
return('The price must be an integer');
}
if (isNaN(data.availability) || data.availability ==''){
return(' The availability must be an integer');
}
if (data.city ==''){
return('Enter city');
}
if (data.state ==''){
return('Enter state');
}
if (data.country ==''){
return('Enter country');
}
if (data.address ==''){
return('Enter address');
}
if (data.reputation < 0 || data.reputation > 1000 || isNaN(data.reputation)){
return('The reputation MUST be an integer >= 0 and <= 1000');
}
if (data.zip_code.length < 5 || data.zip_code.length > 5 || isNaN(data.zip_code)){
return('The zip code MUST be an integer and must have a length of 5');
}
if (data.rating < 0 || data.rating > 5 || isNaN(data.zip_code)){
return('Rating must >= 0 and <= 5');
}
if (!category_array.includes(data.category)){
return('The category can be one of [hotel, alternative, hostel, lodge, resort, guest-house]');
}
if(!isValidURL(data.image)){
return('The image MUST be a valid URL');
}
return null
}
function isValidURL(str) {
var pattern = new RegExp('^((https?:)?\\/\\/)?'+ // protocol
'(?:\\S+(?::\\S*)?@)?' + // authentication
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locater
if (!pattern.test(str)) {
return false;
} else {
return true;
}
}
function gethotel(page){
$.ajax({
method: "GET",
url: "Api.php/?page="+page,
cache : false,
processData: false
})
.done(function( data ) {
obj = JSON.parse(data);
$("#get-json").html(JSON.stringify(obj,undefined, 4))
document.querySelectorAll('code').forEach((block) => {
hljs.highlightBlock(block);
});
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function gethotelfiels(pk,field){
$.ajax({
method: "GET",
url: "Api.php/?pk="+pk+'&field='+field,
cache : false,
processData: false
})
.done(function( data ) {
obj = JSON.parse(data);
$("#get-json").html(JSON.stringify(obj,undefined, 4))
document.querySelectorAll('code').forEach((block) => {
hljs.highlightBlock(block);
});
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function gethotelitem(pk){
$.ajax({
method: "GET",
url: "Api.php/?pk="+pk,
cache : false,
processData: false
})
.done(function( data ) {
obj = JSON.parse(data);
$("#get-json").html(JSON.stringify(obj,undefined, 4))
document.querySelectorAll('code').forEach((block) => {
hljs.highlightBlock(block);
});
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function get_hotel_item_forupdate(pk){
$.ajax({
method: "GET",
url: "Api.php/?pk="+pk,
cache : false,
processData: false
})
.done(function( data ) {
obj = JSON.parse(data);
$("#form-update input[name='name']").val(obj['name'])
$("#form-update input[name='rating']").val(obj['rating'])
$("#form-update select[name='category']").val(obj['category'])
$("#form-update input[name='city']").val(obj.location['city'])
$("#form-update input[name='state']").val(obj.location['state'])
$("#form-update input[name='country']").val(obj.location['country'])
$("#form-update input[name='zip']").val(obj.location['zip_code'])
$("#form-update input[name='address']").val(obj.location['address'])
$("#form-update input[name='image']").val(obj['image'])
$("#form-update input[name='reputation']").val(obj['reputation'])
$("#form-update input[name='price']").val(obj['price'])
$("#form-update input[name='availability']").val(obj['availability'])
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function save(data){
$.ajax({
method: "POST",
url: "Api.php",
data: data,
cache : false,
processData: false
})
.done(function( msg ) {
alert( msg );
$("#form-add input[name='name']").val('')
$("#form-add input[name='rating']").val('')
$("#form-add select[name='category']").val('')
$("#form-add input[name='city']").val('')
$("#form-add input[name='state']").val('')
$("#form-add input[name='country']").val('')
$("#form-add input[name='zip']").val('')
$("#form-add input[name='address']").val('')
$("#form-add input[name='image']").val('')
$("#form-add input[name='reputation']").val('')
$("#form-add input[name='price']").val('')
$("#form-add input[name='availability']").val('')
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function remove(data){
$.ajax({
method: "DELETE",
url: "Api.php",
cache : false,
data:data,
processData: false
})
.done(function( msg ) {
alert( msg );
$("#form-update input[name='name']").val('')
$("#form-update input[name='rating']").val('')
$("#form-update select[name='category']").val('')
$("#form-update input[name='city']").val('')
$("#form-update input[name='state']").val('')
$("#form-update input[name='country']").val('')
$("#form-update input[name='zip']").val('')
$("#form-update input[name='address']").val('')
$("#form-update input[name='image']").val('')
$("#form-update input[name='reputation']").val('')
$("#form-update input[name='price']").val('')
$("#form-update input[name='availability']").val('')
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
function book(data){
$.ajax({
method: "POST",
url: "Booking.php",
data:data,
cache : false,
processData: false
})
.done(function( msg ) {
alert( msg );
})
.fail(function( msg ) {
alert( "error msg: " + msg );
});
}
});
</script>
</body>
</html>