| 1: | <?php
|
| 2: | namespace Opencart\Admin\Controller\Catalog;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Option extends \Opencart\System\Engine\Controller {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: |
|
| 14: | public function index(): void {
|
| 15: | $this->load->language('catalog/option');
|
| 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/option', 'user_token=' . $this->session->data['user_token'] . $url)
|
| 43: | ];
|
| 44: |
|
| 45: | $data['add'] = $this->url->link('catalog/option.form', 'user_token=' . $this->session->data['user_token'] . $url);
|
| 46: | $data['delete'] = $this->url->link('catalog/option.delete', 'user_token=' . $this->session->data['user_token']);
|
| 47: |
|
| 48: | $data['list'] = $this->controller_catalog_option->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('catalog/option', $data));
|
| 57: | }
|
| 58: |
|
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: |
|
| 64: | public function list(): void {
|
| 65: | $this->load->language('catalog/option');
|
| 66: |
|
| 67: | $this->response->setOutput($this->controller_catalog_option->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 = 'od.name';
|
| 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('catalog/option.list', 'user_token=' . $this->session->data['user_token'] . $url);
|
| 109: |
|
| 110: | $data['options'] = [];
|
| 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('catalog/option');
|
| 120: |
|
| 121: | $results = $this->model_catalog_option->getOptions($filter_data);
|
| 122: |
|
| 123: | foreach ($results as $result) {
|
| 124: | $data['options'][] = [
|
| 125: | 'option_id' => $result['option_id'],
|
| 126: | 'name' => $result['name'],
|
| 127: | 'sort_order' => $result['sort_order'],
|
| 128: | 'edit' => $this->url->link('catalog/option.form', 'user_token=' . $this->session->data['user_token'] . '&option_id=' . $result['option_id'] . $url)
|
| 129: | ];
|
| 130: | }
|
| 131: |
|
| 132: | $url = '';
|
| 133: |
|
| 134: | if ($order == 'ASC') {
|
| 135: | $url .= '&order=DESC';
|
| 136: | } else {
|
| 137: | $url .= '&order=ASC';
|
| 138: | }
|
| 139: |
|
| 140: | $data['sort_name'] = $this->url->link('catalog/option.list', 'user_token=' . $this->session->data['user_token'] . '&sort=od.name' . $url);
|
| 141: | $data['sort_sort_order'] = $this->url->link('catalog/option.list', 'user_token=' . $this->session->data['user_token'] . '&sort=o.sort_order' . $url);
|
| 142: |
|
| 143: | $url = '';
|
| 144: |
|
| 145: | if (isset($this->request->get['sort'])) {
|
| 146: | $url .= '&sort=' . $this->request->get['sort'];
|
| 147: | }
|
| 148: |
|
| 149: | if (isset($this->request->get['order'])) {
|
| 150: | $url .= '&order=' . $this->request->get['order'];
|
| 151: | }
|
| 152: |
|
| 153: | $option_total = $this->model_catalog_option->getTotalOptions();
|
| 154: |
|
| 155: | $data['pagination'] = $this->load->controller('common/pagination', [
|
| 156: | 'total' => $option_total,
|
| 157: | 'page' => $page,
|
| 158: | 'limit' => $this->config->get('config_pagination_admin'),
|
| 159: | 'url' => $this->url->link('catalog/option.list', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}')
|
| 160: | ]);
|
| 161: |
|
| 162: | $data['results'] = sprintf($this->language->get('text_pagination'), ($option_total) ? (($page - 1) * $this->config->get('config_pagination_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_pagination_admin')) > ($option_total - $this->config->get('config_pagination_admin'))) ? $option_total : ((($page - 1) * $this->config->get('config_pagination_admin')) + $this->config->get('config_pagination_admin')), $option_total, ceil($option_total / $this->config->get('config_pagination_admin')));
|
| 163: |
|
| 164: | $data['sort'] = $sort;
|
| 165: | $data['order'] = $order;
|
| 166: |
|
| 167: | return $this->load->view('catalog/option_list', $data);
|
| 168: | }
|
| 169: |
|
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: |
|
| 175: | public function form(): void {
|
| 176: | $this->load->language('catalog/option');
|
| 177: |
|
| 178: | $this->document->setTitle($this->language->get('heading_title'));
|
| 179: |
|
| 180: | $data['text_form'] = !isset($this->request->get['option_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
|
| 181: |
|
| 182: | $url = '';
|
| 183: |
|
| 184: | if (isset($this->request->get['sort'])) {
|
| 185: | $url .= '&sort=' . $this->request->get['sort'];
|
| 186: | }
|
| 187: |
|
| 188: | if (isset($this->request->get['order'])) {
|
| 189: | $url .= '&order=' . $this->request->get['order'];
|
| 190: | }
|
| 191: |
|
| 192: | if (isset($this->request->get['page'])) {
|
| 193: | $url .= '&page=' . $this->request->get['page'];
|
| 194: | }
|
| 195: |
|
| 196: | $data['breadcrumbs'] = [];
|
| 197: |
|
| 198: | $data['breadcrumbs'][] = [
|
| 199: | 'text' => $this->language->get('text_home'),
|
| 200: | 'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'])
|
| 201: | ];
|
| 202: |
|
| 203: | $data['breadcrumbs'][] = [
|
| 204: | 'text' => $this->language->get('heading_title'),
|
| 205: | 'href' => $this->url->link('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url)
|
| 206: | ];
|
| 207: |
|
| 208: | $data['save'] = $this->url->link('catalog/option.save', 'user_token=' . $this->session->data['user_token']);
|
| 209: | $data['back'] = $this->url->link('catalog/option', 'user_token=' . $this->session->data['user_token'] . $url);
|
| 210: |
|
| 211: | if (isset($this->request->get['option_id'])) {
|
| 212: | $this->load->model('catalog/option');
|
| 213: |
|
| 214: | $option_info = $this->model_catalog_option->getOption($this->request->get['option_id']);
|
| 215: | }
|
| 216: |
|
| 217: | if (isset($this->request->get['option_id'])) {
|
| 218: | $data['option_id'] = (int)$this->request->get['option_id'];
|
| 219: | } else {
|
| 220: | $data['option_id'] = 0;
|
| 221: | }
|
| 222: |
|
| 223: | $this->load->model('localisation/language');
|
| 224: |
|
| 225: | $data['languages'] = $this->model_localisation_language->getLanguages();
|
| 226: |
|
| 227: | if (isset($this->request->get['option_id'])) {
|
| 228: | $data['option_description'] = $this->model_catalog_option->getDescriptions($this->request->get['option_id']);
|
| 229: | } else {
|
| 230: | $data['option_description'] = [];
|
| 231: | }
|
| 232: |
|
| 233: | if (!empty($option_info)) {
|
| 234: | $data['type'] = $option_info['type'];
|
| 235: | } else {
|
| 236: | $data['type'] = '';
|
| 237: | }
|
| 238: |
|
| 239: | if (!empty($option_info)) {
|
| 240: | $data['sort_order'] = $option_info['sort_order'];
|
| 241: | } else {
|
| 242: | $data['sort_order'] = '';
|
| 243: | }
|
| 244: |
|
| 245: | if (isset($this->request->get['option_id'])) {
|
| 246: | $option_values = $this->model_catalog_option->getValueDescriptions($this->request->get['option_id']);
|
| 247: | } else {
|
| 248: | $option_values = [];
|
| 249: | }
|
| 250: |
|
| 251: | $this->load->model('tool/image');
|
| 252: |
|
| 253: | $data['option_values'] = [];
|
| 254: |
|
| 255: | foreach ($option_values as $option_value) {
|
| 256: | if ($option_value['image'] && is_file(DIR_IMAGE . html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'))) {
|
| 257: | $image = $option_value['image'];
|
| 258: | $thumb = $option_value['image'];
|
| 259: | } else {
|
| 260: | $image = '';
|
| 261: | $thumb = 'no_image.png';
|
| 262: | }
|
| 263: |
|
| 264: | $data['option_values'][] = [
|
| 265: | 'option_value_id' => $option_value['option_value_id'],
|
| 266: | 'option_value_description' => $option_value['option_value_description'],
|
| 267: | 'image' => $image,
|
| 268: | 'thumb' => $this->model_tool_image->resize($thumb, $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height')),
|
| 269: | 'sort_order' => $option_value['sort_order']
|
| 270: | ];
|
| 271: | }
|
| 272: |
|
| 273: | $data['placeholder'] = $this->model_tool_image->resize('no_image.png', $this->config->get('config_image_default_width'), $this->config->get('config_image_default_height'));
|
| 274: |
|
| 275: | $data['user_token'] = $this->session->data['user_token'];
|
| 276: |
|
| 277: | $data['header'] = $this->load->controller('common/header');
|
| 278: | $data['column_left'] = $this->load->controller('common/column_left');
|
| 279: | $data['footer'] = $this->load->controller('common/footer');
|
| 280: |
|
| 281: | $this->response->setOutput($this->load->view('catalog/option_form', $data));
|
| 282: | }
|
| 283: |
|
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: |
|
| 289: | public function save(): void {
|
| 290: | $this->load->language('catalog/option');
|
| 291: |
|
| 292: | $json = [];
|
| 293: |
|
| 294: | if (!$this->user->hasPermission('modify', 'catalog/option')) {
|
| 295: | $json['error']['warning'] = $this->language->get('error_permission');
|
| 296: | }
|
| 297: |
|
| 298: | foreach ($this->request->post['option_description'] as $language_id => $value) {
|
| 299: | if (!oc_validate_length($value['name'], 1, 128)) {
|
| 300: | $json['error']['name_' . $language_id] = $this->language->get('error_name');
|
| 301: | }
|
| 302: | }
|
| 303: |
|
| 304: | if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox') && !isset($this->request->post['option_value'])) {
|
| 305: | $json['error']['warning'] = $this->language->get('error_type');
|
| 306: | }
|
| 307: |
|
| 308: | if (isset($this->request->post['option_value'])) {
|
| 309: | if (isset($this->request->post['option_id'])) {
|
| 310: | $this->load->model('catalog/product');
|
| 311: |
|
| 312: | $option_value_data = [];
|
| 313: |
|
| 314: | foreach ($this->request->post['option_value'] as $option_value) {
|
| 315: | if ($option_value['option_value_id']) {
|
| 316: | $option_value_data[] = $option_value['option_value_id'];
|
| 317: | }
|
| 318: | }
|
| 319: |
|
| 320: | $product_option_values = $this->model_catalog_product->getOptionValuesByOptionId($this->request->post['option_id']);
|
| 321: |
|
| 322: | foreach ($product_option_values as $product_option_value) {
|
| 323: | if (!in_array($product_option_value['option_value_id'], $option_value_data)) {
|
| 324: | $json['error']['warning'] = sprintf($this->language->get('error_value'), $this->model_catalog_product->getTotalProductsByOptionValueId($product_option_value['option_value_id']));
|
| 325: | }
|
| 326: | }
|
| 327: | }
|
| 328: | }
|
| 329: |
|
| 330: | if (isset($this->request->post['option_value'])) {
|
| 331: | foreach ($this->request->post['option_value'] as $option_value_id => $option_value) {
|
| 332: | foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
|
| 333: | if (!oc_validate_length($option_value_description['name'], 1, 128)) {
|
| 334: | $json['error']['option_value_' . $option_value_id . '_' . $language_id] = $this->language->get('error_option_value');
|
| 335: | }
|
| 336: | }
|
| 337: | }
|
| 338: | }
|
| 339: |
|
| 340: | if (isset($json['error']) && !isset($json['error']['warning'])) {
|
| 341: | $json['error']['warning'] = $this->language->get('error_warning');
|
| 342: | }
|
| 343: |
|
| 344: | if (!$json) {
|
| 345: | $this->load->model('catalog/option');
|
| 346: |
|
| 347: | if (!$this->request->post['option_id']) {
|
| 348: | $json['option_id'] = $this->model_catalog_option->addOption($this->request->post);
|
| 349: | } else {
|
| 350: | $this->model_catalog_option->editOption($this->request->post['option_id'], $this->request->post);
|
| 351: | }
|
| 352: |
|
| 353: | $json['success'] = $this->language->get('text_success');
|
| 354: | }
|
| 355: |
|
| 356: | $this->response->addHeader('Content-Type: application/json');
|
| 357: | $this->response->setOutput(json_encode($json));
|
| 358: | }
|
| 359: |
|
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: |
|
| 365: | public function delete(): void {
|
| 366: | $this->load->language('catalog/option');
|
| 367: |
|
| 368: | $json = [];
|
| 369: |
|
| 370: | if (isset($this->request->post['selected'])) {
|
| 371: | $selected = $this->request->post['selected'];
|
| 372: | } else {
|
| 373: | $selected = [];
|
| 374: | }
|
| 375: |
|
| 376: | if (!$this->user->hasPermission('modify', 'catalog/option')) {
|
| 377: | $json['error'] = $this->language->get('error_permission');
|
| 378: | }
|
| 379: |
|
| 380: | $this->load->model('catalog/product');
|
| 381: |
|
| 382: | foreach ($selected as $option_id) {
|
| 383: | $product_total = $this->model_catalog_product->getTotalProductsByOptionId($option_id);
|
| 384: |
|
| 385: | if ($product_total) {
|
| 386: | $json['error'] = sprintf($this->language->get('error_product'), $product_total);
|
| 387: | }
|
| 388: | }
|
| 389: |
|
| 390: | if (!$json) {
|
| 391: | $this->load->model('catalog/option');
|
| 392: |
|
| 393: | foreach ($selected as $option_id) {
|
| 394: | $this->model_catalog_option->deleteOption($option_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: | |
| 405: | |
| 406: | |
| 407: | |
| 408: |
|
| 409: | public function autocomplete(): void {
|
| 410: | $json = [];
|
| 411: |
|
| 412: | if (isset($this->request->get['filter_name'])) {
|
| 413: | $this->load->language('catalog/option');
|
| 414: |
|
| 415: | $this->load->model('catalog/option');
|
| 416: | $this->load->model('tool/image');
|
| 417: |
|
| 418: | $filter_data = [
|
| 419: | 'filter_name' => $this->request->get['filter_name'],
|
| 420: | 'start' => 0,
|
| 421: | 'limit' => 5
|
| 422: | ];
|
| 423: |
|
| 424: | $options = $this->model_catalog_option->getOptions($filter_data);
|
| 425: |
|
| 426: | foreach ($options as $option) {
|
| 427: | $option_value_data = [];
|
| 428: |
|
| 429: | if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
|
| 430: | $option_values = $this->model_catalog_option->getValues($option['option_id']);
|
| 431: |
|
| 432: | foreach ($option_values as $option_value) {
|
| 433: | if ($option_value['image'] && is_file(DIR_IMAGE . html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'))) {
|
| 434: | $image = $option_value['image'];
|
| 435: | } else {
|
| 436: | $image = 'no_image.png';
|
| 437: | }
|
| 438: |
|
| 439: | $option_value_data[] = [
|
| 440: | 'option_value_id' => $option_value['option_value_id'],
|
| 441: | 'name' => strip_tags(html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8')),
|
| 442: | 'image' => $this->model_tool_image->resize($image, 50, 50)
|
| 443: | ];
|
| 444: | }
|
| 445: |
|
| 446: | $sort_order = [];
|
| 447: |
|
| 448: | foreach ($option_value_data as $key => $value) {
|
| 449: | $sort_order[$key] = $value['name'];
|
| 450: | }
|
| 451: |
|
| 452: | array_multisort($sort_order, SORT_ASC, $option_value_data);
|
| 453: | }
|
| 454: |
|
| 455: | $type = '';
|
| 456: |
|
| 457: | if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox') {
|
| 458: | $type = $this->language->get('text_choose');
|
| 459: | }
|
| 460: |
|
| 461: | if ($option['type'] == 'text' || $option['type'] == 'textarea') {
|
| 462: | $type = $this->language->get('text_input');
|
| 463: | }
|
| 464: |
|
| 465: | if ($option['type'] == 'file') {
|
| 466: | $type = $this->language->get('text_file');
|
| 467: | }
|
| 468: |
|
| 469: | if ($option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
|
| 470: | $type = $this->language->get('text_date');
|
| 471: | }
|
| 472: |
|
| 473: | $json[] = [
|
| 474: | 'option_id' => $option['option_id'],
|
| 475: | 'name' => strip_tags(html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8')),
|
| 476: | 'category' => $type,
|
| 477: | 'type' => $option['type'],
|
| 478: | 'option_value' => $option_value_data
|
| 479: | ];
|
| 480: | }
|
| 481: | }
|
| 482: |
|
| 483: | $sort_order = [];
|
| 484: |
|
| 485: | foreach ($json as $key => $value) {
|
| 486: | $sort_order[$key] = $value['name'];
|
| 487: | }
|
| 488: |
|
| 489: | array_multisort($sort_order, SORT_ASC, $json);
|
| 490: |
|
| 491: | $this->response->addHeader('Content-Type: application/json');
|
| 492: | $this->response->setOutput(json_encode($json));
|
| 493: | }
|
| 494: | }
|
| 495: | |