| 1: | <?php
|
| 2: | namespace Opencart\Admin\Model\Catalog;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Manufacturer extends \Opencart\System\Engine\Model {
|
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: |
|
| 16: | public function addManufacturer(array $data): int {
|
| 17: | $this->db->query("INSERT INTO `" . DB_PREFIX . "manufacturer` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `image` = '" . $this->db->escape((string)$data['image']) . "', `sort_order` = '" . (int)$data['sort_order'] . "'");
|
| 18: |
|
| 19: | $manufacturer_id = $this->db->getLastId();
|
| 20: |
|
| 21: |
|
| 22: | if (isset($data['manufacturer_store'])) {
|
| 23: | foreach ($data['manufacturer_store'] as $store_id) {
|
| 24: | $this->model_catalog_manufacturer->addStore($manufacturer_id, $store_id);
|
| 25: | }
|
| 26: | }
|
| 27: |
|
| 28: |
|
| 29: | $this->load->model('design/seo_url');
|
| 30: |
|
| 31: | foreach ($data['manufacturer_seo_url'] as $store_id => $language) {
|
| 32: | foreach ($language as $language_id => $keyword) {
|
| 33: | $this->model_design_seo_url->addSeoUrl('manufacturer_id', $manufacturer_id, $keyword, $store_id, $language_id);
|
| 34: | }
|
| 35: | }
|
| 36: |
|
| 37: |
|
| 38: | if (isset($data['manufacturer_layout'])) {
|
| 39: | foreach ($data['manufacturer_layout'] as $store_id => $layout_id) {
|
| 40: | if ($layout_id) {
|
| 41: | $this->model_catalog_manufacturer->addLayout($manufacturer_id, $store_id, $layout_id);
|
| 42: | }
|
| 43: | }
|
| 44: | }
|
| 45: |
|
| 46: | $this->cache->delete('manufacturer');
|
| 47: |
|
| 48: | return $manufacturer_id;
|
| 49: | }
|
| 50: |
|
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | public function editManufacturer(int $manufacturer_id, array $data): void {
|
| 60: | $this->db->query("UPDATE `" . DB_PREFIX . "manufacturer` SET `name` = '" . $this->db->escape((string)$data['name']) . "', `image` = '" . $this->db->escape((string)$data['image']) . "', `sort_order` = '" . (int)$data['sort_order'] . "' WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 61: |
|
| 62: |
|
| 63: | $this->deleteStores($manufacturer_id);
|
| 64: |
|
| 65: | if (isset($data['manufacturer_store'])) {
|
| 66: | foreach ($data['manufacturer_store'] as $store_id) {
|
| 67: | $this->model_catalog_manufacturer->addStore($manufacturer_id, $store_id);
|
| 68: | }
|
| 69: | }
|
| 70: |
|
| 71: |
|
| 72: | $this->load->model('design/seo_url');
|
| 73: |
|
| 74: | $this->model_design_seo_url->deleteSeoUrlsByKeyValue('manufacturer_id', $manufacturer_id);
|
| 75: |
|
| 76: | if (isset($data['manufacturer_seo_url'])) {
|
| 77: | foreach ($data['manufacturer_seo_url'] as $store_id => $language) {
|
| 78: | foreach ($language as $language_id => $keyword) {
|
| 79: | $this->model_design_seo_url->addSeoUrl('manufacturer_id', $manufacturer_id, $keyword, $store_id, $language_id);
|
| 80: | }
|
| 81: | }
|
| 82: | }
|
| 83: |
|
| 84: |
|
| 85: | $this->model_catalog_manufacturer->deleteLayouts($manufacturer_id);
|
| 86: |
|
| 87: | if (isset($data['manufacturer_layout'])) {
|
| 88: | foreach ($data['manufacturer_layout'] as $store_id => $layout_id) {
|
| 89: | if ($layout_id) {
|
| 90: | $this->model_catalog_manufacturer->addLayout($manufacturer_id, $store_id, $layout_id);
|
| 91: | }
|
| 92: | }
|
| 93: | }
|
| 94: |
|
| 95: | $this->cache->delete('manufacturer');
|
| 96: | }
|
| 97: |
|
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: |
|
| 105: | public function deleteManufacturer(int $manufacturer_id): void {
|
| 106: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 107: |
|
| 108: | $this->model_catalog_manufacturer->deleteStores($manufacturer_id);
|
| 109: | $this->model_catalog_manufacturer->deleteLayouts($manufacturer_id);
|
| 110: |
|
| 111: | $this->load->model('design/seo_url');
|
| 112: |
|
| 113: | $this->model_design_seo_url->deleteSeoUrlsByKeyValue('manufacturer_id', $manufacturer_id);
|
| 114: |
|
| 115: | $this->cache->delete('manufacturer');
|
| 116: | }
|
| 117: |
|
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: |
|
| 125: | public function getManufacturer(int $manufacturer_id): array {
|
| 126: | $query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "manufacturer` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 127: |
|
| 128: | return $query->row;
|
| 129: | }
|
| 130: |
|
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: |
|
| 138: | public function getManufacturers(array $data = []): array {
|
| 139: | $sql = "SELECT * FROM `" . DB_PREFIX . "manufacturer`";
|
| 140: |
|
| 141: | if (!empty($data['filter_name'])) {
|
| 142: | $sql .= " WHERE LCASE(`name`) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'";
|
| 143: | }
|
| 144: |
|
| 145: | $sort_data = [
|
| 146: | 'name',
|
| 147: | 'sort_order'
|
| 148: | ];
|
| 149: |
|
| 150: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
|
| 151: | $sql .= " ORDER BY " . $data['sort'];
|
| 152: | } else {
|
| 153: | $sql .= " ORDER BY `name`";
|
| 154: | }
|
| 155: |
|
| 156: | if (isset($data['order']) && ($data['order'] == 'DESC')) {
|
| 157: | $sql .= " DESC";
|
| 158: | } else {
|
| 159: | $sql .= " ASC";
|
| 160: | }
|
| 161: |
|
| 162: | if (isset($data['start']) || isset($data['limit'])) {
|
| 163: | if ($data['start'] < 0) {
|
| 164: | $data['start'] = 0;
|
| 165: | }
|
| 166: |
|
| 167: | if ($data['limit'] < 1) {
|
| 168: | $data['limit'] = 20;
|
| 169: | }
|
| 170: |
|
| 171: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
|
| 172: | }
|
| 173: |
|
| 174: | $query = $this->db->query($sql);
|
| 175: |
|
| 176: | return $query->rows;
|
| 177: | }
|
| 178: |
|
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: |
|
| 184: | public function getTotalManufacturers(): int {
|
| 185: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "manufacturer`");
|
| 186: |
|
| 187: | return (int)$query->row['total'];
|
| 188: | }
|
| 189: |
|
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: |
|
| 198: | public function addStore(int $manufacturer_id, int $store_id): void {
|
| 199: | $this->db->query("INSERT INTO `" . DB_PREFIX . "manufacturer_to_store` SET `manufacturer_id` = '" . (int)$manufacturer_id . "', `store_id` = '" . (int)$store_id . "'");
|
| 200: | }
|
| 201: |
|
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: |
|
| 209: | public function deleteStores(int $manufacturer_id): void {
|
| 210: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_store` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 211: | }
|
| 212: |
|
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: |
|
| 220: | public function deleteStoresByStoreId(int $store_id): void {
|
| 221: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_store` WHERE `store_id` = '" . (int)$store_id . "'");
|
| 222: | }
|
| 223: |
|
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: |
|
| 231: | public function getStores(int $manufacturer_id): array {
|
| 232: | $manufacturer_store_data = [];
|
| 233: |
|
| 234: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "manufacturer_to_store` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 235: |
|
| 236: | foreach ($query->rows as $result) {
|
| 237: | $manufacturer_store_data[] = $result['store_id'];
|
| 238: | }
|
| 239: |
|
| 240: | return $manufacturer_store_data;
|
| 241: | }
|
| 242: |
|
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: |
|
| 252: | public function addLayout(int $manufacturer_id, int $store_id, int $layout_id): void {
|
| 253: | $this->db->query("INSERT INTO `" . DB_PREFIX . "manufacturer_to_layout` SET `manufacturer_id` = '" . (int)$manufacturer_id . "', store_id = '" . (int)$store_id . "', `layout_id` = '" . (int)$layout_id . "'");
|
| 254: | }
|
| 255: |
|
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: |
|
| 263: | public function deleteLayouts(int $manufacturer_id): void {
|
| 264: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_layout` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 265: | }
|
| 266: |
|
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: |
|
| 274: | public function deleteLayoutsByLayoutId(int $layout_id): void {
|
| 275: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
|
| 276: | }
|
| 277: |
|
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: |
|
| 285: | public function deleteLayoutsByStoreId(int $store_id): void {
|
| 286: | $this->db->query("DELETE FROM `" . DB_PREFIX . "manufacturer_to_layout` WHERE `store_id` = '" . (int)$store_id . "'");
|
| 287: | }
|
| 288: |
|
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: |
|
| 296: | public function getLayouts(int $manufacturer_id): array {
|
| 297: | $manufacturer_layout_data = [];
|
| 298: |
|
| 299: | $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "manufacturer_to_layout` WHERE `manufacturer_id` = '" . (int)$manufacturer_id . "'");
|
| 300: |
|
| 301: | foreach ($query->rows as $result) {
|
| 302: | $manufacturer_layout_data[$result['store_id']] = $result['layout_id'];
|
| 303: | }
|
| 304: |
|
| 305: | return $manufacturer_layout_data;
|
| 306: | }
|
| 307: |
|
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: |
|
| 315: | public function getTotalLayoutsByLayoutId(int $layout_id): int {
|
| 316: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "manufacturer_to_layout` WHERE `layout_id` = '" . (int)$layout_id . "'");
|
| 317: |
|
| 318: | return (int)$query->row['total'];
|
| 319: | }
|
| 320: | }
|
| 321: | |