1: | <?php
2: | namespace Opencart\Admin\Model\Catalog;
3: | /**
4: | * Class Subscription Plan
5: | *
6: | * @package Opencart\Admin\Model\Catalog
7: | */
8: | class SubscriptionPlan extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Subscription Plan
11: | *
12: | * @param array<string, mixed> $data
13: | *
14: | * @return int
15: | */
16: | public function addSubscriptionPlan(array $data): int {
17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "subscription_plan` SET `trial_frequency` = '" . $this->db->escape((string)$data['trial_frequency']) . "', `trial_duration` = '" . (int)$data['trial_duration'] . "', `trial_cycle` = '" . (int)$data['trial_cycle'] . "', `trial_status` = '" . (int)$data['trial_status'] . "', `frequency` = '" . $this->db->escape((string)$data['frequency']) . "', `duration` = '" . (int)$data['duration'] . "', `cycle` = '" . (int)$data['cycle'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
18: |
19: | $subscription_plan_id = $this->db->getLastId();
20: |
21: | foreach ($data['subscription_plan_description'] as $language_id => $subscription_plan_description) {
22: | $this->model_catalog_subscription_plan->addDescription($subscription_plan_id, $language_id, $subscription_plan_description);
23: | }
24: |
25: | return $subscription_plan_id;
26: | }
27: |
28: | /**
29: | * Edit Subscription Plan
30: | *
31: | * @param int $subscription_plan_id
32: | * @param array<string, mixed> $data
33: | *
34: | * @return void
35: | */
36: | public function editSubscriptionPlan(int $subscription_plan_id, array $data): void {
37: | $this->db->query("UPDATE `" . DB_PREFIX . "subscription_plan` SET `trial_frequency` = '" . $this->db->escape((string)$data['trial_frequency']) . "', `trial_duration` = '" . (int)$data['trial_duration'] . "', `trial_cycle` = '" . (int)$data['trial_cycle'] . "', `trial_status` = '" . (int)$data['trial_status'] . "', `frequency` = '" . $this->db->escape((string)$data['frequency']) . "', `duration` = '" . (int)$data['duration'] . "', `cycle` = '" . (int)$data['cycle'] . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `subscription_plan_id` = '" . (int)$subscription_plan_id . "'");
38: |
39: | $this->model_catalog_subscription_plan->deleteDescriptions($subscription_plan_id);
40: |
41: | foreach ($data['subscription_plan_description'] as $language_id => $subscription_plan_description) {
42: | $this->model_catalog_subscription_plan->addDescription($subscription_plan_id, $language_id, $subscription_plan_description);
43: | }
44: | }
45: |
46: | /**
47: | * Copy Subscription Plan
48: | *
49: | * @param int $subscription_plan_id
50: | *
51: | * @return void
52: | */
53: | public function copySubscriptionPlan(int $subscription_plan_id): void {
54: | $this->model_catalog_subscription_plan->addSubscriptionPlan($this->model_catalog_subscription_plan->getSubscriptionPlan($subscription_plan_id) + ['subscription_plan_description' => $this->model_catalog_subscription_plan->getDescription($subscription_plan_id)]);
55: | }
56: |
57: | /**
58: | * Delete Subscription Plan
59: | *
60: | * @param int $subscription_plan_id
61: | *
62: | * @return void
63: | */
64: | public function deleteSubscriptionPlan(int $subscription_plan_id): void {
65: | $this->db->query("DELETE FROM `" . DB_PREFIX . "subscription_plan` WHERE `subscription_plan_id` = '" . (int)$subscription_plan_id . "'");
66: |
67: | $this->model_catalog_subscription_plan->deleteDescriptions($subscription_plan_id);
68: |
69: | $this->load->model('catalog/product');
70: |
71: | $this->model_catalog_product->deleteSubscriptionsBySubscriptionPlanId($subscription_plan_id);
72: | }
73: |
74: | /**
75: | * Get Subscription Plan
76: | *
77: | * @param int $subscription_plan_id
78: | *
79: | * @return array<string, mixed>
80: | */
81: | public function getSubscriptionPlan(int $subscription_plan_id): array {
82: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription_plan` sp LEFT JOIN `" . DB_PREFIX . "subscription_plan_description` spd ON (sp.`subscription_plan_id` = spd.`subscription_plan_id`) WHERE sp.`subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND spd.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
83: |
84: | return $query->row;
85: | }
86: |
87: | /**
88: | * Get Subscription Plans
89: | *
90: | * @param array<string, mixed> $data
91: | *
92: | * @return array<int, array<string, mixed>>
93: | */
94: | public function getSubscriptionPlans(array $data = []): array {
95: | $sql = "SELECT * FROM `" . DB_PREFIX . "subscription_plan` `sp` LEFT JOIN `" . DB_PREFIX . "subscription_plan_description` `spd` ON (`sp`.`subscription_plan_id` = `spd`.`subscription_plan_id`) WHERE `spd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
96: |
97: | if (!empty($data['filter_name'])) {
98: | $sql .= " AND LCASE(`spd`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
99: | }
100: |
101: | $sort_data = [
102: | 'spd.name',
103: | 'sp.sort_order'
104: | ];
105: |
106: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
107: | $sql .= " ORDER BY " . $data['sort'];
108: | } else {
109: | $sql .= " ORDER BY `spd`.`name`";
110: | }
111: |
112: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
113: | $sql .= " DESC";
114: | } else {
115: | $sql .= " ASC";
116: | }
117: |
118: | if (isset($data['start']) || isset($data['limit'])) {
119: | if ($data['start'] < 0) {
120: | $data['start'] = 0;
121: | }
122: |
123: | if ($data['limit'] < 1) {
124: | $data['limit'] = 20;
125: | }
126: |
127: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
128: | }
129: |
130: | $query = $this->db->query($sql);
131: |
132: | return $query->rows;
133: | }
134: |
135: | /**
136: | * Add Description
137: | *
138: | * @param int $subscription_plan_id
139: | * @param int $language_id
140: | * @param array<string, mixed> $data
141: | *
142: | * @return void
143: | */
144: | public function addDescription(int $subscription_plan_id, int $language_id, array $data): void {
145: | $this->db->query("INSERT INTO `" . DB_PREFIX . "subscription_plan_description` SET `subscription_plan_id` = '" . (int)$subscription_plan_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
146: | }
147: |
148: | /**
149: | * Delete Descriptions
150: | *
151: | * @param int $subscription_plan_id
152: | *
153: | * @return void
154: | */
155: | public function deleteDescriptions(int $subscription_plan_id): void {
156: | $this->db->query("DELETE FROM `" . DB_PREFIX . "subscription_plan_description` WHERE `subscription_plan_id` = '" . (int)$subscription_plan_id . "'");
157: | }
158: |
159: | /**
160: | * Delete Descriptions By Language ID
161: | *
162: | * @param int $language_id
163: | */
164: | public function deleteDescriptionsByLanguageId(int $language_id): void {
165: | $this->db->query("DELETE FROM `" . DB_PREFIX . "subscription_plan_description` WHERE `language_id` = '" . (int)$language_id . "'");
166: | }
167: |
168: | /**
169: | * Get Descriptions
170: | *
171: | * @param int $subscription_plan_id
172: | *
173: | * @return array<int, array<string, string>>
174: | */
175: | public function getDescriptions(int $subscription_plan_id): array {
176: | $subscription_plan_description_data = [];
177: |
178: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription_plan_description` WHERE `subscription_plan_id` = '" . (int)$subscription_plan_id . "'");
179: |
180: | foreach ($query->rows as $result) {
181: | $subscription_plan_description_data[$result['language_id']] = ['name' => $result['name']];
182: | }
183: |
184: | return $subscription_plan_description_data;
185: | }
186: |
187: | /**
188: | * Get Descriptions By Language ID
189: | *
190: | * @param int $language_id
191: | *
192: | * @return array<int, array<string, string>>
193: | */
194: | public function getDescriptionsByLanguageId(int $language_id): array {
195: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "subscription_plan_description` WHERE `language_id` = '" . (int)$language_id . "'");
196: |
197: | return $query->rows;
198: | }
199: |
200: | /**
201: | * Get Total Subscription Plans
202: | *
203: | * @return int
204: | */
205: | public function getTotalSubscriptionPlans(): int {
206: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "subscription_plan`");
207: |
208: | return (int)$query->row['total'];
209: | }
210: | }
211: |

OpenCart API API documentation generated by ApiGen dev-master