<?php
/**
* @version $Id: sobi2.searchbot.php 909 2007-06-24 13:29:03Z Radek Suski $
* @package SOBI2 searchbot
* @copyright Copyright (C) 2006 Sigsiu.NET (http://sigsiu.net). All rights reserved.
* @license see http://www.gnu.org/copyleft/gpl.html GNU/GPL.
* package_name is free software and parts of it may contain or be derived from the
* GNU General Public License or other free or open source software licenses.
* sobi2.searchbot.php 26-Sep-2006 11:25:39 Radek
*/
/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
$_MAMBOTS->registerFunction( 'onSearch', 'botSearchSOBI2' );
function searchSobi($catid = 0, $phrase = 'any', $ordering = null, $searchbot = false, $searchString = null, $config) {
global $mosConfig_offset, $database;
$items = null;
$countResults = 0;
$fieldData = array();
$and = null;
$dataForFields = array();
$sobi2IDs = array();
if($ordering)
$config->listingOrdering = $ordering;
if(!$phrase || $phrase == null)
$phrase = 'any';
if($searchString) {
$customFields = true;
$searchString = $config->clearSQLinjection(trim($searchString));
switch ($phrase) {
case 'exact':
$where = "LOWER(sdata.data_txt) LIKE '%{$searchString}%'";
break;
case 'any':
case 'all':
if($phrase == 'any')
$and_or = ' OR ';
else if($phrase == 'all')
$and_or = ' AND ';
$words = explode( ' ', $searchString );
$count = 0;
foreach ($words as $word) {
if($count != 0)
$where .= "{$and_or} LOWER(sdata.data_txt) LIKE '%{$word}%' ";
else
$where = " LOWER(sdata.data_txt) LIKE '%{$word}%' ";
$count++;
}
break;
}
/*
* now get the fields in there we have to looking for
*/
$query = "SELECT sobifields.fieldid " .
"FROM `#__sobi2_fields` AS sobifields " .
"WHERE ((`in_search` = 1 OR `in_search` = 2 ) AND `enabled` = 1)";
$database->setQuery( $query );
$searchInTo = $database->loadObjectList();
$and = null;
if(count($searchInTo) != 0) {
foreach($searchInTo as $field) {
$query = "SELECT DISTINCT `itemid` " .
"FROM `#__sobi2_fields` AS sfield " .
"LEFT JOIN `#__sobi2_fields_data` AS sdata ON sfield.fieldid = sdata.fieldid " .
"WHERE (sfield.fieldid = '{$field->fieldid}' AND {$where})";
$database->setQuery($query);
if(count($database->loadObjectList()) > 0) {
foreach($database->loadObjectList() as $item) {
$sobi2IDs[] = $item->itemid;
}
}
else
$customFields = false;
}
}
$sobi2IDs = array_unique($sobi2IDs);
/*
* last part - getting this information from sobi2_items table
*/
switch ($phrase) {
case 'exact':
$where = "LOWER(title) LIKE '%{$searchString}%' ";
$where .= "OR LOWER(metakey) LIKE '%{$searchString}%' ";
$where .= "OR LOWER(metadesc) LIKE '%{$searchString}%' ";
break;
case 'any':
case 'all':
if($phrase == 'any')
$and_or = ' OR ';
else if($phrase == 'all')
$and_or = ' AND ';
$words = explode( ' ', $searchString );
$count = 0;
foreach ($words as $word) {
if($count != 0)
$where .= "{$and_or} ( LOWER(title) LIKE '%{$word}%' ) ";
else
$where = "(( LOWER(title) LIKE '%{$word}%' )";
$count++;
}
$count = 0;
foreach ($words as $word) {
if($count != 0)
$where .= "{$and_or} ( LOWER(metakey) LIKE '%{$word}%' ) ";
else
$where .= "OR ( LOWER(metakey) LIKE '%{$word}%' ) ";
$count++;
}
$count = 0;
foreach ($words as $word) {
if($count != 0)
$where .= "{$and_or} ( LOWER(metadesc) LIKE '%{$word}%' ) ";
else
$where .= "OR ( LOWER(metadesc) LIKE '%{$word}%' )";
$count++;
}
$where .= "«») ";
break;
}
$and = str_replace("sdata.itemid","`itemid`",$and);
$query = "SELECT DISTINCT `itemid` FROM `#__sobi2_item` WHERE ({$where} {$and})";
$database->setQuery($query);
if(count($database->loadObjectList()) > 0) {
foreach($database->loadObjectList() as $item)
$sobi2IDs[] = $item->itemid;
}
$sobi2IDs = array_unique($sobi2IDs);
if(count($sobi2IDs) != 0) {
/*
* now we have all id's from items contains search strings
*/
$count = 0;
foreach($sobi2IDs as $sobi2 => $id) {
if($count != 0)
$whereId .= "OR items.itemid = '{$id}' ";
else
$whereId = " items.itemid = '{$id}' ";
$count++;
}
$now = date( 'Y-m-d H:i:«»s', time() + $mosConfig_offset * 60 * 60 );
$query = "SELECT itemid, title, owner, image, icon, metadesc, publish_up FROM `#__sobi2_item` AS items " .
"WHERE ( ({$whereId}) AND `published` = 1 AND (`publish_down` > '{$now}' OR `publish_down` = '{$config->nullDate}' ) ) ORDER BY {$config->listingOrdering}";
$database->setQuery($query);
$items = $database->loadObjectList();
$sobi2IDs = array_unique($sobi2IDs);
$countResults = count($sobi2IDs);
}
}
/*
* end of search function
*/
return $items;
}
function botSearchSOBI2( $text, $phrase = 'any', $ordering = null ) {
global $database, $my,$mosConfig_absolute_path;
require_once($mosConfig_absolute_path.'/components/com_sobi2/config.class.php' );
$config = new sobi2Config;
switch ( $ordering ) {
case 'oldest':
$order = 'items.publish_up ASC';
break;
case 'popular':
$order='items.hits ASC';
break;
case 'alpha':
$order = 'items.title ASC';
break;
case 'newest':
default:
$order = 'items.publish_up DESC';
}
$sobiItems = searchSobi(0, $phrase, $order, true, $text, $config);
$rows = array();
if(count($sobiItems) != 0) {
foreach($sobiItems as $sobi) {
$data = new sobiSearchResults ;
$data->title = $sobi->title;
$data->created = $sobi->publish_up;
$data->section = $config->componentName;
$query = "SELECT `data_txt` FROM `#__sobi2_fields_data` WHERE `itemid` = {$sobi->itemid} AND `fieldid` = '13'";
$database->setQuery($query);
$desc = $database->loadResult();
if($desc)
$data->text = $config->getSobiStr($desc);
else
$data->text = $sobi->metadesc;
$data->browsernav = 2;
$url='index.php?option=com_sobi2&sobi2Task=sobi2Details&sobi2Id='.$sobi->itemid.'&Itemid='.$config->sobi2Itemid;
$url = sefRelToAbs($url);
$data->href = $url;
$rows[] = $data;
unset($data);
}
}
return $rows;
}
class sobiSearchResults {
var $title = null;
var $created = null;
var $section = null;
var $text = null;
var $browsernav = null;
var $url = null;
}
?>
|