1: <?php
2: class Settings_Model extends CI_Model {
3: 4: 5:
6: function __construct() {
7: parent::__construct();
8: }
9:
10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: function get_setting($name) {
23: if (!is_string($name)) {
24: throw new Exception('$name argument is not a string : ' . $name);
25: }
26:
27: if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) {
28: throw new Exception('$name setting does not exist in database : ' . $name);
29: }
30:
31: $query = $this->db->get_where('ea_settings', array('name' => $name));
32: $setting = ($query->num_rows() > 0) ? $query->row() : '';
33: return $setting->value;
34: }
35:
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: function set_setting($name, $value) {
48: if (!is_string($name)) {
49: throw new Exception('$name argument is not a string : ' . $name);
50: }
51:
52: $query = $this->db->get_where('ea_settings', array('name' => $name));
53: if ($query->num_rows() > 0) {
54:
55: if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) {
56: throw new Exception('Could not update database setting.');
57: }
58: $setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id);
59: } else {
60:
61: $insert_data = array(
62: 'name' => $name,
63: 'value' => $value
64: );
65: if (!$this->db->insert('ea_settings', $insert_data)) {
66: throw new Exception('Could not insert database setting');
67: }
68: $setting_id = intval($this->db->insert_id());
69: }
70:
71: return $setting_id;
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81:
82: function remove_setting($name) {
83: if (!is_string($name)) {
84: throw new Exception('$name is not a string : ' . $name);
85: }
86:
87: if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) {
88: return FALSE;
89: }
90:
91: return $this->db->delete('ea_settings', array('name' => $name));
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105: public function save_settings($settings) {
106: if (!is_array($settings)) {
107: throw new Exception('$settings argument is invalid: '. print_r($settings, TRUE));
108: }
109:
110: foreach($settings as $setting) {
111: $this->db->where('name', $setting['name']);
112: if (!$this->db->update('ea_settings', array('value' => $setting['value']))) {
113: throw new Exception('Could not save setting (' . $setting['name']
114: . ' - ' . $setting['value'] . ')');
115: }
116: }
117:
118: return TRUE;
119: }
120:
121: 122: 123: 124: 125:
126: public function get_settings() {
127: return $this->db->get('ea_settings')->result_array();
128: }
129: }
130:
131:
132: