<?php
namespace app\models\front;
use app\models\base\Image;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use Yii;

class News extends \app\models\News
{
	public static function find()
	{
		return parent::find()
			->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])
			->andWhere(["news.active"=>'Y'])
			->orderBy(["dt_pub"=>SORT_DESC]);
	}

	public static function findFilter()
	{
		$session = Yii::$app->session;
		$x = $session->get('filterx');
		if( is_array( $x ) && count( $x ) > 0 ){
			return parent::find()
			->select( 'news.*' )
			->from(new Expression("news FORCE INDEX (top,calendar)"))
			->leftJoin('tags_relation', '`tags_relation`.`news_id` = `news`.`id`')
			->Where(['tags_relation.topic_id'=>$x])
			->andWhere(["<=","dt_pub",date("Y-m-d H:i:00")])
			->andWhere(["news.active"=>'Y'])
			->orderBy(["dt_pub"=>SORT_DESC]);
		}else{
			return self::find();
		}
	}

	public static function keyFilter()
	{
		$session = Yii::$app->session;
		$x = $session->get('filterx');
		if( is_array( $x ) && count( $x ) > 0 ){
			sort($x);
			return implode( '-', $x );
		}
		return '';
	}

	public function afterFind()
	{
		parent::afterFind();
	}
}
