1: | <?php
|
2: | namespace Opencart\Admin\Model\Extension\Opencart\Report;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class ProductViewed extends \Opencart\System\Engine\Model {
|
9: | |
10: | |
11: | |
12: | |
13: |
|
14: | public function install(): void {
|
15: | $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "product_viewed` (
|
16: | `product_id` INT(11) NOT NULL,
|
17: | `viewed` INT(11) NOT NULL,
|
18: | PRIMARY KEY (`product_id`)
|
19: | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci");
|
20: | }
|
21: |
|
22: | |
23: | |
24: | |
25: | |
26: |
|
27: | public function uninstall(): void {
|
28: | $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "product_viewed`");
|
29: | }
|
30: |
|
31: | |
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: |
|
39: | public function addReport(int $product_id, int $viewed): void {
|
40: | $this->db->query("INSERT INTO `" . DB_PREFIX . "product_viewed` SET `product_id` = '" . (int)$product_id . "', `viewed` = '" . (int)$viewed . "'");
|
41: | }
|
42: |
|
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: |
|
51: | public function getViewed(int $start = 0, int $limit = 10): array {
|
52: | if ($start < 0) {
|
53: | $start = 0;
|
54: | }
|
55: |
|
56: | if ($limit < 1) {
|
57: | $limit = 10;
|
58: | }
|
59: |
|
60: | $query = $this->db->query("SELECT `product_id`, `viewed` FROM `" . DB_PREFIX . "product_viewed` ORDER BY `viewed` DESC LIMIT " . (int)$start . "," . (int)$limit);
|
61: |
|
62: | return $query->rows;
|
63: | }
|
64: |
|
65: | |
66: | |
67: | |
68: | |
69: |
|
70: | public function getTotalViewed(): int {
|
71: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "product_viewed`");
|
72: |
|
73: | return (int)$query->row['total'];
|
74: | }
|
75: |
|
76: | |
77: | |
78: | |
79: | |
80: |
|
81: | public function getTotal(): int {
|
82: | $query = $this->db->query("SELECT SUM(`viewed`) AS `total` FROM `" . DB_PREFIX . "product_viewed`");
|
83: |
|
84: | return (int)$query->row['total'];
|
85: | }
|
86: |
|
87: | |
88: | |
89: | |
90: | |
91: |
|
92: | public function clear(): void {
|
93: | $this->db->query("TRUNCATE TABLE `" . DB_PREFIX . "product_viewed`");
|
94: | }
|
95: | }
|
96: | |