1: <?php
2: namespace Opencart\Admin\Controller\Design;
3: /**
4: * Class Theme
5: *
6: * @package Opencart\Admin\Controller\Design
7: */
8: class Theme extends \Opencart\System\Engine\Controller {
9: /**
10: * Index
11: *
12: * @return void
13: */
14: public function index(): void {
15: $this->load->language('design/theme');
16:
17: $this->document->setTitle($this->language->get('heading_title'));
18:
19: $url = '';
20:
21: if (isset($this->request->get['page'])) {
22: $url .= '&page=' . (int)$this->request->get['page'];
23: }
24:
25: $data['breadcrumbs'] = [];
26:
27: $data['breadcrumbs'][] = [
28: 'text' => $this->language->get('text_home'),
29: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
30: ];
31:
32: $data['breadcrumbs'][] = [
33: 'text' => $this->language->get('heading_title'),
34: 'href' => $this->url->link('design/theme', 'user_token=' . $this->session->data['user_token'] . $url)
35: ];
36:
37: $data['add'] = $this->url->link('design/theme.form', 'user_token=' . $this->session->data['user_token'] . $url);
38: $data['delete'] = $this->url->link('design/theme.delete', 'user_token=' . $this->session->data['user_token']);
39:
40: $data['list'] = $this->controller_design_theme->getList();
41:
42: $data['user_token'] = $this->session->data['user_token'];
43:
44: $data['header'] = $this->load->controller('common/header');
45: $data['column_left'] = $this->load->controller('common/column_left');
46: $data['footer'] = $this->load->controller('common/footer');
47:
48: $this->response->setOutput($this->load->view('design/theme', $data));
49: }
50:
51: /**
52: * List
53: *
54: * @return void
55: */
56: public function list(): void {
57: $this->load->language('design/theme');
58:
59: $this->response->setOutput($this->controller_design_theme->getList());
60: }
61:
62: /**
63: * Get List
64: *
65: * @return string
66: */
67: public function getList(): string {
68: $this->load->language('design/theme');
69:
70: if (isset($this->request->get['page'])) {
71: $page = (int)$this->request->get['page'];
72: } else {
73: $page = 1;
74: }
75:
76: $url = '';
77:
78: if (isset($this->request->get['page'])) {
79: $url .= '&page=' . $this->request->get['page'];
80: }
81:
82: $data['action'] = $this->url->link('design/theme.list', 'user_token=' . $this->session->data['user_token'] . $url);
83:
84: $data['themes'] = [];
85:
86: $this->load->model('design/theme');
87: $this->load->model('setting/store');
88:
89: $results = $this->model_design_theme->getThemes(($page - 1) * $this->config->get('config_pagination_admin'), $this->config->get('config_pagination_admin'));
90:
91: foreach ($results as $result) {
92: $store_info = $this->model_setting_store->getStore($result['store_id']);
93:
94: if ($store_info) {
95: $store = $store_info['name'];
96: } else {
97: $store = '';
98: }
99:
100: $data['themes'][] = [
101: 'theme_id' => $result['theme_id'],
102: 'route' => $result['route'],
103: 'store' => ($result['store_id'] ? $store : $this->language->get('text_default')),
104: 'status' => $result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
105: 'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
106: 'edit' => $this->url->link('design/theme.form', 'user_token=' . $this->session->data['user_token'] . '&theme_id=' . $result['theme_id']),
107: 'delete' => $this->url->link('design/theme.delete', 'user_token=' . $this->session->data['user_token'] . '&theme_id=' . $result['theme_id'])
108: ];
109: }
110:
111: $theme_total = $this->model_design_theme->getTotalThemes();
112:
113: $data['pagination'] = $this->load->controller('common/pagination', [
114: 'total' => $theme_total,
115: 'page' => $page,
116: 'limit' => $this->config->get('config_pagination_admin'),
117: 'url' => $this->url->link('design/theme.list', 'user_token=' . $this->session->data['user_token'] . '&page={page}')
118: ]);
119:
120: $data['results'] = sprintf($this->language->get('text_pagination'), ($theme_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($theme_total - $this->config->get('config_pagination_admin'))) ? $theme_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $theme_total, ceil($theme_total / $this->config->get('config_pagination_admin')));
121:
122: return $this->load->view('design/theme_list', $data);
123: }
124:
125: /**
126: * Get Form
127: *
128: * @return void
129: */
130: public function form(): void {
131: $this->load->language('design/theme');
132:
133: $this->document->setTitle($this->language->get('heading_title'));
134:
135: $data['text_form'] = !isset($this->request->get['theme_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
136:
137: $url = '';
138:
139: if (isset($this->request->get['page'])) {
140: $url .= '&page=' . $this->request->get['page'];
141: }
142:
143: $data['breadcrumbs'] = [];
144:
145: $data['breadcrumbs'][] = [
146: 'text' => $this->language->get('text_home'),
147: 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
148: ];
149:
150: $data['breadcrumbs'][] = [
151: 'text' => $this->language->get('heading_title'),
152: 'href' => $this->url->link('design/theme', 'user_token=' . $this->session->data['user_token'] . $url)
153: ];
154:
155: $data['save'] = $this->url->link('design/theme.save', 'user_token=' . $this->session->data['user_token']);
156: $data['back'] = $this->url->link('design/theme', 'user_token=' . $this->session->data['user_token'] . $url);
157:
158: if (isset($this->request->get['theme_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
159: $this->load->model('design/theme');
160:
161: $theme_info = $this->model_design_theme->getTheme($this->request->get['theme_id']);
162: }
163:
164: if (isset($this->request->get['theme_id'])) {
165: $data['theme_id'] = (int)$this->request->get['theme_id'];
166: } else {
167: $data['theme_id'] = 0;
168: }
169:
170: $this->load->model('setting/store');
171:
172: $data['stores'] = $this->model_setting_store->getStores();
173:
174: if (!empty($theme_info)) {
175: $data['store_id'] = $theme_info['store_id'];
176: } else {
177: $data['store_id'] = 0;
178: }
179:
180: // We grab the files from the default template directory
181: $files = [];
182:
183: $path = DIR_CATALOG . 'view/template/';
184:
185: $directory = [$path];
186:
187: while (count($directory) != 0) {
188: $next = array_shift($directory);
189:
190: if (is_dir($next)) {
191: foreach (glob(rtrim($next, '/') . '/{*,.[!.]*,..?*}', GLOB_BRACE) as $file) {
192: $directory[] = $file;
193: }
194: }
195:
196: // Add the file to the files to be deleted array
197: $files[] = $next;
198: }
199:
200: sort($files);
201:
202: $data['templates'] = [];
203:
204: foreach ($files as $file) {
205: if (is_file($file)) {
206: $data['templates'][] = substr(substr($file, 0, strrpos($file, '.')), strlen($path));
207: }
208: }
209:
210: // We grab the files from the extension template directory
211: $data['extensions'] = [];
212:
213: $files = [];
214:
215: $extensions = glob(DIR_EXTENSION . '*', GLOB_ONLYDIR);
216:
217: foreach ($extensions as $extension) {
218: $extension = basename($extension);
219:
220: $path = DIR_EXTENSION . $extension . '/catalog/view/template';
221:
222: $directory = [$path];
223:
224: while (count($directory) != 0) {
225: $next = array_shift($directory);
226:
227: if (is_dir($next)) {
228: foreach (glob(rtrim($next, '/') . '/{*,.[!.]*,..?*}', GLOB_BRACE) as $file) {
229: $directory[] = $file;
230: }
231: }
232:
233: // Add the file to the files to be deleted array
234: $files[] = $next;
235: }
236:
237: sort($files);
238:
239: foreach ($files as $file) {
240: if (is_file($file)) {
241: $data['extensions'][] = 'extension/' . $extension . substr(substr($file, 0, strrpos($file, '.')), strlen($path));
242: }
243: }
244: }
245:
246: if (!empty($theme_info)) {
247: $data['route'] = $theme_info['route'];
248: } else {
249: $data['route'] = '';
250: }
251:
252: if (!empty($theme_info)) {
253: $data['code'] = $theme_info['code'];
254: } else {
255: $data['code'] = '';
256: }
257:
258: if (!empty($theme_info)) {
259: $data['status'] = $theme_info['status'];
260: } else {
261: $data['status'] = 1;
262: }
263:
264: $data['user_token'] = $this->session->data['user_token'];
265:
266: $data['header'] = $this->load->controller('common/header');
267: $data['column_left'] = $this->load->controller('common/column_left');
268: $data['footer'] = $this->load->controller('common/footer');
269:
270: $this->response->setOutput($this->load->view('design/theme_form', $data));
271: }
272:
273: /**
274: * Template
275: *
276: * @return void
277: */
278: public function template(): void {
279: $this->load->language('design/theme');
280:
281: $json = [];
282:
283: if (isset($this->request->get['path'])) {
284: $path = $this->request->get['path'];
285: } else {
286: $path = '';
287: }
288:
289: // Default template load
290: if (substr($path, 0, 10) != 'extension/') {
291: $directory = DIR_CATALOG . 'view/template';
292: $file = $directory . '/' . $path . '.twig';
293: } else {
294: // Extension template load
295: $part = explode('/', $path);
296:
297: $directory = DIR_EXTENSION . $part[1] . '/catalog/view/template';
298:
299: unset($part[0]);
300: unset($part[1]);
301:
302: $file = $directory . '/' . implode('/', $part) . '.twig';
303: }
304:
305: if (!is_file($file) || (substr(str_replace('\\', '/', realpath($file)), 0, strlen($directory)) != $directory)) {
306: $json['code'] = '';
307: }
308:
309: if (!$json) {
310: $json['code'] = file_get_contents($file);
311: }
312:
313: $this->response->addHeader('Content-Type: application/json');
314: $this->response->setOutput(json_encode($json));
315: }
316:
317: /**
318: * Save
319: *
320: * @return void
321: */
322: public function save(): void {
323: $this->load->language('design/theme');
324:
325: $json = [];
326:
327: if (isset($this->request->post['route'])) {
328: $route = $this->request->post['route'];
329: } else {
330: $route = '';
331: }
332:
333: // Check user has permission
334: if (!$this->user->hasPermission('modify', 'design/theme')) {
335: $json['error'] = $this->language->get('error_permission');
336: }
337:
338: $directory = DIR_CATALOG . 'view/template';
339: $file = $directory . '/' . $route . '.twig';
340:
341: if (!is_file($file) || (substr(str_replace('\\', '/', realpath($file)), 0, strlen($directory)) != $directory)) {
342: $json['error'] = $this->language->get('error_file');
343: }
344:
345: // Extension template load
346: if (substr($route, 0, 10) == 'extension/') {
347: $part = explode('/', $route);
348:
349: $directory = DIR_EXTENSION . $part[1] . '/catalog/view/template';
350:
351: unset($part[0]);
352: unset($part[1]);
353:
354: $route = implode('/', $part);
355:
356: $file = $directory . '/' . $route . '.twig';
357:
358: if (!is_file($file) || substr(str_replace('\\', '/', realpath($file)), 0, strlen($directory)) != $directory) {
359: $json['error'] = $this->language->get('error_file');
360: }
361: }
362:
363: if (!$json) {
364: $this->load->model('design/theme');
365:
366: if (!$this->request->post['theme_id']) {
367: $json['theme_id'] = $this->model_design_theme->addTheme($this->request->post);
368: } else {
369: $this->model_design_theme->editTheme($this->request->post['theme_id'], $this->request->post);
370: }
371:
372: $json['success'] = $this->language->get('text_success');
373: }
374:
375: $this->response->addHeader('Content-Type: application/json');
376: $this->response->setOutput(json_encode($json));
377: }
378:
379: /**
380: * Delete
381: *
382: * @return void
383: */
384: public function delete(): void {
385: $this->load->language('design/theme');
386:
387: $json = [];
388:
389: if (isset($this->request->post['selected'])) {
390: $selected = $this->request->post['selected'];
391: } else {
392: $selected = [];
393: }
394:
395: // Check user has permission
396: if (!$this->user->hasPermission('modify', 'design/theme')) {
397: $json['error'] = $this->language->get('error_permission');
398: }
399:
400: if (!$json) {
401: $this->load->model('design/theme');
402:
403: foreach ($selected as $theme_id) {
404: $this->model_design_theme->deleteTheme($theme_id);
405: }
406:
407: $json['success'] = $this->language->get('text_success');
408: }
409:
410: $this->response->addHeader('Content-Type: application/json');
411: $this->response->setOutput(json_encode($json));
412: }
413: }
414: