1: <?php
2: class Settings_Model extends CI_Model {
3: /**
4: * Class Constructor
5: */
6: function __construct() {
7: parent::__construct();
8: }
9:
10: /**
11: * Get setting value from database.
12: *
13: * This method returns a system setting from the
14: * database.
15: *
16: * @expectedException InvalidArgumentException Raises whenever the
17: * $name argument is not a string, or does not exist in the database.
18: *
19: * @param string $name The database setting name.
20: * @return string Returns the database value for
21: * the selected setting.
22: */
23: function get_setting($name) {
24: if (!is_string($name)) { // Check argument type.
25: throw new InvalidArgumentException('$name argument is not a string : ' . $name);
26: }
27:
28: if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { // Check if setting exists in db.
29: throw new InvalidArgumentException('$name setting does not exist in database : ' . $name);
30: }
31:
32: $query = $this->db->get_where('ea_settings', array('name' => $name));
33: $setting = ($query->num_rows() > 0) ? $query->row() : '';
34: return $setting->value;
35: }
36:
37: /**
38: * This method sets the value for a specific setting
39: * on the database. If the setting doesn't exist, it
40: * is going to be created, otherwise updated.
41: *
42: * @expectedException DatabaseException Raises whenever an error
43: * occures during the insert or the update operation.
44: * @expectedException InvalidArgumentException Raises whenever
45: * the $name argument is not a string.
46: *
47: * @param string $name The setting name.
48: * @param type $value The setting value.
49: * @return int Returns the setting database id.
50: */
51: function set_setting($name, $value) {
52: if (!is_string($name)) {
53: throw new InvalidArgumentException('$name argument is not a string : ' . $name);
54: }
55:
56: $query = $this->db->get_where('ea_settings', array('name' => $name));
57: if ($query->num_rows() > 0) {
58: // Update setting
59: if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) {
60: throw new DatabaseException('Could not update database setting.');
61: }
62: $setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id);
63: } else {
64: // Insert setting
65: $insert_data = array(
66: 'name' => $name,
67: 'value' => $value
68: );
69: if (!$this->db->insert('ea_settings', $insert_data)) {
70: throw new DatabaseException('Could not insert database setting');
71: }
72: $setting_id = intval($this->db->insert_id());
73: }
74:
75: return $setting_id;
76: }
77:
78: /**
79: * Remove a setting from the database.
80: *
81: * @expectedException InvalidArgumentException Raises whenever
82: * the $name parameter is not a string.
83: *
84: * @param string $name The setting name to be removed.
85: * @return bool Returns the delete operation result.
86: */
87: function remove_setting($name) {
88: if (!is_string($name)) {
89: throw new InvalidArgumentException('$name is not a string : ' . $name);
90: }
91:
92: if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) {
93: return FALSE; // There is no such setting.
94: }
95:
96: return $this->db->delete('ea_settings', array('name' => $name));
97: }
98: }
99:
100: /* End of file settings_model.php */
101: /* Location: ./application/models/settings_model.php */