custom_field.php (11550B)
1 <?php 2 class ModelCustomerCustomField extends Model { 3 public function addCustomField($data) { 4 $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_field` SET type = '" . $this->db->escape($data['type']) . "', value = '" . $this->db->escape($data['value']) . "', validation = '" . $this->db->escape($data['validation']) . "', location = '" . $this->db->escape($data['location']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "'"); 5 6 $custom_field_id = $this->db->getLastId(); 7 8 foreach ($data['custom_field_description'] as $language_id => $value) { 9 $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($value['name']) . "'"); 10 } 11 12 if (isset($data['custom_field_customer_group'])) { 13 foreach ($data['custom_field_customer_group'] as $custom_field_customer_group) { 14 if (isset($custom_field_customer_group['customer_group_id'])) { 15 $this->db->query("INSERT INTO " . DB_PREFIX . "custom_field_customer_group SET custom_field_id = '" . (int)$custom_field_id . "', customer_group_id = '" . (int)$custom_field_customer_group['customer_group_id'] . "', required = '" . (int)(isset($custom_field_customer_group['required']) ? 1 : 0) . "'"); 16 } 17 } 18 } 19 20 if (isset($data['custom_field_value'])) { 21 foreach ($data['custom_field_value'] as $custom_field_value) { 22 $this->db->query("INSERT INTO " . DB_PREFIX . "custom_field_value SET custom_field_id = '" . (int)$custom_field_id . "', sort_order = '" . (int)$custom_field_value['sort_order'] . "'"); 23 24 $custom_field_value_id = $this->db->getLastId(); 25 26 foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) { 27 $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']) . "'"); 28 } 29 } 30 } 31 32 return $custom_field_id; 33 } 34 35 public function editCustomField($custom_field_id, $data) { 36 $this->db->query("UPDATE `" . DB_PREFIX . "custom_field` SET type = '" . $this->db->escape($data['type']) . "', value = '" . $this->db->escape($data['value']) . "', validation = '" . $this->db->escape($data['validation']) . "', location = '" . $this->db->escape($data['location']) . "', status = '" . (int)$data['status'] . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 37 38 $this->db->query("DELETE FROM " . DB_PREFIX . "custom_field_description WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 39 40 foreach ($data['custom_field_description'] as $language_id => $value) { 41 $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($value['name']) . "'"); 42 } 43 44 $this->db->query("DELETE FROM " . DB_PREFIX . "custom_field_customer_group WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 45 46 if (isset($data['custom_field_customer_group'])) { 47 foreach ($data['custom_field_customer_group'] as $custom_field_customer_group) { 48 if (isset($custom_field_customer_group['customer_group_id'])) { 49 $this->db->query("INSERT INTO " . DB_PREFIX . "custom_field_customer_group SET custom_field_id = '" . (int)$custom_field_id . "', customer_group_id = '" . (int)$custom_field_customer_group['customer_group_id'] . "', required = '" . (int)(isset($custom_field_customer_group['required']) ? 1 : 0) . "'"); 50 } 51 } 52 } 53 54 $this->db->query("DELETE FROM " . DB_PREFIX . "custom_field_value WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 55 $this->db->query("DELETE FROM " . DB_PREFIX . "custom_field_value_description WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 56 57 if (isset($data['custom_field_value'])) { 58 foreach ($data['custom_field_value'] as $custom_field_value) { 59 if ($custom_field_value['custom_field_value_id']) { 60 $this->db->query("INSERT INTO " . DB_PREFIX . "custom_field_value SET custom_field_value_id = '" . (int)$custom_field_value['custom_field_value_id'] . "', custom_field_id = '" . (int)$custom_field_id . "', sort_order = '" . (int)$custom_field_value['sort_order'] . "'"); 61 } else { 62 $this->db->query("INSERT INTO " . DB_PREFIX . "custom_field_value SET custom_field_id = '" . (int)$custom_field_id . "', sort_order = '" . (int)$custom_field_value['sort_order'] . "'"); 63 } 64 65 $custom_field_value_id = $this->db->getLastId(); 66 67 foreach ($custom_field_value['custom_field_value_description'] as $language_id => $custom_field_value_description) { 68 $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']) . "'"); 69 } 70 } 71 } 72 } 73 74 public function deleteCustomField($custom_field_id) { 75 $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 76 $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_description` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 77 $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_customer_group` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 78 $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_value` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 79 $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_field_value_description` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 80 } 81 82 public function getCustomField($custom_field_id) { 83 $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') . "'"); 84 85 return $query->row; 86 } 87 88 public function getCustomFields($data = array()) { 89 if (empty($data['filter_customer_group_id'])) { 90 $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') . "'"; 91 } else { 92 $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') . "'"; 93 } 94 95 if (!empty($data['filter_name'])) { 96 $sql .= " AND cfd.name LIKE '" . $this->db->escape($data['filter_name']) . "%'"; 97 } 98 99 if (!empty($data['filter_customer_group_id'])) { 100 $sql .= " AND cfcg.customer_group_id = '" . (int)$data['filter_customer_group_id'] . "'"; 101 } 102 103 $sort_data = array( 104 'cfd.name', 105 'cf.type', 106 'cf.location', 107 'cf.status', 108 'cf.sort_order' 109 ); 110 111 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 112 $sql .= " ORDER BY " . $data['sort']; 113 } else { 114 $sql .= " ORDER BY cfd.name"; 115 } 116 117 if (isset($data['order']) && ($data['order'] == 'DESC')) { 118 $sql .= " DESC"; 119 } else { 120 $sql .= " ASC"; 121 } 122 123 if (isset($data['start']) || isset($data['limit'])) { 124 if ($data['start'] < 0) { 125 $data['start'] = 0; 126 } 127 128 if ($data['limit'] < 1) { 129 $data['limit'] = 20; 130 } 131 132 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 133 } 134 135 $query = $this->db->query($sql); 136 137 return $query->rows; 138 } 139 140 public function getCustomFieldDescriptions($custom_field_id) { 141 $custom_field_data = array(); 142 143 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "custom_field_description WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 144 145 foreach ($query->rows as $result) { 146 $custom_field_data[$result['language_id']] = array('name' => $result['name']); 147 } 148 149 return $custom_field_data; 150 } 151 152 public function getCustomFieldValue($custom_field_value_id) { 153 $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') . "'"); 154 155 return $query->row; 156 } 157 158 public function getCustomFieldValues($custom_field_id) { 159 $custom_field_value_data = array(); 160 161 $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"); 162 163 foreach ($custom_field_value_query->rows as $custom_field_value) { 164 $custom_field_value_data[$custom_field_value['custom_field_value_id']] = array( 165 'custom_field_value_id' => $custom_field_value['custom_field_value_id'], 166 'name' => $custom_field_value['name'] 167 ); 168 } 169 170 return $custom_field_value_data; 171 } 172 173 public function getCustomFieldCustomerGroups($custom_field_id) { 174 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "custom_field_customer_group` WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 175 176 return $query->rows; 177 } 178 179 public function getCustomFieldValueDescriptions($custom_field_id) { 180 $custom_field_value_data = array(); 181 182 $custom_field_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "custom_field_value WHERE custom_field_id = '" . (int)$custom_field_id . "'"); 183 184 foreach ($custom_field_value_query->rows as $custom_field_value) { 185 $custom_field_value_description_data = array(); 186 187 $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'] . "'"); 188 189 foreach ($custom_field_value_description_query->rows as $custom_field_value_description) { 190 $custom_field_value_description_data[$custom_field_value_description['language_id']] = array('name' => $custom_field_value_description['name']); 191 } 192 193 $custom_field_value_data[] = array( 194 'custom_field_value_id' => $custom_field_value['custom_field_value_id'], 195 'custom_field_value_description' => $custom_field_value_description_data, 196 'sort_order' => $custom_field_value['sort_order'] 197 ); 198 } 199 200 return $custom_field_value_data; 201 } 202 203 public function getTotalCustomFields() { 204 $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "custom_field`"); 205 206 return $query->row['total']; 207 } 208 }