<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Setupsystem extends CI_Model {
	function __construct() {
		parent::__construct();
	}
	
	public function getAllSetups($countOnly = FALSE) {
		$sql = "SELECT code, value FROM system_setup ORDER BY code ASC";
		$q = $this->db->query($sql);
		$return = NULL;
		if($result = ($q && ($r = $q->result())) ? $this->ObjToArray($r) : NULL) {
			foreach($result as $key=>$value) {
				$return[$value['code']] = $value['value'];
			}
		}
		return $return;
	}
	
	public function getSetupValue($code) {
		$sql = "SELECT value FROM system_setup WHERE UPPER(code) = '".(strtoupper($code))."'";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result()) && $r[0]) ? $r[0]->value : NULL;
	}
	
	public function getAllCountries() {
		$sql = "SELECT ID, name FROM _sys_country ORDER BY name ASC";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result())) ? $this->ObjToArray($r) : NULL;
	}
	
	public function getAllStates($country = NULL) {
		$sql = "SELECT ID, name FROM _sys_state WHERE 1 = 1 ".(($country) ? " AND country = ".$country : NULL)." ORDER BY name ASC";
		$q = $this->db->query($sql);
		return ($q && ($r = $q->result())) ? $this->ObjToArray($r) : NULL;
	}
	
	public function ObjToArray($obj) {
		return ($obj) ? json_decode(json_encode($obj),true) : NULL;
	}
}
?>
