├── readme.md ├── settings.sql └── Settings.php /readme.md: -------------------------------------------------------------------------------- 1 | # CI-Settings 2 | 3 | CI-Settings is a simple settings library used to manage settings for a site. 4 | 5 | ## Author 6 | 7 | * [Eric Barnes](http://ericlbarnes.com/) 8 | 9 | 10 | ## Installation 11 | 12 | Copy Settings.php to your library folder. Then add the settings table to your database. -------------------------------------------------------------------------------- /settings.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `gh_settings` ( 2 | `option_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 3 | `option_name` varchar(64) NOT NULL DEFAULT '', 4 | `option_value` mediumtext NOT NULL, 5 | `option_group` varchar(55) NOT NULL DEFAULT 'site', 6 | `auto_load` enum('no','yes') NOT NULL DEFAULT 'yes', 7 | PRIMARY KEY (`option_id`,`option_name`), 8 | KEY `option_name` (`option_name`), 9 | KEY `auto_load` (`auto_load`) 10 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ; 11 | 12 | -- 13 | -- Dumping data for table `gh_settings` 14 | -- 15 | 16 | INSERT INTO `gh_settings` VALUES(1, 'script_version', '', 'script', 'yes'); 17 | INSERT INTO `gh_settings` VALUES(2, 'script_build', '', 'script', 'yes'); 18 | INSERT INTO `gh_settings` VALUES(3, 'script_db_version', '', 'script', 'yes'); 19 | INSERT INTO `gh_settings` VALUES(4, 'site_name', 'Demo Site', 'site', 'yes'); 20 | INSERT INTO `gh_settings` VALUES(5, 'site_keywords', 'keywords, go, here', 'site', 'yes'); 21 | INSERT INTO `gh_settings` VALUES(6, 'site_description', 'Demo Site description', 'site', 'yes'); 22 | INSERT INTO `gh_settings` VALUES(7, 'site_email', 'noreply@example.com', 'site', 'yes'); -------------------------------------------------------------------------------- /Settings.php: -------------------------------------------------------------------------------- 1 | 7 | * @copyright Copyright (c) Eric Barnes 8 | * @since Version 1.0 9 | */ 10 | 11 | // ------------------------------------------------------------------------ 12 | 13 | /** 14 | * Settings Library 15 | * 16 | * Used to manage site settings 17 | * 18 | * @package CI Settings 19 | * @subpackage Libraries 20 | */ 21 | class Settings 22 | { 23 | /** 24 | * Global CI Object 25 | */ 26 | private $_ci; 27 | 28 | /** 29 | * Settings array used to pass settings to template 30 | * 31 | * @access private 32 | * @var array 33 | */ 34 | private $settings = array(); 35 | 36 | /** 37 | * Settings group array 38 | * 39 | * @access private 40 | * @var array 41 | */ 42 | private $settings_group = array(); 43 | 44 | // ------------------------------------------------------------------------ 45 | 46 | /** 47 | * Constructor assign CI instance 48 | * 49 | * @return void 50 | */ 51 | public function __construct() 52 | { 53 | $this->_ci =& get_instance(); 54 | self::get_settings(); 55 | } 56 | 57 | // ------------------------------------------------------------------------ 58 | 59 | /** 60 | * Get Settings 61 | * 62 | * Get all the auto loaded settings from the db. 63 | * 64 | * @return array 65 | */ 66 | public function get_settings() 67 | { 68 | // If the array is not empty we already have them. 69 | if ( ! empty ($this->settings)) 70 | { 71 | return $this->settings; 72 | } 73 | 74 | if ( ! $this->_ci->db->table_exists('settings')) 75 | { 76 | return FALSE; 77 | } 78 | 79 | $this->_ci->db->select('option_name,option_value') 80 | ->from('settings') 81 | ->where('auto_load', 'yes'); 82 | 83 | $query = $this->_ci->db->get(); 84 | 85 | if ($query->num_rows() == 0) 86 | { 87 | return FALSE; 88 | } 89 | 90 | foreach ($query->result() as $k => $row) 91 | { 92 | $this->settings[$row->option_name] = $row->option_value; 93 | $this->_ci->config->set_item($row->option_name, $row->option_value); 94 | } 95 | 96 | return $this->settings; 97 | } 98 | 99 | // ------------------------------------------------------------------------ 100 | 101 | /** 102 | * Get Setting (Notice Singular) 103 | * 104 | * Used to pull out one specific setting from the settings table. 105 | * 106 | * Here is an example: 107 | * 108 | * settings->get_setting('site_name'); 110 | * ?> 111 | * 112 | * 113 | * @access public 114 | * @param string 115 | * @return mixed 116 | */ 117 | public function get_setting($option_name) 118 | { 119 | if ( ! $option_name) 120 | { 121 | return FALSE; 122 | } 123 | 124 | // First check the auto loaded settings 125 | if (isset($this->settings[$option_name])) 126 | { 127 | return $this->settings[$option_name]; 128 | } 129 | 130 | // Now lets try the settings table 131 | $this->_ci->db->select('option_value') 132 | ->from('settings') 133 | ->where('option_name', $option_name); 134 | 135 | $query = $this->_ci->db->get(); 136 | 137 | if ($query->num_rows() > 0) 138 | { 139 | $row = $query->row(); 140 | // Add it to the main settings array 141 | $this->settings[$option_name] = $row->option_value; 142 | 143 | return $row->option_value; 144 | } 145 | 146 | // Still nothing. How about config? 147 | // This will retun FALSE if not found. 148 | return $this->_ci->config->item($option_name); 149 | } 150 | 151 | // ------------------------------------------------------------------------ 152 | 153 | /** 154 | * Get Settings By Group 155 | * 156 | * Get all the settings from one group 157 | * 158 | * @param string 159 | * @return object 160 | */ 161 | public function get_settings_by_group($option_group = '') 162 | { 163 | if ( ! $option_group) 164 | { 165 | return FALSE; 166 | } 167 | 168 | $this->_ci->db->select('option_name,option_value') 169 | ->from('settings') 170 | ->where('option_group', $option_group); 171 | 172 | $query = $this->_ci->db->get(); 173 | 174 | if ($query->num_rows() == 0) 175 | { 176 | return FALSE; 177 | } 178 | 179 | foreach ($query->result() as $k => $row) 180 | { 181 | $this->settings[$row->option_name] = $row->option_value; 182 | $arr[$row->option_name] = $row->option_value; 183 | } 184 | 185 | return $arr; 186 | } 187 | 188 | // ------------------------------------------------------------------------ 189 | 190 | /** 191 | * Edit Setting 192 | * 193 | * @param string $option_name 194 | * @param string $option_value 195 | * @return bool 196 | */ 197 | public function edit_setting($option_name, $option_value) 198 | { 199 | $this->_ci->db->where('option_name', $option_name); 200 | $this->_ci->db->update('settings', array('option_value' => $option_value)); 201 | 202 | if ($this->_ci->db->affected_rows() == 0) 203 | { 204 | return FALSE; 205 | } 206 | 207 | return TRUE; 208 | } 209 | 210 | // ------------------------------------------------------------------------ 211 | 212 | /** 213 | * Delete Setting by group 214 | * 215 | * @param string $option_group 216 | * @return bool 217 | */ 218 | public function delete_settings_by_group($option_group) 219 | { 220 | $this->_ci->db->delete('settings', array('option_group' => $option_group)); 221 | 222 | if ($this->_ci->db->affected_rows() == 0) 223 | { 224 | return FALSE; 225 | } 226 | 227 | return TRUE; 228 | } 229 | 230 | // ------------------------------------------------------------------------ 231 | 232 | /** 233 | * Add Setting 234 | * 235 | * Add a new setting but first check and make sure it doesn't exist already exit. 236 | * 237 | * @param string $option_name 238 | * @param string $option_value 239 | * @param string $option_group 240 | * @param string $auto_load 241 | * @return bool 242 | */ 243 | public function add_setting($option_name, $option_value = '', $option_group = 'addon', $auto_load = 'no') 244 | { 245 | // Check and make sure it isn't already added. 246 | $this->_ci->db->select('option_value') 247 | ->from('settings') 248 | ->where('option_name', $option_name); 249 | 250 | $query = $this->_ci->db->get(); 251 | 252 | if ($query->num_rows() > 0) 253 | { 254 | return $this->edit_setting($option_name, $option_value); 255 | } 256 | 257 | // Now insert it 258 | $data = array( 259 | 'option_name' => $option_name, 260 | 'option_value' => $option_value, 261 | 'option_group' => $option_group, 262 | 'auto_load' => $auto_load, 263 | ); 264 | 265 | $this->_ci->db->insert('settings', $data); 266 | 267 | if ($this->_ci->db->affected_rows() == 0) 268 | { 269 | return FALSE; 270 | } 271 | 272 | return TRUE; 273 | } 274 | 275 | // ------------------------------------------------------------------------ 276 | 277 | /** 278 | * Delete Setting 279 | * 280 | * @param string $option_group 281 | * @return bool 282 | */ 283 | public function delete_setting($option_name) 284 | { 285 | $this->_ci->db->delete('settings', array('option_name' => $option_name)); 286 | 287 | if ($this->_ci->db->affected_rows() == 0) 288 | { 289 | return FALSE; 290 | } 291 | 292 | return TRUE; 293 | } 294 | } 295 | 296 | /* End of file Settings.php */ --------------------------------------------------------------------------------