/** * @param template $template * @param string $query_where * @param int $query_limit */ function nws_render(&$template, $query_where = '', $query_limit = 20) { global $config, $user; $announce_list = doquery("SELECT a.*, UNIX_TIMESTAMP(`tsTimeStamp`) AS unix_time, u.authlevel, s.*\n FROM\n {{announce}} AS a\n LEFT JOIN {{survey}} AS s ON s.survey_announce_id = a.idAnnounce\n LEFT JOIN {{users}} AS u ON u.id = a.user_id\n {$query_where}\n ORDER BY `tsTimeStamp` DESC, idAnnounce" . ($query_limit ? " LIMIT {$query_limit}" : '')); $template->assign_var('NEWS_COUNT', db_num_rows($announce_list)); $users = array(); while ($announce = db_fetch($announce_list)) { if ($announce['user_id'] && !isset($users[$announce['user_id']])) { $users[$announce['user_id']] = db_user_by_id($announce['user_id']); } $survey_vote = array('survey_vote_id' => 1); $survey_complete = strtotime($announce['survey_until']) < SN_TIME_NOW; if ($announce['survey_id'] && !empty($user['id'])) { $survey_vote = !$survey_complete ? $survey_vote = doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$announce['survey_id']} AND survey_vote_user_id = {$user['id']} LIMIT 1;", true) : array(); } $announce_exploded = explode("<br /><br />", cht_message_parse($announce['strAnnounce'], false, intval($announce['authlevel']))); $template->assign_block_vars('announces', array('ID' => $announce['idAnnounce'], 'TIME' => date(FMT_DATE_TIME, $announce['unix_time'] + SN_CLIENT_TIME_DIFF), 'ANNOUNCE' => cht_message_parse($announce['strAnnounce'], false, intval($announce['authlevel'])), 'DETAIL_URL' => $announce['detail_url'], 'USER_NAME' => isset($users[$announce['user_id']]) && $users[$announce['user_id']] ? player_nick_render_to_html($users[$announce['user_id']], array('color' => true)) : js_safe_string($announce['user_name']), 'NEW' => $announce['unix_time'] + $config->game_news_actual >= SN_TIME_NOW, 'FUTURE' => $announce['unix_time'] > SN_TIME_NOW, 'SURVEY_ID' => $announce['survey_id'], 'SURVEY_TEXT' => $announce['survey_question'], 'SURVEY_CAN_VOTE' => empty($survey_vote) && !$survey_complete, 'SURVEY_COMPLETE' => $survey_complete, 'SURVEY_UNTIL' => $announce['survey_until'])); foreach ($announce_exploded as $announce_paragraph) { $template->assign_block_vars('announces.paragraph', array('TEXT' => $announce_paragraph)); } if ($announce['survey_id']) { $survey_query = doquery("SELECT survey_answer_text AS `TEXT`, count(DISTINCT survey_vote_id) AS `VOTES`\n FROM `{{survey_answers}}` AS sa\n LEFT JOIN `{{survey_votes}}` AS sv ON sv.survey_parent_answer_id = sa.survey_answer_id\n WHERE sa.survey_parent_id = {$announce['survey_id']}\n GROUP BY survey_answer_id\n ORDER BY survey_answer_id;"); $survey_vote_result = array(); $total_votes = 0; while ($row = db_fetch($survey_query)) { $survey_vote_result[] = $row; $total_votes += $row['VOTES']; } if (empty($survey_vote) && !$survey_complete) { // Can vote $survey_query = doquery("SELECT * FROM {{survey_answers}} WHERE survey_parent_id = {$announce['survey_id']} ORDER BY survey_answer_id;"); while ($row = db_fetch($survey_query)) { $template->assign_block_vars('announces.survey_answers', array('ID' => $row['survey_answer_id'], 'TEXT' => $row['survey_answer_text'])); } } else { // Show result foreach ($survey_vote_result as &$vote_result) { $vote_percent = $total_votes ? $vote_result['VOTES'] / $total_votes * 100 : 0; $vote_result['PERCENT'] = $vote_percent; $vote_result['PERCENT_TEXT'] = round($vote_percent, 1); $vote_result['VOTES'] = pretty_number($vote_result['VOTES']); $template->assign_block_vars('announces.survey_votes', $vote_result); } } // Dirty hack $template->assign_block_vars('announces.total_votes', array('TOTAL_VOTES' => $total_votes)); } } }
function sn_chat_msg_view($template = null) { global $config, $skip_fleet_update, $user, $lang; define('IN_AJAX', true); $skip_fleet_update = true; $history = sys_get_param_str('history'); if (!$history) { $config->array_set('users', $user['id'], 'chat_last_refresh', SN_TIME_MICRO); } $page = 0; $last_message = ''; $alliance = 0; $template_result['.']['chat'] = array(); if (!$history && $config->_MODE != CACHER_NO_CACHE && $config->chat_timeout && SN_TIME_MICRO - $config->array_get('users', $user['id'], 'chat_last_activity') > $config->chat_timeout) { $result['disable'] = true; $template_result['.']['chat'][] = array('TIME' => date(FMT_DATE_TIME, htmlentities(SN_CLIENT_TIME_LOCAL, ENT_QUOTES, 'utf-8')), 'DISABLE' => true); } else { $alliance = sys_get_param_str('ally') && $user['ally_id'] ? $user['ally_id'] : 0; $page_limit = 20; // Chat rows Limit $where_add = ''; $last_message = 0; if ($history) { $rows = doquery("SELECT count(1) AS CNT FROM {{chat}} WHERE ally_id = '{$alliance}';", true); $page_count = ceil($rows['CNT'] / $page_limit); for ($i = 0; $i < $page_count; $i++) { $template_result['.']['page'][] = array('NUMBER' => $i); } $page = min($page_count, max(0, sys_get_param_int('sheet'))); } else { $last_message = sys_get_param_id('last_message'); $where_add = $last_message ? "AND `messageid` > {$last_message}" : ''; } $start_row = $page * $page_limit; $query = doquery("SELECT c.*, u.authlevel\n FROM\n {{chat}} AS c\n LEFT JOIN {{users}} AS u ON u.id = c.chat_message_sender_id\n WHERE c.chat_message_recipient_id IS NULL AND c.ally_id = '{$alliance}' {$where_add} ORDER BY messageid DESC LIMIT {$start_row}, {$page_limit};"); while ($chat_row = db_fetch($query)) { // Little magik here - to retain HTML codes from DB and stripping HTML codes from nick $chat_row['user'] = player_nick_render_to_html($chat_row['user']); $nick_stripped = htmlentities(strip_tags($chat_row['user']), ENT_QUOTES, 'utf-8'); $nick = str_replace(strip_tags($chat_row['user']), $nick_stripped, $chat_row['user']); if (!$history) { $nick = "<span style=\"cursor: pointer;\" onclick=\"addSmiley('({$nick_stripped})');\">{$nick}</span>"; } $template_result['.']['chat'][] = array('TIME' => cht_message_parse(date(FMT_DATE_TIME, $chat_row['timestamp'] + SN_CLIENT_TIME_DIFF)), 'NICK' => $nick, 'TEXT' => cht_message_parse($chat_row['message'], false, intval($chat_row['authlevel']))); $last_message = max($last_message, $chat_row['messageid']); } } $template_result['.']['chat'] = array_reverse($template_result['.']['chat']); $template_result += array('PAGE' => $page, 'ALLY' => $alliance, 'HISTORY' => $history); $template = gettemplate('chat_messages', $template); $template->assign_recursive($template_result); if ($history) { display($template, "{$lang['chat_history']} - {$lang[$alliance ? 'chat_ally' : 'chat_common']}", true, '', false, true); } else { $result['last_message'] = $last_message; ob_start(); displayP($template); $result['html'] = ob_get_contents(); ob_end_clean(); print json_encode($result); } die; }