1: | <?php
2: | namespace Opencart\Admin\Controller\Catalog;
3: | /**
4: | * Class Subscription Plan
5: | *
6: | * @package Opencart\Admin\Controller\Catalog
7: | */
8: | class SubscriptionPlan extends \Opencart\System\Engine\Controller {
9: | /**
10: | * Index
11: | *
12: | * @return void
13: | */
14: | public function index(): void {
15: | $this->load->language('catalog/subscription_plan');
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('catalog/subscription_plan', 'user_token=' . $this->session->data['user_token'] . $url)
43: | ];
44: |
45: | $data['add'] = $this->url->link('catalog/subscription_plan.form', 'user_token=' . $this->session->data['user_token'] . $url);
46: | $data['copy'] = $this->url->link('catalog/subscription_plan.copy', 'user_token=' . $this->session->data['user_token'] . $url);
47: | $data['delete'] = $this->url->link('catalog/subscription_plan.delete', 'user_token=' . $this->session->data['user_token']);
48: |
49: | $data['list'] = $this->getList();
50: |
51: | $data['user_token'] = $this->session->data['user_token'];
52: |
53: | $data['header'] = $this->load->controller('common/header');
54: | $data['column_left'] = $this->load->controller('common/column_left');
55: | $data['footer'] = $this->load->controller('common/footer');
56: |
57: | $this->response->setOutput($this->load->view('catalog/subscription_plan', $data));
58: | }
59: |
60: | /**
61: | * List
62: | *
63: | * @return void
64: | */
65: | public function list(): void {
66: | $this->load->language('catalog/subscription_plan');
67: |
68: | $this->response->setOutput($this->getList());
69: | }
70: |
71: | /**
72: | * Get List
73: | *
74: | * @return string
75: | */
76: | protected function getList(): string {
77: | if (isset($this->request->get['sort'])) {
78: | $sort = (string)$this->request->get['sort'];
79: | } else {
80: | $sort = 'rd.name';
81: | }
82: |
83: | if (isset($this->request->get['order'])) {
84: | $order = (string)$this->request->get['order'];
85: | } else {
86: | $order = 'ASC';
87: | }
88: |
89: | if (isset($this->request->get['page'])) {
90: | $page = (int)$this->request->get['page'];
91: | } else {
92: | $page = 1;
93: | }
94: |
95: | $url = '';
96: |
97: | if (isset($this->request->get['sort'])) {
98: | $url .= '&sort=' . $this->request->get['sort'];
99: | }
100: |
101: | if (isset($this->request->get['order'])) {
102: | $url .= '&order=' . $this->request->get['order'];
103: | }
104: |
105: | if (isset($this->request->get['page'])) {
106: | $url .= '&page=' . $this->request->get['page'];
107: | }
108: |
109: | $data['action'] = $this->url->link('catalog/subscription_plan.list', 'user_token=' . $this->session->data['user_token'] . $url);
110: |
111: | $data['subscription_plans'] = [];
112: |
113: | $filter_data = [
114: | 'sort' => $sort,
115: | 'order' => $order,
116: | 'start' => ($page - 1) * $this->config->get('config_pagination_admin'),
117: | 'limit' => $this->config->get('config_pagination_admin')
118: | ];
119: |
120: | $this->load->model('catalog/subscription_plan');
121: |
122: | $results = $this->model_catalog_subscription_plan->getSubscriptionPlans($filter_data);
123: |
124: | foreach ($results as $result) {
125: | $data['subscription_plans'][] = [
126: | 'subscription_plan_id' => $result['subscription_plan_id'],
127: | 'name' => $result['name'],
128: | 'status' => $result['status'],
129: | 'sort_order' => $result['sort_order'],
130: | 'edit' => $this->url->link('catalog/subscription_plan.form', 'user_token=' . $this->session->data['user_token'] . '&subscription_plan_id=' . $result['subscription_plan_id'] . $url)
131: | ];
132: | }
133: |
134: | $url = '';
135: |
136: | if ($order == 'ASC') {
137: | $url .= '&order=DESC';
138: | } else {
139: | $url .= '&order=ASC';
140: | }
141: |
142: | $data['sort_name'] = $this->url->link('catalog/subscription_plan.list', 'user_token=' . $this->session->data['user_token'] . '&sort=spd.name' . $url);
143: | $data['sort_sort_order'] = $this->url->link('catalog/subscription_plan.list', 'user_token=' . $this->session->data['user_token'] . '&sort=sp.sort_order' . $url);
144: |
145: | $url = '';
146: |
147: | if (isset($this->request->get['sort'])) {
148: | $url .= '&sort=' . $this->request->get['sort'];
149: | }
150: |
151: | if (isset($this->request->get['order'])) {
152: | $url .= '&order=' . $this->request->get['order'];
153: | }
154: |
155: | $subscription_plan_total = $this->model_catalog_subscription_plan->getTotalSubscriptionPlans();
156: |
157: | $data['pagination'] = $this->load->controller('common/pagination', [
158: | 'total' => $subscription_plan_total,
159: | 'page' => $page,
160: | 'limit' => $this->config->get('config_pagination_admin'),
161: | 'url' => $this->url->link('catalog/subscription_plan.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
162: | ]);
163: |
164: | $data['results'] = sprintf($this->language->get('text_pagination'), ($subscription_plan_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($subscription_plan_total - $this->config->get('config_pagination_admin'))) ? $subscription_plan_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $subscription_plan_total, ceil($subscription_plan_total / $this->config->get('config_pagination_admin')));
165: |
166: | $data['sort'] = $sort;
167: | $data['order'] = $order;
168: |
169: | return $this->load->view('catalog/subscription_plan_list', $data);
170: | }
171: |
172: | /**
173: | * Form
174: | *
175: | * @return void
176: | */
177: | public function form(): void {
178: | $this->load->language('catalog/subscription_plan');
179: |
180: | $this->document->setTitle($this->language->get('heading_title'));
181: |
182: | $data['text_form'] = !isset($this->request->get['subscription_plan_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
183: |
184: | $url = '';
185: |
186: | if (isset($this->request->get['sort'])) {
187: | $url .= '&sort=' . $this->request->get['sort'];
188: | }
189: |
190: | if (isset($this->request->get['order'])) {
191: | $url .= '&order=' . $this->request->get['order'];
192: | }
193: |
194: | if (isset($this->request->get['page'])) {
195: | $url .= '&page=' . $this->request->get['page'];
196: | }
197: |
198: | $data['breadcrumbs'] = [];
199: |
200: | $data['breadcrumbs'][] = [
201: | 'text' => $this->language->get('text_home'),
202: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
203: | ];
204: |
205: | $data['breadcrumbs'][] = [
206: | 'text' => $this->language->get('heading_title'),
207: | 'href' => $this->url->link('catalog/subscription_plan', 'user_token=' . $this->session->data['user_token'] . $url)
208: | ];
209: |
210: | $data['save'] = $this->url->link('catalog/subscription_plan.save', 'user_token=' . $this->session->data['user_token']);
211: | $data['back'] = $this->url->link('catalog/subscription_plan', 'user_token=' . $this->session->data['user_token'] . $url);
212: |
213: | if (isset($this->request->get['subscription_plan_id'])) {
214: | $this->load->model('catalog/subscription_plan');
215: |
216: | $subscription_info = $this->model_catalog_subscription_plan->getSubscriptionPlan($this->request->get['subscription_plan_id']);
217: | }
218: |
219: | if (isset($this->request->get['subscription_plan_id'])) {
220: | $data['subscription_plan_id'] = (int)$this->request->get['subscription_plan_id'];
221: | } else {
222: | $data['subscription_plan_id'] = 0;
223: | }
224: |
225: | $this->load->model('localisation/language');
226: |
227: | $data['languages'] = $this->model_localisation_language->getLanguages();
228: |
229: | if (isset($this->request->get['subscription_plan_id'])) {
230: | $data['subscription_plan_description'] = $this->model_catalog_subscription_plan->getDescriptions($this->request->get['subscription_plan_id']);
231: | } else {
232: | $data['subscription_plan_description'] = [];
233: | }
234: |
235: | $data['frequencies'] = [];
236: |
237: | $data['frequencies'][] = [
238: | 'text' => $this->language->get('text_day'),
239: | 'value' => 'day'
240: | ];
241: |
242: | $data['frequencies'][] = [
243: | 'text' => $this->language->get('text_week'),
244: | 'value' => 'week'
245: | ];
246: |
247: | $data['frequencies'][] = [
248: | 'text' => $this->language->get('text_semi_month'),
249: | 'value' => 'semi_month'
250: | ];
251: |
252: | $data['frequencies'][] = [
253: | 'text' => $this->language->get('text_month'),
254: | 'value' => 'month'
255: | ];
256: |
257: | $data['frequencies'][] = [
258: | 'text' => $this->language->get('text_year'),
259: | 'value' => 'year'
260: | ];
261: |
262: | if (!empty($subscription_info)) {
263: | $data['trial_frequency'] = $subscription_info['trial_frequency'];
264: | } else {
265: | $data['trial_frequency'] = '';
266: | }
267: |
268: | if (!empty($subscription_info)) {
269: | $data['trial_duration'] = $subscription_info['trial_duration'];
270: | } else {
271: | $data['trial_duration'] = '0';
272: | }
273: |
274: | if (!empty($subscription_info)) {
275: | $data['trial_cycle'] = $subscription_info['trial_cycle'];
276: | } else {
277: | $data['trial_cycle'] = '1';
278: | }
279: |
280: | if (!empty($subscription_info)) {
281: | $data['trial_status'] = $subscription_info['trial_status'];
282: | } else {
283: | $data['trial_status'] = 0;
284: | }
285: |
286: | if (!empty($subscription_info)) {
287: | $data['frequency'] = $subscription_info['frequency'];
288: | } else {
289: | $data['frequency'] = '';
290: | }
291: |
292: | if (!empty($subscription_info)) {
293: | $data['duration'] = $subscription_info['duration'];
294: | } else {
295: | $data['duration'] = 0;
296: | }
297: |
298: | if (!empty($subscription_info)) {
299: | $data['cycle'] = $subscription_info['cycle'];
300: | } else {
301: | $data['cycle'] = 1;
302: | }
303: |
304: | if (!empty($subscription_info)) {
305: | $data['status'] = $subscription_info['status'];
306: | } else {
307: | $data['status'] = 0;
308: | }
309: |
310: | if (!empty($subscription_info)) {
311: | $data['sort_order'] = $subscription_info['sort_order'];
312: | } else {
313: | $data['sort_order'] = 0;
314: | }
315: |
316: | $data['user_token'] = $this->session->data['user_token'];
317: |
318: | $data['header'] = $this->load->controller('common/header');
319: | $data['column_left'] = $this->load->controller('common/column_left');
320: | $data['footer'] = $this->load->controller('common/footer');
321: |
322: | $this->response->setOutput($this->load->view('catalog/subscription_plan_form', $data));
323: | }
324: |
325: | /**
326: | * Save
327: | *
328: | * @return void
329: | */
330: | public function save(): void {
331: | $this->load->language('catalog/subscription_plan');
332: |
333: | $json = [];
334: |
335: | if (!$this->user->hasPermission('modify', 'catalog/subscription_plan')) {
336: | $json['error']['warning'] = $this->language->get('error_permission');
337: | }
338: |
339: | foreach ($this->request->post['subscription_plan_description'] as $language_id => $value) {
340: | if (!oc_validate_length($value['name'], 3, 255)) {
341: | $json['error']['name_' . $language_id] = $this->language->get('error_name');
342: | }
343: | }
344: |
345: | if ($this->request->post['trial_duration'] && (int)$this->request->post['trial_duration'] < 1) {
346: | $json['error']['trial_duration'] = $this->language->get('error_trial_duration');
347: | }
348: |
349: | if (isset($json['error']) && !isset($json['error']['warning'])) {
350: | $json['error']['warning'] = $this->language->get('error_warning');
351: | }
352: |
353: | if (!$json) {
354: | $this->load->model('catalog/subscription_plan');
355: |
356: | if (!$this->request->post['subscription_plan_id']) {
357: | $json['subscription_plan_id'] = $this->model_catalog_subscription_plan->addSubscriptionPlan($this->request->post);
358: | } else {
359: | $this->model_catalog_subscription_plan->editSubscriptionPlan($this->request->post['subscription_plan_id'], $this->request->post);
360: | }
361: |
362: | $json['success'] = $this->language->get('text_success');
363: | }
364: |
365: | $this->response->addHeader('Content-Type: application/json');
366: | $this->response->setOutput(json_encode($json));
367: | }
368: |
369: | /**
370: | * Copy
371: | *
372: | * @return void
373: | */
374: | public function copy(): void {
375: | $this->load->language('catalog/subscription_plan');
376: |
377: | $json = [];
378: |
379: | if (isset($this->request->post['selected'])) {
380: | $selected = $this->request->post['selected'];
381: | } else {
382: | $selected = [];
383: | }
384: |
385: | if (!$this->user->hasPermission('modify', 'catalog/subscription_plan')) {
386: | $json['error'] = $this->language->get('error_permission');
387: | }
388: |
389: | if (!$json) {
390: | $this->load->model('catalog/subscription_plan');
391: |
392: | foreach ($selected as $subscription_plan_id) {
393: | $this->model_catalog_subscription_plan->copySubscriptionPlan($subscription_plan_id);
394: | }
395: |
396: | $json['success'] = $this->language->get('text_success');
397: | }
398: |
399: | $this->response->addHeader('Content-Type: application/json');
400: | $this->response->setOutput(json_encode($json));
401: | }
402: |
403: | /**
404: | * Delete
405: | *
406: | * @return void
407: | */
408: | public function delete(): void {
409: | $this->load->language('catalog/subscription_plan');
410: |
411: | $json = [];
412: |
413: | if (isset($this->request->post['selected'])) {
414: | $selected = $this->request->post['selected'];
415: | } else {
416: | $selected = [];
417: | }
418: |
419: | if (!$this->user->hasPermission('modify', 'catalog/subscription_plan')) {
420: | $json['error'] = $this->language->get('error_permission');
421: | }
422: |
423: | $this->load->model('catalog/product');
424: |
425: | foreach ($selected as $subscription_id) {
426: | $product_total = $this->model_catalog_product->getTotalProductsBySubscriptionPlanId($subscription_id);
427: |
428: | if ($product_total) {
429: | $json['error'] = sprintf($this->language->get('error_product'), $product_total);
430: | }
431: | }
432: |
433: | if (!$json) {
434: | $this->load->model('catalog/subscription_plan');
435: |
436: | foreach ($selected as $subscription_plan_id) {
437: | $this->model_catalog_subscription_plan->deleteSubscriptionPlan($subscription_plan_id);
438: | }
439: |
440: | $json['success'] = $this->language->get('text_success');
441: | }
442: |
443: | $this->response->addHeader('Content-Type: application/json');
444: | $this->response->setOutput(json_encode($json));
445: | }
446: | }
447: |

OpenCart API API documentation generated by ApiGen dev-master