1: | <?php
|
2: | namespace Opencart\Admin\Controller\Cms;
|
3: | |
4: | |
5: | |
6: | |
7: |
|
8: | class Article extends \Opencart\System\Engine\Controller {
|
9: | |
10: | |
11: | |
12: | |
13: |
|
14: | public function index(): void {
|
15: | $this->load->language('cms/article');
|
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/article', 'user_token=' . $this->session->data['user_token'] . $url)
|
43: | ];
|
44: |
|
45: | $data['add'] = $this->url->link('cms/article.form', 'user_token=' . $this->session->data['user_token'] . $url);
|
46: | $data['delete'] = $this->url->link('cms/article.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/article', $data));
|
57: | }
|
58: |
|
59: | |
60: | |
61: | |
62: | |
63: |
|
64: | public function list(): void {
|
65: | $this->load->language('cms/article');
|
66: |
|
67: | $this->response->setOutput($this->getList());
|
68: | }
|
69: |
|
70: | |
71: | |
72: | |
73: | |
74: |
|
75: | protected function getList(): string {
|
76: | if (isset($this->request->get['sort'])) {
|
77: | $sort = (string)$this->request->get['sort'];
|
78: | } else {
|
79: | $sort = 'a.date_added';
|
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/article.list', 'user_token=' . $this->session->data['user_token'] . $url);
|
109: |
|
110: | $data['articles'] = [];
|
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/article');
|
120: |
|
121: | $results = $this->model_cms_article->getArticles($filter_data);
|
122: |
|
123: | foreach ($results as $result) {
|
124: | $data['articles'][] = [
|
125: | 'article_id' => $result['article_id'],
|
126: | 'name' => $result['name'],
|
127: | 'author' => $result['author'],
|
128: | 'rating' => $result['rating'],
|
129: | 'status' => $result['status'],
|
130: | 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
|
131: | 'edit' => $this->url->link('cms/article.form', 'user_token=' . $this->session->data['user_token'] . '&article_id=' . $result['article_id'] . $url)
|
132: | ];
|
133: | }
|
134: |
|
135: | $url = '';
|
136: |
|
137: | if ($order == 'ASC') {
|
138: | $url .= '&order=DESC';
|
139: | } else {
|
140: | $url .= '&order=ASC';
|
141: | }
|
142: |
|
143: | $data['sort_name'] = $this->url->link('cms/article.list', 'user_token=' . $this->session->data['user_token'] . '&sort=ad.name' . $url);
|
144: | $data['sort_author'] = $this->url->link('cms/article.list', 'user_token=' . $this->session->data['user_token'] . '&sort=a.author' . $url);
|
145: | $data['sort_rating'] = $this->url->link('cms/article.list', 'user_token=' . $this->session->data['user_token'] . '&sort=a.rating' . $url);
|
146: | $data['sort_date_added'] = $this->url->link('cms/article.list', 'user_token=' . $this->session->data['user_token'] . '&sort=a.date_added' . $url);
|
147: |
|
148: | $url = '';
|
149: |
|
150: | if (isset($this->request->get['sort'])) {
|
151: | $url .= '&sort=' . $this->request->get['sort'];
|
152: | }
|
153: |
|
154: | if (isset($this->request->get['order'])) {
|
155: | $url .= '&order=' . $this->request->get['order'];
|
156: | }
|
157: |
|
158: | $article_total = $this->model_cms_article->getTotalArticles();
|
159: |
|
160: | $data['pagination'] = $this->load->controller('common/pagination', [
|
161: | 'total' => $article_total,
|
162: | 'page' => $page,
|
163: | 'limit' => $this->config->get('config_pagination_admin'),
|
164: | 'url' => $this->url->link('cms/article.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
|
165: | ]);
|
166: |
|
167: | $data['results'] = sprintf($this->language->get('text_pagination'), ($article_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($article_total - $this->config->get('config_pagination_admin'))) ? $article_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $article_total, ceil($article_total / $this->config->get('config_pagination_admin')));
|
168: |
|
169: | $data['sort'] = $sort;
|
170: | $data['order'] = $order;
|
171: |
|
172: | return $this->load->view('cms/article_list', $data);
|
173: | }
|
174: |
|
175: | |
176: | |
177: | |
178: | |
179: |
|
180: | public function form(): void {
|
181: | $this->load->language('cms/article');
|
182: |
|
183: | $this->document->setTitle($this->language->get('heading_title'));
|
184: |
|
185: | $this->document->addScript('view/javascript/ckeditor/ckeditor.js');
|
186: | $this->document->addScript('view/javascript/ckeditor/adapters/jquery.js');
|
187: |
|
188: | $data['text_form'] = !isset($this->request->get['article_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
|
189: |
|
190: | $url = '';
|
191: |
|
192: | if (isset($this->request->get['sort'])) {
|
193: | $url .= '&sort=' . $this->request->get['sort'];
|
194: | }
|
195: |
|
196: | if (isset($this->request->get['order'])) {
|
197: | $url .= '&order=' . $this->request->get['order'];
|
198: | }
|
199: |
|
200: | if (isset($this->request->get['page'])) {
|
201: | $url .= '&page=' . $this->request->get['page'];
|
202: | }
|
203: |
|
204: | $data['breadcrumbs'] = [];
|
205: |
|
206: | $data['breadcrumbs'][] = [
|
207: | 'text' => $this->language->get('text_home'),
|
208: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
|
209: | ];
|
210: |
|
211: | $data['breadcrumbs'][] = [
|
212: | 'text' => $this->language->get('heading_title'),
|
213: | 'href' => $this->url->link('cms/article', 'user_token=' . $this->session->data['user_token'] . $url)
|
214: | ];
|
215: |
|
216: | $data['save'] = $this->url->link('cms/article.save', 'user_token=' . $this->session->data['user_token']);
|
217: | $data['back'] = $this->url->link('cms/article', 'user_token=' . $this->session->data['user_token'] . $url);
|
218: |
|
219: | if (isset($this->request->get['article_id'])) {
|
220: | $this->load->model('cms/article');
|
221: |
|
222: | $article_info = $this->model_cms_article->getArticle($this->request->get['article_id']);
|
223: | }
|
224: |
|
225: | if (isset($this->request->get['article_id'])) {
|
226: | $data['article_id'] = (int)$this->request->get['article_id'];
|
227: | } else {
|
228: | $data['article_id'] = 0;
|
229: | }
|
230: |
|
231: | $this->load->model('localisation/language');
|
232: |
|
233: | $data['languages'] = $this->model_localisation_language->getLanguages();
|
234: |
|
235: | $this->load->model('tool/image');
|
236: |
|
237: | $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
|
238: |
|
239: | $data['article_description'] = [];
|
240: |
|
241: | if (isset($this->request->get['article_id'])) {
|
242: | $results = $this->model_cms_article->getDescriptions($this->request->get['article_id']);
|
243: |
|
244: | foreach ($results as $key => $result) {
|
245: | $data['article_description'][$key] = $result;
|
246: |
|
247: | if ($result['image'] && is_file(DIR_IMAGE . html_entity_decode($result['image'], ENT_QUOTES, 'UTF-8'))) {
|
248: | $data['article_description'][$key]['thumb'] = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
|
249: | } else {
|
250: | $data['article_description'][$key]['thumb'] = $data['placeholder'];
|
251: | }
|
252: | }
|
253: | }
|
254: |
|
255: | if (!empty($article_info)) {
|
256: | $data['author'] = $article_info['author'];
|
257: | } else {
|
258: | $data['author'] = $this->user->getFirstName() . ' ' . $this->user->getLastName();
|
259: | }
|
260: |
|
261: | $this->load->model('cms/topic');
|
262: |
|
263: | $data['topics'] = $this->model_cms_topic->getTopics();
|
264: |
|
265: | if (!empty($article_info)) {
|
266: | $data['topic_id'] = $article_info['topic_id'];
|
267: | } else {
|
268: | $data['topic_id'] = 0;
|
269: | }
|
270: |
|
271: | $data['stores'] = [];
|
272: |
|
273: | $data['stores'][] = [
|
274: | 'store_id' => 0,
|
275: | 'name' => $this->language->get('text_default')
|
276: | ];
|
277: |
|
278: | $this->load->model('setting/store');
|
279: |
|
280: | $stores = $this->model_setting_store->getStores();
|
281: |
|
282: | foreach ($stores as $store) {
|
283: | $data['stores'][] = [
|
284: | 'store_id' => $store['store_id'],
|
285: | 'name' => $store['name']
|
286: | ];
|
287: | }
|
288: |
|
289: | if (isset($this->request->get['article_id'])) {
|
290: | $data['article_store'] = $this->model_cms_article->getStores($this->request->get['article_id']);
|
291: | } else {
|
292: | $data['article_store'] = [0];
|
293: | }
|
294: |
|
295: | if (!empty($article_info)) {
|
296: | $data['status'] = $article_info['status'];
|
297: | } else {
|
298: | $data['status'] = true;
|
299: | }
|
300: |
|
301: | if (isset($this->request->get['article_id'])) {
|
302: | $this->load->model('design/seo_url');
|
303: |
|
304: | $data['article_seo_url'] = $this->model_design_seo_url->getSeoUrlsByKeyValue('article_id', $this->request->get['article_id']);
|
305: | } else {
|
306: | $data['article_seo_url'] = [];
|
307: | }
|
308: |
|
309: | $this->load->model('design/layout');
|
310: |
|
311: | $data['layouts'] = $this->model_design_layout->getLayouts();
|
312: |
|
313: | if (isset($this->request->get['article_id'])) {
|
314: | $data['article_layout'] = $this->model_cms_article->getLayouts($this->request->get['article_id']);
|
315: | } else {
|
316: | $data['article_layout'] = [];
|
317: | }
|
318: |
|
319: | $data['user_token'] = $this->session->data['user_token'];
|
320: |
|
321: | $data['header'] = $this->load->controller('common/header');
|
322: | $data['column_left'] = $this->load->controller('common/column_left');
|
323: | $data['footer'] = $this->load->controller('common/footer');
|
324: |
|
325: | $this->response->setOutput($this->load->view('cms/article_form', $data));
|
326: | }
|
327: |
|
328: | |
329: | |
330: | |
331: | |
332: |
|
333: | public function save(): void {
|
334: | $this->load->language('cms/article');
|
335: |
|
336: | $json = [];
|
337: |
|
338: | if (!$this->user->hasPermission('modify', 'cms/article')) {
|
339: | $json['error']['warning'] = $this->language->get('error_permission');
|
340: | }
|
341: |
|
342: | foreach ($this->request->post['article_description'] as $language_id => $value) {
|
343: | if (!oc_validate_length($value['name'], 1, 255)) {
|
344: | $json['error']['name_' . $language_id] = $this->language->get('error_name');
|
345: | }
|
346: |
|
347: | if (!oc_validate_length($value['meta_title'], 1, 255)) {
|
348: | $json['error']['meta_title_' . $language_id] = $this->language->get('error_meta_title');
|
349: | }
|
350: | }
|
351: |
|
352: | if (!oc_validate_length($this->request->post['author'], 3, 64)) {
|
353: | $json['error']['author'] = $this->language->get('error_author');
|
354: | }
|
355: |
|
356: | if ($this->request->post['article_seo_url']) {
|
357: | $this->load->model('design/seo_url');
|
358: |
|
359: | foreach ($this->request->post['article_seo_url'] as $store_id => $language) {
|
360: | foreach ($language as $language_id => $keyword) {
|
361: | if (!oc_validate_length($keyword, 1, 64)) {
|
362: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword');
|
363: | }
|
364: |
|
365: | if (!oc_validate_seo_url($keyword)) {
|
366: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_character');
|
367: | }
|
368: |
|
369: | $seo_url_info = $this->model_design_seo_url->getSeoUrlByKeyword($keyword, $store_id);
|
370: |
|
371: | if ($seo_url_info && (!isset($this->request->post['article_id']) || $seo_url_info['key'] != 'article_id' || $seo_url_info['value'] != (int)$this->request->post['article_id'])) {
|
372: | $json['error']['keyword_' . $store_id . '_' . $language_id] = $this->language->get('error_keyword_exists');
|
373: | }
|
374: | }
|
375: | }
|
376: | }
|
377: |
|
378: | if (isset($json['error']) && !isset($json['error']['warning'])) {
|
379: | $json['error']['warning'] = $this->language->get('error_warning');
|
380: | }
|
381: |
|
382: | if (!$json) {
|
383: | $this->load->model('cms/article');
|
384: |
|
385: | if (!$this->request->post['article_id']) {
|
386: | $json['article_id'] = $this->model_cms_article->addArticle($this->request->post);
|
387: | } else {
|
388: | $this->model_cms_article->editArticle($this->request->post['article_id'], $this->request->post);
|
389: | }
|
390: |
|
391: | $json['success'] = $this->language->get('text_success');
|
392: | }
|
393: |
|
394: | $this->response->addHeader('Content-Type: application/json');
|
395: | $this->response->setOutput(json_encode($json));
|
396: | }
|
397: |
|
398: | |
399: | |
400: | |
401: | |
402: |
|
403: | public function delete(): void {
|
404: | $this->load->language('cms/article');
|
405: |
|
406: | $json = [];
|
407: |
|
408: | if (isset($this->request->post['selected'])) {
|
409: | $selected = $this->request->post['selected'];
|
410: | } else {
|
411: | $selected = [];
|
412: | }
|
413: |
|
414: | if (!$this->user->hasPermission('modify', 'cms/article')) {
|
415: | $json['error'] = $this->language->get('error_permission');
|
416: | }
|
417: |
|
418: | if (!$json) {
|
419: | $this->load->model('cms/article');
|
420: |
|
421: | foreach ($selected as $article_id) {
|
422: | $this->model_cms_article->deleteArticle($article_id);
|
423: | }
|
424: |
|
425: | $json['success'] = $this->language->get('text_success');
|
426: | }
|
427: |
|
428: | $this->response->addHeader('Content-Type: application/json');
|
429: | $this->response->setOutput(json_encode($json));
|
430: | }
|
431: |
|
432: | |
433: | |
434: | |
435: | |
436: |
|
437: | public function rating(): void {
|
438: | $this->load->language('cms/article');
|
439: |
|
440: | $json = [];
|
441: |
|
442: | if (isset($this->request->get['page'])) {
|
443: | $page = (int)$this->request->get['page'];
|
444: | } else {
|
445: | $page = 1;
|
446: | }
|
447: |
|
448: | if (!$this->user->hasPermission('modify', 'cms/article')) {
|
449: | $json['error'] = $this->language->get('error_permission');
|
450: | }
|
451: |
|
452: | if (!$json) {
|
453: | $limit = 100;
|
454: |
|
455: | $filter_data = [
|
456: | 'sort' => 'date_added',
|
457: | 'order' => 'ASC',
|
458: | 'start' => ($page - 1) * $limit,
|
459: | 'limit' => $limit
|
460: | ];
|
461: |
|
462: | $this->load->model('cms/article');
|
463: |
|
464: | $results = $this->model_cms_article->getArticles($filter_data);
|
465: |
|
466: | foreach ($results as $result) {
|
467: | $like = 0;
|
468: | $dislike = 0;
|
469: |
|
470: | $ratings = $this->model_cms_article->getRatings($result['article_id']);
|
471: |
|
472: | foreach ($ratings as $rating) {
|
473: | if ($rating['rating'] == 1) {
|
474: | $like = $rating['total'];
|
475: | }
|
476: |
|
477: | if ($rating['rating'] == 0) {
|
478: | $dislike = $rating['total'];
|
479: | }
|
480: | }
|
481: |
|
482: | $this->model_cms_article->editRating($result['article_id'], $like - $dislike);
|
483: | }
|
484: |
|
485: | $article_total = $this->model_cms_article->getTotalArticles();
|
486: |
|
487: | $start = ($page - 1) * $limit;
|
488: | $end = ($start > ($article_total - $limit)) ? $article_total : ($start + $limit);
|
489: |
|
490: | if ($end < $article_total) {
|
491: | $json['text'] = sprintf($this->language->get('text_next'), $start ?: 1, $end, $article_total);
|
492: |
|
493: | $json['next'] = $this->url->link('cms/article.rating', 'user_token=' . $this->session->data['user_token'] . '&page=' . ($page + 1), true);
|
494: | } else {
|
495: | $json['success'] = $this->language->get('text_success');
|
496: |
|
497: | $json['next'] = '';
|
498: | }
|
499: | }
|
500: |
|
501: | $this->response->addHeader('Content-Type: application/json');
|
502: | $this->response->setOutput(json_encode($json));
|
503: | }
|
504: | }
|
505: | |