Вход Регистрация
Файл: adultscript-2.0.3-pro/files/modules/video/components/search.php
Строк: 234
<?php
defined
('_VALID') or die('Restricted Access!');
defined('_SEARCH') or die('Restricted Access!');
class 
VComponent_video_search extends VModule_video
{
    public function 
__construct()
    {
        
parent::__construct();
    }
    
    public function 
render()
    {
        
$request    = VUri::query();
        if (isset(
$request['2']) && $request['2']) {
            
VF::redirect(BASE_URL.'/search/video/?s='.str_replace('-', '+', $request['2']), 301);
        }
        
        
$filter        = VF::factory('filter');
        
$query        = (isset($_GET['s'])) ? $filter->clean(trim($_GET['s'])) : null;
        
        
$orders    = array(
            
'relevance'        => __('relevant'),
            
'recent'        => __('recent'),
            
'popular'        => __('popular'),
            
'rated'            => __('rated'),
            
'discussed'        => __('discussed'),
            
'favorited'        => __('favorited'),
            
'downloaded'    => __('downloaded')
        );        
        
$order        = (isset($_GET['o']) && isset($orders[trim($_GET['o'])])) ? trim($_GET['o']) : 'relevance';
        
        
$timelines    = array(
            
'today'        => __('todays'),
            
'yesterday'    => __('yesterdays'),
            
'week'        => __('this-weeks'),
            
'month'        => __('this-months'),
            
'year'        => __('this-years')
        );
        
$timeline    = (isset($_GET['t']) && isset($timelines[trim($_GET['t'])])) ? trim($_GET['t']) : 'all';
        
        
$durations    = array(
            
'short'        => __('short'),
            
'medium'    => __('medium'),
            
'long'        => __('long')
        );
        
$duration    = (isset($_GET['d']) && isset($durations[trim($_GET['d'])])) ? trim($_GET['d']) : 'all';                
        
        
$category    = (isset($_GET['c'])) ? (int) trim($_GET['c']) : 'all';
        
$page        = (isset($_GET['page'])) ? (int) trim($_GET['page']) : 1;
        
        if (
$category != 'all' && $category !== 0) {
            
$this->db->query("
                SELECT cat_id, name
                FROM #__video_categories
                WHERE cat_id = "
.$category."
                AND status = '1'
                LIMIT 1
            "
);
            
            if (!
$this->db->affected_rows()) {
                
VModule::load('404', true);
            }
            
            
$name                 = $this->db->fetch_field('name');
            
$this->tpl->name    = $name;
        }
        
        
VLanguage::load('frontend.video');
    
        if (
$query != '') {
            
// lets limit this to 10 words
            
$regex    = (utf8_is_ascii($query)) ? '/[^A-Za-z0-9, ]+/' : '/[^pLpNpZ,s]+/u';
            
$query    = preg_replace($regex, ' ', $query);
            
$query     = preg_replace('/ss+/', ' ', $query);
            
$query    = VText::truncate_words($query, 20, '');
            
$query    = utf8_strtolower(utf8_trim($query));
            
            
$this->set_popular_search($query);
            
$this->tpl->searches    = $this->get_popular_searches($query);
            
            if (
VCfg::get('video.search_type') == 'simple') {
                
$this->search_mysql($query, $order, $timeline, $duration, $category, $page);
            } else {
                
$this->search_sphinx($query, $order, $timeline, $duration, $category, $page);
            }
        }
        
        
        
$time_name        = ($timeline != 'all') ? $timelines[$timeline].' ' : '';
        
$dur_name        = ($duration != 'all') ? $durations[$duration].' ' : '';
        
$order_name        = $orders[$order].' ';
        
$category_name    = ($category != 'all') ? $name.' ' : '';
        
$page_add        = ($page > 1) ? ' - '.__('page').' '.$page : '';
        
        
$this->tpl->menu        = 'video';
        
        
$this->tpl->title        = ($query) ? $time_name.$order_name.$dur_name.$category_name.__('video-results').': "'.$query.'"' : null;
        
$this->tpl->meta_title    = __('search-meta-title', array($time_name.$order_name.$dur_name.$category_name, $query.$page_add, VF::cfg_item('site_name')));
        
        
$this->tpl->canonical    = BASE_URL.'/search/video/?s='.$query;
        
$this->tpl->canonicalm    = MOBILE_URL.'/search/video/?s='.$query;
        
        if (
$page > 1) {
            
$this->tpl->prev_url    = build_search_url($query, $order, $timeline, $duration, $category, $page-1);
        }

        if (
$page < $this->tpl->pagination['total_pages']) {
            
$this->tpl->next_url    = build_search_url($query, $order, $timeline, $duration, $category, $page+1);
        }        

        
$this->tpl->query        = $query;
        
$this->tpl->page        = $page;
        
$this->tpl->order        = $order;
        
$this->tpl->timeline    = $timeline;
        
$this->tpl->duration    = $duration;
        
$this->tpl->category    = $category;
        
        
$this->tpl->categories    = $this->get_video_categories();
        
        
$this->tpl->load(array('header', 'video_search', 'footer'));
        
$this->tpl->display();
    }
    
    private function 
set_popular_search($query)
    {
        
$this->db->query("
            SELECT hits
            FROM #__video_search
            WHERE keyword = '"
.$this->db->escape($query)."'
            LIMIT 1
        "
);
        
        if (
$this->db->affected_rows()) {
            
$this->db->query("
                UPDATE #__video_search
                SET hits = hits+1
                WHERE keyword = '"
.$this->db->escape($query)."'
                LIMIT 1
            "
);
        } else {
            
$this->db->query("
                INSERT INTO #__video_search
                SET keyword = '"
.$this->db->escape($query)."',
                    hits = 1
            "
);            
        }
    }
    
    private function 
get_popular_searches($query)
    {
        
$this->db->query("
            SELECT keyword
            FROM #__video_search
            WHERE keyword LIKE '"
.$this->db->escape($query)."%'
            AND keyword != '"
.$this->db->escape($query)."'
            ORDER BY hits DESC
            LIMIT 10
        "
);
        
        return 
$this->db->fetch_rows();
    }
    
    private function 
search_mysql($query, $order, $timeline, $duration, $category = 0, $page = 1)
    {
        
$query        = str_replace('+', ' ', $query);
        
$sql_add    = null;
        if (
$timeline == 'today') {
              
$start_time  = strtotime('today');
            
$end_time    = $start_time+86400;
            
$sql_add  .= " AND v.add_time > ".$start_time." AND v.add_time < ".$end_time;          
        } elseif (
$timeline == 'yesterday') {
            
$start_time  = strtotime('yesterday');
            
$end_time    = $start_time+86400;
            
$sql_add  .= " AND v.add_time > ".$start_time." AND v.add_time < ".$end_time;          
        } elseif (
$timeline == 'week') {
            
$now         = time();
            
$start_time  = (date('w', $now) === 1) ? strtotime('today', $now) : strtotime('last Monday', $now);
            
$sql_add  .= " AND v.add_time > ".$start_time;          
        } elseif (
$timeline == 'month') {
            
$start_time = strtotime('first day of '.date('F Y'));
            
$sql_add  .= " AND v.add_time > ".$start_time;          
        } elseif (
$timeline == 'year') {
            
$start_time = strtotime('first day of '.date('Y'));
            
$sql_add  .= " AND v.add_time > ".$start_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',
            
'recent'         => 'v.add_time',
            
'popular'        => 'v.total_views',
            
'rated'            => 'v.likes',
            
'favorited'        => 'v.total_favorites',
            
'discussed'        => 'v.total_comments',
            
'downloaded'      => 'v.total_downloads'
        
);          
    
        if (
$category == 'all') {
              
$sql_count    = "SELECT COUNT(*) AS total_videos
                             FROM #__video AS v
                           WHERE MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE)
                           AND v.status = 1"
.$sql_add;
            
$sql        = "SELECT v.video_id, v.title, v.slug, v.likes, v.rating, v.rated_by, v.duration, v.thumb,
                              v.thumbs, v.total_views, v.add_time, v.ext, v.hd, v.premium, u.username,
                              MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE) AS relevance
                             FROM #__video AS v
                           LEFT JOIN #__video_tags AS t ON (t.video_id = v.video_id AND t.name = '"
.$this->db->escape($query)."')
                           LEFT JOIN #__user AS u ON (u.user_id = v.user_id AND u.status = '1')          
                           WHERE MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE)
                           AND v.status = 1"
