| 1: | <?php
|
| 2: | namespace Opencart\Admin\Model\Customer;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class CustomField extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: |
|
| 16: | public function addCustomField(array $data): int {
|
| 17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field` SET `type` = '" . $this->db->escape((string)$data['type']) . "', `value` = '" . $this->db->escape((string)$data['value']) . "', `validation` = '" . $this->db->escape((string)$data['validation']) . "', `location` = '" . $this->db->escape((string)$data['location']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
| 18: |
|
| 19: | $custom_field_id = $this->db->getLastId();
|
| 20: |
|
| 21: | foreach ($data['custom_field_description'] as $language_id => $custom_field_description) {
|
| 22: | $this->addDescription($custom_field_id, $language_id, $custom_field_description);
|
| 23: | }
|
| 24: |
|
| 25: | if (isset($data['custom_field_customer_group'])) {
|
| 26: | foreach ($data['custom_field_customer_group'] as $custom_field_customer_group) {
|
| 27: | if (isset($custom_field_customer_group['customer_group_id'])) {
|
| 28: | $this->addCustomerGroup($custom_field_id, $custom_field_customer_group);
|
| 29: | }
|
| 30: | }
|
| 31: | }
|
| 32: |
|
| 33: | if (isset($data['custom_field_value'])) {
|
| 34: | foreach ($data['custom_field_value'] as $custom_field_value) {
|
| 35: | $this->addValue($custom_field_id, $custom_field_value);
|
| 36: | }
|
| 37: | }
|
| 38: |
|
| 39: | return $custom_field_id;
|
| 40: | }
|
| 41: |
|
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: |
|
| 50: | public function editCustomField(int $custom_field_id, array $data): void {
|
| 51: | $this->db->query("UPDATE `" . DB_PREFIX . "custom_field` SET `type` = '" . $this->db->escape((string)$data['type']) . "', `value` = '" . $this->db->escape((string)$data['value']) . "', `validation` = '" . $this->db->escape((string)$data['validation']) . "', `location` = '" . $this->db->escape((string)$data['location']) . "', `status` = '" . (bool)($data['status'] ?? 0) . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 52: |
|
| 53: | $this->deleteDescriptions($custom_field_id);
|
| 54: |
|
| 55: | foreach ($data['custom_field_description'] as $language_id => $custom_field_description) {
|
| 56: | $this->addDescription($custom_field_id, $language_id, $custom_field_description);
|
| 57: | }
|
| 58: |
|
| 59: | $this->deleteCustomerGroups($custom_field_id);
|
| 60: |
|
| 61: | if (isset($data['custom_field_customer_group'])) {
|
| 62: | foreach ($data['custom_field_customer_group'] as $custom_field_customer_group) {
|
| 63: | if (isset($custom_field_customer_group['customer_group_id'])) {
|
| 64: | $this->addCustomerGroup($custom_field_id, $custom_field_customer_group);
|
| 65: | }
|
| 66: | }
|
| 67: | }
|
| 68: |
|
| 69: | $this->deleteValues($custom_field_id);
|
| 70: |
|
| 71: | if (isset($data['custom_field_value'])) {
|
| 72: | foreach ($data['custom_field_value'] as $custom_field_value) {
|
| 73: | $this->addValue($custom_field_id, $custom_field_value);
|
| 74: | }
|
| 75: | }
|
| 76: | }
|
| 77: |
|
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: |
|
| 85: | public function deleteCustomField(int $custom_field_id): void {
|
| 86: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 87: |
|
| 88: | $this->deleteDescriptions($custom_field_id);
|
| 89: | $this->deleteCustomerGroups($custom_field_id);
|
| 90: | $this->deleteValues($custom_field_id);
|
| 91: | }
|
| 92: |
|
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: |
|
| 100: | public function getCustomField(int $custom_field_id): array {
|
| 101: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field` `cf` LEFT JOIN `" . DB_PREFIX . "custom_field_description` `cfd` ON (`cf`.`custom_field_id` = `cfd`.`custom_field_id`) WHERE `cf`.`custom_field_id` = '" . (int)$custom_field_id . "' AND `cfd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
| 102: |
|
| 103: | return $query->row;
|
| 104: | }
|
| 105: |
|
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: |
|
| 113: | public function getCustomFields(array $data = []): array {
|
| 114: | if (empty($data['filter_customer_group_id'])) {
|
| 115: | $sql = "SELECT * FROM `" . DB_PREFIX . "custom_field` `cf` LEFT JOIN `" . DB_PREFIX . "custom_field_description` `cfd` ON (`cf`.`custom_field_id` = `cfd`.`custom_field_id`) WHERE `cfd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
|
| 116: | } else {
|
| 117: | $sql = "SELECT * FROM `" . DB_PREFIX . "custom_field_customer_group` `cfcg` LEFT JOIN `" . DB_PREFIX . "custom_field` `cf` ON (`cfcg`.`custom_field_id` = `cf`.`custom_field_id`) LEFT JOIN `" . DB_PREFIX . "custom_field_description` `cfd` ON (`cf`.`custom_field_id` = `cfd`.`custom_field_id`) WHERE `cfd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
|
| 118: | }
|
| 119: |
|
| 120: | if (!empty($data['filter_name'])) {
|
| 121: | $sql .= " AND LCASE(`cfd`.`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
|
| 122: | }
|
| 123: |
|
| 124: | if (isset($data['filter_status'])) {
|
| 125: | $sql .= " AND `cf`.`status` = '" . (bool)$data['filter_status'] . "'";
|
| 126: | }
|
| 127: |
|
| 128: | if (isset($data['filter_location'])) {
|
| 129: | $sql .= " AND `cf`.`location` = '" . $this->db->escape((string)$data['filter_location']) . "'";
|
| 130: | }
|
| 131: |
|
| 132: | if (!empty($data['filter_customer_group_id'])) {
|
| 133: | $sql .= " AND `cfcg`.`customer_group_id` = '" . (int)$data['filter_customer_group_id'] . "'";
|
| 134: | }
|
| 135: |
|
| 136: | $sort_data = [
|
| 137: | 'cfd.name',
|
| 138: | 'cf.type',
|
| 139: | 'cf.location',
|
| 140: | 'cf.status',
|
| 141: | 'cf.sort_order'
|
| 142: | ];
|
| 143: |
|
| 144: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
| 145: | $sql .= " ORDER BY " . $data['sort'];
|
| 146: | } else {
|
| 147: | $sql .= " ORDER BY `cfd`.`name`";
|
| 148: | }
|
| 149: |
|
| 150: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
| 151: | $sql .= " DESC";
|
| 152: | } else {
|
| 153: | $sql .= " ASC";
|
| 154: | }
|
| 155: |
|
| 156: | if (isset($data['start']) || isset($data['limit'])) {
|
| 157: | if ($data['start'] < 0) {
|
| 158: | $data['start'] = 0;
|
| 159: | }
|
| 160: |
|
| 161: | if ($data['limit'] < 1) {
|
| 162: | $data['limit'] = 20;
|
| 163: | }
|
| 164: |
|
| 165: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
| 166: | }
|
| 167: |
|
| 168: | $query = $this->db->query($sql);
|
| 169: |
|
| 170: | return $query->rows;
|
| 171: | }
|
| 172: |
|
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: |
|
| 178: | public function getTotalCustomFields(): int {
|
| 179: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "custom_field`");
|
| 180: |
|
| 181: | return (int)$query->row['total'];
|
| 182: | }
|
| 183: |
|
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: |
|
| 193: | public function addDescription(int $custom_field_id, int $language_id, array $data): void {
|
| 194: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field_description` SET `custom_field_id` = '" . (int)$custom_field_id . "', `language_id` = '" . (int)$language_id . "', `name` = '" . $this->db->escape($data['name']) . "'");
|
| 195: | }
|
| 196: |
|
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: |
|
| 204: | public function deleteDescriptions(int $custom_field_id): void {
|
| 205: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_description` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 206: | }
|
| 207: |
|
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: |
|
| 215: | public function deleteDescriptionsByLanguageId(int $language_id): void {
|
| 216: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 217: | }
|
| 218: |
|
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: |
|
| 226: | public function getDescriptions(int $custom_field_id): array {
|
| 227: | $custom_field_data = [];
|
| 228: |
|
| 229: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_description` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 230: |
|
| 231: | foreach ($query->rows as $result) {
|
| 232: | $custom_field_data[$result['language_id']] = ['name' => $result['name']];
|
| 233: | }
|
| 234: |
|
| 235: | return $custom_field_data;
|
| 236: | }
|
| 237: |
|
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: |
|
| 245: | public function getDescriptionsByLanguageId(int $language_id): array {
|
| 246: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 247: |
|
| 248: | return $query->rows;
|
| 249: | }
|
| 250: |
|
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: |
|
| 259: | public function addCustomerGroup(int $custom_field_id, array $data): void {
|
| 260: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field_customer_group` SET `custom_field_id` = '" . (int)$custom_field_id . "', `customer_group_id` = '" . (int)$data['customer_group_id'] . "', `required` = '" . (int)(isset($data['required']) ? 1 : 0) . "'");
|
| 261: | }
|
| 262: |
|
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: |
|
| 270: | public function deleteCustomerGroups(int $custom_field_id): void {
|
| 271: | $this->db->query("delete FROM `" . DB_PREFIX . "custom_field_customer_group` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 272: | }
|
| 273: |
|
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: |
|
| 281: | public function getCustomerGroups(int $custom_field_id): array {
|
| 282: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_customer_group` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 283: |
|
| 284: | return $query->rows;
|
| 285: | }
|
| 286: |
|
| 287: | |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: |
|
| 295: | public function addValue(int $custom_field_id, array $data): int {
|
| 296: | if ($data['custom_field_value_id']) {
|
| 297: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field_value` SET `custom_field_value_id` = '" . (int)$data['custom_field_value_id'] . "', `custom_field_id` = '" . (int)$custom_field_id . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
| 298: | } else {
|
| 299: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field_value` SET `custom_field_id` = '" . (int)$custom_field_id . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
| 300: | }
|
| 301: |
|
| 302: | $custom_field_value_id = $this->db->getLastId();
|
| 303: |
|
| 304: | foreach ($data['custom_field_value_description'] as $language_id => $custom_field_value_description) {
|
| 305: | $this->addValueDescription($custom_field_value_id, $custom_field_id, $language_id, $custom_field_value_description);
|
| 306: | }
|
| 307: |
|
| 308: | return $custom_field_value_id;
|
| 309: | }
|
| 310: |
|
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | |
| 317: |
|
| 318: | public function deleteValues(int $custom_field_id): void {
|
| 319: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_value` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 320: |
|
| 321: | $this->deleteValueDescriptions($custom_field_id);
|
| 322: | }
|
| 323: |
|
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: |
|
| 331: | public function getValue(int $custom_field_value_id): array {
|
| 332: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_value` `cfv` LEFT JOIN `" . DB_PREFIX . "custom_field_value_description` `cfvd` ON (`cfv`.`custom_field_value_id` = `cfvd`.`custom_field_value_id`) WHERE `cfv`.`custom_field_value_id` = '" . (int)$custom_field_value_id . "' AND `cfvd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
|
| 333: |
|
| 334: | return $query->row;
|
| 335: | }
|
| 336: |
|
| 337: | |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: |
|
| 344: | public function getValues(int $custom_field_id): array {
|
| 345: | $custom_field_value_data = [];
|
| 346: |
|
| 347: | $custom_field_value_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_value` `cfv` LEFT JOIN `" . DB_PREFIX . "custom_field_value_description` `cfvd` ON (`cfv`.`custom_field_value_id` = `cfvd`.`custom_field_value_id`) WHERE `cfv`.`custom_field_id` = '" . (int)$custom_field_id . "' AND `cfvd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `cfv`.`sort_order` ASC");
|
| 348: |
|
| 349: | foreach ($custom_field_value_query->rows as $custom_field_value) {
|
| 350: | $custom_field_value_data[$custom_field_value['custom_field_value_id']] = [
|
| 351: | 'custom_field_value_id' => $custom_field_value['custom_field_value_id'],
|
| 352: | 'name' => $custom_field_value['name']
|
| 353: | ];
|
| 354: | }
|
| 355: |
|
| 356: | return $custom_field_value_data;
|
| 357: | }
|
| 358: |
|
| 359: | |
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: |
|
| 369: | public function addValueDescription(int $custom_field_value_id, int $custom_field_id, int $language_id, array $custom_field_value_description): void {
|
| 370: | $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field_value_description` SET `custom_field_value_id` = '" . (int)$custom_field_value_id . "', `language_id` = '" . (int)$language_id . "', `custom_field_id` = '" . (int)$custom_field_id . "', `name` = '" . $this->db->escape($custom_field_value_description['name']) . "'");
|
| 371: | }
|
| 372: |
|
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: |
|
| 380: | public function deleteValueDescriptions(int $custom_field_id): void {
|
| 381: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_value_description` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 382: | }
|
| 383: |
|
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | |
| 389: | |
| 390: |
|
| 391: | public function deleteValueDescriptionsByLanguageId(int $language_id): void {
|
| 392: | $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_value_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 393: | }
|
| 394: |
|
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: |
|
| 402: | public function getValueDescriptions(int $custom_field_id): array {
|
| 403: | $custom_field_value_data = [];
|
| 404: |
|
| 405: | $custom_field_value_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_value` WHERE `custom_field_id` = '" . (int)$custom_field_id . "'");
|
| 406: |
|
| 407: | foreach ($custom_field_value_query->rows as $custom_field_value) {
|
| 408: | $custom_field_value_description_data = [];
|
| 409: |
|
| 410: | $custom_field_value_description_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_value_description` WHERE `custom_field_value_id` = '" . (int)$custom_field_value['custom_field_value_id'] . "'");
|
| 411: |
|
| 412: | foreach ($custom_field_value_description_query->rows as $custom_field_value_description) {
|
| 413: | $custom_field_value_description_data[$custom_field_value_description['language_id']] = ['name' => $custom_field_value_description['name']];
|
| 414: | }
|
| 415: |
|
| 416: | $custom_field_value_data[] = [
|
| 417: | 'custom_field_value_id' => $custom_field_value['custom_field_value_id'],
|
| 418: | 'custom_field_value_description' => $custom_field_value_description_data,
|
| 419: | 'sort_order' => $custom_field_value['sort_order']
|
| 420: | ];
|
| 421: | }
|
| 422: |
|
| 423: | return $custom_field_value_data;
|
| 424: | }
|
| 425: |
|
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: |
|
| 433: | public function getValueDescriptionsByLanguageId(int $language_id): array {
|
| 434: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_value_description` WHERE `language_id` = '" . (int)$language_id . "'");
|
| 435: |
|
| 436: | return $query->rows;
|
| 437: | }
|
| 438: | }
|
| 439: | |