if ($db->isError()) { $smarty->assign('tpl_content', 'content-error.tpl.html'); $smarty->assign('error', 'Database error has occured: ' . $db->getError()); $smarty->display('index.tpl.html'); exit; } $total_pages = ceil($total_items / $ITEMS_PER_SITE_PAGE); $current_page = isset($HandlerMatches[2]) ? $HandlerMatches[2] : 1; if ($current_page > $total_pages) { $current_page = 1; } # Fetch ITEMS_PER_INDEX_PAGE for each site # $item_offset = $ITEMS_PER_SITE_PAGE * ($current_page - 1); $item_query = "SELECT " . join(',', $ITEM_FIELDS) . " FROM items " . "WHERE site_id = {$site['id']} AND visible = 1 " . "ORDER BY date_added DESC, id DESC LIMIT {$item_offset}, {$ITEMS_PER_SITE_PAGE}"; $items = $db->fetchAllQueryAssoc($item_query, 'id'); if ($db->isError()) { $smarty->assign('tpl_content', 'content-error.tpl.html'); $smarty->assign('error', 'Database error has occured: ' . $db->getError()); $smarty->display('index.tpl.html'); exit; } $site['items'] = $items; # Get comment count for each item. # Use a single query to get the count for all items. # $item_ids = array(); foreach ($items as $item_idx => $item) { array_push($item_ids, $items[$item_idx]['id']); # Initially set comment count to 0, and in the next piece of code # set the correct comment count by querying database.
$smarty->assign('comment_error', $comment_error); $smarty->assign('existing_comment', isset($_POST['comment']) ? $_POST['comment'] : ''); } if (!$comment_error) { $add_comment_query = "INSERT INTO comments (comment, item_id, user_id, anonymous_name, date_added, ip_address) VALUES " . "('{$escaped_comment}', {$item['id']}, {$user_id}, '{$anonymous_name}', {$time_now}, '{$_SERVER['REMOTE_ADDR']}')"; if (!$db->query($add_comment_query)) { $comment_error = "There was a database error while adding your comment: " . $db->getError() . "! Sorry..."; $smarty->assign('comment_error', $comment_error); $smarty->assign('existing_comment', isset($_POST['comment']) ? $_POST['comment'] : ''); } } } # Get comments for this item # $comments_query = "SELECT c.id id, c.comment comment, c.date_added date_added, c.anonymous_name as anonymous_name, u.username username FROM comments c " . "LEFT JOIN users u ON c.user_id = u.id WHERE c.item_id = {$item['id']} ORDER BY date_added ASC"; $comments = $db->fetchAllQueryAssoc($comments_query); if ($db->isError()) { $smarty->assign('tpl_content', 'content-error.tpl.html'); $smarty->assign('error', 'Database error has occured: ' . $db->getError()); $smarty->display('index.tpl.html'); exit; } $item['comments']['count'] = count($comments); # Prepare human readable time difference between current time and comment time # function human_time_diff($date) { $time_diff = time() - $date; if ($time_diff < 3600) { $minutes = ceil($time_diff / 60); $human_diff = "{$minutes} minute" . ($minutes != 1 ? 's' : '');
exit; } require_once 'mysmarty.php'; require_once 'system/db.sqlite.php'; define('PAGE_NAME', 'page-index'); define('CACHE_TIME', 60); $unique_page_name = PAGE_NAME; $smarty = new MySmarty(); if ($smarty->is_cached('index.tpl.html', $unique_page_name)) { $smarty->display('index.tpl.html', $unique_page_name); exit; } $db = new SQLite($SQLITE_DB_PATH); # find the sites to display # $sites = $db->fetchAllQueryAssoc('SELECT id, name, sane_name, url FROM sites WHERE visible = 1 ORDER BY priority ASC'); if ($db->isError()) { $smarty->assign('tpl_content', 'content-error.tpl.html'); $smarty->assign('error', 'Database error has occured: ' . $db->getError()); # TODO: figure out how not to cache a page $smarty->display('index.tpl.html'); exit; } # Fetch ITEMS_PER_INDEX_PAGE for each site # foreach ($sites as $site_idx => $site) { $item_query = "SELECT " . join(',', $ITEM_FIELDS) . " FROM items " . "WHERE site_id = {$site['id']} AND visible = 1 " . "ORDER BY date_added DESC, id DESC LIMIT {$ITEMS_PER_INDEX_PAGE}"; $items = $db->fetchAllQueryAssoc($item_query, 'id'); if ($db->isError()) { $smarty->assign('tpl_content', 'content-error.tpl.html'); $smarty->assign('error', 'Database error has occured: ' . $db->getError());