1: <?php
2: namespace Opencart\Admin\Model\Extension\Opencart\Report;
3: /**
4: * Class ProductViewed
5: *
6: * @package Opencart\Admin\Model\Extension\Opencart\Report
7: */
8: class ProductViewed extends \Opencart\System\Engine\Model {
9: /**
10: * Install
11: *
12: * @return void
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: * Uninstall
24: *
25: * @return void
26: */
27: public function uninstall(): void {
28: $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "product_viewed`");
29: }
30:
31: /**
32: * Add Report
33: *
34: * @param int $product_id
35: * @param int $viewed
36: *
37: * @return void
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: * Get Viewed
45: *
46: * @param int $start
47: * @param int $limit
48: *
49: * @return array<int, array<string, mixed>>
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: * Get Total Viewed
67: *
68: * @return int
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: * Get Total
78: *
79: * @return int
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: * Clear
89: *
90: * @return void
91: */
92: public function clear(): void {
93: $this->db->query("TRUNCATE TABLE `" . DB_PREFIX . "product_viewed`");
94: }
95: }
96: