<?php
class ModelSaleRingsize extends Model {
	public function addRingSize($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "ring_size SET ring_size = '" . $this->db->escape($data['ring_size']) . "', country = '" . $data['country'] . "',status = '" . $data['status'] . "'");
	}
	public function editRingSize($ring_size_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "ring_size SET ring_size = '" . $this->db->escape($data['ring_size']) . "', country = '" . $data['country'] . "',status = '" . $data['status'] . "' WHERE ring_size_id = '" . (int)$ring_size_id . "'");
		
	}
	
	public function deleteRingSize($ring_size_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "ring_size WHERE ring_size_id = '" . (int)$ring_size_id . "'");		
	}
	
	public function getRingSize($ring_size_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "ring_size WHERE ring_size_id = '" . (int)$ring_size_id . "'");
		
		return $query->row;
	}
	
	
	public function getRingSizes($data = array()) {
		$sql = "SELECT * FROM " . DB_PREFIX . "ring_size";
		
		$sort_data = array(
			'ring_size',
			'country',
			'status'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY ring_size";	
		}	
		
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotalRingSize() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "ring_size");
		
		return $query->row['total'];
	}
}
?>