weight_class.php (4814B)
1 <?php 2 class ModelLocalisationWeightClass extends Model { 3 public function addWeightClass($data) { 4 $this->db->query("INSERT INTO " . DB_PREFIX . "weight_class SET value = '" . (float)$data['value'] . "'"); 5 6 $weight_class_id = $this->db->getLastId(); 7 8 foreach ($data['weight_class_description'] as $language_id => $value) { 9 $this->db->query("INSERT INTO " . DB_PREFIX . "weight_class_description SET weight_class_id = '" . (int)$weight_class_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', unit = '" . $this->db->escape($value['unit']) . "'"); 10 } 11 12 $this->cache->delete('weight_class'); 13 14 return $weight_class_id; 15 } 16 17 public function editWeightClass($weight_class_id, $data) { 18 $this->db->query("UPDATE " . DB_PREFIX . "weight_class SET value = '" . (float)$data['value'] . "' WHERE weight_class_id = '" . (int)$weight_class_id . "'"); 19 20 $this->db->query("DELETE FROM " . DB_PREFIX . "weight_class_description WHERE weight_class_id = '" . (int)$weight_class_id . "'"); 21 22 foreach ($data['weight_class_description'] as $language_id => $value) { 23 $this->db->query("INSERT INTO " . DB_PREFIX . "weight_class_description SET weight_class_id = '" . (int)$weight_class_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', unit = '" . $this->db->escape($value['unit']) . "'"); 24 } 25 26 $this->cache->delete('weight_class'); 27 } 28 29 public function deleteWeightClass($weight_class_id) { 30 $this->db->query("DELETE FROM " . DB_PREFIX . "weight_class WHERE weight_class_id = '" . (int)$weight_class_id . "'"); 31 $this->db->query("DELETE FROM " . DB_PREFIX . "weight_class_description WHERE weight_class_id = '" . (int)$weight_class_id . "'"); 32 33 $this->cache->delete('weight_class'); 34 } 35 36 public function getWeightClasses($data = array()) { 37 if ($data) { 38 $sql = "SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'"; 39 40 $sort_data = array( 41 'title', 42 'unit', 43 'value' 44 ); 45 46 if (isset($data['sort']) && in_array($data['sort'], $sort_data)) { 47 $sql .= " ORDER BY " . $data['sort']; 48 } else { 49 $sql .= " ORDER BY title"; 50 } 51 52 if (isset($data['order']) && ($data['order'] == 'DESC')) { 53 $sql .= " DESC"; 54 } else { 55 $sql .= " ASC"; 56 } 57 58 if (isset($data['start']) || isset($data['limit'])) { 59 if ($data['start'] < 0) { 60 $data['start'] = 0; 61 } 62 63 if ($data['limit'] < 1) { 64 $data['limit'] = 20; 65 } 66 67 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 68 } 69 70 $query = $this->db->query($sql); 71 72 return $query->rows; 73 } else { 74 $weight_class_data = $this->cache->get('weight_class.' . (int)$this->config->get('config_language_id')); 75 76 if (!$weight_class_data) { 77 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); 78 79 $weight_class_data = $query->rows; 80 81 $this->cache->set('weight_class.' . (int)$this->config->get('config_language_id'), $weight_class_data); 82 } 83 84 return $weight_class_data; 85 } 86 } 87 88 public function getWeightClass($weight_class_id) { 89 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class wc LEFT JOIN " . DB_PREFIX . "weight_class_description wcd ON (wc.weight_class_id = wcd.weight_class_id) WHERE wc.weight_class_id = '" . (int)$weight_class_id . "' AND wcd.language_id = '" . (int)$this->config->get('config_language_id') . "'"); 90 91 return $query->row; 92 } 93 94 public function getWeightClassDescriptionByUnit($unit) { 95 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class_description WHERE unit = '" . $this->db->escape($unit) . "' AND language_id = '" . (int)$this->config->get('config_language_id') . "'"); 96 97 return $query->row; 98 } 99 100 public function getWeightClassDescriptions($weight_class_id) { 101 $weight_class_data = array(); 102 103 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "weight_class_description WHERE weight_class_id = '" . (int)$weight_class_id . "'"); 104 105 foreach ($query->rows as $result) { 106 $weight_class_data[$result['language_id']] = array( 107 'title' => $result['title'], 108 'unit' => $result['unit'] 109 ); 110 } 111 112 return $weight_class_data; 113 } 114 115 public function getTotalWeightClasses() { 116 $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "weight_class"); 117 118 return $query->row['total']; 119 } 120 }