SearchController.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\controllers;
  3. use app\models\base\NewsTopic;
  4. use app\models\base\Story;
  5. use app\models\base\Tags;
  6. use app\models\News;
  7. use yii\sphinx\Query;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. class SearchController extends Controller
  11. {
  12. public function actionIndex($topic = NULL,$q=NULL, $index = NULL)
  13. {
  14. $topic = NewsTopic::findOne(['url'=>$topic]);
  15. if(!$topic instanceof NewsTopic){
  16. $topic = new \stdClass();
  17. $topic->id = NULL;
  18. }
  19. $tag = \Yii::$app->request->get('tag',NULL);
  20. $story = \Yii::$app->request->get('story',NULL);
  21. if( !is_null($tag) ){
  22. $tag = $tag*1;
  23. $news_query = \app\models\News::find()->alias('n')->joinWith('tags t')->andFilterWhere(['t.id'=>$tag])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
  24. //return $this->render("index",["topic"=>$topic, "news_query"=>$news_query]);
  25. //echo $news_query->createCommand()->rawSql;
  26. }else if( !is_null($story) ){
  27. $story = $story*1;
  28. $news_query = \app\models\News::find()->alias('n')->joinWith('story t')->andFilterWhere(['t.id'=>$story])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
  29. //echo $news_query->createCommand()->rawSql;
  30. }else{
  31. $news_query = \app\models\News::find()->alias('n')->joinWith('topics t')->andFilterWhere(['t.id'=>$topic->id])->andWhere(['n.active'=>'Y'])->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])->groupBy('n.id');
  32. }
  33. $dateRange = \Yii::$app->request->get('daterange',NULL);
  34. // if( trim($q) == '' && !(!is_null($dateRange) && $dateRange!="") ) return $this->redirect('/news');
  35. if(!is_null($dateRange) && $dateRange!=""){
  36. $dateRange = explode(" — ",$dateRange);
  37. $dateRange[0] = date("Y-m-d 00:00:01",strtotime($dateRange[0]));
  38. $dateRange[1] = isset($dateRange[1])?date("Y-m-d 23:59:59",strtotime($dateRange[1])):date("Y-m-d 23:59:59",strtotime($dateRange[0]));
  39. $news_query->andWhere(["BETWEEN","dt_pub",$dateRange[0],$dateRange[1]]);
  40. }else{
  41. if( (is_null($q) || trim($q) == '') && (is_null($tag) && is_null($story)) ){
  42. //$news_query = \app\models\News::find()->alias('n')->andWhere(['n.id'=>-1]);
  43. if(\Yii::$app->request->get("page",0)>500) {
  44. \Yii::$app->request->setQueryParams(["page"=>500]);
  45. }
  46. $news_query = \app\models\front\News::findFilter();
  47. }
  48. }
  49. if(!is_null($q) && trim($q) != '' && is_null($tag) ){
  50. switch ($index){
  51. case 'video':
  52. $index_name = 'amic_news_t137';
  53. break;
  54. default:
  55. $index_name = 'amic_news';
  56. }
  57. $news_query->andWhere([
  58. "n.id"=>(new Query())
  59. ->from($index_name)
  60. ->select(['id'])
  61. ->addOptions(['max_matches' => 1000])
  62. ->addOptions(['field_weights'=> ['title' => 10, 'body' => 8, 'lead'=>5] ])
  63. ->match($q)
  64. ->limit(1000)
  65. ->column()
  66. ]);
  67. }
  68. return $this->render("index",["topic"=>$topic, "news_query"=>$news_query]);
  69. }
  70. }