Файл: adultscript-2.0.3-pro/files/mobile/components/video_search.php
Строк: 182
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_mobile_video_search extends VComponent_mobile_video
{
public function __construct()
{
parent::__construct();
}
public function render()
{
$query = (isset($_GET['query'])) ? trim($_GET['query']) : null;
if (!$query) {
$this->notfound();
}
$timelines = array(
'all' => 1,
'today' => 1,
'yesterday' => 1,
'week' => 1,
'month' => 1,
'year' => 1
);
$timeline = (isset($_GET['time'])) ? trim($_GET['time']) : 'all';
if (!isset($timelines[$timeline])) {
$this->notfound();
}
$durations = array(
'all' => 1,
'short' => 1,
'medium' => 1,
'long' => 1
);
$duration = (isset($_GET['duration'])) ? trim($_GET['duration']) : 'all';
if (!isset($durations[$duration])) {
$this->notfound();
}
$sorts = array(
'relevance' => 1,
'added' => 1,
'views' => 1,
'rating' => 1,
'duration' => 1
);
$sort = (isset($_GET['sort'])) ? trim($_GET['sort']) : 'relevance';
if (!isset($sorts[$sort])) {
$this->notfound();
}
$page = (isset($_GET['page'])) ? (int) trim($_GET['page']) : 1;
$this->mcfg['search_type'] = 'simple';
if ($this->mcfg['search_type'] == 'simple') {
$this->search_mysql($query, $page, $sort, $timeline, $duration);
} else {
$this->search_sphinx($query, $page, $sort, $timeline, $duration);
}
$page_add = ($page > 1) ? ' - '.__('page').' '.$page : '';
$this->tpl->menu = 'video';
$this->tpl->title = __('search-title', array($query, $page_add));
$this->tpl->meta_title = __('search-meta-title', array($query.$page_add, $this->mcfg['site_name']));
$this->tpl->meta_desc = __('search-meta-desc', array($query.$page_add, $this->mcfg['meta_desc']));
$this->tpl->canonical = BASE_URL.'/search/video/'.str_replace(' ', '-', $query).'/';
if ($page > 1) {
$this->tpl->canonical .= $page.'/';
}
$this->tpl->order = $sort;
$this->tpl->timeline = $timeline;
$this->tpl->duration = $duration;
$this->tpl->page = $page;
$this->tpl->query = $query;
$this->tpl->load(array('header', 'video_search', 'footer'));
$this->tpl->display();
}
private function search_mysql($query, $page, $sort = 'relevance', $timeline = 'all', $duration = 'all')
{
$sql_add = null;
$today = strtotime('12:00:00');
if ($timeline == 'today') {
$sql_add = ' AND v.add_time > '.$today;
} elseif ($timeline == 'yesterday') {
$sql_add = ' AND v.add_time > '.strtotime('-1 day', $today).' AND v.add_time < '.$today;
} elseif ($timeline == 'week') {
$sql_add = ' AND v.add_time > '.strtotime('this week', time());
} elseif ($timeline == 'month') {
$sql_add = ' AND v.add_time > '.strtotime('first day of this month', time());
} elseif ($timeline == 'year') {
$sql_add = ' AND v.add_time > '.strtotime('first day of January', time());
}
if ($duration == 'short') {
$sql_add .= ' AND v.duration < 300';
} elseif ($duration == 'medium') {
$sql_add .= ' AND v.duration >= 300 AND v.duration < 1200';
} elseif ($duration == 'long') {
$sql_add .= ' AND v.duration >= 1200';
}
$orders = array(
'relevance' => 'relevance',
'added' => 'v.add_time',
'views' => 'v.total_views',
'rating' => 'v.rating',
'duration' => 'v.duration'
);
$search = str_replace('-', ' ', $query);
$sql_count = "SELECT COUNT(*) AS total_videos
FROM #__video AS v
WHERE MATCH (v.title) AGAINST ('".$this->db->escape($search)."')
AND v.status = 1
AND v.mobile = '1'".$sql_add;
$total_videos = $this->db->get_field($sql_count, 'total_videos');
$pagination = VPagination::get($page, $total_videos, $this->mcfg['videos_per_page']);
$sql = "SELECT v.video_id, v.title, v.slug, v.rating, v.rated_by, v.duration, v.thumb,
v.thumbs, v.total_views, v.add_time, v.ext, v.premium,
MATCH (v.title) AGAINST ('".$this->db->escape($search)."') AS relevance
FROM #__video AS v
WHERE MATCH (v.title) AGAINST ('".$this->db->escape($search)."')
AND v.status = 1
AND v.mobile = '1'".$sql_add."
ORDER BY ".$orders[$sort]." DESC
LIMIT ".$pagination['limit'];
$this->tpl->pagination = $pagination;
$this->tpl->videos = $this->db->get_rows($sql);
}
private function search_sphinx($query, $page, $sort = 'relevance', $timeline = 'all', $duration = 'all')
{
VF::load('sphinxapi.sphinxapi');
$offset = ($page >= 2) ? (($page - 1) * $this->mcfg['browse_per_page']) : 0;
$sphinx = new SphinxClient();
$sphinx->SetServer(VF::cfg_item('sphinx_host'), VF::cfg_item('sphinx_port'));
$sphinx->SetConnectTimeout(1);
$sphinx->SetFieldWeights(array('title' => 100));
$query = $sphinx->EscapeString($query);
if ($timeline == 'today') {
$sphinx->SetFilterRange('add_time', strtotime('today'), time());
} elseif ($timeline == 'yesterday') {
$sphinx->SetFilterRange('add_Time', strtotime('yesterday'), strtotime('today'));
} elseif ($timeline == 'week') {
$sphinx->SetFilterRange('add_time', strtotime('-1 week'), time());
} elseif ($timeline == 'month') {
$sphinx->SetFilterRange('add_time', strtotime('-1 month'), time());
} elseif ($timeline == 'year') {
$sphinx->SetFilterRange('add_time', strtotime('-1 year'), time());
}
if ($duration == 'short') {
$sphinx->SetFilterRange('duration', 1, 300);
} elseif ($duration == 'medium') {
$sphinx->SetFilterRange('duration', 301, 1200);
} elseif ($duration == 'long') {
$sphinx->SetFilterRange('duration', 1200, 100000);
}
if ($order == 'relevance' ) {
$sphinx->SetSortMode(SPH_SORT_RELEVANCE);
} elseif ($order == 'recent') {
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'add_time');
} elseif ($order == 'views') {
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'total_views');
} elseif ($order == 'longest') {
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'duration');
}
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
$sphinx->SetLimits($offset, $this->mcfg['browse_per_page'], 1000);
$sphinx->SetFilter('mobile', array(1));
$results = $sphinx->Query(str_replace('-', ' ', $query), 'videos');
$pagination = VPagination::get($page, $results['total'], $this->vcfg['videos_per_page']);
$videos = array();
if (isset($results['matches']) && is_array($results['matches'])) {
$ids = implode(',', array_keys($results['matches']));
$sql = "SELECT v.video_id, v.title, v.slug, v.rating, v.duration, v.thumb,
v.total_views, v.add_time
FROM #__video AS v
WHERE v.video_id IN (".$ids.")
ORDER BY FIELD(v.video_id,".$ids.")";
$videos = $this->db->get_rows($sql);
}
$this->tpl->pagination = $pagination;
$this->tpl->videos = $videos;
}
}
function build_search_url($query, $page, $sort, $timeline, $duration)
{
$url = MOBILE_REL.'/search/video/?query='.$query;
if ($page > 1) {
$url .= '&page='.$page;
}
if ($sort != 'all') {
$url .= '&sort='.$sort;
}
if ($timeline != 'all') {
$url .= '&time='.$timeline;
}
if ($duration != 'all') {
$url .= '&duration='.$duration;
}
return $url;
}