File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.search.php
<?php
/**
* This file contains the iwp_search class
*
* @version $Id$
*
*
* @package IWP
* @subpackage IWP_API
*/
/**
* IWP Search Class
* This is the class that handles all searching of content items within the application
*
* @package IWP
* @subpackage IWP_API
*/
class iwp_search extends iwp_base {
public $contentTypes = array();
public $startDate = null;
public $endDate = null;
public $categories = array();
public $fields = array('title', 'content');
public $searchQuery = '';
public $paging = null;
public $perPage = 10;
public $pageNum = 1;
protected $dbQuery = '';
/**
* Instance
* This static variable holds the current instance of this object being loaded.
* So using the getInstance function anywhere will return the very same instance.
*
* @var object Instance
*/
public static $Instance;
/**
* getInstance
* This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
*
* @return object Returns the instantiated object
**/
public static function getInstance(){
if(!isset(self::$Instance)){
self::$Instance = new self();
}
return self::$Instance;
}
public function BuildSearchQuery(){
$where = array('contentMatch'=>array(), 'conditions'=>array());
$joins = array();
$searchQuery = $this->db->Quote($this->searchQuery);
$order = 'score desc';
$fields = array();
$catSearch = array();
// urls
$joins[] = "inner join " . IWP_TABLE_URLS . " as ur on c.contentid=ur.associd and ur.assoctype='content'";
$joins[] = "left join " . IWP_TABLE_CONTENTTYPES . " as ct on c.typeid=ct.typeid";
// start date
if(!is_null($this->startDate)){
$where['conditions'][] = 'startdate >= "'.GetMysqlDateTime($this->startDate).'"';
}
// end date
if(!is_null($this->endDate)){
$where['conditions'][] = 'startdate <= "'.GetMysqlDateTime($this->endDate).'"';
}
// in content types
if(is_array($this->contentTypes) && sizeof($this->contentTypes) > 0 && !in_array('0', $this->contentTypes, false)){
// ensure all the content types are valid ID numbers -- prevent injection
$this->contentTypes = array_filter($this->contentTypes, 'iwp_IsId');
if(sizeof($this->contentTypes) > 0){
$where['conditions'][] = 'c.typeid IN('.implode(',', $this->contentTypes).')';
}
}
// ensure the 'can be searched' option is enabled for the content types
$where['conditions'][] = 'ct.searchable = 1';
// in categories
if(is_array($this->categories) && sizeof($this->categories) > 0 && !in_array('0', $this->categories, false)){
if(in_array(-1, $this->categories)){
// this means they want to search uncategorised content
$catSearch[] = 'ca.contentid IS NULL';
}
// ensure all the content types are valid ID numbers -- prevent injection
$this->categories = array_filter($this->categories, 'iwp_IsId');
if(sizeof($this->categories) > 0){
$catSearch[] = 'ca.categoryid IN('.implode(',', $this->categories).')';
}
if(sizeof($catSearch)>0){
$joins[] = 'left join ' . IWP_TABLE_CATASSOC . ' as ca on c.contentid=ca.contentid';
$where['conditions'][] = '('.implode(' or ', $catSearch).')';
}
}
// authors
if(in_array('author', $this->fields)){
$joins[] = "left join ".IWP_TABLE_USERS." as us on find_in_set(us.userid, c.author)";
$where['contentMatch'][] = 'us.firstname = \''.$searchQuery.'\'';
$where['contentMatch'][] = 'us.lastname = \''.$searchQuery.'\'';
$where['contentMatch'][] = 'us.firstname LIKE \'%'.$searchQuery.'%\'';
$where['contentMatch'][] = 'us.lastname LIKE \'%'.$searchQuery.'%\'';
$where['contentMatch'][] = 'CONCAT(us.firstname, " ", us.lastname) LIKE \'%'.$searchQuery.'%\'';
$fields[] = 'us.firstname';
$fields[] = 'us.lastname';
}
// work out the scoring
if(in_array('title', $this->fields)){
$fields[] = 'c.title';
}
if(in_array('content', $this->fields)){
$fields[] = 'c.content';
}
$fullText = $this->db->FullText($fields, $searchQuery);
$where['contentMatch'][] = $fullText;
$isTitle = "c.title = '".$searchQuery."'";
$inTitle = "c.title LIKE '%".$searchQuery."%'";
$where['contentMatch'][] = $inTitle;
$rowScore = " ((".$fullText.") * 10 ) + ((".$isTitle.") * 10) + ((".$inTitle.") * 1)";
// search fields
$query = 'select SQL_CALC_FOUND_ROWS *, c.contentid as rowid, ('.$rowScore.') as score from ' . IWP_TABLE_CONTENT . ' as c ' . implode(' ', $joins) . ' ';
$query .= ' where /*%%visibility%%*/ (1=1) ';
if(sizeof($where['conditions']) > 0){
$query .= ' AND (' . implode(' and ', $where['conditions']) .')';
}
if(sizeof($where['contentMatch']) > 0){
$query .= ' AND (' . implode(' or ', $where['contentMatch']) .')';
}
$query .= ' group by c.contentid order by score desc ';
$query .= ' limit ' . (($this->pageNum * $this->perPage) - $this->perPage) .', '.(int)$this->perPage;
$this->dbQuery = $query;
return array('where'=>$where, 'joins'=>$joins);
}
public function Search($buildQuery=true){
if($buildQuery){
$this->BuildSearchQuery();
}
return $this->dbQuery;
}
}