1: <?php
2: namespace Opencart\Catalog\Controller\Cms;
3: /**
4: * Class Blog
5: *
6: * @package Opencart\Catalog\Controller\Cms
7: */
8: class Blog extends \Opencart\System\Engine\Controller {
9: /**
10: * @return void
11: */
12: public function index(): void {
13: $this->load->language('cms/blog');
14:
15: if (isset($this->request->get['search'])) {
16: $filter_search = (string)$this->request->get['search'];
17: } else {
18: $filter_search = '';
19: }
20:
21: if (isset($this->request->get['tag'])) {
22: $filter_tag = (string)$this->request->get['tag'];
23: } else {
24: $filter_tag = '';
25: }
26:
27: if (isset($this->request->get['topic_id'])) {
28: $filter_topic_id = (int)$this->request->get['topic_id'];
29: } else {
30: $filter_topic_id = 0;
31: }
32:
33: if (isset($this->request->get['author'])) {
34: $filter_author = (string)$this->request->get['author'];
35: } else {
36: $filter_author = '';
37: }
38:
39: if (isset($this->request->get['sort'])) {
40: $sort = (string)$this->request->get['sort'];
41: } else {
42: $sort = 'date_added';
43: }
44:
45: if (isset($this->request->get['order'])) {
46: $order = (string)$this->request->get['order'];
47: } else {
48: $order = 'DESC';
49: }
50:
51: if (isset($this->request->get['page'])) {
52: $page = (int)$this->request->get['page'];
53: } else {
54: $page = 1;
55: }
56:
57: $data['breadcrumbs'] = [];
58:
59: $data['breadcrumbs'][] = [
60: 'text' => $this->language->get('text_home'),
61: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
62: ];
63:
64: $url = '';
65:
66: if (isset($this->request->get['sort'])) {
67: $url .= '&sort=' . $this->request->get['sort'];
68: }
69:
70: if (isset($this->request->get['order'])) {
71: $url .= '&order=' . $this->request->get['order'];
72: }
73:
74: if (isset($this->request->get['page'])) {
75: $url .= '&page=' . $this->request->get['page'];
76: }
77:
78: $data['breadcrumbs'][] = [
79: 'text' => $this->language->get('text_blog'),
80: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . $url)
81: ];
82:
83: $this->load->model('cms/topic');
84:
85: $topic_info = $this->model_cms_topic->getTopic($filter_topic_id);
86:
87: if ($topic_info) {
88: $url = '';
89:
90: if (isset($this->request->get['search'])) {
91: $url .= '&search=' . $this->request->get['search'];
92: }
93:
94: if (isset($this->request->get['tag'])) {
95: $url .= '&tag=' . $this->request->get['tag'];
96: }
97:
98: if (isset($this->request->get['topic_id'])) {
99: $url .= '&topic_id=' . $this->request->get['topic_id'];
100: }
101:
102: if (isset($this->request->get['author'])) {
103: $url .= '&author=' . $this->request->get['author'];
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: $data['breadcrumbs'][] = [
119: 'text' => $topic_info['name'],
120: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . $url)
121: ];
122: }
123:
124: if ($topic_info) {
125: $this->document->setTitle($topic_info['meta_title']);
126: $this->document->setDescription($topic_info['meta_description']);
127: $this->document->setKeywords($topic_info['meta_keyword']);
128:
129: $data['heading_title'] = $topic_info['name'];
130: } else {
131: $this->document->setTitle($this->language->get('heading_title'));
132:
133: $data['heading_title'] = $this->language->get('heading_title');
134: }
135:
136: if ($topic_info) {
137: $data['description'] = html_entity_decode($topic_info['description'], ENT_QUOTES, 'UTF-8');
138: } else {
139: $data['description'] = '';
140: }
141:
142: $this->load->model('tool/image');
143:
144: if (!empty($topic_info['image']) && is_file(DIR_IMAGE . html_entity_decode($topic_info['image'], ENT_QUOTES, 'UTF-8'))) {
145: $data['image'] = $this->model_tool_image->resize($topic_info['image'], $this->config->get('config_image_topic_width'), $this->config->get('config_image_topic_height'));
146: } else {
147: $data['image'] = '';
148: }
149:
150: $limit = $this->config->get('config_pagination');
151:
152: $data['articles'] = [];
153:
154: $filter_data = [
155: 'filter_search' => $filter_search,
156: 'filter_topic_id' => $filter_topic_id,
157: 'filter_author' => $filter_author,
158: 'filter_tag' => $filter_tag,
159: 'sort' => $sort,
160: 'order' => $order,
161: 'start' => ($page - 1) * $limit,
162: 'limit' => $limit
163: ];
164:
165: $this->load->model('cms/article');
166:
167: $results = $this->model_cms_article->getArticles($filter_data);
168:
169: foreach ($results as $result) {
170: $description = trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')));
171:
172: if (oc_strlen($description) > $this->config->get('config_article_description_length')) {
173: $description = oc_substr($description, 0, $this->config->get('config_article_description_length')) . '..';
174: }
175:
176: if ($result['image'] && is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
177: $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_article_width'), $this->config->get('config_image_article_height'));
178: } else {
179: $image = '';
180: }
181:
182: $data['articles'][] = [
183: 'article_id' => $result['article_id'],
184: 'name' => $result['name'],
185: 'description' => $description,
186: 'image' => $image,
187: 'author' => $result['author'],
188: 'filter_author' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&author=' . $result['author'] . $url),
189: 'comment_total' => $this->model_cms_article->getTotalComments($result['article_id'], ['parent_id' => 0]),
190: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
191: 'href' => $this->url->link('cms/blog.info', 'language=' . $this->config->get('config_language') . '&article_id=' . $result['article_id'] . $url)
192: ];
193: }
194:
195: $url = '';
196:
197: if (isset($this->request->get['search'])) {
198: $url .= '&search=' . $this->request->get['search'];
199: }
200:
201: if (isset($this->request->get['tag'])) {
202: $url .= '&tag=' . $this->request->get['tag'];
203: }
204:
205: if (isset($this->request->get['topic_id'])) {
206: $url .= '&topic_id=' . $this->request->get['topic_id'];
207: }
208:
209: if (isset($this->request->get['author'])) {
210: $url .= '&author=' . (string)$this->request->get['author'];
211: }
212:
213: if (isset($this->request->get['sort'])) {
214: $url .= '&sort=' . $this->request->get['sort'];
215: }
216:
217: if (isset($this->request->get['order'])) {
218: $url .= '&order=' . $this->request->get['order'];
219: }
220:
221: $article_total = $this->model_cms_article->getTotalArticles($filter_data);
222:
223: $data['pagination'] = $this->load->controller('common/pagination', [
224: 'total' => $article_total,
225: 'page' => $page,
226: 'limit' => $limit,
227: 'url' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . $url . '&page={page}')
228: ]);
229:
230: $data['results'] = sprintf($this->language->get('text_pagination'), ($article_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($article_total - $limit)) ? $article_total : ((($page - 1) * $limit) + $limit), $article_total, ceil($article_total / $limit));
231:
232: $data['search'] = $filter_search;
233: $data['topic_id'] = $filter_topic_id;
234:
235: $data['topics'] = [];
236:
237: $results = $this->model_cms_topic->getTopics();
238:
239: foreach ($results as $result) {
240: $data['topics'][] = [
241: 'topic_id' => $result['topic_id'],
242: 'name' => $result['name']
243: ];
244: }
245:
246: $url = '';
247:
248: if (isset($this->request->get['search'])) {
249: $url .= '&search=' . $this->request->get['search'];
250: }
251:
252: if (isset($this->request->get['tag'])) {
253: $url .= '&tag=' . $this->request->get['tag'];
254: }
255:
256: if (isset($this->request->get['topic_id'])) {
257: $url .= '&topic_id=' . $this->request->get['topic_id'];
258: }
259:
260: if (isset($this->request->get['author'])) {
261: $url .= '&author=' . (string)$this->request->get['author'];
262: }
263:
264: $data['sorts'] = [];
265:
266: $data['sorts'][] = [
267: 'text' => $this->language->get('text_date_added_asc'),
268: 'value' => 'date_added-ASC',
269: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&sort=date_added&order=ASC' . $url)
270: ];
271:
272: $data['sorts'][] = [
273: 'text' => $this->language->get('text_date_added_desc'),
274: 'value' => 'date_added-DESC',
275: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&sort=date_added&order=DESC' . $url)
276: ];
277:
278: $data['sorts'][] = [
279: 'text' => $this->language->get('text_rating_asc'),
280: 'value' => 'rating-ASC',
281: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&sort=rating&order=ASC' . $url)
282: ];
283:
284: $data['sorts'][] = [
285: 'text' => $this->language->get('text_rating_desc'),
286: 'value' => 'rating-DESC',
287: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&sort=rating&order=DESC' . $url)
288: ];
289:
290: $data['sort'] = $sort;
291: $data['order'] = $order;
292:
293: // https://developers.google.com/search/blog/2011/09/pagination-with-relnext-and-relprev
294: if ($page == 1) {
295: $this->document->addLink($this->url->link('cms/blog', 'language=' . $this->config->get('config_language')), 'canonical');
296: } else {
297: $this->document->addLink($this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&page=' . $page), 'canonical');
298: }
299:
300: if ($page > 1) {
301: $this->document->addLink($this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . (($page - 2) ? '&page=' . ($page - 1) : '')), 'prev');
302: }
303:
304: if (ceil($article_total / $limit) > $page) {
305: $this->document->addLink($this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&page=' . ($page + 1)), 'next');
306: }
307:
308: $data['language'] = $this->config->get('config_language');
309:
310: $data['column_left'] = $this->load->controller('common/column_left');
311: $data['column_right'] = $this->load->controller('common/column_right');
312: $data['content_top'] = $this->load->controller('common/content_top');
313: $data['content_bottom'] = $this->load->controller('common/content_bottom');
314: $data['footer'] = $this->load->controller('common/footer');
315: $data['header'] = $this->load->controller('common/header');
316:
317: $this->response->setOutput($this->load->view('cms/blog', $data));
318: }
319:
320: /**
321: * Info
322: *
323: * @return \Opencart\System\Engine\Action|null
324: */
325: public function info(): ?\Opencart\System\Engine\Action {
326: $this->load->language('cms/blog');
327:
328: if (isset($this->request->get['article_id'])) {
329: $article_id = (int)$this->request->get['article_id'];
330: } else {
331: $article_id = 0;
332: }
333:
334: if (isset($this->request->get['topic_id'])) {
335: $topic_id = (int)$this->request->get['topic_id'];
336: } else {
337: $topic_id = 0;
338: }
339:
340: $this->load->model('cms/article');
341:
342: $article_info = $this->model_cms_article->getArticle($article_id);
343:
344: if ($article_info) {
345: $this->document->setTitle($article_info['meta_title']);
346: $this->document->setDescription($article_info['meta_description']);
347: $this->document->setKeywords($article_info['meta_keyword']);
348:
349: $data['breadcrumbs'] = [];
350:
351: $data['breadcrumbs'][] = [
352: 'text' => $this->language->get('text_home'),
353: 'href' => $this->url->link('common/home', 'language=' . $this->config->get('config_language'))
354: ];
355:
356: $data['breadcrumbs'][] = [
357: 'text' => $this->language->get('text_blog'),
358: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language'))
359: ];
360:
361: $url = '';
362:
363: if (isset($this->request->get['search'])) {
364: $url .= '&search=' . $this->request->get['search'];
365: }
366:
367: if (isset($this->request->get['tag'])) {
368: $url .= '&tag=' . $this->request->get['tag'];
369: }
370:
371: if (isset($this->request->get['topic_id'])) {
372: $url .= '&topic_id=' . $this->request->get['topic_id'];
373: }
374:
375: if (isset($this->request->get['author'])) {
376: $url .= '&author=' . $this->request->get['author'];
377: }
378:
379: if (isset($this->request->get['page'])) {
380: $url .= '&page=' . $this->request->get['page'];
381: }
382:
383: $this->load->model('cms/topic');
384:
385: $topic_info = $this->model_cms_topic->getTopic($topic_id);
386:
387: if ($topic_info) {
388: $data['breadcrumbs'][] = [
389: 'text' => $topic_info['name'],
390: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . $url)
391: ];
392: }
393:
394: $data['breadcrumbs'][] = [
395: 'text' => $article_info['name'],
396: 'href' => $this->url->link('cms/blog.info', 'language=' . $this->config->get('config_language') . '&article_id=' . $article_id . $url)
397: ];
398:
399: $data['heading_title'] = $article_info['name'];
400:
401: $this->load->model('tool/image');
402:
403: if (!empty($article_info['image']) && is_file(DIR_IMAGE . html_entity_decode($article_info['image'], ENT_QUOTES, 'UTF-8'))) {
404: $data['image'] = $this->model_tool_image->resize($article_info['image'], $this->config->get('config_image_article_width'), $this->config->get('config_image_article_height'));
405: } else {
406: $data['image'] = '';
407: }
408:
409: $data['description'] = html_entity_decode($article_info['description'], ENT_QUOTES, 'UTF-8');
410: $data['author'] = $article_info['author'];
411: $data['filter_author'] = $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&author=' . $article_info['author']);
412: $data['date_added'] = date($this->language->get('date_format_short'), strtotime($article_info['date_added']));
413:
414: $data['tags'] = [];
415:
416: if ($article_info['tag']) {
417: $tags = explode(',', trim($article_info['tag'], ','));
418:
419: foreach ($tags as $tag) {
420: $data['tags'][] = [
421: 'tag' => trim($tag),
422: 'href' => $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . '&tag=' . trim($tag))
423: ];
424: }
425: }
426:
427: $data['comment'] = $this->config->get('config_comment_status') ? $this->load->controller('cms/comment') : '';
428: $data['comment_total'] = $this->model_cms_article->getTotalComments($article_id, ['parent_id' => 0]);
429:
430: $data['continue'] = $this->url->link('cms/blog', 'language=' . $this->config->get('config_language') . $url);
431:
432: $data['column_left'] = $this->load->controller('common/column_left');
433: $data['column_right'] = $this->load->controller('common/column_right');
434: $data['content_top'] = $this->load->controller('common/content_top');
435: $data['content_bottom'] = $this->load->controller('common/content_bottom');
436: $data['footer'] = $this->load->controller('common/footer');
437: $data['header'] = $this->load->controller('common/header');
438:
439: $this->response->setOutput($this->load->view('cms/blog_info', $data));
440: } else {
441: return new \Opencart\System\Engine\Action('error/not_found');
442: }
443:
444: return null;
445: }
446: }
447: