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 Exception
17 *
18 * @param string $name The database setting name.
19 * @return string Returns the database value for
20 * the selected setting.
21 */
22 function get_setting($name) {
23 if (!is_string($name)) { // Check argument type.
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) { // Check if setting exists in db.
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 * This method sets the value for a specific setting
38 * on the database. If the setting doesn't exist, it
39 * is going to be created, otherwise updated.
40 *
41 * @expectedException Exception
42 *
43 * @param string $name The setting name.
44 * @param type $value The setting value.
45 * @return int Returns the setting database id.
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 // Update setting
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 // Insert setting
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 * Remove a setting from the database.
76 *
77 * @expectedException Exception
78 *
79 * @param string $name The setting name to be removed.
80 * @return bool Returns the delete operation result.
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; // There is no such setting.
89 }
90
91 return $this->db->delete('ea_settings', array('name' => $name));
92 }
93
94 /**
95 * Saves all the system settings into the database.
96 *
97 * This method is usefull when trying to save all the system settings at once instead of
98 * saving them one by one.
99 *
100 * @param array $settings Contains all the system settings.
101 * @return bool Returns the save operation result.
102 *
103 * @throws Exception When the update operation won't work for a specific setting.
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 * Returns all the system settings at once.
123 *
124 * @return array Array of all the system settings stored in the 'ea_settings' table.
125 */
126 public function get_settings() {
127 return $this->db->get('ea_settings')->result_array();
128 }
129 }
130
131 /* End of file settings_model.php */
132 /* Location: ./application/models/settings_model.php */