.$sql_add;
        } else {
              
$sql_count    = "SELECT COUNT(*) AS total_videos
                             FROM #__video AS v, #__video_category AS c
                           WHERE MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE)
                           AND c.cat_id = "
.$category."
                           AND v.status = 1"
.$sql_add;
            
$sql        = "SELECT v.video_id, v.title, v.slug, v.likes, v.rating, v.rated_by, v.duration, v.thumb,
                              v.thumbs, v.total_views, v.add_time, v.ext, v.hd, v.premium, u.username,
                              MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE) AS relevance
                             FROM #__video AS v
                             INNER JOIN #__video_category AS c ON (c.video_id = v.video_id AND c.cat_id = "
.$category.")
                           LEFT JOIN #__video_tags AS t ON (t.video_id = v.video_id AND t.name = '"
.$this->db->escape($query)."')
                           LEFT JOIN #__user AS u ON (u.user_id = v.user_id AND u.status = '1')          
                           WHERE MATCH (v.title) AGAINST ('"
.$this->db->escape($query)."*' IN BOOLEAN MODE)
                           AND v.status = 1"
.$sql_add;
        }
        
        
$total_videos            = $this->db->get_field($sql_count, 'total_videos');
        
        
$this->tpl->pagination        = VPagination::get($page, $total_videos, VCfg::get('video.search_per_page'));
        
