shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

country.php (2873B)


      1 <?php
      2 class ModelLocalisationCountry extends Model {
      3 	public function addCountry($data) {
      4 		$this->db->query("INSERT INTO " . DB_PREFIX . "country SET name = '" . $this->db->escape($data['name']) . "', iso_code_2 = '" . $this->db->escape($data['iso_code_2']) . "', iso_code_3 = '" . $this->db->escape($data['iso_code_3']) . "', address_format = '" . $this->db->escape($data['address_format']) . "', postcode_required = '" . (int)$data['postcode_required'] . "', status = '" . (int)$data['status'] . "'");
      5 
      6 		$this->cache->delete('country');
      7 		
      8 		return $this->db->getLastId();
      9 	}
     10 
     11 	public function editCountry($country_id, $data) {
     12 		$this->db->query("UPDATE " . DB_PREFIX . "country SET name = '" . $this->db->escape($data['name']) . "', iso_code_2 = '" . $this->db->escape($data['iso_code_2']) . "', iso_code_3 = '" . $this->db->escape($data['iso_code_3']) . "', address_format = '" . $this->db->escape($data['address_format']) . "', postcode_required = '" . (int)$data['postcode_required'] . "', status = '" . (int)$data['status'] . "' WHERE country_id = '" . (int)$country_id . "'");
     13 
     14 		$this->cache->delete('country');
     15 	}
     16 
     17 	public function deleteCountry($country_id) {
     18 		$this->db->query("DELETE FROM " . DB_PREFIX . "country WHERE country_id = '" . (int)$country_id . "'");
     19 
     20 		$this->cache->delete('country');
     21 	}
     22 
     23 	public function getCountry($country_id) {
     24 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "country WHERE country_id = '" . (int)$country_id . "'");
     25 
     26 		return $query->row;
     27 	}
     28 
     29 	public function getCountries($data = array()) {
     30 		if ($data) {
     31 			$sql = "SELECT * FROM " . DB_PREFIX . "country";
     32 
     33 			$sort_data = array(
     34 				'name',
     35 				'iso_code_2',
     36 				'iso_code_3'
     37 			);
     38 
     39 			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
     40 				$sql .= " ORDER BY " . $data['sort'];
     41 			} else {
     42 				$sql .= " ORDER BY name";
     43 			}
     44 
     45 			if (isset($data['order']) && ($data['order'] == 'DESC')) {
     46 				$sql .= " DESC";
     47 			} else {
     48 				$sql .= " ASC";
     49 			}
     50 
     51 			if (isset($data['start']) || isset($data['limit'])) {
     52 				if ($data['start'] < 0) {
     53 					$data['start'] = 0;
     54 				}
     55 
     56 				if ($data['limit'] < 1) {
     57 					$data['limit'] = 20;
     58 				}
     59 
     60 				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
     61 			}
     62 
     63 			$query = $this->db->query($sql);
     64 
     65 			return $query->rows;
     66 		} else {
     67 			$country_data = $this->cache->get('country.admin');
     68 
     69 			if (!$country_data) {
     70 				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "country ORDER BY name ASC");
     71 
     72 				$country_data = $query->rows;
     73 
     74 				$this->cache->set('country.admin', $country_data);
     75 			}
     76 
     77 			return $country_data;
     78 		}
     79 	}
     80 
     81 	public function getTotalCountries() {
     82 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "country");
     83 
     84 		return $query->row['total'];
     85 	}
     86 }