Keyword

k2 Category description search

More
10 years 1 month ago #126317 by matt
k2 Category description search was created by matt
Hi, I have seen that this topic has come up often. I have attempted to write a separate plugin just to handle title and category description. however I can not seem to get it to work. If I go into mysql and query the DB directly I have no problems with LIKE '% searchword %' i can pull up the appropriate category descriptions, no problem. However the plugin doesn't seem to work. I have tried to modify a generic php search file and a k2 search file and nothing. Any suggestions?

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126318 by Lefteris
Replied by Lefteris on topic Re: k2 Category description search
Hi. It seems that you have messed up the search functionality a bit. K2 search plugin is designed to work with core Joomla! search component. The file that makes the search when you use the K2 search module ( this is the one using the generic.php layout ) is components/com_k2/models/itemlist.php

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126319 by matt
Replied by matt on topic Re: k2 Category description search
I'm sorry. I should have been more specific and clarified. I modified your k2 search plugin and reinstalled it as a separate plugin. However I can not get the default Joomla search to search category titles, category description, or category image captions and I need it to. here's the modified php plugin:

Is it my query that's wrong?



<?php
/**
* @version $Id: k2.php 1812 2013-01-14 18:45:06Z lefteris.kavadas $
* @package K2
* @author JoomlaWorks www.joomlaworks.net
* @copyright Copyright (c) 2006 - 2013 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: www.gnu.org/copyleft/gpl.html
* @modified Jeno Kovacs - Offlajn Store This email address is being protected from spambots. You need JavaScript enabled to view it.
*/

// no direct access
defined('_JEXEC') or die ;

jimport('joomla.plugin.plugin');
jimport('joomla.html.parameter');

class plgSearchK2Category extends JPlugin
{

function onContentSearchAreas()
{
return $this->onSearchAreas();
}

function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
{
return $this->onSearch($text, $phrase, $ordering, $areas);
}

function onSearchAreas()
{
JPlugin::loadLanguage('plg_search_k2', JPATH_ADMINISTRATOR);
static $areas = array('k2' => 'K2_ITEMS');
return $areas;
}

function onSearch($text, $phrase = '', $ordering = '', $areas = null)
{
JPlugin::loadLanguage('plg_search_k2', JPATH_ADMINISTRATOR);
jimport('joomla.html.parameter');
$mainframe = JFactory::getApplication();
$db = JFactory::getDBO();
$jnow = JFactory::getDate();
$now = K2_JVERSION == '15' ? $jnow->toMySQL() : $jnow->toSql();

$nullDate = $db->getNullDate();
$user = JFactory::getUser();
if (K2_JVERSION != '15')
{
$accessCheck = " IN(".implode(',', $user->getAuthorisedViewLevels()).") ";
}
else
{
$aid = $user->get('aid');
$accessCheck = " <= {$aid} ";
}
$tagIDs = array();
$catIDs = 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($this->onSearchAreas())))
{
return array();
}
}

$plugin = JPluginHelper::getPlugin('search', 'k2');
$pluginParams = class_exists('JParameter') ? new JParameter($plugin->params) : new JRegistry($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);
$escaped = K2_JVERSION == '15' ? $db->getEscaped($tagQuery, true) : $db->escape($tagQuery, true);
$tagQuery = $db->Quote($escaped, false);

$query = "SELECT id FROM #__k2_tags WHERE MATCH(name) AGAINST ({$tagQuery} IN BOOLEAN MODE) AND published=1";
$db->setQuery($query);
$tagIDs = K2_JVERSION == '30' ? $db->loadColumn() : $db->loadResultArray();

if (count($tagIDs))
{
JArrayHelper::toInteger($tagIDs);
$query = "SELECT itemID FROM #__k2_tags_xref WHERE tagID IN (".implode(',', $tagIDs).")";
$db->setQuery($query);
$catIDs = K2_JVERSION == '30' ? $db->loadColumn() : $db->loadResultArray();
}
}

