-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
450 lines (383 loc) · 11.6 KB
/
functions.php
File metadata and controls
450 lines (383 loc) · 11.6 KB
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
<?php
include('scripts.php');
include('versions.php');
include('reviews.php');
function connect()
{
$mysqli = new mysqli("localhost", "favedcou", "Ethandylan1!", "favedcou_api");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
return $mysqli;
}
function getscripts()
{
//TODO change this query so it gets the most recent version number and average review score
$sql = "select * from Scripts";
$con = connect();
$res = mysqli_query($con,$sql);
$a = array();
while($row = mysqli_fetch_assoc($res))
{
$s = new Scripts;
$s->Name = $row['name'];
array_push($a, $s);
}
return $a;
}
function getReviews()
{
$sql = "select * from Scripts s left join Reviews r on s.scriptid = r.scriptid";
$con = connect();
$res = mysqli_query($con,$sql);
$a = array();
while($row = mysqli_fetch_assoc($res))
{
if(!is_null($row['reviewid']))
{
$r = new Reviews;
$r->ReviewId = $row['reviewid'];
$r->ScriptName = $row['name'];
$r->Review = $row['review'];
$r->Score = $row['score'];
array_push($a, $r);
}
}
return $a;
}
function GetScriptByName($name)
{
$sql = "select v.code from versions v left join Scripts s on v.scriptid = s.scriptid where s.name = '".$name."' ";
$con = connect();
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
if($row)
return $row['code'];
else
return "null";
}
function AddScriptToDb($data)
{
//first need to do some sanity checks lets make sure that th
$scriptName = $data->name;
$scriptCode = $data->code;
$versionInfo = $data->versionInfo;
$versionNo = $data->versionNumber;
//first going to check to see if there is a script with this name
$sql = 'select * from Scripts where name="'.$scriptName.'"';
$result = mysqli_query(connect(),$sql);
$row = mysqli_fetch_assoc($result);
if($row)
return "error";
else
{
//if the script doesnt exisit then we need to insert it this iwll give it a version number of 1.0 also requires some comments!
$sql = "INSERT INTO Scripts (name) values ('".$scriptName."')";
$mycon = connect();
mysqli_query($mycon,$sql);
$id = mysqli_insert_id($mycon);
//now to insert the rest of the data to the version table
$sql = "INSERT INTO versions (scriptid,versioncode,versioninfo,code) values (".$id.",".$versionNo.",'".$versionInfo."','".$scriptCode."')";
if(mysqli_query($mycon,$sql))
return "success";
else
return "not inserted";
}
}
function addScriptToDbByName($data,$name)
{
//first need to do some sanity checks lets make sure that th
$scriptName = $name;
$scriptCode = $data->code;
$versionInfo = $data->versionInfo;
$versionNo = $data->versionNumber;
//first going to check to see if there is a script with this name
$sql = 'select * from Scripts where name="'.$scriptName.'"';
$result = mysqli_query(connect(),$sql);
$row = mysqli_fetch_assoc($result);
if($row)
return "error";
else
{
//if the script doesnt exisit then we need to insert it this iwll give it a version number of 1.0 also requires some comments!
$sql = "INSERT INTO Scripts (name) values ('".$scriptName."')";
$mycon = connect();
mysqli_query($mycon,$sql);
$id = mysqli_insert_id($mycon);
//now to insert the rest of the data to the version table
$sql = "INSERT INTO versions (scriptid,versioncode,versioninfo,code) values (".$id.",".$versionNo.",'".$versionInfo."','".$scriptCode."')";
if(mysqli_query($mycon,$sql))
return "success";
else
return "not inserted";
}
}
function deleteVersion($name,$vno)
{
//first we need to get the id of the script
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$id = $row['scriptid'];
//now the version id of the version number
$sql ="select versionid from versions where versioncode =".$versionNo." and scriptid = ".$scriptid;
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row)
return "no version id found";
$versionid = $row['versionid'];
//now to delete!
$sql = "DELETE FROM versions where scriptid=".$id." and versionid = ".$versionid;
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
function deleteScript($name)
{
//first we need to get the id of the script
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$id = $row['scriptid'];
//now we have the id we can go ahead and remove all traces from the database, this includes reviews and versions.
//versions.
$sql = "DELETE FROM versions where scriptid = ".$id;
if(!mysqli_query($con,$sql))
return 'error';
//review table
$sql = "DELETE FROM Reviews where scriptid = ".$id;
if(!mysqli_query($con,$sql))
return 'error';
//scripts table
$sql = "DELETE FROM Scripts where scriptid = ".$id;
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
function updateScript($data,$name)
{
$scriptName = $name;
$scriptCode = $data->code;
$versionInfo = $data->versionInfo;
$versionNo = $data->versionNumber;
//first get the id of the script
$sql = "select scriptid from Scripts where name ='".$scriptName."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row)
return "no script id found";
$scriptid = $row['scriptid'];
//now we need to up date the script where the version id is correct and also the script id.
$sql ="select versionid from versions where versioncode =".$versionNo." and scriptid = ".$scriptid;
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row)
return "no version id found";
$versionid = $row['versionid'];
// now we check to see if the versioninfo has been updated or not, or wether it is just the code itself.
if($versionInfo != '')
{
$sql = 'UPDATE versions set code = "'.$scriptCode.'", versioninfo = "'.$versionInfo.'" where versionid = '.$versionid;
}
else
{
$sql = 'UPDATE versions set (code = "'.$scriptCode.'") where versionid = '.$versionid;
}
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
function getVersions()
{
$sql = "select * from Scripts s left join versions v on v.scriptid = s.scriptid";
$con = connect();
$res = mysqli_query($con,$sql);
$a = array();
while($row = mysqli_fetch_assoc($res))
{
$v = new Versions;
$v->ScriptName = $row['name'];
$v->ScriptCode = $row['code'];
$v->VersionNumber = $row['versioncode'];
$v->VersionInfo = $row['versioninfo'];
array_push($a, $v);
}
return $a;
}
function getScriptByVersions($name,$vno)
{
//get script id first
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$scriptid = $row['scriptid'];
$sql = "select code from versions where versioncode = ".$vno." and scriptid = ".$scriptid;
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
if($row)
return $row['code'];
else
return "null";
}
function getReviewById($name,$reviewid)
{
$sql = "select * from Reviews where reviewid = ".$reviewid;
$con = connect();
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
if($row)
return $row['review'].' Score = '.$row['score'];
else
return "null";
}
function getVersionsOfScripts($name)
{
$sql = "select * from Scripts s left join versions v on v.scriptid = s.scriptid where s.name ='".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$a = array();
while($row = mysqli_fetch_assoc($res))
{
$v = new Versions;
$v->ScriptName = $row['name'];
$v->ScriptCode = $row['code'];
$v->VersionNumber = $row['versioncode'];
$v->VersionInfo = $row['versioninfo'];
array_push($a, $v);
}
return $a;
}
function getReviewsOfScript($scriptName)
{
$sql = "select * from Scripts s left join Reviews r on s.scriptid = r.scriptid where s.name='".$scriptName."'";
$con = connect();
$res = mysqli_query($con,$sql);
$a = array();
while($row = mysqli_fetch_assoc($res))
{
$r = new Reviews;
$r->ReviewId = $row['reviewid'];
$r->ScriptName = $row['name'];
$r->Review = $row['review'];
$r->Score = $row['score'];
array_push($a, $r);
}
return $a;
}
function addVersionToDatabase($data,$name)
{
//first need to do some sanity checks lets make sure that th
$scriptName = $name;
$scriptCode = $data->code;
$versionInfo = $data->versionInfo;
$versionNo = $data->versionNumber;
//first need the script id for the name
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$scriptid = $row['scriptid'];
//first lets get the current max version id to see if it is smaller than new one
$sql = "select max(versioncode) as versioncode from versions where scriptid = ".$scriptid;
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row)
$maxvno = 0;
else
$maxvno = $row['versioncode'];
if($versionNo <= $maxvno)
return 'invalid version number';
//now to insert the rest of the data to the version table
$sql = "INSERT INTO versions (scriptid,versioncode,versioninfo,code) values (".$scriptid.",".$versionNo.",'".$versionInfo."','".$scriptCode."')";
if(mysqli_query($con,$sql))
return "success";
else
return "not inserted";
}
function addReviewToDatabase($data,$name)
{
//first need to do some sanity checks lets make sure that th
$scriptName = $name;
$review = $data->review;
$score = $data->score;
//first need the script id for the name
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$scriptid = $row['scriptid'];
//now to insert the rest of the data to the version table
$sql = "INSERT INTO Reviews (scriptid,review,score) values (".$scriptid.",'".$review."',".$score.")";
if(mysqli_query($mycon,$sql))
return "success";
else
return "not inserted";
}
function updateVersionOfScript($data,$name,$vno)
{
$scriptName = $name;
$scriptCode = $data->code;
$versionInfo = $data->versionInfo;
$versionNo = $vno;
//again first lets get the scriptid
$sql = "select scriptid from Scripts where name = '".$name."'";
$con = connect();
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row['scriptid'])
return "not found";
$scriptid = $row['scriptid'];
//now the version id
$sql ="select versionid from versions where versioncode =".$versionNo." and scriptid = ".$scriptid;
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
if(!$row)
return "no version id found";
$versionid = $row['versionid'];
// now we check to see if the versioninfo has been updated or not, or wether it is just the code itself.
if($versionInfo != '')
{
$sql = 'UPDATE versions set code = "'.$scriptCode.'", versioninfo = "'.$versionInfo.'" where versionid = '.$versionid;
}
else
{
$sql = 'UPDATE versions set (code = "'.$scriptCode.'") where versionid = '.$versionid;
}
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
function updateReview($data,$reviewid)
{
$review = $data->review;
$score = $data->score;
$sql = "UPDATE Reviews SET (review = '".$review."', score=".$score.") where reviewid = ".$reviewid;
$con = connect();
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
function deleteReview($reviewid)
{
$sql = "DELETE FROM Reviews WHERE reviewid = ".$reviewid;
$con = connect();
if(!mysqli_query($con,$sql))
return 'error';
return 'success';
}
?>