Example #1
0
function index()
{
    global $path;
    global $template;
    $conditionspre = '';
    $conditionspost = '';
    $conditionsselect = '';
    $extratitle = '';
    $orderby = "newest";
    $order = 'created desc';
    $defaultorder = 1;
    $nopagination = 0;
    $page = 1;
    $searchstringoriginal = '';
    if (!empty($_GET['search'])) {
        $searchstringoriginal = sanitize($_GET['search'], "string");
    }
    if (!empty($_GET['order'])) {
        if ($_GET['order'] == "votes") {
            $order = "votes desc";
            $orderby = "votes";
            $defaultorder = 0;
        } else {
            if ($_GET['order'] == "oldest") {
                $order = "created asc";
                $orderby = "oldest";
                $defaultorder = 0;
            } else {
                if ($_GET['order'] == "relevance") {
                    $order = "score desc";
                    $orderby = "relevance";
                    $defaultorder = 0;
                } else {
                    if ($_GET['order'] == "newest") {
                        $defaultorder = 0;
                    }
                }
            }
        }
    }
    if (!empty($_GET['page'])) {
        $page = sanitize($_GET['page'], "int");
    }
    $type = '';
    if (!empty($_GET['type'])) {
        $type = "&type=" . sanitize($_GET['type'], "string");
        if (sanitize($_GET['type'], "string") == "unanswered") {
            //	$conditionspost .= " questions.id NOT IN (select questions.id from questions,answers where questions.id = answers.questionid and answers.accepted = 1) and ";
            $conditionspost .= " questions.accepted = 0 and questions.kb = 0 and ";
            $extratitle = " " . _("not yet answered");
        } else {
            $extratitle = " " . _("active");
            $order = " updated desc ";
            $nopagination = 1;
        }
    }
    $template->set('nopagination', $nopagination);
    $search = '';
    $searchstring = urldecode($searchstringoriginal);
    if (!empty($searchstringoriginal)) {
        $search = "&search=" . urlencode($searchstring);
        $conditionspost .= " MATCH(title, description) AGAINST ('" . escape($searchstring) . "') and ";
        $conditionsselect .= ",MATCH(title, description) AGAINST ('" . escape($searchstring) . "') AS score  ";
        $extratitle = " " . _("showing") . " " . $searchstring;
        if ($defaultorder == 1) {
            $orderby = "relevance";
            $order = 'score desc';
        }
    }
    $template->set('searchstring', $searchstring);
    $tag = '';
    if (!empty($_GET['tag'])) {
        $tag = "&tag=" . createSlug($_GET['tag']);
        $conditionspre .= ",tags_questions, tags";
        $conditionspost .= " tags_questions.questionid = questions.id and tags.id = tags_questions.tagid and tags.tag LIKE '" . escape(createSlug($_GET['tag'])) . "' and ";
        $extratitle = " " . _("tagged") . " " . createSlug($_GET['tag']);
    }
    $offset = ($page - 1) * QUESTIONS_PER_PAGE;
    $sql = "select count(questions.id) count from questions {$conditionspre} WHERE {$conditionspost} 1";
    $query = mysql_query($sql);
    $result = mysql_fetch_array($query);
    $template->set('questionscount', $result['count']);
    $paging = new Pagination();
    $paging->set('urlscheme', '?order=' . $orderby . $tag . $type . $search . '&page=%page%');
    $paging->set('perpage', QUESTIONS_PER_PAGE);
    $paging->set('page', $page);
    $paging->set('total', $result['count']);
    $paging->set('order', $orderby);
    $paging->set('search', $search);
    $template->set('pagination', $paging->display());
    $paging->set('urlscheme', '?order=%label%' . $tag . $type . $search . '&page=1');
    $template->set('orderOptions', $paging->displayOptions());
    $template->set('extratitle', $extratitle);
    $sql = "select questions.* {$conditionsselect} from questions {$conditionspre} WHERE {$conditionspost} 1 order by {$order}, created desc LIMIT " . QUESTIONS_PER_PAGE . " OFFSET {$offset}";
    $query = mysql_query($sql);
    $questions = array();
    while ($result = mysql_fetch_array($query)) {
        $sql_nest = "select tag from tags_questions, tags where questionid = '" . escape($result['id']) . "' and tags.id = tags_questions.tagid order by tag";
        $query_nest = mysql_query($sql_nest);
        $tags = array();
        while ($result_nest = mysql_fetch_array($query_nest)) {
            $tags[] = $result_nest['tag'];
        }
        $description = truncate(trim(sanitize(Markdown($result['description']), "string")));
        if (!empty($searchstring)) {
            $description = highlight(excerpt(trim(sanitize(Markdown($result['description']), "string")), $searchstring), $searchstring);
            $result['title'] = highlight($result['title'], $searchstring);
        }
        $questions[] = array("title" => $result['title'], "created" => $result['created'], "updated" => $result['updated'], "userid" => $result['userid'], "link" => $result['link'], "slug" => $result['slug'], "answers" => $result['answers'], "accepted" => $result['accepted'], "kb" => $result['kb'], "votes" => $result['votes'], "id" => $result['id'], "tags" => $tags, "description" => $description);
    }
    $template->set('questions', $questions);
}