if ($phrase == 'exact')
{
$text = JString::trim($text, '"');
$escaped = K2_JVERSION == '15' ? $db->getEscaped($text, true) : $db->escape($text, true);
$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);
$escaped = K2_JVERSION == '15' ? $db->getEscaped($text, true) : $db->escape($text, true);
$text = $db->Quote($escaped, false);
}
//Category Exclusion Developed By: Offlajn Team
$categoryExc = "";
if ($pluginParams->get('excludecats')) {
$categoryExc = " AND i.catid NOT IN (".implode(",", $pluginParams->get('excludecats')).") ";
}

//Item Exclusion Developed By: Offlajn Team
$catExc = "";
if ($pluginParams->get('excludeitems')) {
$catExc = " AND i.id NOT IN (".$pluginParams->get('excludeitems').") ";
}

$query = "
SELECT c.id,c.title AS title,
c.name as section,
c.image_caption,
c.image_credits,
c.created,
c.description,
CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug
FROM #__k2_categories AS c
INNER JOIN #__k2_categories AS c ON c.id=i.catid AND c.access {$accessCheck}
WHERE (";
if ($pluginParams->get('search_tags') && count($catIDs))
{
JArrayHelper::toInteger($catIDs);
$query .= " c.id IN (".implode(',', $catIDs).") OR ";
}



$query .= "MATCH(c.title, c.image_caption, c.image_credits, c.name, c.description) AGAINST ({$text} IN BOOLEAN MODE)
)
AND c.published = 1
AND c.access {$accessCheck}
AND c.trash = 0";
if (K2_JVERSION != '15' && $mainframe->isSite() && $mainframe->getLanguageFilter())
{
$languageTag = JFactory::getLanguage()->getTag();
$query .= " AND c.language IN (".$db->Quote($languageTag).", ".$db->Quote('*').") ";
}

$query .= $catExc; //Items Exclusion
$query .= $categoryExc; //Category Exclusion

$query .= " GROUP BY i.id ";

switch ($ordering)
{
case 'oldest' :
$query .= 'ORDER BY c.created ASC';
break;

case 'popular' :
$query .= 'ORDER BY c.hits DESC';
break;

case 'alpha' :
$query .= 'ORDER BY c.title ASC';
break;

case 'category' :
$query .= 'ORDER BY c.name ASC, c.title ASC';
break;

case 'newest' :
default :
$query .= 'ORDER BY c.created DESC';
break;
}


$db->setQuery($query, 0, $limit);
$list = $db->loadObjectList();
$limit -= count($list);
if (isset($list))
{
foreach ($list as $key => $cat)
{
$list[$key]->href = JRoute::_(K2HelperRoute::getItemRoute($cat->slug, $cat->catslug));

}
}
$rows[] = $list;
}

$results = array();
if (count($rows))
{
foreach ($rows as $row)
{
$new_row = array();
foreach ($row as $key => $cat)
{
if (JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$cat->id).'_L.jpg')) {
$cat->image = 'media/k2/items/cache/'.md5("Image".$cat->id).'_L.jpg';
} else if(JFile::exists(JPATH_SITE.DS.'media'.DS.'k2'.DS.'items'.DS.'cache'.DS.md5("Image".$cat->id).'_L.png')) {
$cat->image = 'media/k2/items/cache/'.md5("Image".$cat->id).'_L.png';
}
$cat->browsernav = '';
$cat->tag = $searchText;
if (searchHelper::checkNoHTML($cat, $searchText, array('text', 'title')))
{
$new_row[] = $cat;
}
}
$results = array_merge($results, (array)$new_row);
}
}

return $results;
}

}

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126320 by Lefteris
Replied by Lefteris on topic Re: k2 Category description search
It's because you are trying to search using MySQL full text search indexing. This requires that you add some database indexes. Instead of that i recommend to search those fields using the LIKE keyword. You can see an example in the search plugin of K2 2.6.8 since we moved all tables to InnoDB ( for new installations ) .

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago - 10 years 1 month ago #126321 by matt
Replied by matt on topic Re: k2 Category description search
Hi,

OK, am still not getting anything to show up. I changed the code in plugins/search/k2_category_search/k2cat.php to the following:

// no direct access
defined('_JEXEC') or die ;

jimport('joomla.plugin.plugin');
jimport('joomla.html.parameter');

