oct_sreview_reviews.php
20.5 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
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
<?php
/**
* @copyright OCTemplates
* @support https://octemplates.net/
* @license LICENSE.txt
*/
class ModelOCTemplatesModuleOCTSreviewReviews extends Model {
public function addReview($data) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_reviews
SET
author = '" . $this->db->escape($data['author']) . "',
text = '" . $this->db->escape(strip_tags($data['text'])) . "',
status = '" . (int)$data['status'] . "',
admin_text = '" . $this->db->escape(strip_tags($data['admin_text'])) . "',
date_added = '" . $this->db->escape($data['date_added']) . "'
");
$oct_sreview_review_id = $this->db->getLastId();
return $oct_sreview_review_id;
}
public function editReview($oct_sreview_review_id, $data) {
$this->db->query("
UPDATE
" . DB_PREFIX . "oct_sreview_reviews
SET
author = '" . $this->db->escape($data['author']) . "',
text = '" . $this->db->escape(strip_tags($data['text'])) . "',
status = '" . (int)$data['status'] . "',
date_added = '" . $this->db->escape($data['date_added']) . "',
admin_text = '" . $this->db->escape(strip_tags($data['admin_text'])) . "',
date_modified = NOW()
WHERE
oct_sreview_review_id = '" . (int)$oct_sreview_review_id . "'
");
}
public function deleteReview($oct_sreview_review_id) {
$reviews = $this->getReviewVote($oct_sreview_review_id);
if ($reviews) {
foreach ($reviews as $review) {
$this->db->query("
UPDATE
" . DB_PREFIX . "oct_sreview_subject
SET
subject_rating_count" . (int)$review['rating'] . " = (subject_rating_count" . (int)$review['rating'] . " - " . (int)$review['rating'] . ")
WHERE
oct_sreview_subject_id = '" . (int)$review['oct_sreview_subject_id'] . "'
");
}
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_reviews_vote
WHERE
oct_sreview_review_id = '" . (int)$oct_sreview_review_id . "'
");
}
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_reviews
WHERE
oct_sreview_review_id = '" . (int)$oct_sreview_review_id . "'
");
}
public function getReviewVote($oct_sreview_review_id) {
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_reviews_vote
WHERE
oct_sreview_review_id = '" . (int)$oct_sreview_review_id . "'
");
return $query->rows;
}
public function getReview($oct_sreview_review_id) {
$query = $this->db->query("
SELECT
DISTINCT *
FROM
" . DB_PREFIX . "oct_sreview_reviews
WHERE
oct_sreview_review_id = '" . (int)$oct_sreview_review_id . "'
");
return $query->row;
}
public function getReviews($data = []) {
$sql = "
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_reviews
WHERE
date_added != '00-00-0000'
";
if (isset($data['filter_status'])) {
$sql .= " AND status = '" . $data['filter_status'] . "'";
}
if (!empty($data['filter_author'])) {
$sql .= " AND author LIKE '" . $this->db->escape($data['filter_author']) . "%'";
}
if (!empty($data['filter_date_added'])) {
$sql .= " AND DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
}
$sort_data = [
'author',
'rating',
'status',
'date_added'
];
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY date_added";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
public function getTotalReviews($data = []) {
$sql = "
SELECT
COUNT(*) AS total
FROM
" . DB_PREFIX . "oct_sreview_reviews
WHERE
date_added != '00-00-0000'
";
if (!empty($data['filter_author'])) {
$sql .= " AND author LIKE '" . $this->db->escape($data['filter_author']) . "%'";
}
if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
$sql .= " AND status = '" . (int)$data['filter_status'] . "'";
}
if (!empty($data['filter_date_added'])) {
$sql .= " AND DATE(date_added) = DATE('" . $this->db->escape($data['filter_date_added']) . "')";
}
$query = $this->db->query($sql);
return $query->row['total'];
}
/* Subjects */
public function addSubject($data) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_subject
SET
status = '" . (int)$data['status'] . "',
sort_order = '" . (int)$data['sort_order'] . "',
date_added = NOW()
");
$oct_sreview_subject_id = $this->db->getLastId();
foreach ($data['subject_description'] as $language_id => $value) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_subject_description
SET
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "',
language_id = '" . (int)$language_id . "',
name = '" . $this->db->escape($value['name']) . "'
");
}
if (isset($data['subject_store'])) {
foreach ($data['subject_store'] as $store_id) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_subject_to_store
SET
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "',
store_id = '" . (int)$store_id . "'
");
}
}
$this->cache->delete('oct_sreview_subject');
return $oct_sreview_subject_id;
}
public function editSubject($oct_sreview_subject_id, $data) {
$this->db->query("
UPDATE
" . DB_PREFIX . "oct_sreview_subject
SET
status = '" . (int)$data['status'] . "',
sort_order = '" . (int)$data['sort_order'] . "',
date_modified = NOW()
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_subject_description
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
foreach ($data['subject_description'] as $language_id => $value) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_subject_description
SET
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "',
language_id = '" . (int)$language_id . "',
name = '" . $this->db->escape($value['name']) . "'
");
}
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_subject_to_store
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
if (isset($data['subject_store'])) {
foreach ($data['subject_store'] as $store_id) {
$this->db->query("
INSERT INTO
" . DB_PREFIX . "oct_sreview_subject_to_store
SET
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "',
store_id = '" . (int)$store_id . "'
");
}
}
$this->cache->delete('oct_sreview_subject');
}
public function copySubject($oct_sreview_subject_id) {
$query = $this->db->query("
SELECT
DISTINCT *
FROM
" . DB_PREFIX . "oct_sreview_subject s
LEFT JOIN
" . DB_PREFIX . "oct_sreview_subject_description sd
ON
(s.oct_sreview_subject_id = sd.oct_sreview_subject_id)
WHERE
s.oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
AND sd.language_id = '" . (int)$this->config->get('config_language_id') . "'
");
if ($query->num_rows) {
$data = $query->row;
$data['status'] = '0';
$data['subject_rating_count1'] = '0';
$data['subject_rating_count2'] = '0';
$data['subject_rating_count3'] = '0';
$data['subject_rating_count4'] = '0';
$data['subject_rating_count5'] = '0';
$data['subject_description'] = $this->getSubjectDescriptions($oct_sreview_subject_id);
$data['subject_store'] = $this->getSubjectStores($oct_sreview_subject_id);
$this->addSubject($data);
}
}
public function deleteSubject($oct_sreview_subject_id) {
$reviews = $this->getReviewSubjectVote($oct_sreview_subject_id);
if ($reviews) {
foreach ($reviews as $review) {
$this->db->query("
UPDATE
" . DB_PREFIX . "oct_sreview_subject
SET
subject_rating_count" . (int)$review['rating'] . " = (subject_rating_count" . (int)$review['rating'] . " - " . (int)$review['rating'] . ")
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
}
$this->db->query("DELETE FROM " . DB_PREFIX . "oct_sreview_reviews_vote WHERE oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'");
}
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_subject
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_subject_description
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
$this->db->query("
DELETE FROM
" . DB_PREFIX . "oct_sreview_subject_to_store
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
$this->cache->delete('oct_sreview_subject');
}
public function getReviewSubjectVote($oct_sreview_subject_id) {
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_reviews_vote
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
return $query->rows;
}
public function getSubject($oct_sreview_subject_id) {
$query = $this->db->query("
SELECT
DISTINCT *
FROM
" . DB_PREFIX . "oct_sreview_subject s
LEFT JOIN
" . DB_PREFIX . "oct_sreview_subject_description sd
ON
(s.oct_sreview_subject_id = sd.oct_sreview_subject_id)
WHERE
s.oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
AND sd.language_id = '" . (int)$this->config->get('config_language_id') . "'
");
return $query->row;
}
// SEO URLS
public function getSeoUrls() {
$sreview_seo_url_data = array();
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "seo_url WHERE query = 'octemplates/module/oct_sreview_reviews'");
foreach ($query->rows as $result) {
$sreview_seo_url_data[$result['store_id']][$result['language_id']] = $result['keyword'];
}
return $sreview_seo_url_data;
}
public function editSeoUrls($data) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "seo_url` WHERE query = 'octemplates/module/oct_sreview_reviews'");
if (isset($data['sreview_seo_url'])) {
foreach ($data['sreview_seo_url'] as $store_id => $language) {
foreach ($language as $language_id => $keyword) {
if (!empty($keyword)) {
$this->db->query("INSERT INTO " . DB_PREFIX . "seo_url SET store_id = '" . (int)$store_id . "', language_id = '" . (int)$language_id . "', query = 'octemplates/module/oct_sreview_reviews', keyword = '" . $this->db->escape($keyword) . "'");
}
}
}
}
}
public function getSubjects($data = array()) {
$sql = "
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_subject s
LEFT JOIN
" . DB_PREFIX . "oct_sreview_subject_description sd
ON
(s.oct_sreview_subject_id = sd.oct_sreview_subject_id)
WHERE
sd.language_id = '" . (int)$this->config->get('config_language_id') . "'
";
if (!empty($data['filter_name'])) {
$sql .= " AND sd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
}
if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
$sql .= " AND s.status = '" . (int)$data['filter_status'] . "'";
}
$sql .= " GROUP BY s.oct_sreview_subject_id";
$sort_data = array(
'sd.name',
's.status',
's.sort_order'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
$sql .= " ORDER BY " . $data['sort'];
} else {
$sql .= " ORDER BY sd.name";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC";
} else {
$sql .= " ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
public function getSubjectDescriptions($oct_sreview_subject_id) {
$subject_description_data = array();
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_subject_description
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
foreach ($query->rows as $result) {
$subject_description_data[$result['language_id']] = array(
'name' => $result['name']
);
}
return $subject_description_data;
}
public function getSubjectStores($oct_sreview_subject_id) {
$subject_store_data = array();
$query = $this->db->query("
SELECT
*
FROM
" . DB_PREFIX . "oct_sreview_subject_to_store
WHERE
oct_sreview_subject_id = '" . (int)$oct_sreview_subject_id . "'
");
foreach ($query->rows as $result) {
$subject_store_data[] = $result['store_id'];
}
return $subject_store_data;
}
public function getTotalSubjects($data = array()) {
$sql = "
SELECT
COUNT(DISTINCT s.oct_sreview_subject_id) AS total
FROM
" . DB_PREFIX . "oct_sreview_subject s
LEFT JOIN
" . DB_PREFIX . "oct_sreview_subject_description sd
ON
(s.oct_sreview_subject_id = sd.oct_sreview_subject_id)
WHERE
sd.language_id = '" . (int)$this->config->get('config_language_id') . "'
";
if (!empty($data['filter_name'])) {
$sql .= " AND sd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
}
if (isset($data['filter_status']) && !is_null($data['filter_status'])) {
$sql .= " AND s.status = '" . (int)$data['filter_status'] . "'";
}
$query = $this->db->query($sql);
return $query->row['total'];
}
public function installTables() {
$sql1 = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "oct_sreview_subject` (";
$sql1 .= " `oct_sreview_subject_id` int(11) NOT NULL AUTO_INCREMENT,";
$sql1 .= " `sort_order` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `subject_rating_count1` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `subject_rating_count2` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `subject_rating_count3` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `subject_rating_count4` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `subject_rating_count5` int(11) NOT NULL DEFAULT '0',";
$sql1 .= " `status` tinyint(1) NOT NULL DEFAULT '0',";
$sql1 .= " `date_added` datetime NOT NULL,";
$sql1 .= " `date_modified` datetime NOT NULL,";
$sql1 .= " PRIMARY KEY (`oct_sreview_subject_id`)";
$sql1 .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;";
$this->db->query($sql1);
$sql2 = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "oct_sreview_subject_description` (";
$sql2 .= " `oct_sreview_subject_id` int(11) NOT NULL,";
$sql2 .= " `language_id` int(11) NOT NULL,";
$sql2 .= " `name` varchar(255) NOT NULL,";
$sql2 .= " PRIMARY KEY (`oct_sreview_subject_id`,`language_id`),";
$sql2 .= " KEY `name` (`name`)";
$sql2 .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db->query($sql2);
$sql3 = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "oct_sreview_subject_to_store` (";
$sql3 .= " `oct_sreview_subject_id` int(11) NOT NULL,";
$sql3 .= " `store_id` int(11) NOT NULL DEFAULT '0',";
$sql3 .= " PRIMARY KEY (`oct_sreview_subject_id`,`store_id`)";
$sql3 .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$this->db->query($sql3);
$sql4 = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "oct_sreview_reviews` (";
$sql4 .= " `oct_sreview_review_id` int(11) NOT NULL AUTO_INCREMENT,";
$sql4 .= " `author` varchar(64) NOT NULL,";
$sql4 .= " `text` text NOT NULL,";
$sql4 .= " `admin_text` text NOT NULL,";
$sql4 .= " `status` tinyint(1) NOT NULL DEFAULT '0',";
$sql4 .= " `avg_rating` float NOT NULL DEFAULT '0',";
$sql4 .= " `date_added` datetime NOT NULL,";
$sql4 .= " `date_modified` datetime NOT NULL,";
$sql4 .= " PRIMARY KEY (`oct_sreview_review_id`)";
$sql4 .= ") ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;";
$this->db->query($sql4);
$sql5 = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "oct_sreview_reviews_vote` (";
$sql5 .= " `oct_sreview_review_id` int(11) NOT NULL,";
$sql5 .= " `oct_sreview_subject_id` int(11) NOT NULL,";
$sql5 .= " `rating` int(11) NOT NULL";
$sql5 .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$this->db->query($sql5);
}
public function deleteTables() {
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "oct_sreview_subject`;");
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "oct_sreview_subject_description`;");
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "oct_sreview_subject_to_store`;");
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "oct_sreview_reviews`;");
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "oct_sreview_reviews_vote`;");
}
}