1: | <?php
2: | namespace Opencart\Admin\Model\Design;
3: | /**
4: | * Class Seo Url
5: | *
6: | * @package Opencart\Admin\Model\Design
7: | */
8: | class SeoUrl extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Seo Url
11: | *
12: | * @param string $key
13: | * @param string $value
14: | * @param string $keyword
15: | * @param int $store_id
16: | * @param int $language_id
17: | * @param int $sort_order
18: | *
19: | * @return int
20: | */
21: | public function addSeoUrl(string $key, string $value, string $keyword, int $store_id, int $language_id, int $sort_order = 0): int {
22: | $this->db->query("INSERT INTO `" . DB_PREFIX . "seo_url` SET `store_id` = '" . (int)$store_id . "', `language_id` = '" . (int)$language_id . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "', `keyword` = '" . $this->db->escape($keyword) . "', `sort_order` = '" . (int)$sort_order . "'");
23: |
24: | return $this->db->getLastId();
25: | }
26: |
27: | /**
28: | * Edit Seo Url
29: | *
30: | * @param int $seo_url_id
31: | * @param string $key
32: | * @param string $value
33: | * @param string $keyword
34: | * @param int $store_id
35: | * @param int $language_id
36: | * @param int $sort_order
37: | *
38: | * @return void
39: | */
40: | public function editSeoUrl(int $seo_url_id, string $key, string $value, string $keyword, int $store_id, int $language_id, int $sort_order = 0): void {
41: | $this->db->query("UPDATE `" . DB_PREFIX . "seo_url` SET `store_id` = '" . (int)$store_id . "', `language_id` = '" . (int)$language_id . "', `key` = '" . $this->db->escape($key) . "', `value` = '" . $this->db->escape($value) . "', `keyword` = '" . $this->db->escape((string)$keyword) . "', `sort_order` = '" . (int)$sort_order . "' WHERE `seo_url_id` = '" . (int)$seo_url_id . "'");
42: | }
43: |
44: | /**
45: | * Delete Seo Url
46: | *
47: | * @param int $seo_url_id
48: | *
49: | * @return void
50: | */
51: | public function deleteSeoUrl(int $seo_url_id): void {
52: | $this->db->query("DELETE FROM `" . DB_PREFIX . "seo_url` WHERE `seo_url_id` = '" . (int)$seo_url_id . "'");
53: | }
54: |
55: | /**
56: | * Delete Seo Urls by Key Value pair
57: | *
58: | * @param string $key
59: | * @param string $value
60: | * @param int $store_id
61: | * @param int $language_id
62: | *
63: | * @return void
64: | */
65: | public function deleteSeoUrlsByKeyValue(string $key, string $value, int $store_id = 0, int $language_id = 0): void {
66: | $sql = "DELETE FROM `" . DB_PREFIX . "seo_url` WHERE `key` = '" . $this->db->escape($key) . "' AND `value` LIKE '" . $this->db->escape($value) . "'";
67: |
68: | if ($store_id) {
69: | $sql .= " AND `store_id` = '" . (int)$store_id . "'";
70: | }
71: |
72: | if ($language_id) {
73: | $sql .= " AND `language_id` = '" . (int)$language_id . "'";
74: | }
75: |
76: | $this->db->query($sql);
77: | }
78: |
79: | /**
80: | * Delete Seo Urls By Language ID
81: | *
82: | * @param int $language_id
83: | *
84: | * @return void
85: | */
86: | public function deleteSeoUrlsByLanguageId(int $language_id): void {
87: | $this->db->query("DELETE FROM `" . DB_PREFIX . "seo_url` WHERE `language_id` = '" . (int)$language_id . "'");
88: | }
89: |
90: | /**
91: | * Delete Seo Urls By Store ID
92: | *
93: | * @param int $store_id
94: | *
95: | * @return void
96: | */
97: | public function deleteSeoUrlsByStoreId(int $store_id): void {
98: | $this->db->query("DELETE FROM `" . DB_PREFIX . "seo_url` WHERE `store_id` = '" . (int)$store_id . "'");
99: | }
100: |
101: | /**
102: | * Get Seo Url
103: | *
104: | * @param int $seo_url_id
105: | *
106: | * @return array<string, mixed>
107: | */
108: | public function getSeoUrl(int $seo_url_id): array {
109: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE `seo_url_id` = '" . (int)$seo_url_id . "'");
110: |
111: | return $query->row;
112: | }
113: |
114: | /**
115: | * Get Seo Url By Key Value
116: | *
117: | * @param string $key
118: | * @param string $value
119: | * @param int $store_id
120: | * @param int $language_id
121: | *
122: | * @return array<string, mixed>
123: | */
124: | public function getSeoUrlByKeyValue(string $key, string $value, int $store_id = 0, int $language_id = 0): array {
125: | $sql = "SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE `key` = '" . $this->db->escape($key) . "' AND `value` LIKE '" . $this->db->escape($value) . "'";
126: |
127: | if ($store_id) {
128: | $sql .= " AND `store_id` = '" . (int)$store_id . "'";
129: | }
130: |
131: | if ($language_id) {
132: | $sql .= " AND `language_id` = '" . (int)$language_id . "'";
133: | }
134: |
135: | $query = $this->db->query($sql);
136: |
137: | return $query->row;
138: | }
139: |
140: | /**
141: | * Get Seo Url By Keyword
142: | *
143: | * @param string $keyword
144: | * @param int $store_id
145: | * @param int $language_id
146: | *
147: | * @return array<string, mixed>
148: | */
149: | public function getSeoUrlByKeyword(string $keyword, int $store_id, int $language_id = 0): array {
150: | $sql = "SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE (`keyword` = '" . $this->db->escape($keyword) . "' OR LCASE(`keyword`) LIKE '" . $this->db->escape('%/' . oc_strtolower($keyword)) . "') AND `store_id` = '" . (int)$store_id . "'";
151: |
152: | if ($language_id) {
153: | $sql .= " AND `language_id` = '" . (int)$language_id . "'";
154: | }
155: |
156: | $query = $this->db->query($sql);
157: |
158: | return $query->row;
159: | }
160: |
161: | /**
162: | * Get Seo Urls
163: | *
164: | * @param array<string, mixed> $data
165: | *
166: | * @return array<int, array<string, mixed>>
167: | */
168: | public function getSeoUrls(array $data = []): array {
169: | $sql = "SELECT *, (SELECT `name` FROM `" . DB_PREFIX . "store` `s` WHERE `s`.`store_id` = `su`.`store_id`) AS `store`, (SELECT `name` FROM `" . DB_PREFIX . "language` `l` WHERE `l`.`language_id` = `su`.`language_id`) AS `language` FROM `" . DB_PREFIX . "seo_url` `su`";
170: |
171: | $implode = [];
172: |
173: | if (!empty($data['filter_keyword'])) {
174: | $implode[] = "LCASE(`keyword`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_keyword'])) . "'";
175: | }
176: |
177: | if (!empty($data['filter_key'])) {
178: | $implode[] = "LCASE(`key`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_key'])) . "'";
179: | }
180: |
181: | if (!empty($data['filter_value'])) {
182: | $implode[] = "LCASE(`value`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_value'])) . "'";
183: | }
184: |
185: | if (isset($data['filter_store_id']) && $data['filter_store_id'] !== '') {
186: | $implode[] = "`store_id` = '" . (int)$data['filter_store_id'] . "'";
187: | }
188: |
189: | if (!empty($data['filter_language_id']) && $data['filter_language_id'] !== '') {
190: | $implode[] = "`language_id` = '" . (int)$data['filter_language_id'] . "'";
191: | }
192: |
193: | if ($implode) {
194: | $sql .= " WHERE " . implode(" AND ", $implode);
195: | }
196: |
197: | $sort_data = [
198: | 'keyword',
199: | 'key',
200: | 'value',
201: | 'sort_order',
202: | 'store_id',
203: | 'language_id'
204: | ];
205: |
206: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
207: | $sql .= " ORDER BY `" . $data['sort'] . "`";
208: | } else {
209: | $sql .= " ORDER BY `key`";
210: | }
211: |
212: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
213: | $sql .= " DESC";
214: | } else {
215: | $sql .= " ASC";
216: | }
217: |
218: | if (isset($data['start']) || isset($data['limit'])) {
219: | if ($data['start'] < 0) {
220: | $data['start'] = 0;
221: | }
222: |
223: | if ($data['limit'] < 1) {
224: | $data['limit'] = 20;
225: | }
226: |
227: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
228: | }
229: |
230: | $query = $this->db->query($sql);
231: |
232: | return $query->rows;
233: | }
234: |
235: | /**
236: | * Get Seo Urls By Key Value
237: | *
238: | * @param string $key
239: | * @param string $value
240: | *
241: | * @return array<int, array<int, string>>
242: | */
243: | public function getSeoUrlsByKeyValue(string $key, string $value): array {
244: | $seo_url_data = [];
245: |
246: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE `key` = '" . $this->db->escape($key) . "' AND `value` LIKE '" . $this->db->escape($value) . "'");
247: |
248: | foreach ($query->rows as $result) {
249: | $seo_url_data[$result['store_id']][$result['language_id']] = $result['keyword'];
250: | }
251: |
252: | return $seo_url_data;
253: | }
254: |
255: | /**
256: | * Get Seo Urls By Store Id
257: | *
258: | * @param int $store_id
259: | *
260: | * @return array<int, array<string, mixed>>
261: | */
262: | public function getSeoUrlsByStoreId(int $store_id): array {
263: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE `store_id` = '" . (int)$store_id . "'");
264: |
265: | return $query->rows;
266: | }
267: |
268: | /**
269: | * Get Seo Urls By Language Id
270: | *
271: | * @param int $language_id
272: | *
273: | * @return array<int, array<string, mixed>>
274: | */
275: | public function getSeoUrlsByLanguageId(int $language_id): array {
276: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "seo_url` WHERE `language_id` = '" . (int)$language_id . "'");
277: |
278: | return $query->rows;
279: | }
280: |
281: | /**
282: | * Get Total Seo Urls
283: | *
284: | * @param array<string, mixed> $data
285: | *
286: | * @return int
287: | */
288: | public function getTotalSeoUrls(array $data = []): int {
289: | $sql = "SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "seo_url`";
290: |
291: | $implode = [];
292: |
293: | if (!empty($data['filter_keyword'])) {
294: | $implode[] = "LCASE(`keyword`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_keyword'])) . "'";
295: | }
296: |
297: | if (!empty($data['filter_key'])) {
298: | $implode[] = "LCASE(`key`) = '" . $this->db->escape(oc_strtolower($data['filter_key'])) . "'";
299: | }
300: |
301: | if (!empty($data['filter_value'])) {
302: | $implode[] = "LCASE(`value`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_value'])) . "'";
303: | }
304: |
305: | if (!empty($data['filter_store_id']) && $data['filter_store_id'] !== '') {
306: | $implode[] = "`store_id` = '" . (int)$data['filter_store_id'] . "'";
307: | }
308: |
309: | if (!empty($data['filter_language_id']) && $data['filter_language_id'] !== '') {
310: | $implode[] = "`language_id` = '" . (int)$data['filter_language_id'] . "'";
311: | }
312: |
313: | if ($implode) {
314: | $sql .= " WHERE " . implode(" AND ", $implode);
315: | }
316: |
317: | $query = $this->db->query($sql);
318: |
319: | return (int)$query->row['total'];
320: | }
321: | }
322: |

OpenCart API API documentation generated by ApiGen dev-master