function Insert($table, $data) { $this->db->insert($table, $data); $newId = $this->db->insert_id(); return $newId; }
/** * 以資料庫記錄Log * * 20140511 注意,資料庫中的log資料表欄位需要新增「action_key」 * * log資料表的建置SQL如下: * * * @param CI_DB $db * @param String|Int $action 如果是用string,則會記錄在資料庫的 action_key * @param Object $data */ function kals_log($db, $action, $data = array()) { $url = get_referer_url(FALSE); $webpage_id = NULL; if ($url !== FALSE) { /* $CI =& get_instance(); if (isset($CI->webpage) == FALSE || is_null($CI->webpage)) $CI->load->library('kals_resource/Webpage'); $webpage_id = $CI->webpage->filter_webpage_id($url); */ $webpage_id = get_context_webpage()->get_id(); } $user_id = NULL; $note = NULL; if (is_array($data) && (isset($data['user_id']) || isset($data['memo']))) { if (isset($data['user_id'])) { $user_id = $data['user_id']; } if (isset($data['memo'])) { $note = $data['memo']; if (is_array($note) || is_object($note)) { $note = json_encode($note); } if ($note === '') { $note = NULL; } } } else { if (defined("JSON_UNESCAPED_UNICODE")) { $note = json_encode($data, JSON_UNESCAPED_UNICODE); } else { $note = kals_json_encode($data); } } if (is_null($user_id)) { $user = get_context_user(); if (isset($user)) { $user_id = $user->get_id(); } } $CI =& get_instance(); $action_key_mapper = $CI->config->item("log.action_key_mapper"); // 根據action的類型,修改action資料 $action_id = null; $action_key = null; if (is_int($action) || strval(intval($action)) === $action) { $action_id = $action; if (array_key_exists($action_id, $action_key_mapper)) { $action_key = $action_key_mapper[$action_id]; } } else { $action_key = $action; } $db->insert('log', array('webpage_id' => $webpage_id, 'user_id' => $user_id, 'user_ip' => get_client_ip(), 'action' => $action_id, 'action_key' => $action_key, 'note' => $note)); }