shop.balmet.com

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

geo_zone.php (4793B)


      1 <?php
      2 class ModelLocalisationGeoZone extends Model {
      3 	public function addGeoZone($data) {
      4 		$this->db->query("INSERT INTO " . DB_PREFIX . "geo_zone SET name = '" . $this->db->escape($data['name']) . "', description = '" . $this->db->escape($data['description']) . "', date_added = NOW()");
      5 
      6 		$geo_zone_id = $this->db->getLastId();
      7 
      8 		if (isset($data['zone_to_geo_zone'])) {
      9 			foreach ($data['zone_to_geo_zone'] as $value) {
     10 				$this->db->query("DELETE FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "' AND country_id = '" . (int)$value['country_id'] . "' AND zone_id = '" . (int)$value['zone_id'] . "'");				
     11 
     12 				$this->db->query("INSERT INTO " . DB_PREFIX . "zone_to_geo_zone SET country_id = '" . (int)$value['country_id'] . "', zone_id = '" . (int)$value['zone_id'] . "', geo_zone_id = '" . (int)$geo_zone_id . "', date_added = NOW()");
     13 			}
     14 		}
     15 
     16 		$this->cache->delete('geo_zone');
     17 		
     18 		return $geo_zone_id;
     19 	}
     20 
     21 	public function editGeoZone($geo_zone_id, $data) {
     22 		$this->db->query("UPDATE " . DB_PREFIX . "geo_zone SET name = '" . $this->db->escape($data['name']) . "', description = '" . $this->db->escape($data['description']) . "', date_modified = NOW() WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
     23 
     24 		$this->db->query("DELETE FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
     25 
     26 		if (isset($data['zone_to_geo_zone'])) {
     27 			foreach ($data['zone_to_geo_zone'] as $value) {
     28 				$this->db->query("DELETE FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "' AND country_id = '" . (int)$value['country_id'] . "' AND zone_id = '" . (int)$value['zone_id'] . "'");				
     29 
     30 				$this->db->query("INSERT INTO " . DB_PREFIX . "zone_to_geo_zone SET country_id = '" . (int)$value['country_id'] . "', zone_id = '" . (int)$value['zone_id'] . "', geo_zone_id = '" . (int)$geo_zone_id . "', date_added = NOW()");
     31 			}
     32 		}
     33 
     34 		$this->cache->delete('geo_zone');
     35 	}
     36 
     37 	public function deleteGeoZone($geo_zone_id) {
     38 		$this->db->query("DELETE FROM " . DB_PREFIX . "geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
     39 		$this->db->query("DELETE FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
     40 
     41 		$this->cache->delete('geo_zone');
     42 	}
     43 
     44 	public function getGeoZone($geo_zone_id) {
     45 		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
     46 
     47 		return $query->row;
     48 	}
     49 
     50 	public function getGeoZones($data = array()) {
     51 		if ($data) {
     52 			$sql = "SELECT * FROM " . DB_PREFIX . "geo_zone";
     53 
     54 			$sort_data = array(
     55 				'name',
     56 				'description'
     57 			);
     58 
     59 			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
     60 				$sql .= " ORDER BY " . $data['sort'];
     61 			} else {
     62 				$sql .= " ORDER BY name";
     63 			}
     64 
     65 			if (isset($data['order']) && ($data['order'] == 'DESC')) {
     66 				$sql .= " DESC";
     67 			} else {
     68 				$sql .= " ASC";
     69 			}
     70 
     71 			if (isset($data['start']) || isset($data['limit'])) {
     72 				if ($data['start'] < 0) {
     73 					$data['start'] = 0;
     74 				}
     75 
     76 				if ($data['limit'] < 1) {
     77 					$data['limit'] = 20;
     78 				}
     79 
     80 				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
     81 			}
     82 
     83 			$query = $this->db->query($sql);
     84 
     85 			return $query->rows;
     86 		} else {
     87 			$geo_zone_data = $this->cache->get('geo_zone');
     88 
     89 			if (!$geo_zone_data) {
     90 				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "geo_zone ORDER BY name ASC");
     91 
     92 				$geo_zone_data = $query->rows;
     93 
     94 				$this->cache->set('geo_zone', $geo_zone_data);
     95 			}
     96 
     97 			return $geo_zone_data;
     98 		}
     99 	}
    100 
    101 	public function getTotalGeoZones() {
    102 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "geo_zone");
    103 
    104 		return $query->row['total'];
    105 	}
    106 
    107 	public function getZoneToGeoZones($geo_zone_id) {
    108 		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
    109 
    110 		return $query->rows;
    111 	}
    112 
    113 	public function getTotalZoneToGeoZoneByGeoZoneId($geo_zone_id) {
    114 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
    115 
    116 		return $query->row['total'];
    117 	}
    118 
    119 	public function getTotalZoneToGeoZoneByCountryId($country_id) {
    120 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "zone_to_geo_zone WHERE country_id = '" . (int)$country_id . "'");
    121 
    122 		return $query->row['total'];
    123 	}
    124 
    125 	public function getTotalZoneToGeoZoneByZoneId($zone_id) {
    126 		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "zone_to_geo_zone WHERE zone_id = '" . (int)$zone_id . "'");
    127 
    128 		return $query->row['total'];
    129 	}
    130 }