<?php
class Settings_model extends CI_Model{
	
	public function __construct(){
	    parent::__construct();
		$this->load->database();
	}

	public function get($metakey=''){
		if(!empty($metakey)){
			$this->db->where('meta_key', $metakey);
			$val = $this->db->get('settings', 1, 0);
			return $val->num_rows()>0?$val->row(0)->meta_value:'';
		}
		return false;
	}

	public function exists($metakey=''){
		if(!empty($metakey)){
			$val = $this->db->get_where('settings', array('meta_key' => $metakey), 1, 0);
			return ($val->num_rows()>0);
		}
		return false;
	}

	public function add($metakey='', $metavalue=''){
		if(!empty($metakey) && !empty($metavalue)){
			$data = array(
				'meta_key' => $metakey,
				'meta_value' => $metavalue,
				'updated' => date("Y-m-d H:i:s")
			);
			return $this->db->insert('settings', $data);
		}
		return false;
	}

	public function update($metakey='', $metavalue=''){
		if(!empty($metakey)){
			if($this->exists($metakey)){
				$this->db->set('meta_value', $metavalue);
				$this->db->set('updated', date("Y-m-d H:i:s"));
				$this->db->where('meta_key', $metakey);
				return $this->db->update('settings');
			} else return $this->add($metakey, $metavalue);
		}
		return false;
	}

	public function delete($metakey=''){
		if($this->exists($metakey)){
			return $this->db->delete('settings', array('meta_key' => $metakey));
		}
		return false;
	}
}