class plgSearchK2 extends JPlugin
{

function onContentSearchAreas()
{
return $this->onSearchAreas();
}

function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
{
return $this->onSearch($text, $phrase, $ordering, $areas);
}

function onSearchAreas()
{
JPlugin::loadLanguage('plg_search_k2', JPATH_ADMINISTRATOR);
static $areas = array('k2' => 'K2_CATEGORIES');
return $areas;
}

function onSearch($text, $phrase = '', $ordering = '', $areas = null)
{
JPlugin::loadLanguage('plg_search_k2', JPATH_ADMINISTRATOR);
jimport('joomla.html.parameter');
$mainframe = JFactory::getApplication();
$db = JFactory::getDBO();
$jnow = JFactory::getDate();
$now = K2_JVERSION == '15' ? $jnow->toMySQL() : $jnow->toSql();

$nullDate = $db->getNullDate();
$user = JFactory::getUser();
if (K2_JVERSION != '15')
{
$accessCheck = " IN(".implode(',', $user->getAuthorisedViewLevels()).") ";
}
else
{
$aid = $user->get('aid');
$accessCheck = " <= {$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($this->onSearchAreas())))
{
return array();
}
}

$plugin = JPluginHelper::getPlugin('search', 'k2_Category_Search');
$pluginParams = class_exists('JParameter') ? new JParameter($plugin->params) : new JRegistry($plugin->params);

$limit = $pluginParams->def('search_limit', 50);

$text = JString::trim($text);
if ($text == '')
{
return array();
}

$rows = array();

if ($limit > 0)
{

if ($phrase == 'exact')
{
$text = JString::trim($text, '"');
$escaped = K2_JVERSION == '15' ? $db->getEscaped($text, true) : $db->escape($text, true);
$quoted = $db->Quote($escaped);
$where = " ( LOWER(c.name) = ".$quoted." OR LOWER(c.description) = ".$quoted.") ";
}
else
{
$escaped = K2_JVERSION == '15' ? $db->getEscaped($text, true) : $db->escape($text, true);
$quoted = $db->Quote('%'.$escaped.'%', false);
$where = " ( LOWER(c.name) LIKE ".$quoted." OR LOWER(c.description) LIKE ".$quoted.") ";
}

if ($pluginParams->get('search_tags') && count($itemIDs))
{
JArrayHelper::toInteger($itemIDs);
$where .= " OR c.id IN (".implode(',', $itemIDs);
}
$query = "
SELECT c.name AS title,
c.description AS text,
c.id
c.alias,
c.created,
CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug
FROM #__k2_categories AS c
WHERE (".$where.")
AND c.published = 1
AND c.access {$accessCheck}
AND c.trash = 0
";
if (K2_JVERSION != '15' && $mainframe->isSite() && $mainframe->getLanguageFilter())
{
$languageTag = JFactory::getLanguage()->getTag();
$query .= " AND c.language IN (".$db->Quote($languageTag).", ".$db->Quote('*').")";
}
$query .= " GROUP BY c.id ";

switch ($ordering)
{
case 'oldest' :
$query .= 'ORDER BY c.created ASC';
break;


case 'alpha' :
$query .= 'ORDER BY c.name ASC';
break;


case 'newest' :
default :
$query .= 'ORDER BY c.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')))
{
$new_row[] = $item;
}
}
$results = array_merge($results, (array)$new_row);
}
}

return $results;
}

}

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126322 by matt
Replied by matt on topic Re: k2 Category description search
Is there anyone who has a suggestion to make this query work?

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126323 by Bob
Replied by Bob on topic Re: k2 Category description search
I too would really like to see this as a regular function of search modules. You would think after adding metatag info to categories that this would be next on the list.

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126324 by matt
Replied by matt on topic Re: k2 Category description search
Is there a reason that this functionality couldn't be incorporated into K2? Is it because of the structure of K2 that prevents it. I am only asking, because it seems like it would be an easy fix. the modified plugin I posted above should work as an additional pugin. I cant see why it isn't.

Please Log in or Create an account to join the conversation.

More
10 years 1 month ago #126325 by Lefteris
Replied by Lefteris on topic Re: k2 Category description search
@matt

Open the Joomla! debugger to see the generated query in the list of queries at the bottom of the page. This will help you find why it's not working. Regarding why it's not included in the core, this was a matter of decision taken when K2 was designed. It can also be a matter of performance in some cases.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum