Example #1
0
function plgSearchItems($text, $phrase = '', $ordering = '', $areas = null)
{
    $mainframe =& JFactory::getApplication();
    $db =& JFactory::getDBO();
    $user =& JFactory::getUser();
    $aid = $user->get('aid');
    require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_search' . DS . 'helpers' . DS . 'search.php';
    require_once JPATH_SITE . DS . 'components' . DS . 'com_k2' . DS . 'helpers' . DS . 'route.php';
    $searchText = $text;
    if (is_array($areas)) {
        if (!array_intersect($areas, array_keys(plgSearchItemsAreas()))) {
            return array();
        }
    }
    $plugin =& JPluginHelper::getPlugin('search', 'k2');
    $pluginParams = new JParameter($plugin->params);
    $limit = $pluginParams->def('search_limit', 50);
    $text = trim($text);
    if ($text == '') {
        return array();
    }
    $rows = array();
    if ($limit > 0) {
        $query = "SELECT i.title AS title,\n\t    i.metadesc, \n\t    i.metakey, \n\t    c.name as section, \n\t    i.image_caption, \n\t    i.image_credits, \n\t    i.video_caption, \n\t    i.video_credits, \n\t    i.extra_fields_search,";
        if ($pluginParams->get('search_tags')) {
            $query .= " tags.name as tag,";
        }
        $query .= " i.created,\n    \tCONCAT(i.introtext, i.fulltext) AS text, \n    \tCASE WHEN CHAR_LENGTH(i.alias) THEN CONCAT_WS(':', i.id, i.alias) ELSE i.id END as slug, \n    \tCASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug  \n    \tFROM #__k2_items AS i INNER JOIN #__k2_categories AS c ON c.id=i.catid AND c.access <= " . $user->get('gid');
        if ($pluginParams->get('search_tags')) {
            $query .= " LEFT JOIN #__k2_tags_xref tags_xref ON tags_xref.itemID = i.id";
            $query .= " LEFT JOIN #__k2_tags tags ON tags.id = tags_xref.tagID";
        }
        $query .= " WHERE MATCH(i.title, i.introtext, i.`fulltext`";
        if ($pluginParams->get('search_tags')) {
            $query .= ",tags.name";
        }
        $query .= ",i.extra_fields_search,i.image_caption,i.image_credits,i.video_caption,i.video_credits,i.metadesc,i.metakey)";
        if ($phrase == 'exact') {
            $text = $db->Quote('"' . $db->getEscaped($text, true) . '"', false);
            $query .= " AGAINST ({$text}  IN BOOLEAN MODE)";
        } else {
            $text = $db->Quote($db->getEscaped($text, true), false);
            $query .= " AGAINST ({$text})";
        }
        $query .= " AND i.trash = 0\n\t    AND i.published = 1 \n\t    AND i.access <= " . $aid . " \n\t    AND c.published = 1 \n\t    AND c.access <= " . $aid . "\n\t    AND c.trash = 0\n\t    GROUP BY i.id ";
        switch ($ordering) {
            case 'oldest':
                $query .= 'ORDER BY i.created ASC';
                break;
            case 'popular':
                $query .= 'ORDER BY i.hits DESC';
                break;
            case 'alpha':
                $query .= 'ORDER BY i.title ASC';
                break;
            case 'category':
                $query .= 'ORDER BY c.name ASC, i.title ASC';
                break;
            case 'newest':
            default:
                $query .= 'ORDER BY i.created DESC';
                break;
        }
        $db->setQuery($query, 0, $limit);
        $list = $db->loadObjectList();
        $limit -= count($list);
        if (isset($list)) {
            foreach ($list as $key => $item) {
                $list[$key]->href = JRoute::_(K2HelperRoute::getItemRoute($item->slug, $item->catslug));
            }
        }
        $rows[] = $list;
    }
    $results = array();
    if (count($rows)) {
        foreach ($rows as $row) {
            $new_row = array();
            foreach ($row as $key => $item) {
                $item->browsernav = '';
                $check = array('text', 'title', 'metakey', 'metadesc', 'section', 'image_caption', 'image_credits', 'video_caption', 'video_credits', 'extra_fields_search');
                if ($pluginParams->get('search_tags')) {
                    $check[] = 'tag';
                }
                if (searchHelper::checkNoHTML($item, $searchText, array('text', 'title', 'metakey', 'metadesc', 'section', 'tag', 'image_caption', 'image_credits', 'video_caption', 'video_credits', 'extra_fields_search'))) {
                    $new_row[] = $item;
                }
            }
            $results = array_merge($results, (array) $new_row);
        }
    }
    return $results;
}
Example #2
0
function plgSearchItems($text, $phrase = '', $ordering = '', $areas = null)
{
    $mainframe =& JFactory::getApplication();
    $db =& JFactory::getDBO();
    $jnow =& JFactory::getDate();
    $now = $jnow->toMySQL();
    $nullDate = $db->getNullDate();
    $user =& JFactory::getUser();
    $access = $user->get('aid');
    $tagIDs = array();
    $itemIDs = array();
    require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_search' . DS . 'helpers' . DS . 'search.php';
    require_once JPATH_SITE . DS . 'components' . DS . 'com_k2' . DS . 'helpers' . DS . 'route.php';
    $searchText = $text;
    if (is_array($areas)) {
        if (!array_intersect($areas, array_keys(plgSearchItemsAreas()))) {
            return array();
        }
    }
    $plugin =& JPluginHelper::getPlugin('search', 'k2');
    $pluginParams = new JParameter($plugin->params);
    $limit = $pluginParams->def('search_limit', 50);
    $text = JString::trim($text);
    if ($text == '') {
        return array();
    }
    $rows = array();
    if ($limit > 0) {
        if ($pluginParams->get('search_tags')) {
            $tagQuery = JString::str_ireplace('*', '', $text);
            $words = explode(' ', $tagQuery);
            for ($i = 0; $i < count($words); $i++) {
                $words[$i] .= '*';
            }
            $tagQuery = implode(' ', $words);
            $tagQuery = $db->Quote($db->getEscaped($tagQuery, true), false);
            $query = "SELECT id FROM #__k2_tags WHERE MATCH(name) AGAINST ({$tagQuery} IN BOOLEAN MODE) AND published=1";
            $db->setQuery($query);
            $tagIDs = $db->loadResultArray();
            if (count($tagIDs)) {
                JArrayHelper::toInteger($tagIDs);
                $query = "SELECT itemID FROM #__k2_tags_xref WHERE tagID IN (" . implode(',', $tagIDs) . ")";
                $db->setQuery($query);
                $itemIDs = $db->loadResultArray();
            }
        }
        if ($phrase == 'exact') {
            $text = JString::trim($text, '"');
            $text = $db->Quote('"' . $db->getEscaped($text, true) . '"', false);
        } else {
            $text = JString::str_ireplace('*', '', $text);
            $words = explode(' ', $text);
            for ($i = 0; $i < count($words); $i++) {
                if ($phrase == 'all') {
                    $words[$i] = '+' . $words[$i];
                }
                $words[$i] .= '*';
            }
            $text = implode(' ', $words);
            $text = $db->Quote($db->getEscaped($text, true), false);
        }
        $query = "\n\t\tSELECT i.title AS title,\n\t    i.metadesc,\n\t    i.metakey,\n\t    c.name as section,\n\t    i.image_caption,\n\t    i.image_credits,\n\t    i.video_caption,\n\t    i.video_credits,\n\t    i.extra_fields_search,\n\t    i.created,\n    \tCONCAT(i.introtext, i.fulltext) AS text,\n    \tCASE WHEN CHAR_LENGTH(i.alias) THEN CONCAT_WS(':', i.id, i.alias) ELSE i.id END as slug,\n    \tCASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug\n    \tFROM #__k2_items AS i\n    \tINNER JOIN #__k2_categories AS c ON c.id=i.catid AND c.access <= {$access}\n\t\tWHERE (";
        if ($pluginParams->get('search_tags') && count($itemIDs)) {
            JArrayHelper::toInteger($itemIDs);
            $query .= " i.id IN (" . implode(',', $itemIDs) . ") OR ";
        }
        $query .= "MATCH(i.title, i.introtext, i.`fulltext`,i.extra_fields_search,i.image_caption,i.image_credits,i.video_caption,i.video_credits,i.metadesc,i.metakey) AGAINST ({$text} IN BOOLEAN MODE)\n\t\t)\n\t\tAND i.trash = 0\n\t    AND i.published = 1\n\t    AND i.access <= {$access}\n\t    AND c.published = 1\n\t    AND c.access <= {$access}\n\t    AND c.trash = 0\n\t    AND ( i.publish_up = " . $db->Quote($nullDate) . " OR i.publish_up <= " . $db->Quote($now) . " )\n        AND ( i.publish_down = " . $db->Quote($nullDate) . " OR i.publish_down >= " . $db->Quote($now) . " )\n\t    GROUP BY i.id ";
        switch ($ordering) {
            case 'oldest':
                $query .= 'ORDER BY i.created ASC';
                break;
            case 'popular':
                $query .= 'ORDER BY i.hits DESC';
                break;
            case 'alpha':
                $query .= 'ORDER BY i.title ASC';
                break;
            case 'category':
                $query .= 'ORDER BY c.name ASC, i.title ASC';
                break;
            case 'newest':
            default:
                $query .= 'ORDER BY i.created DESC';
                break;
        }
        $db->setQuery($query, 0, $limit);
        $list = $db->loadObjectList();
        $limit -= count($list);
        if (isset($list)) {
            foreach ($list as $key => $item) {
                $list[$key]->href = JRoute::_(K2HelperRoute::getItemRoute($item->slug, $item->catslug));
            }
        }
        $rows[] = $list;
    }
    $results = array();
    if (count($rows)) {
        foreach ($rows as $row) {
            $new_row = array();
            foreach ($row as $key => $item) {
                $item->browsernav = '';
                $item->tag = $searchText;
                if (searchHelper::checkNoHTML($item, $searchText, array('text', 'title', 'metakey', 'metadesc', 'section', 'image_caption', 'image_credits', 'video_caption', 'video_credits', 'extra_fields_search', 'tag'))) {
                    $new_row[] = $item;
                }
            }
            $results = array_merge($results, (array) $new_row);
        }
    }
    return $results;
}