$this->tpl->videos            = $this->db->get_rows($sql." ORDER BY ".$orders[$order]." DESC LIMIT ".$this->tpl->pagination['limit']);
    }

    private function 
search_sphinx($query, $order, $timeline, $duration, $category, $page)        
    {
        
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 == 'rated') {
              
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'likes');
        } elseif (
$order == 'discussed') {
            
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'total_comments');
        } elseif (
$order == 'downloaded') {
            
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'total_downloads');
        } elseif (
$order == 'downloaded') {
            
$sphinx->SetSortMode(SPH_SORT_ATTR_DESC, 'total_favorites');
        }        
        
        
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED2);
        
$sphinx->SetLimits($offset, VCfg::get('video.browse_per_page'), 1000);
        
$sphinx->SetFilter('mobile', array(1));
        
$results        = $sphinx->Query(str_replace('-', ' ', $query), 'videos');

        
$pagination     = VPagination::get($page, $results['total'], VCfg::get('video.search_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.likes, v.rating, v.rated_by, v.duration, v.thumb,
                              v.thumbs, v.total_views, v.add_time, v.ext, v.hd, v.premium, u.username
                           LEFT JOIN #__user AS u ON (u.user_id = v.user_id AND u.status = '1')
                           WHERE v.video_id IN ("
.$ids.")
                           ORDER BY FIELD(v.video_id, "
.$ids.")";
            
$videos        = $this->db->get_rows();
        }
        
        
$this->tpl->pagination    = $pagination;
        
$this->tpl->videos        = $videos;
    }
}

function 
build_search_url($query, $order, $timeline = 'all', $duration = 'all', $category = 'all', $page = false)
{
    
$args    = array(
        
's' => $query,
        
'o' => $order,
        
't'    => ($timeline != 'all') ? $timeline : null,
        
'c' => ($category != 'all') ? $category : null,
        
'd'    => ($duration != 'all') ? $duration : null
    
);
    
    if (
$page) {
        
$args['page'] = $page;
    }
    
    return 
REL_URL.'/search/video/?'.http_build_query($args);
}
Онлайн: 2
Реклама