Ejemplo n.º 1
0
function advsearch2($option)
{
    global $savantConf, $Itemid, $mtconf;
    $database =& JFactory::getDBO();
    $document =& JFactory::getDocument();
    require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'mfields.class.php';
    require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'mAdvancedSearch.class.php';
    $document->setTitle(JText::_('Advanced search results'));
    # Load up search ID if available
    $search_id = JRequest::getInt('search_id', 0);
    if ($search_id > 0) {
        $database->setQuery('SELECT search_text FROM #__mt_searchlog WHERE search_id = ' . $database->quote($search_id));
        $post = unserialize($database->loadResult());
    } else {
        $post = JRequest::get('post');
    }
    # Load all published CORE & custom fields
    $database->setQuery("SELECT cf.*, '0' AS link_id, '' AS value, '0' AS attachment, ft.ft_class FROM #__mt_customfields AS cf " . "\nLEFT JOIN #__mt_fieldtypes AS ft ON ft.field_type=cf.field_type" . "\nWHERE cf.published='1' ORDER BY ordering ASC");
    $fields = new mFields($database->loadObjectList());
    $searchParams = $fields->loadSearchParams($post);
    $advsearch = new mAdvancedSearch($database);
    if (intval($post['searchcondition']) == 2) {
        $advsearch->useAndOperator();
    } else {
        $advsearch->useOrOperator();
    }
    # Search Category
    $search_cat = intval($post['cat_id']);
    $only_subcats_sql = '';
    if ($search_cat > 0 && is_int($search_cat)) {
        $mtCats = new mtCats($database);
        $subcats = $mtCats->getSubCats_Recursive($search_cat, true);
        $subcats[] = $search_cat;
        if (!empty($subcats)) {
            $advsearch->limitToCategory($subcats);
        }
    }
    $fields->resetPointer();
    while ($fields->hasNext()) {
        $field = $fields->getField();
        $searchFields = $field->getSearchFields();
        if (isset($searchFields[0]) && isset($searchParams[$searchFields[0]]) && $searchParams[$searchFields[0]] != '') {
            foreach ($searchFields as $searchField) {
                $searchFieldValues[] = $searchParams[$searchField];
            }
            if (!empty($searchFieldValues) && $searchFieldValues[0] != '') {
                if (is_array($searchFieldValues[0]) && empty($searchFieldValues[0][0])) {
                    // Do nothing
                } else {
                    $tmp_where_cond = call_user_func_array(array($field, 'getWhereCondition'), $searchFieldValues);
                    if (!is_null($tmp_where_cond)) {
                        $advsearch->addCondition($field, $searchFieldValues);
                    }
                }
            }
            unset($searchFieldValues);
        }
        $fields->next();
    }
    $limit = JRequest::getInt('limit', $mtconf->get('fe_num_of_searchresults'), 'get');
    $limitstart = JRequest::getInt('limitstart', 0, 'get');
    if ($limitstart < 0) {
        $limitstart = 0;
    }
    $advsearch->search(1, 1);
    // Total Results
    $total = $advsearch->getTotal();
    if ($search_id <= 0 && $total > 0) {
        # Store search for later retrieval.
        if ($search_id < 1) {
            $database->setQuery("INSERT INTO #__mt_searchlog (search_text) VALUES ('" . serialize($post) . "')");
            if (!$database->query()) {
                echo $database->getErrorMsg();
            }
        }
        # Get the above search ID
        $database->setQuery("SELECT search_id FROM #__mt_searchlog WHERE search_text ='" . serialize($post) . "'");
        $database->query();
        $search_id = $database->loadResult();
        $document->addCustomTag('<meta http-equiv="Refresh" content="1; URL=' . JRoute::_("index.php?option=com_mtree&task=advsearch2&search_id={$search_id}&Itemid={$Itemid}") . '">');
        # Savant template
        $savant = new Savant2($savantConf);
        $savant->assign('redirect_url', JRoute::_("index.php?option=com_mtree&task=advsearch2&search_id={$search_id}&Itemid={$Itemid}"));
        $savant->display('page_advSearchRedirect.tpl.php');
    } else {
        $links = $advsearch->loadResultList($limitstart, $limit);
        # Page Navigation
        jimport('joomla.html.pagination');
        $pageNav = new JPagination($total, $limitstart, $limit);
        # Pathway
        $pathWay = new mtPathWay();
        # Savant template
        $savant = new Savant2($savantConf);
        assignCommonListlinksVar($savant, $links, $pathWay, $pageNav);
        $savant->assign('search_id', $search_id);
        $savant->display('page_advSearchResults.tpl.php');
    }
}
Ejemplo n.º 2
0
function advsearch2($option)
{
    global $mtconf;
    $database =& JFactory::getDBO();
    $post = JRequest::get('post');
    $search_where = JRequest::getVar('search_where', '', 'post');
    // 1: Listing, 2: Category
    $limit = JRequest::getInt('limit', 15);
    $limitstart = JRequest::getInt('limitstart', 0);
    $owner = JRequest::getVar('owner', '', 'post');
    $searchParams = array();
    # Load all published CORE & custom fields
    $database->setQuery("SELECT cf.*, '0' AS link_id, '' AS value, '0' AS attachment, ft.ft_class FROM #__mt_customfields AS cf " . "\nLEFT JOIN #__mt_fieldtypes AS ft ON ft.field_type=cf.field_type" . "\nWHERE cf.published='1' ORDER BY ordering ASC");
    $fields = new mFields($database->loadObjectList());
    $searchParams = $fields->loadSearchParams($post);
    foreach (array('publishing', 'link_template', 'link_rating', 'rating_2', 'link_votes', 'votes_2', 'link_hits', 'hits_2', 'reviews', 'reviews_2', 'internal_notes', 'metakey', 'metadesc', 'price_2', 'price') as $otherField) {
        $searchParams[$otherField] = JRequest::getVar($otherField, '', 'post');
    }
    # Search query
    if ($search_where == 1) {
        $where = array();
        $having = '';
        $advsearch = new mAdvancedSearch($database);
        if (JRequest::getInt('searchcondition', 1, 'post') == '2') {
            $advsearch->useAndOperator();
        } else {
            $advsearch->useOrOperator();
        }
        $fields->resetPointer();
        while ($fields->hasNext()) {
            $field = $fields->getField();
            $searchFields = $field->getSearchFields();
            if (array_key_exists(0, $searchFields) && isset($searchParams[$searchFields[0]]) && !empty($searchParams[$searchFields[0]])) {
                foreach ($searchFields as $searchField) {
                    if (isset($searchParams[$searchField])) {
                        $searchFieldValues[] = $searchParams[$searchField];
                    }
                }
                if (count($searchFieldValues) > 0 && !empty($searchFieldValues[0])) {
                    if (is_array($searchFieldValues[0]) && empty($searchFieldValues[0][0])) {
                        // Do nothing
                    } else {
                        $tmp_where_cond = call_user_func_array(array($field, 'getWhereCondition'), $searchFieldValues);
                        if (!is_null($tmp_where_cond)) {
                            $advsearch->addCondition($field, $searchFieldValues);
                        }
                    }
                }
                unset($searchFieldValues);
            }
            $fields->next();
        }
        if (!empty($searchParams['metadesc'])) {
            $advsearch->addRawCondition('metadesc LIKE \'%' . $database->getEscaped($searchParams['metadesc'], true) . '%\'');
        }
        if (!empty($searchParams['metakey'])) {
            $advsearch->addRawCondition('metakey LIKE \'%' . $database->getEscaped($searchParams['metakey'], true) . '%\'');
        }
        if (!empty($searchParams['internal_notes'])) {
            $advsearch->addRawCondition('internal_notes LIKE \'%' . $database->getEscaped($searchParams['internal_notes']) . '%\'');
        }
        if (!empty($searchParams['link_template'])) {
            $advsearch->addRawCondition('link_template = ' . $database->quote($searchParams['link_template']));
        }
        if (is_numeric($searchParams['link_rating']) && $searchParams['link_rating'] >= 0 && $searchParams['link_rating'] <= 5) {
            switch ($searchParams['rating_2']) {
                case 1:
                    $advsearch->addRawCondition('link_rating = ' . $database->quote($searchParams['link_rating']));
                    break;
                case 2:
                    $advsearch->addRawCondition('link_rating > ' . $database->quote($searchParams['link_rating']));
                    break;
                case 3:
                    $advsearch->addRawCondition('link_rating < ' . $database->quote($searchParams['link_rating']));
                    break;
            }
        }
        // votes
        if (is_numeric($searchParams['link_votes']) && $searchParams['link_votes'] >= 0) {
            switch ($searchParams['votes_2']) {
                case 1:
                    $advsearch->addRawCondition('link_votes = ' . $database->quote($searchParams['link_votes']));
                    break;
                case 2:
                    $advsearch->addRawCondition('link_votes > ' . $database->quote($searchParams['link_votes']));
                    break;
                case 3:
                    $advsearch->addRawCondition('link_votes < ' . $database->quote($searchParams['link_votes']));
                    break;
            }
        }
        // hits
        if (is_numeric($searchParams['link_hits']) && $searchParams['link_hits'] >= 0) {
            switch ($searchParams['hits_2']) {
                case 1:
                    $advsearch->addRawCondition('link_hits = ' . $database->quote($searchParams['link_hits']));
                    break;
                case 2:
                    $advsearch->addRawCondition('link_hits > ' . $database->quote($searchParams['link_hits']));
                    break;
                case 3:
                    $advsearch->addRawCondition('link_hits < ' . $database->quote($searchParams['link_hits']));
                    break;
            }
        }
        // price
        if (is_numeric($searchParams['price']) && $searchParams['price'] >= 0) {
            switch ($searchParams['price_2']) {
                case 1:
                    $advsearch->addRawCondition('price = ' . $database->quote($searchParams['price']));
                    break;
                case 2:
                    $advsearch->addRawCondition('price > ' . $database->quote($searchParams['price']));
                    break;
                case 3:
                    $advsearch->addRawCondition('price < ' . $database->quote($searchParams['price']));
                    break;
            }
        }
        $jdate = JFactory::getDate();
        $now = $jdate->toMySQL();
        $nullDate = $database->getNullDate();
        switch ($searchParams['publishing']) {
            case 2:
                // Published
                $advsearch->addRawCondition("( (publish_up = " . $database->Quote($nullDate) . " OR publish_up <= '{$now}')  AND " . "(publish_down = " . $database->Quote($nullDate) . " OR publish_down >= '{$now}') AND " . "link_published = '1' )");
                break;
            case 3:
                // Unpublished
                $advsearch->addRawCondition("link_published = '0'");
                break;
            case 4:
                // Pending
                $advsearch->addRawCondition("( (publish_up => '{$now}' OR publish_up = " . $database->Quote($nullDate) . ") AND link_published = '1' )");
                break;
            case 5:
                // Expired
                $advsearch->addRawCondition("( publish_down < '{$now}' AND link_published = '1' )");
                break;
            case 6:
                // Pending Listing, waiting for approval
                $advsearch->addRawCondition("link_approved <= 0");
                break;
        }
        # Check if this owner exists
        # Lookup owner's userid. Return error if does not exists
        if (!empty($owner)) {
            $database->setQuery('SELECT id FROM #__users WHERE username ='******'l.user_id = ' . $database->quote($owner_id));
            } else {
                echo "<script> alert('" . JText::_('Invalid owner select again') . "'); window.history.go(-1); </script>\n";
                exit;
            }
        }
        $advsearch->search();
        // Total Results
        $total = $advsearch->getTotal();
        // Links
        $where[] = "cl.main = '1'";
        $where[] = "cl.link_id = l.link_id";
    } else {
        // Total Results
        $database->setQuery("SELECT COUNT(*) FROM #__mt_cats WHERE cat_name LIKE '%" . $database->getEscaped($search_text, true) . "%'");
        $total = $database->loadResult();
        // Categories
        $database->setQuery("SELECT * FROM #__mt_cats WHERE cat_name LIKE '%" . $database->getEscaped($search_text, true) . "%' ORDER BY cat_name ASC LIMIT {$limitstart}, {$limit}");
    }
    $results = $advsearch->loadResultList($limitstart, $limit);
    # Page Navigation
    jimport('joomla.html.pagination');
    $pageNav = new JPagination($total, $limitstart, $limit);
    # Get Pathway
    $pathWay = new mtPathWay();
    # Results Output
    // /*
    if ($search_where == 1) {
        // Links
        HTML_mtree::advsearchresults_links($results, $fields, $pageNav, $pathWay, $search_where, $option);
    } else {
        // Categories
        HTML_mtree::searchresults_categories($results, $pageNav, $pathWay, $search_where, $option);
    }
    // */
}