1: | <?php
2: | namespace Opencart\Catalog\Controller\Product;
3: | /**
4: | * Class Search
5: | *
6: | * @package Opencart\Catalog\Controller\Product
7: | */
8: | class Search extends \Opencart\System\Engine\Controller {
9: | /**
10: | * @return void
11: | */
12: | public function index(): void {
13: | $this->load->language('product/search');
14: |
15: | if (isset($this->request->get['search'])) {
16: | $filter_search = $this->request->get['search'];
17: | } else {
18: | $filter_search = '';
19: | }
20: |
21: | if (isset($this->request->get['description'])) {
22: | $filter_description = $this->request->get['description'];
23: | } else {
24: | $filter_description = '';
25: | }
26: |
27: | if (isset($this->request->get['tag'])) {
28: | $filter_tag = $this->request->get['tag'];
29: | } else {
30: | $filter_tag = '';
31: | }
32: |
33: | if (isset($this->request->get['category_id'])) {
34: | $filter_category_id = (int)$this->request->get['category_id'];
35: | } else {
36: | $filter_category_id = 0;
37: | }
38: |
39: | if (isset($this->request->get['sub_category'])) {
40: | $filter_sub_category = $this->request->get['sub_category'];
41: | } else {
42: | $filter_sub_category = 0;
43: | }
44: |
45: | if (isset($this->request->get['sort'])) {
46: | $sort = $this->request->get['sort'];
47: | } else {
48: | $sort = 'p.sort_order';
49: | }
50: |
51: | if (isset($this->request->get['order'])) {
52: | $order = $this->request->get['order'];
53: | } else {
54: | $order = 'ASC';
55: | }
56: |
57: | if (isset($this->request->get['page'])) {
58: | $page = (int)$this->request->get['page'];
59: | } else {
60: | $page = 1;
61: | }
62: |
63: | if (isset($this->request->get['limit']) && (int)$this->request->get['limit']) {
64: | $limit = (int)$this->request->get['limit'];
65: | } else {
66: | $limit = $this->config->get('config_pagination');
67: | }
68: |
69: | if (isset($this->request->get['search'])) {
70: | $this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->request->get['search']);
71: | } elseif (isset($this->request->get['tag'])) {
72: | $this->document->setTitle($this->language->get('heading_title') . ' - ' . $this->language->get('heading_tag') . $this->request->get['tag']);
73: | } else {
74: | $this->document->setTitle($this->language->get('heading_title'));
75: | }
76: |
77: | $data['breadcrumbs'] = [];
78: |
79: | $data['breadcrumbs'][] = [
80: | 'text' => $this->language->get('text_home'),
81: | 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
82: | ];
83: |
84: | $url = '';
85: |
86: | if (isset($this->request->get['search'])) {
87: | $url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
88: | }
89: |
90: | if (isset($this->request->get['description'])) {
91: | $url .= '&description=' . $this->request->get['description'];
92: | }
93: |
94: | if (isset($this->request->get['tag'])) {
95: | $url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
96: | }
97: |
98: | if (isset($this->request->get['category_id'])) {
99: | $url .= '&category_id=' . $this->request->get['category_id'];
100: | }
101: |
102: | if (isset($this->request->get['sub_category'])) {
103: | $url .= '&sub_category=' . $this->request->get['sub_category'];
104: | }
105: |
106: | if (isset($this->request->get['sort'])) {
107: | $url .= '&sort=' . $this->request->get['sort'];
108: | }
109: |
110: | if (isset($this->request->get['order'])) {
111: | $url .= '&order=' . $this->request->get['order'];
112: | }
113: |
114: | if (isset($this->request->get['page'])) {
115: | $url .= '&page=' . $this->request->get['page'];
116: | }
117: |
118: | if (isset($this->request->get['limit'])) {
119: | $url .= '&limit=' . $this->request->get['limit'];
120: | }
121: |
122: | $data['breadcrumbs'][] = [
123: | 'text' => $this->language->get('heading_title'),
124: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . $url)
125: | ];
126: |
127: | if (isset($this->request->get['search'])) {
128: | $data['heading_title'] = $this->language->get('heading_title') . ' - ' . $this->request->get['search'];
129: | } else {
130: | $data['heading_title'] = $this->language->get('heading_title');
131: | }
132: |
133: | $data['text_compare'] = sprintf($this->language->get('text_compare'), isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0);
134: |
135: | $data['compare'] = $this->url->link('product/compare', 'language=' . $this->config->get('config_language'));
136: |
137: | $this->load->model('catalog/category');
138: |
139: | // 3 Level Category Search
140: | $data['categories'] = [];
141: |
142: | $this->load->model('catalog/category');
143: |
144: | $categories_1 = $this->model_catalog_category->getCategories(0);
145: |
146: | foreach ($categories_1 as $category_1) {
147: | $level_2_data = [];
148: |
149: | $categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);
150: |
151: | foreach ($categories_2 as $category_2) {
152: | $level_3_data = [];
153: |
154: | $categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);
155: |
156: | foreach ($categories_3 as $category_3) {
157: | $level_3_data[] = [
158: | 'category_id' => $category_3['category_id'],
159: | 'name' => $category_3['name'],
160: | ];
161: | }
162: |
163: | $level_2_data[] = [
164: | 'category_id' => $category_2['category_id'],
165: | 'name' => $category_2['name'],
166: | 'children' => $level_3_data
167: | ];
168: | }
169: |
170: | $data['categories'][] = [
171: | 'category_id' => $category_1['category_id'],
172: | 'name' => $category_1['name'],
173: | 'children' => $level_2_data
174: | ];
175: | }
176: |
177: | $data['products'] = [];
178: |
179: | if ($filter_search || $filter_tag) {
180: | $filter_data = [
181: | 'filter_search' => $filter_search,
182: | 'filter_description' => $filter_description,
183: | 'filter_tag' => $filter_tag ? $filter_tag : $filter_search,
184: | 'filter_category_id' => $filter_category_id,
185: | 'filter_sub_category' => $filter_sub_category,
186: | 'sort' => $sort,
187: | 'order' => $order,
188: | 'start' => ($page - 1) * $limit,
189: | 'limit' => $limit
190: | ];
191: |
192: | $this->load->model('catalog/product');
193: | $this->load->model('tool/image');
194: |
195: | $results = $this->model_catalog_product->getProducts($filter_data);
196: |
197: | foreach ($results as $result) {
198: | $description = trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')));
199: |
200: | if (oc_strlen($description) > $this->config->get('config_product_description_length')) {
201: | $description = oc_substr($description, 0, $this->config->get('config_product_description_length')) . '..';
202: | }
203: |
204: | if ($result['image'] && is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
205: | $image = $result['image'];
206: | } else {
207: | $image = 'placeholder.png';
208: | }
209: |
210: | if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
211: | $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
212: | } else {
213: | $price = false;
214: | }
215: |
216: | if ((float)$result['special']) {
217: | $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
218: | } else {
219: | $special = false;
220: | }
221: |
222: | if ($this->config->get('config_tax')) {
223: | $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price'], $this->session->data['currency']);
224: | } else {
225: | $tax = false;
226: | }
227: |
228: | $product_data = [
229: | 'product_id' => $result['product_id'],
230: | 'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
231: | 'name' => $result['name'],
232: | 'description' => $description,
233: | 'price' => $price,
234: | 'special' => $special,
235: | 'tax' => $tax,
236: | 'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
237: | 'rating' => $result['rating'],
238: | 'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $result['product_id'] . $url)
239: | ];
240: |
241: | $data['products'][] = $this->load->controller('product/thumb', $product_data);
242: | }
243: |
244: | $url = '';
245: |
246: | if (isset($this->request->get['search'])) {
247: | $url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
248: | }
249: |
250: | if (isset($this->request->get['description'])) {
251: | $url .= '&description=' . $this->request->get['description'];
252: | }
253: |
254: | if (isset($this->request->get['tag'])) {
255: | $url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
256: | }
257: |
258: | if (isset($this->request->get['category_id'])) {
259: | $url .= '&category_id=' . $this->request->get['category_id'];
260: | }
261: |
262: | if (isset($this->request->get['sub_category'])) {
263: | $url .= '&sub_category=' . $this->request->get['sub_category'];
264: | }
265: |
266: | if (isset($this->request->get['limit'])) {
267: | $url .= '&limit=' . $this->request->get['limit'];
268: | }
269: |
270: | $data['sorts'] = [];
271: |
272: | $data['sorts'][] = [
273: | 'text' => $this->language->get('text_default'),
274: | 'value' => 'p.sort_order-ASC',
275: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=p.sort_order&order=ASC' . $url)
276: | ];
277: |
278: | $data['sorts'][] = [
279: | 'text' => $this->language->get('text_name_asc'),
280: | 'value' => 'pd.name-ASC',
281: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=pd.name&order=ASC' . $url)
282: | ];
283: |
284: | $data['sorts'][] = [
285: | 'text' => $this->language->get('text_name_desc'),
286: | 'value' => 'pd.name-DESC',
287: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=pd.name&order=DESC' . $url)
288: | ];
289: |
290: | $data['sorts'][] = [
291: | 'text' => $this->language->get('text_price_asc'),
292: | 'value' => 'p.price-ASC',
293: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=p.price&order=ASC' . $url)
294: | ];
295: |
296: | $data['sorts'][] = [
297: | 'text' => $this->language->get('text_price_desc'),
298: | 'value' => 'p.price-DESC',
299: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=p.price&order=DESC' . $url)
300: | ];
301: |
302: | if ($this->config->get('config_review_status')) {
303: | $data['sorts'][] = [
304: | 'text' => $this->language->get('text_rating_desc'),
305: | 'value' => 'rating-DESC',
306: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=rating&order=DESC' . $url)
307: | ];
308: |
309: | $data['sorts'][] = [
310: | 'text' => $this->language->get('text_rating_asc'),
311: | 'value' => 'rating-ASC',
312: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=rating&order=ASC' . $url)
313: | ];
314: | }
315: |
316: | $data['sorts'][] = [
317: | 'text' => $this->language->get('text_model_asc'),
318: | 'value' => 'p.model-ASC',
319: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=p.model&order=ASC' . $url)
320: | ];
321: |
322: | $data['sorts'][] = [
323: | 'text' => $this->language->get('text_model_desc'),
324: | 'value' => 'p.model-DESC',
325: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . '&sort=p.model&order=DESC' . $url)
326: | ];
327: |
328: | $url = '';
329: |
330: | if (isset($this->request->get['search'])) {
331: | $url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
332: | }
333: |
334: | if (isset($this->request->get['description'])) {
335: | $url .= '&description=' . $this->request->get['description'];
336: | }
337: |
338: | if (isset($this->request->get['tag'])) {
339: | $url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
340: | }
341: |
342: | if (isset($this->request->get['category_id'])) {
343: | $url .= '&category_id=' . $this->request->get['category_id'];
344: | }
345: |
346: | if (isset($this->request->get['sub_category'])) {
347: | $url .= '&sub_category=' . $this->request->get['sub_category'];
348: | }
349: |
350: | if (isset($this->request->get['sort'])) {
351: | $url .= '&sort=' . $this->request->get['sort'];
352: | }
353: |
354: | if (isset($this->request->get['order'])) {
355: | $url .= '&order=' . $this->request->get['order'];
356: | }
357: |
358: | $data['limits'] = [];
359: |
360: | $limits = array_unique([$this->config->get('config_pagination'), 25, 50, 75, 100]);
361: |
362: | sort($limits);
363: |
364: | foreach ($limits as $value) {
365: | $data['limits'][] = [
366: | 'text' => $value,
367: | 'value' => $value,
368: | 'href' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . $url . '&limit=' . $value)
369: | ];
370: | }
371: |
372: | $url = '';
373: |
374: | if (isset($this->request->get['search'])) {
375: | $url .= '&search=' . urlencode(html_entity_decode($this->request->get['search'], ENT_QUOTES, 'UTF-8'));
376: | }
377: |
378: | if (isset($this->request->get['description'])) {
379: | $url .= '&description=' . $this->request->get['description'];
380: | }
381: |
382: | if (isset($this->request->get['tag'])) {
383: | $url .= '&tag=' . urlencode(html_entity_decode($this->request->get['tag'], ENT_QUOTES, 'UTF-8'));
384: | }
385: |
386: | if (isset($this->request->get['category_id'])) {
387: | $url .= '&category_id=' . $this->request->get['category_id'];
388: | }
389: |
390: | if (isset($this->request->get['sub_category'])) {
391: | $url .= '&sub_category=' . $this->request->get['sub_category'];
392: | }
393: |
394: | if (isset($this->request->get['sort'])) {
395: | $url .= '&sort=' . $this->request->get['sort'];
396: | }
397: |
398: | if (isset($this->request->get['order'])) {
399: | $url .= '&order=' . $this->request->get['order'];
400: | }
401: |
402: | if (isset($this->request->get['limit'])) {
403: | $url .= '&limit=' . $this->request->get['limit'];
404: | }
405: |
406: | $product_total = $this->model_catalog_product->getTotalProducts($filter_data);
407: |
408: | $data['pagination'] = $this->load->controller('common/pagination', [
409: | 'total' => $product_total,
410: | 'page' => $page,
411: | 'limit' => $limit,
412: | 'url' => $this->url->link('product/search', 'language=' . $this->config->get('config_language') . $url . '&page={page}')
413: | ]);
414: |
415: | $data['results'] = sprintf($this->language->get('text_pagination'), ($product_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($product_total - $limit)) ? $product_total : ((($page - 1) * $limit) + $limit), $product_total, ceil($product_total / $limit));
416: |
417: | if (isset($this->request->get['search']) && $this->config->get('config_customer_search')) {
418: | $this->load->model('account/search');
419: |
420: | if ($this->customer->isLogged()) {
421: | $customer_id = $this->customer->getId();
422: | } else {
423: | $customer_id = 0;
424: | }
425: |
426: | if (isset($this->request->server['REMOTE_ADDR'])) {
427: | $ip = $this->request->server['REMOTE_ADDR'];
428: | } else {
429: | $ip = '';
430: | }
431: |
432: | $search_data = [
433: | 'keyword' => $filter_tag ? $filter_tag : $filter_search,
434: | 'description' => $filter_description,
435: | 'category_id' => $filter_category_id,
436: | 'sub_category' => $filter_sub_category,
437: | 'products' => $product_total,
438: | 'customer_id' => $customer_id,
439: | 'ip' => $ip
440: | ];
441: |
442: | $this->model_account_search->addSearch($search_data);
443: | }
444: | }
445: |
446: | $data['search'] = $filter_search;
447: | $data['description'] = $filter_description;
448: | $data['category_id'] = $filter_category_id;
449: | $data['sub_category'] = $filter_sub_category;
450: |
451: | $data['sort'] = $sort;
452: | $data['order'] = $order;
453: | $data['limit'] = $limit;
454: |
455: | $data['language'] = $this->config->get('config_language');
456: |
457: | $data['column_left'] = $this->load->controller('common/column_left');
458: | $data['column_right'] = $this->load->controller('common/column_right');
459: | $data['content_top'] = $this->load->controller('common/content_top');
460: | $data['content_bottom'] = $this->load->controller('common/content_bottom');
461: | $data['footer'] = $this->load->controller('common/footer');
462: | $data['header'] = $this->load->controller('common/header');
463: |
464: | $this->response->setOutput($this->load->view('product/search', $data));
465: | }
466: | }
467: |

OpenCart API API documentation generated by ApiGen dev-master