Back to Opencart

File admin\model\catalog\attribute.php

docs/api/source-admin.model.catalog.attribute.html

4.1.0.316.0 KB
Original Source

Namespaces

Classes

| 1: | <?php | | 2: | namespace Opencart\Admin\Model\Catalog; | | 3: | /** | | 4: | * Class Attribute | | 5: | * | | 6: | * Can be called from $this->load->model('catalog/attribute'); | | 7: | * | | 8: | * @package Opencart\Admin\Model\Catalog | | 9: | */ | | 10: | class Attribute extends \Opencart\System\Engine\Model { | | 11: | /** | | 12: | * Add Attribute | | 13: | * | | 14: | * Create a new attribute record in the database. | | 15: | * | | 16: | * @param array<string, mixed> $data | | 17: | * | | 18: | * @return int returns the primary key of the new attribute record | | 19: | */ | | 20: | public function addAttribute(array $data): int { | | 21: | $this->db->query("INSERT INTO " . DB\_PREFIX . "attribute SET attribute\_group\_id = '" . (int)$data['attribute_group_id'] . "', sort_order = '" . (int)$data['sort_order'] . "'"); | | 22: | | | 23: | $attribute_id = $this->db->getLastId(); | | 24: | | | 25: | foreach ($data['attribute_description'] as $language_id => $attribute_description) { | | 26: | $this->model_catalog_attribute->addDescription($attribute_id, $language_id, $attribute_description); | | 27: | } | | 28: | | | 29: | return $attribute_id; | | 30: | } | | 31: | | | 32: | /** | | 33: | * Edit Attribute | | 34: | * | | 35: | * Edit attribute record in the database. | | 36: | * | | 37: | * @param int $attribute_id primary key of the attribute record to edit | | 38: | * @param array<string, mixed> $data array of data | | 39: | * | | 40: | * @return void | | 41: | */ | | 42: | public function editAttribute(int $attribute_id, array $data): void { | | 43: | $this->db->query("UPDATE " . DB\_PREFIX . "attribute SET attribute\_group\_id = '" . (int)$data['attribute_group_id'] . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE attribute_id = '" . (int)$attribute_id . "'"); | | 44: | | | 45: | $this->model_catalog_attribute->deleteDescriptions($attribute_id); | | 46: | | | 47: | foreach ($data['attribute_description'] as $language_id => $attribute_description) { | | 48: | $this->model_catalog_attribute->addDescription($attribute_id, $language_id, $attribute_description); | | 49: | } | | 50: | } | | 51: | | | 52: | /** | | 53: | * Delete Attribute | | 54: | * | | 55: | * Delete attribute record in the database. | | 56: | * | | 57: | * @param int $attribute_id primary key of the attribute record to be deleted | | 58: | * | | 59: | * @return void | | 60: | */ | | 61: | public function deleteAttribute(int $attribute_id): void { | | 62: | $this->db->query("DELETE FROM " . DB\_PREFIX . "attribute WHERE attribute\_id = '" . (int)$attribute_id . "'"); | | 63: | | | 64: | $this->model_catalog_attribute->deleteDescriptions($attribute_id); | | 65: | } | | 66: | | | 67: | /** | | 68: | * Get Attribute | | 69: | * | | 70: | * Get the record of the attribute record in the database. | | 71: | * | | 72: | * @param int $attribute_id primary key of the attribute record to be fetched | | 73: | * | | 74: | * @return array<string, mixed> | | 75: | */ | | 76: | public function getAttribute(int $attribute_id): array { | | 77: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "attribute a LEFT JOIN " . DB\_PREFIX . "attribute\_description ad ON (a.attribute\_id = ad.attribute\_id) WHERE a.attribute\_id = '" . (int)$attribute_id . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "'"); | | 78: | | | 79: | return $query->row; | | 80: | } | | 81: | | | 82: | /** | | 83: | * Get Attributes | | 84: | * | | 85: | * Get the record of the attribute record in the database. | | 86: | * | | 87: | * @param array<string, mixed> $data array of filters | | 88: | * | | 89: | * @return array<int, array<string, mixed>> | | 90: | */ | | 91: | public function getAttributes(array $data = []): array { | | 92: | $sql = "SELECT *, (SELECT agd.name FROM " . DB\_PREFIX . "attribute\_group\_description agd WHERE agd.attribute\_group\_id = a.attribute\_group\_id AND agd.language\_id = '" . (int)$this->config->get('config_language_id') . "') AS attribute_group FROM " . DB\_PREFIX . "attribute a LEFT JOIN " . DB\_PREFIX . "attribute\_description ad ON (a.attribute\_id = ad.attribute\_id) WHERE ad.language\_id = '" . (int)$this->config->get('config_language_id') . "'"; | | 93: | | | 94: | if (!empty($data['filter_name'])) { | | 95: | $sql .= " AND LCASE(ad.name) LIKE '" . $this->db->escape(oc_strtolower($data['filter_name']) . '%') . "'"; | | 96: | } | | 97: | | | 98: | if (!empty($data['filter_attribute_group_id'])) { | | 99: | $sql .= " AND a.attribute_group_id = '" . (int)$data['filter_attribute_group_id'] . "'"; | | 100: | } | | 101: | | | 102: | $sort_data = [ | | 103: | 'ad.name', | | 104: | 'attribute_group', | | 105: | 'a.sort_order' | | 106: | ]; | | 107: | | | 108: | if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { | | 109: | $sql .= " ORDER BY " . $data['sort']; | | 110: | } else { | | 111: | $sql .= " ORDER BY attribute_group, ad.name"; | | 112: | } | | 113: | | | 114: | if (isset($data['order']) && ($data['order'] == 'DESC')) { | | 115: | $sql .= " DESC"; | | 116: | } else { | | 117: | $sql .= " ASC"; | | 118: | } | | 119: | | | 120: | if (isset($data['start']) || isset($data['limit'])) { | | 121: | if ($data['start'] < 0) { | | 122: | $data['start'] = 0; | | 123: | } | | 124: | | | 125: | if ($data['limit'] < 1) { | | 126: | $data['limit'] = 20; | | 127: | } | | 128: | | | 129: | $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; | | 130: | } | | 131: | | | 132: | $query = $this->db->query($sql); | | 133: | | | 134: | return $query->rows; | | 135: | } | | 136: | | | 137: | /** | | 138: | * Get Total Attributes | | 139: | * | | 140: | * Get the total number of attribute records in the database. | | 141: | * | | 142: | * @return int total number of attribute records | | 143: | */ | | 144: | public function getTotalAttributes(): int { | | 145: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "attribute"); | | 146: | | | 147: | if ($query->num_rows) { | | 148: | return (int)$query->row['total']; | | 149: | } else { | | 150: | return 0; | | 151: | } | | 152: | } | | 153: | | | 154: | /** | | 155: | * Get Total Attributes By Attribute Group ID | | 156: | * | | 157: | * Get the total number of attribute records with group ID in the database. | | 158: | * | | 159: | * @param int $attribute_group_id foreign key of the attribute record to be fetched | | 160: | * | | 161: | * @return int total number of attribute records that have attribute group ID | | 162: | */ | | 163: | public function getTotalAttributesByAttributeGroupId(int $attribute_group_id): int { | | 164: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "attribute WHERE attribute\_group\_id = '" . (int)$attribute_group_id . "'"); | | 165: | | | 166: | return (int)$query->row['total']; | | 167: | } | | 168: | | | 169: | /** | | 170: | * Add Description | | 171: | * | | 172: | * @param int $attribute_id primary key of the attribute record | | 173: | * @param int $language_id primary key of the attribute language | | 174: | * @param array<string, mixed> $data | | 175: | * | | 176: | * @return void | | 177: | */ | | 178: | public function addDescription(int $attribute_id, int $language_id, array $data): void { | | 179: | $this->db->query("INSERT INTO " . DB\_PREFIX . "attribute\_description SET attribute\_id = '" . (int)$attribute_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($data['name']) . "'"); | | 180: | } | | 181: | | | 182: | /** | | 183: | * Delete Descriptions | | 184: | * | | 185: | * Delete attribute description record in the database. | | 186: | * | | 187: | * @param int $attribute_id primary key of the attribute record to be fetched | | 188: | * | | 189: | * @return void | | 190: | */ | | 191: | public function deleteDescriptions(int $attribute_id): void { | | 192: | $this->db->query("DELETE FROM " . DB\_PREFIX . "attribute\_description WHERE attribute\_id = '" . (int)$attribute_id . "'"); | | 193: | } | | 194: | | | 195: | /** | | 196: | * Delete Descriptions By Language ID | | 197: | * | | 198: | * Delete attribute description record in the database. | | 199: | * | | 200: | * @param int $language_id primary key of the attribute language | | 201: | * | | 202: | * @return void | | 203: | */ | | 204: | public function deleteDescriptionsByLanguageId(int $language_id): void { | | 205: | $this->db->query("DELETE FROM " . DB\_PREFIX . "attribute\_description WHERE language\_id = '" . (int)$language_id . "'"); | | 206: | } | | 207: | | | 208: | /** | | 209: | * Get Description | | 210: | * | | 211: | * Get the record of the attribute description record in the database. | | 212: | * | | 213: | * @param int $attribute_id primary key of the attribute record to be fetched | | 214: | * @param int $language_id primary key of the attribute language | | 215: | * | | 216: | * @return array<string, mixed> | | 217: | */ | | 218: | public function getDescription(int $attribute_id, int $language_id): array { | | 219: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "attribute\_description WHERE attribute\_id = '" . (int)$attribute_id . "' AND language_id = '" . (int)$language_id . "'"); | | 220: | | | 221: | return $query->row; | | 222: | } | | 223: | | | 224: | /** | | 225: | * Get Descriptions | | 226: | * | | 227: | * Get the record of the attribute record in the database. | | 228: | * | | 229: | * @param int $attribute_id primary key of the attribute record to be fetched | | 230: | * | | 231: | * @return array<int, array<string, string>> Descriptions | | 232: | */ | | 233: | public function getDescriptions(int $attribute_id): array { | | 234: | $attribute_data = []; | | 235: | | | 236: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "attribute\_description WHERE attribute\_id = '" . (int)$attribute_id . "'"); | | 237: | | | 238: | foreach ($query->rows as $result) { | | 239: | $attribute_data[$result['language_id']] = ['name' => $result['name']]; | | 240: | } | | 241: | | | 242: | return $attribute_data; | | 243: | } | | 244: | | | 245: | /** | | 246: | * Get Descriptions By Language ID | | 247: | * | | 248: | * Get the record of the attribute record in the database. | | 249: | * | | 250: | * @param int $language_id primary key of the attribute language | | 251: | * | | 252: | * @return array<int, array<string, string>> Descriptions by language_id | | 253: | */ | | 254: | public function getDescriptionsByLanguageId(int $language_id): array { | | 255: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "attribute\_description WHERE language\_id = '" . (int)$language_id . "'"); | | 256: | | | 257: | return $query->rows; | | 258: | } | | 259: | } | | 260: | |

OpenCart API API documentation generated by ApiGen dev-master