2013-04-14 22:42:40 +03:00
|
|
|
<?php
|
2013-04-20 20:20:16 +03:00
|
|
|
class Settings_Model extends CI_Model {
|
2013-05-04 00:26:04 +03:00
|
|
|
/**
|
|
|
|
* Class Constructor
|
|
|
|
*/
|
2013-04-20 20:20:16 +03:00
|
|
|
function __construct() {
|
2013-04-14 22:42:40 +03:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-04 00:26:04 +03:00
|
|
|
* Get setting value from database.
|
|
|
|
*
|
2013-04-14 22:42:40 +03:00
|
|
|
* This method returns a system setting from the
|
|
|
|
* database.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @expectedException Exception
|
2013-05-15 18:03:47 +03:00
|
|
|
*
|
2013-04-14 22:42:40 +03:00
|
|
|
* @param string $name The database setting name.
|
|
|
|
* @return string Returns the database value for
|
|
|
|
* the selected setting.
|
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
function get_setting($name) {
|
2013-05-15 18:03:47 +03:00
|
|
|
if (!is_string($name)) { // Check argument type.
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('$name argument is not a string : ' . $name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) { // Check if setting exists in db.
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('$name setting does not exist in database : ' . $name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:42:40 +03:00
|
|
|
$query = $this->db->get_where('ea_settings', array('name' => $name));
|
|
|
|
$setting = ($query->num_rows() > 0) ? $query->row() : '';
|
|
|
|
return $setting->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method sets the value for a specific setting
|
|
|
|
* on the database. If the setting doesn't exist, it
|
|
|
|
* is going to be created, otherwise updated.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @expectedException Exception
|
2013-05-15 18:03:47 +03:00
|
|
|
*
|
2013-04-14 22:42:40 +03:00
|
|
|
* @param string $name The setting name.
|
|
|
|
* @param type $value The setting value.
|
2013-05-15 18:03:47 +03:00
|
|
|
* @return int Returns the setting database id.
|
2013-04-14 22:42:40 +03:00
|
|
|
*/
|
2013-05-04 00:26:04 +03:00
|
|
|
function set_setting($name, $value) {
|
2013-05-15 18:03:47 +03:00
|
|
|
if (!is_string($name)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('$name argument is not a string : ' . $name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-14 22:42:40 +03:00
|
|
|
$query = $this->db->get_where('ea_settings', array('name' => $name));
|
|
|
|
if ($query->num_rows() > 0) {
|
|
|
|
// Update setting
|
2013-05-15 18:03:47 +03:00
|
|
|
if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Could not update database setting.');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
$setting_id = intval($this->db->get_where('ea_settings', array('name' => $name))->row()->id);
|
2013-04-14 22:42:40 +03:00
|
|
|
} else {
|
|
|
|
// Insert setting
|
2013-05-04 00:26:04 +03:00
|
|
|
$insert_data = array(
|
|
|
|
'name' => $name,
|
2013-04-14 22:42:40 +03:00
|
|
|
'value' => $value
|
|
|
|
);
|
2013-05-15 18:03:47 +03:00
|
|
|
if (!$this->db->insert('ea_settings', $insert_data)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('Could not insert database setting');
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
$setting_id = intval($this->db->insert_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $setting_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a setting from the database.
|
|
|
|
*
|
2013-07-06 03:00:04 +03:00
|
|
|
* @expectedException Exception
|
2013-05-15 18:03:47 +03:00
|
|
|
*
|
|
|
|
* @param string $name The setting name to be removed.
|
|
|
|
* @return bool Returns the delete operation result.
|
|
|
|
*/
|
|
|
|
function remove_setting($name) {
|
|
|
|
if (!is_string($name)) {
|
2013-07-06 03:00:04 +03:00
|
|
|
throw new Exception('$name is not a string : ' . $name);
|
2013-05-15 18:03:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->db->get_where('ea_settings', array('name' => $name))->num_rows() == 0) {
|
|
|
|
return FALSE; // There is no such setting.
|
2013-04-14 22:42:40 +03:00
|
|
|
}
|
|
|
|
|
2013-05-15 18:03:47 +03:00
|
|
|
return $this->db->delete('ea_settings', array('name' => $name));
|
2013-04-14 22:42:40 +03:00
|
|
|
}
|
2013-09-14 19:10:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves all the system settings into the database.
|
|
|
|
*
|
|
|
|
* This method is usefull when trying to save all the system settings at once instead of
|
|
|
|
* saving them one by one.
|
|
|
|
*
|
|
|
|
* @param array $settings Contains all the system settings.
|
|
|
|
* @return bool Returns the save operation result.
|
|
|
|
*
|
|
|
|
* @throws Exception When the update operation won't work for a specific setting.
|
|
|
|
*/
|
|
|
|
public function save_settings($settings) {
|
|
|
|
if (!is_array($settings)) {
|
|
|
|
throw new Exception('$settings argument is invalid: '. print_r($settings, TRUE));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($settings as $name=>$value) {
|
|
|
|
if (!$this->db->update('ea_settings', array('value' => $value), array('name' => $name))) {
|
|
|
|
throw new Exception('Could not save setting (' . $name . ' - ' . $value . ')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the system settings at once.
|
|
|
|
*
|
|
|
|
* @return array Array of all the system settings stored in the 'ea_settings' table.
|
|
|
|
*/
|
|
|
|
public function get_settings() {
|
|
|
|
return $this->db->get('ea_settings')->result_array();
|
|
|
|
}
|
2013-04-14 22:42:40 +03:00
|
|
|
}
|
2013-05-04 00:26:04 +03:00
|
|
|
|
|
|
|
/* End of file settings_model.php */
|
|
|
|
/* Location: ./application/models/settings_model.php */
|