1: <?php
2: namespace Opencart\Admin\Controller\Cms;
3: /**
4: * Class Topic
5: *
6: * @package Opencart\Admin\Controller\Cms
7: */
8: class Topic extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('cms/topic');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['sort'])) {
22: $url .= '&sort=' . $this->request->get['sort'];
23: }
24:
25: if (isset($this->request->get['order'])) {
26: $url .= '&order=' . $this->request->get['order'];
27: }
28:
29: if (isset($this->request->get['page'])) {
30: $url .= '&page=' . $this->request->get['page'];
31: }
32:
33: $data['breadcrumbs'] = [];
34:
35: $data['breadcrumbs'][] = [
36: 'text' => $this->language->get('text_home'),
37: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
38: ];
39:
40: $data['breadcrumbs'][] = [
41: 'text' => $this->language->get('heading_title'),
42: 'href' => $this->url->link('cms/topic', 'user_token=' . $this->session->data['user_token'] . $url)
43: ];
44:
45: $data['add'] = $this->url->link('cms/topic.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: $data['delete'] = $this->url->link('cms/topic.delete', 'user_token=' . $this->session->data['user_token']);
47:
48: $data['list'] = $this->getList();
49:
50: $data['user_token'] = $this->session->data['user_token'];
51:
52: $data['header'] = $this->load->controller('common/header');
53: $data['column_left'] = $this->load->controller('common/column_left');
54: $data['footer'] = $this->load->controller('common/footer');
55:
56: $this->response->setOutput($this->load->view('cms/topic', $data));
57: }
58:
59: /**
60: * List
61: *
62: * @return void
63: */
64: public function list(): void {
65: $this->load->language('cms/topic');
66:
67: $this->response->setOutput($this->getList());
68: }
69:
70: /**
71: * Get List
72: *
73: * @return string
74: */
75: protected function getList(): string {
76: if (isset($this->request->get['sort'])) {
77: $sort = (string)$this->request->get['sort'];
78: } else {
79: $sort = 't.sort_order';
80: }
81:
82: if (isset($this->request->get['order'])) {
83: $order = (string)$this->request->get['order'];
84: } else {
85: $order = 'ASC';
86: }
87:
88: if (isset($this->request->get['page'])) {
89: $page = (int)$this->request->get['page'];
90: } else {
91: $page = 1;
92: }
93:
94: $url = '';
95:
96: if (isset($this->request->get['sort'])) {
97: $url .= '&sort=' . $this->request->get['sort'];
98: }
99:
100: if (isset($this->request->get['order'])) {
101: $url .= '&order=' . $this->request->get['order'];
102: }
103:
104: if (isset($this->request->get['page'])) {
105: $url .= '&page=' . $this->request->get['page'];
106: }
107:
108: $data['action'] = $this->url->link('cms/topic.list', 'user_token=' . $this->session->data['user_token'] . $url);
109:
110: $data['topics'] = [];
111:
112: $filter_data = [
113: 'sort' => $sort,
114: 'order' => $order,
115: 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
116: 'limit' => $this->config->get('config_pagination_admin')
117: ];
118:
119: $this->load->model('cms/topic');
120:
121: $results = $this->model_cms_topic->getTopics($filter_data);
122:
123: foreach ($results as $result) {
124: $data['topics'][] = [
125: 'topic_id' => $result['topic_id'],
126: 'name' => $result['name'],
127: 'status' => $result['status'],
128: 'sort_order' => $result['sort_order'],
129: 'edit' => $this->url->link('cms/topic.form', 'user_token=' . $this->session->data['user_token'] . '&topic_id=' . $result['topic_id'] . $url)
130: ];
131: }
132:
133: $url = '';
134:
135: if ($order == 'ASC') {
136: $url .= '&order=DESC';
137: } else {
138: $url .= '&order=ASC';
139: }
140:
141: $data['sort_name'] = $this->url->link('cms/topic.list', 'user_token=' . $this->session->data['user_token'] . '&sort=bcd.name' . $url);
142: $data['sort_sort_order'] = $this->url->link('cms/topic.list', 'user_token=' . $this->session->data['user_token'] . '&sort=bc.sort_order' . $url);
143:
144: $url = '';
145:
146: if (isset($this->request->get['sort'])) {
147: $url .= '&sort=' . $this->request->get['sort'];
148: }
149:
150: if (isset($this->request->get['order'])) {
151: $url .= '&order=' . $this->request->get['order'];
152: }
153:
154: $topic_total = $this->model_cms_topic->getTotalTopics();
155:
156: $data['pagination'] = $this->load->controller('common/pagination', [
157: 'total' => $topic_total,
158: 'page' => $page,
159: 'limit' => $this->config->get('config_pagination_admin'),
160: 'url' => $this->url->link('cms/topic.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
161: ]);
162:
163: $data['results'] = sprintf($this->language->get('text_pagination'), ($topic_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($topic_total - $this->config->get('config_pagination_admin'))) ? $topic_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $topic_total, ceil($topic_total / $this->config->get('config_pagination_admin')));
164:
165: $data['sort'] = $sort;
166: $data['order'] = $order;
167:
168: return $this->load->view('cms/topic_list', $data);
169: }
170:
171: /**
172: * Form
173: *
174: * @return void
175: */
176: public function form(): void {
177: $this->load->language('cms/topic');
178:
179: $this->document->setTitle($this->language->get('heading_title'));
180:
181: $this->document->addScript('view/javascript/ckeditor/ckeditor.js');
182: $this->document->addScript('view/javascript/ckeditor/adapters/jquery.js');
183:
184: $data['text_form'] = !isset($this->request->get['topic_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
185:
186: $url = '';
187:
188: if (isset($this->request->get['sort'])) {
189: $url .= '&sort=' . $this->request->get['sort'];
190: }
191:
192: if (isset($this->request->get['order'])) {
193: $url .= '&order=' . $this->request->get['order'];
194: }
195:
196: if (isset($this->request->get['page'])) {
197: $url .= '&page=' . $this->request->get['page'];
198: }
199:
200: $data['breadcrumbs'] = [];
201:
202: $data['breadcrumbs'][] = [
203: 'text' => $this->language->get('text_home'),
204: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
205: ];
206:
207: $data['breadcrumbs'][] = [
208: 'text' => $this->language->get('heading_title'),
209: 'href' => $this->url->link('cms/topic', 'user_token=' . $this->session->data['user_token'] . $url)
210: ];
211:
212: $data['save'] = $this->url->link('cms/topic.save', 'user_token=' . $this->session->data['user_token']);
213: $data['back'] = $this->url->link('cms/topic', 'user_token=' . $this->session->data['user_token'] . $url);
214:
215: if (isset($this->request->get['topic_id'])) {
216: $this->load->model('cms/topic');
217:
218: $topic_info = $this->model_cms_topic->getTopic($this->request->get['topic_id']);
219: }
220:
221: if (isset($this->request->get['topic_id'])) {
222: $data['topic_id'] = (int)$this->request->get['topic_id'];
223: } else {
224: $data['topic_id'] = 0;
225: }
226:
227: $this->load->model('localisation/language');
228:
229: $data['languages'] = $this->model_localisation_language->getLanguages();
230:
231: $this->load->model('tool/image');
232:
233: $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
234:
235: $data['topic_description'] = [];
236:
237: if (isset($this->request->get['topic_id'])) {
238: $results = $this->model_cms_topic->getDescriptions($this->request->get['topic_id']);
239:
240: foreach ($results as $key => $result) {
241: $data['topic_description'][$key] = $result;
242:
243: if ($result['image'] && is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
244: $data['topic_description'][$key]['thumb'] = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
245: } else {
246: $data['topic_description'][$key]['thumb'] = $data['placeholder'];
247: }
248: }
249: }
250:
251: $data['stores'] = [];
252:
253: $data['stores'][] = [
254: 'store_id' => 0,
255: 'name' => $this->language->get('text_default')
256: ];
257:
258: $this->load->model('setting/store');
259:
260: $stores = $this->model_setting_store->getStores();
261:
262: foreach ($stores as $store) {
263: $data['stores'][] = [
264: 'store_id' => $store['store_id'],
265: 'name' => $store['name']
266: ];
267: }
268:
269: if (isset($this->request->get['topic_id'])) {
270: $data['topic_store'] = $this->model_cms_topic->getStores($this->request->get['topic_id']);
271: } else {
272: $data['topic_store'] = [0];
273: }
274:
275: if (!empty($topic_info)) {
276: $data['sort_order'] = $topic_info['sort_order'];
277: } else {
278: $data['sort_order'] = 0;
279: }
280:
281: if (!empty($topic_info)) {
282: $data['status'] = $topic_info['status'];
283: } else {
284: $data['status'] = true;
285: }
286:
287: if (isset($this->request->get['topic_id'])) {
288: $this->load->model('design/seo_url');
289:
290: $data['topic_seo_url'] = $this->model_design_seo_url->getSeoUrlsByKeyValue('topic_id', $this->request->get['topic_id']);
291: } else {
292: $data['topic_seo_url'] = [];
293: }
294:
295: $data['user_token'] = $this->session->data['user_token'];
296:
297: $data['header'] = $this->load->controller('common/header');
298: $data['column_left'] = $this->load->controller('common/column_left');
299: $data['footer'] = $this->load->controller('common/footer');
300:
301: $this->response->setOutput($this->load->view('cms/topic_form', $data));
302: }
303:
304: /**
305: * Save
306: *
307: * @return void
308: */
309: public function save(): void {
310: $this->load->language('cms/topic');
311:
312: $json = [];
313:
314: if (!$this->user->hasPermission('modify', 'cms/topic')) {
315: $json['error']['warning'] = $this->language->get('error_permission');
316: }
317:
318: foreach ($this->request->post['topic_description'] as $language_id => $value) {
319: if (!oc_validate_length($value['name'], 1, 255)) {
320: $json['error']['name_' . $language_id] = $this->language->get('error_name');
321: }
322:
323: if (!oc_validate_length($value['meta_title'], 1, 255)) {
324: $json['error']['meta_title_' . $language_id] = $this->language->get('error_meta_title');
325: }
326: }
327:
328: if ($this->request->post['topic_seo_url']) {
329: $this->load->model('design/seo_url');
330:
331: foreach ($this->request->post['topic_seo_url'] as $store_id => $language) {
332: foreach ($language as $language_id => $keyword) {
333: if (!oc_validate_length($keyword, 1, 64)) {
334: $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword');
335: }
336:
337: if (!oc_validate_seo_url($keyword)) {
338: $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_character');
339: }
340:
341: $seo_url_info = $this->model_design_seo_url->getSeoUrlByKeyword($keyword, $store_id);
342:
343: if ($seo_url_info && (!isset($this->request->post['topic_id']) || $seo_url_info['key'] != 'topic_id' || $seo_url_info['value'] != (int)$this->request->post['topic_id'])) {
344: $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_exists');
345: }
346: }
347: }
348: }
349:
350: if (isset($json['error']) && !isset($json['error']['warning'])) {
351: $json['error']['warning'] = $this->language->get('error_warning');
352: }
353:
354: if (!$json) {
355: $this->load->model('cms/topic');
356:
357: if (!$this->request->post['topic_id']) {
358: $json['topic_id'] = $this->model_cms_topic->addTopic($this->request->post);
359: } else {
360: $this->model_cms_topic->editTopic($this->request->post['topic_id'], $this->request->post);
361: }
362:
363: $json['success'] = $this->language->get('text_success');
364: }
365:
366: $this->response->addHeader('Content-Type: application/json');
367: $this->response->setOutput(json_encode($json));
368: }
369:
370: /**
371: * Delete
372: *
373: * @return void
374: */
375: public function delete(): void {
376: $this->load->language('cms/topic');
377:
378: $json = [];
379:
380: if (isset($this->request->post['selected'])) {
381: $selected = $this->request->post['selected'];
382: } else {
383: $selected = [];
384: }
385:
386: if (!$this->user->hasPermission('modify', 'cms/topic')) {
387: $json['error'] = $this->language->get('error_permission');
388: }
389:
390: if (!$json) {
391: $this->load->model('cms/topic');
392:
393: foreach ($selected as $topic_id) {
394: $this->model_cms_topic->deleteTopic($topic_id);
395: }
396:
397: $json['success'] = $this->language->get('text_success');
398: }
399:
400: $this->response->addHeader('Content-Type: application/json');
401: $this->response->setOutput(json_encode($json));
402: }
403: }
404: