zira/forum/models/search.php

152 lines
4.7 KiB
PHP
Raw Permalink Normal View History

2016-07-05 18:54:21 +02:00
<?php
/**
* Zira project.
* search.php
2019-10-09 15:27:31 +02:00
* (c)2016 https://github.com/ziracms/zira
2016-07-05 18:54:21 +02:00
*/
namespace Forum\Models;
use Zira;
use Zira\Orm;
class Search extends Orm {
public static $table = 'forum_search';
public static $pk = 'id';
public static $alias = 'frm_srch';
const MIN_CHARS = 3;
public static function getFields() {
return array(
'id',
'category_id',
'forum_id',
'topic_id',
'keyword'
);
}
public static function getTable() {
return self::$table;
}
public static function getPk() {
return self::$pk;
}
public static function getAlias() {
return self::$alias;
}
public static function getReferences() {
return array(
Category::getClass() => 'category_id',
Forum::getClass() => 'forum_id',
Topic::getClass() => 'topic_id'
);
}
public static function clearTopicIndex($topic) {
self::getCollection()
->delete()
->where('category_id', '=', $topic->category_id)
->and_where('forum_id', '=', $topic->forum_id)
->and_where('topic_id', '=', $topic->id)
->execute();
}
public static function indexTopic($topic) {
self::clearTopicIndex($topic);
if ($topic->published != \Forum\Models\Topic::STATUS_PUBLISHED) return;
$keywords_str = '';
if ($topic->meta_keywords) {
$keywords = explode(',', $topic->meta_keywords);
foreach ($keywords as $keyword) {
$keywords_str .= ' ' . $keyword;
}
}
$text = $topic->title . ' ' . $topic->meta_title . ' ' . $keywords_str;
$text = trim($text);
if (empty($text)) return;
$text = mb_strtolower($text, CHARSET);
$text = preg_replace('/[\x20]+/',' ', $text);
$keywords = explode(' ', $text);
$added = array();
foreach($keywords as $keyword) {
if (in_array($keyword, $added)) continue;
if (mb_strlen($keyword, CHARSET)<self::MIN_CHARS) continue;
$index = new self();
$index->keyword = $keyword;
$index->category_id = $topic->category_id;
$index->forum_id = $topic->forum_id;
$index->topic_id = $topic->id;
$index->save();
$added []= $keyword;
}
}
public static function getTopics($text, $limit = 10, $offset = 0, $category_id=0, $forum_id=0) {
$text = trim($text);
if (empty($text)) return array();
$text = mb_strtolower($text, CHARSET);
$keywords = explode(' ', $text);
$query = self::getCollection();
$added = array();
foreach($keywords as $index=>$keyword) {
if (mb_strlen($keyword, CHARSET)<self::MIN_CHARS) continue;
if (in_array($keyword, $added)) continue;
2019-10-07 12:45:02 +02:00
if (count($added)>0) $query->union();
2016-07-05 18:54:21 +02:00
$query->open_query();
$query->select('topic_id');
$query->where('keyword','like',$keyword.'%');
if ($category_id>0) {
$query->and_where('category_id', '=', $category_id);
}
if ($category_id>0 && $forum_id>0) {
$query->and_where('forum_id', '=', $forum_id);
}
2019-11-25 14:21:07 +01:00
$query->group_by('topic_id');
$query->order_by('id', 'ASC');
2016-07-05 18:54:21 +02:00
$query->limit($limit+$offset);
$query->close_query();
$added []= $keyword;
if (count($added)>=5) break;
}
if (empty($added)) return array();
$query->merge();
$query->limit($limit, $offset);
$rows = $query->get();
$results = array();
foreach($rows as $row) {
$results[]=$row->topic_id;
}
if (empty($results)) return array();
// return \Forum\Models\Topic::getCollection()
// ->select(\Forum\Models\Topic::getFields())
// ->left_join(Zira\Models\User::getClass(), array('user_firstname'=>'firstname', 'user_secondname'=>'secondname', 'user_username'=>'username'))
// ->where('id','in',$results)
// ->get();
$query = \Forum\Models\Topic::getCollection();
foreach($results as $index=>$result) {
if ($index>0) $query->union();
$query->select(\Forum\Models\Topic::getFields())
->left_join(Zira\Models\User::getClass(), array('user_firstname' => 'firstname', 'user_secondname' => 'secondname', 'user_username' => 'username'))
->where('id', '=', $result)
->get();
}
return $query->get();
}
}