query($sql); $news_id = $nexusdb->insert_id; return $news_id; } function nexus_news_update($news_id, $update_arr) { global $nexus_cfg, $nexusdb; $update_clause = ""; reset($update_arr); while (list($key, $val) = each($update_arr)) { $update_clause .= (($update_clause=="")?"":", ") . sprintf("`%s` = '%s' ", mysql_real_escape_string(trim($key)), mysql_real_escape_string(trim($val))); } $sql = "UPDATE `%s` SET %s, `last_modified` = NOW() WHERE `news_id` = %d "; $sql = sprintf($sql, $nexus_cfg["table"]["news"], $update_clause, $news_id); $num_rows_affected = $nexusdb->query($sql); return $num_rows_affected; } function nexus_news_delete($news_id) { global $nexus_cfg, $nexusdb; $news_row = nexus_news_get_row($news_id); // delete files if (trim($news_row->file_path) != "") { @unlink($_SERVER["DOCUMENT_ROOT"] . $news_row->file_path); } if (trim($news_row->thumbnail) != "") { @unlink($_SERVER["DOCUMENT_ROOT"] . $news_row->thumbnail); } $news_dir = sprintf($nexus_cfg["path"]["uploaded_news"]."/", ($news_id % 10), $news_id); if (is_dir($news_dir)) { @unlink($news_dir); } // delete db record $sql = "DELETE FROM `%s` WHERE `news_id` = %d "; $sql = sprintf($sql, $nexus_cfg["table"]["news"], $news_id); $nexusdb->query($sql); return; } function nexus_news_get_row($id) { global $nexus_cfg, $nexusdb; settype($id, "integer"); if ($id < 1) { return false; } $sql = "SELECT * FROM `%s` WHERE `news_id` = %d "; $sql = sprintf($sql, $nexus_cfg["table"]["news"], mysql_real_escape_string(trim($id))); $news = $nexusdb->get_results($sql); if (count($news) < 1) { return false; } return $news[0]; } function nexus_news_get_list($search_info, &$total, &$result, $order_clause, $page=0, $page_size=0) { global $nexus_cfg, $nexusdb; $total = 0; $result = array(); $where_clause = ""; $table_clause = $nexus_cfg["table"]["news"] . " n "; if (is_array($search_info) && count($search_info) > 0) { if (isset($search_info["keyword"])) { $keyword = mysql_real_escape_string(trim($search_info["keyword"])); if ($keyword != "") { $where_clause .= sprintf(" (`title_zh` LIKE '%%%s%%' OR `title_en` LIKE '%%%s%%' OR `title_cn` LIKE '%%%s%%' ) ", $keyword, $keyword, $keyword); } unset($search_info["keyword"]); } reset($search_info); while (list($key, $val) = each($search_info)) { $where_clause .= (($where_clause=="")?"":" AND ") . sprintf("`%s` = '%s' ", mysql_real_escape_string(trim($key)), mysql_real_escape_string(trim($val))) ; } } if ($where_clause != "") { $where_clause = sprintf("WHERE %s", $where_clause); } $sql = "SELECT COUNT(*) AS total FROM %s %s"; $sql = sprintf($sql, $table_clause, $where_clause); $news_total = $nexusdb->get_results($sql); if (count($news_total) < 1) { return false; } $news_total_row = $news_total[0]; $total = $news_total_row->total; if ($order_clause == "") { $order_clause = "ORDER BY order_id ASC, create_time DESC "; } $limit_clause = ""; if ($page_size > 0) { $offset = ($page - 1) * $page_size; $limit_clause = sprintf("LIMIT %d, %d", $offset, $page_size); } $sql = "SELECT n.* FROM %s %s %s %s"; $sql = sprintf($sql, $table_clause, $where_clause, $order_clause, $limit_clause); $result = $nexusdb->get_results($sql); } function nexus_news_upload_file($news_id, $file_type, $file_info, $existing_file='') { global $nexus_cfg, $_SERVER; $upload_path = ''; if (file_exists($file_info['tmp_name'])) { if ($existing_file != "") { $uploaded_upload_path = $_SERVER['DOCUMENT_ROOT'] . $existing_file; if (file_exists($uploaded_upload_path)) { unlink($uploaded_upload_path); } } $target_path = sprintf($nexus_cfg["path"]["uploaded_news"]."/", ($news_id%10), $news_id); $new_file_path = $target_path . basename($file_info['name']); // check directory if (!is_dir($target_path)) { mkdir($target_path); } $new_file_info = pathinfo(basename($file_info["name"])); while (file_exists($new_file_path)) { $new_file_path = sprintf("%s%s_%s.%s", $target_path, $file_type, unique(), $new_file_info["extension"]); } if (move_uploaded_file($file_info['tmp_name'], $new_file_path)) { $upload_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $new_file_path); } } return $upload_path; } ?>