public function save() { set_exception_handler('json_exception_handler'); header('Cache-Control: no-cache, must-revalidate'); header('Content-type: application/json'); $data = file_get_contents("php://input"); $data = json_decode($data, true); $data = $data['data']; if (!$data) { throw new Exception('No data to save'); } $anychanged = false; foreach ($data as $key => $c) { if ($c['type'] == 'string') { if (!$anychanged && $c['value'] != get_db_config_item($key)) { $anychanged = true; } if ($c['value'] != get_db_config_item($key)) { $anychanged = true; set_config_item($key, $c['type'], $c['value']); } } } if (!$anychanged) { echo json_encode(array('status' => 'OK', 'message' => 'No configuration change detected')); } else { echo json_encode(array('status' => 'OK', 'message' => 'All configuration item successfully updated')); } }
function set_config_item($key, $type, $value) { $_ci =& get_instance(); if ($value == 'json' && is_array($value)) { $value = json_encode($value); } $action = null; if (get_db_config_item($key)) { $action = 'update'; } else { $action = 'create'; } // if(!get_global_config_item($key)){ // if(get_db_config_item($key)){ // $action = 'update'; // } else { // $action = 'create'; // } // } else { // if(get_db_config_item($key)){ // $action='update'; // } else { // $action = 'create'; // } // } if ($action == 'create') { $data = array('key' => $key, 'type' => $type, 'value' => $value); $insert_query = $_ci->db->insert('configs', $data); if ($insert_query) { // log_message('info', 'CONFIG creating '.$value.' to '.$key.' as '.$type); return true; } else { return false; } } elseif ($action == 'update') { $_ci->db->where('key', $key); $update_query = $_ci->db->update('configs', array('type' => $type, 'value' => $value)); // log_message('info', 'CONFIG update '.$value.' to '.$key.' as '.$type); } else { return false; } }