-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
executable file
·489 lines (402 loc) · 11.5 KB
/
functions.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
<?php
ob_start();
session_start();
require_once('admin/config.php');
require_once('includes/add_to_cart.php');
/**
* Global scope variable declaration
*/
// $cart can interact with cart
$cart = new Add_to_cart();
function dd($data)
{
echo "<pre>";
var_dump($data);
echo "</pre>";
die();
}
function sanitize($data)
{
global $conn;
$data = trim($data);
return mysqli_real_escape_string($conn, $data);
}
function redirect($url)
{
header("Location: {$url}");
}
function get_all_rows($table)
{
global $conn;
$output = array();
$sql = "SELECT * FROM {$table}";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row;
}
}
return $output;
}
function get_conditional_rows($table, $args = array())
{
global $conn;
$output = array();
$defaults = array(
'order' => 'DESC',
'orderby' => 'id',
'limit' => 100,
'offset' => 0,
'pagination' => false,
'per_page' => 5,
'status' => 1,
'category_id' => array(),
'where' => ''
);
foreach( $defaults as $key => $value ) {
if (!array_key_exists($key, $args)) {
$args[$key] = $value;
}
}
extract($args);
if(count($category_id) > 0) {
$sql = "SELECT * FROM {$table} WHERE status={$status} AND category_id IN (". implode(',', $category_id) .") ORDER BY {$orderby} {$order} LIMIT {$limit}";
} else {
if($where != '') {
$sql = "SELECT * FROM {$table} WHERE status={$status} AND $where ORDER BY {$orderby} {$order} LIMIT {$limit}";
} else {
$sql = "SELECT * FROM {$table} WHERE status={$status} ORDER BY {$orderby} {$order} LIMIT {$limit}";
}
}
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row;
}
}
return $output;
}
function get_joined_data($columns, $tables, $where)
{
global $conn;
$output = array();
$sql = "SELECT {$columns} FROM {$tables} WHERE $where";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row;
}
}
return $output;
}
function get_permalink($path, $id = '', $slug = '', $type = 'id') {
$base = $_SERVER['REQUEST_URI'];
if($type == 'id') {
$permalink = site_url() . $path . '?id=' . $id;
return $permalink;
}
$permalink = site_url() . $path . '/' . $slug;
return $permalink;
}
function get_currency($currency = '$') {
return $currency;
}
function site_url($suffix = WEBSITE_URL_SUFFIX) {
return (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/' . $suffix;
}
function get_cart_page_url($url = '') {
if($url != '') {
return $url;
}
return 'cart.php';
}
function get_shop_page_url($url = '')
{
if ($url != '') {
return $url;
}
return 'shop.php';
}
function get_checkout_page_url($url = '')
{
if ($url != '') {
return $url;
}
return 'checkout.php';
}
function get_category_name_by_id($id) {
global $conn;
$output['name'] = '';
$sql = "SELECT name FROM categories WHERE id={$id}";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output = $row;
}
}
return $output['name'];
}
function get_data_by_id($table, $id, $tableId = 'id')
{
global $conn;
$output = array();
$sql = "SELECT * FROM {$table} WHERE id={$id}";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output = $row;
}
}
return $output;
}
function get_bestseller_products_ids($days = 10, $limit = 4) {
global $conn;
$output = array();
$todayDate = date('Y-m-d h:i:s');
$minusDate = date( "Y-m-d 23:59:59", strtotime( $todayDate . "-{$days} day"));
$sql = "SELECT product_id, SUM(product_qty) AS TotalQuantity FROM order_details WHERE created_at BETWEEN '$minusDate' AND '$todayDate' GROUP BY product_id ORDER BY SUM(product_qty) DESC LIMIT {$limit}";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output[] = $row['product_id'];
}
}
return $output;
}
function get_userinfo_by_username($username)
{
global $conn;
$output = array();
$sql = "SELECT * FROM users WHERE username='{$username}'";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output = $row;
}
}
return $output;
}
function get_joined_data_by_id($columns, $tables, $id, $tableId = 'id', $left = '', $right = '')
{
global $conn;
$output = array();
$sql = "SELECT {$columns} FROM {$tables} WHERE $tableId='{$id}' AND $left=$right";
$result = mysqli_query($conn, $sql);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
$output = $row;
}
}
return $output;
}
function update_data_into_database($table, $data = array(), $where)
{
global $conn;
$cols = array();
foreach ($data as $key => $val) {
$cols[] = "{$key} = '{$val}'";
}
$sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE {$where}";
$result = mysqli_query($conn, $sql);
return $result;
}
function flash_message()
{
$output = '';
if (isset($_GET['message'])) {
$output .= '<div class="alert alert-' . flash_message_type() . '">';
$output .= '<a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a>';
$message = $_GET['message'];
if ($message == 'addsuccess') {
$output .= "Successfully added!";
} elseif ($message == 'empty') {
$output .= "Field can't be empty!";
} elseif ($message == 'dberror') {
$output .= "Something went wrong. Please try again later.";
} elseif ($message == 'editsuccess') {
$output .= "Successfully updated!";
} elseif ($message == 'fileerror') {
$output .= "File error. Please try a different file!";
} elseif ($message == 'deletesuccess') {
$output .= "Successfully deleted!";
}
$output .= '</div>';
}
return $output;
}
function flash_message_type()
{
$output = '';
if (isset($_GET['message'])) {
$message = $_GET['message'];
if ($message == 'addsuccess' || $message == 'deletesuccess') {
$output .= "success";
} elseif ($message == 'empty' || $message == 'dberror') {
$output .= "danger";
} elseif ($message == 'editsuccess') {
$output .= "success";
} elseif ($message == 'fileerror') {
$output .= "danger";
}
}
return $output;
}
function is_unique($table, $data, $tableColumn, $where = '')
{
global $conn;
if($where != '') {
$sql = "SELECT * FROM {$table} WHERE {$tableColumn} = '{$data}' AND {$where}";
} else {
$sql = "SELECT * FROM {$table} WHERE {$tableColumn} = '{$data}'";
}
$result = mysqli_query($conn, $sql);
if ($result) {
$count = mysqli_num_rows($result);
if ($count > 0) {
return false;
} else {
return true;
}
}
return true;
}
function insert_into_database($table, $data) {
global $conn;
$key = array_keys($data);
$val = array_values($data);
$sql = "INSERT INTO $table (" . implode(', ', $key) . ") " . "VALUES ('" . implode("', '", $val) . "')";
$result = mysqli_query($conn, $sql);
if ($result) {
return $result;
} else {
return mysqli_error($conn);
}
}
function get_registration_role() {
return 'subscriber';
}
function get_default_status() {
$status = 1;
return $status;
}
function get_single_product_total_price($sp, $price, $qty) {
if($sp == '' || $sp == null || $sp == 0) {
return $price * $qty;
} else {
return $sp * $qty;
}
}
function get_selling_price($sp, $price) {
if($sp == '' || $sp == null || $sp == 0) {
return $price;
} else {
return $sp;
}
}
function is_user_logged_in() {
if(isset($_SESSION['username'])) {
return true;
}
return false;
}
function get_user_order_info() {
global $conn;
$output = array();
if(isset($_SESSION['username'])) {
$user_id = get_user_id_by_username($_SESSION['username']);
$sql = "SELECT * FROM orders WHERE user_id='{$user_id}' ORDER BY id DESC LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result) {
$output = mysqli_fetch_assoc($result);
}
}
return $output;
}
function get_user_orders_info() {
global $conn;
$output = array();
if(isset($_SESSION['username'])) {
$user_id = get_user_id_by_username($_SESSION['username']);
$sql = "SELECT * FROM orders WHERE user_id='{$user_id}' ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if ($result) {
while($row = mysqli_fetch_assoc($result)) {
$output[] = $row;
}
}
}
return $output;
}
function get_user_order_details_info($order_id) {
global $conn;
$output = array();
$user_id = get_user_id_by_username($_SESSION['username']);
$sql = "SELECT * FROM order_details WHERE order_id='{$order_id}' ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if ($result) {
while($row = mysqli_fetch_assoc($result)) {
$output[] = $row;
}
}
return $output;
}
function get_user_id_by_username($username) {
global $conn;
$output = '';
$sql = "SELECT id FROM users WHERE username='{$username}' LIMIT 1";
$result = mysqli_query($conn, $sql);
if ($result) {
$row = mysqli_fetch_assoc($result);
$output = $row['id'];
} else {
$output = mysqli_error($conn);
}
return $output;
}
function get_value_from_key($arr_key, $arr_val) {
if(array_key_exists($arr_key, $arr_val)) {
return $arr_val[$arr_key];
} else {
return '';
}
}
function authenticate_order_info($order_id, $user_id) {
global $conn;
$output = false;
$sql = "SELECT id FROM orders WHERE user_id='{$user_id}' AND id='{$order_id}' LIMIT 1";
$result = mysqli_query($conn, $sql);
if($result) {
$row = mysqli_fetch_assoc($result);
if(count($row) > 0) {
return true;
}
return false;
}
return false;
}
function get_currency_letter() {
return 'USD';
}
function convert_price_in_asking_currency($asking_currency, $order_total) {
$currency = get_currency_letter();
$asking_currency_rate = 82.44;
$final_price = $order_total * $asking_currency_rate;
return $final_price;
}
function delete_from_db($table, $where) {
global $conn;
$sql = "DELETE FROM {$table} WHERE $where";
$result = mysqli_query($conn, $sql);
return $result;
}
function count_wishlist_product() {
global $conn;
if(isset($_SESSION['username'])) {
$profile_id = get_user_id_by_username($_SESSION['username']);
$sql = "SELECT id FROM wishlist WHERE user_id='{$profile_id}'";
$res = mysqli_query($conn, $sql);
$wishlist_products = mysqli_num_rows($res);
return $wishlist_products;
}
return 0;
}