Exemplo n.º 1
0
function search($option)
{
    global $mainframe, $mtconf;
    $database =& JFactory::getDBO();
    $search_text = JRequest::getVar('search_text', '', 'post');
    $search_where = JRequest::getInt('search_where', 0, 'post');
    // 1: Listing, 2: Category
    $limit = $mainframe->getUserStateFromRequest("viewlistlimit", 'limit', $mtconf->getjconf('list_limit'));
    $limitstart = $mainframe->getUserStateFromRequest("viewcli{$option}limitstart", 'limitstart', 0);
    # Detect search command
    # Quick Go
    $id_found = 0;
    if (substr($search_text, 0, 3) == "id:") {
        $temp = explode(":", $search_text);
        if (is_numeric($temp[1])) {
            $id_found = $temp[1];
        }
    }
    # Search query
    if ($search_where == 1) {
        if ($id_found) {
            $link = new mtLinks($database);
            $link->load($id_found);
            if (!empty($link->link_name)) {
                $mainframe->redirect("index2.php?option=com_mtree&task=editlink&link_id=" . $id_found);
            } else {
                $mainframe->redirect("index2.php?option=com_mtree", JText::_('Your search does not return any result'));
            }
        } else {
            // Total Results
            $database->setQuery('SELECT COUNT(*) FROM #__mt_links ' . "\nWHERE link_name LIKE '%" . $database->getEscaped($search_text, true) . "%'");
            $total = $database->loadResult();
            // Page Navigation
            jimport('joomla.html.pagination');
            $pageNav = new JPagination($total, $limitstart, $limit);
            // Links
            $database->setQuery("SELECT l.*, COUNT(r.rev_id) AS reviews FROM #__mt_links AS l" . "\nLEFT JOIN #__mt_reviews AS r ON r.link_id = l.link_id" . "\nWHERE l.link_name LIKE '%" . $database->getEscaped($search_text, true) . "%'" . "\nGROUP BY l.link_id" . "\nORDER BY l.link_name ASC" . "\nLIMIT " . $pageNav->limitstart . ', ' . $pageNav->limit);
        }
    } else {
        if ($id_found) {
            $cat = new mtCats($database);
            $cat->load($id_found);
            if (!empty($cat->cat_name)) {
                $mainframe->redirect("index2.php?option=com_mtree&task=editcat&cat_id=" . $id_found);
            } else {
                $mainframe->redirect("index2.php?option=com_mtree", JText::_('Your search does not return any result'));
            }
        } else {
            // Total Results
            $database->setQuery("SELECT COUNT(*) FROM #__mt_cats WHERE cat_name LIKE '%" . $database->getEscaped($search_text, true) . "%'");
            $total = $database->loadResult();
            // Page Navigation
            jimport('joomla.html.pagination');
            $pageNav = new JPagination($total, $limitstart, $limit);
            // Categories
            $database->setQuery("SELECT * FROM #__mt_cats WHERE cat_name LIKE '%" . $database->getEscaped($search_text, true) . "%' ORDER BY cat_name ASC LIMIT {$pageNav->limitstart}, {$pageNav->limit}");
        }
    }
    $results = $database->loadObjectList();
    # Get Pathway
    $pathWay = new mtPathWay();
    # Results Output
    if ($search_where == 1) {
        // Links
        HTML_mtree::searchresults_links($results, $pageNav, $pathWay, $search_where, $search_text, $option);
    } else {
        // Categories
        HTML_mtree::searchresults_categories($results, $pageNav, $pathWay, $search_where, $search_text, $option);
    }
}