123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace app\models;
- use app\helpers\Transliterator;
- use app\models\base\Comments;
- use app\models\base\Image;
- use app\models\base\OldNews;
- use yii\db\ActiveQuery;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Inflector;
- /**
- * @property string $url
- * @property $mainPic
- * @property Image $preview
- * @property Image $image
- * @property OldNews $old
- * @property string $publishedAt
- * @property Comments[] $commentsAll
- */
- class News extends \app\models\base\News
- {
- public function getUrl():string
- {
- if(count($this->topics)>0){
- $topic = Transliterator::toUrl($this->topics[0]->title)."/";
- } else{
- $topic = "";
- }
- switch ($this->type){
- case 6:
- return "https://old.amic.ru/long/{$this->id}/";
- break;
- default:
- return "/news/".$topic.Transliterator::toUrl($this->title)."-".$this->id;
- }
- }
- public function getMainPic(){
- }
- /**
- * @return Image
- */
- public function getPreview($type="webp"):Image{
- return Image::findOne($this->id,$type);
- }
- /**
- * @return Image
- */
- public function getImage($type="webp"):Image
- {
- return Image::findOne($this->id,$type);
- }
- public function getOld():ActiveQuery
- {
- return $this->hasOne(OldNews::class,['alias'=>'alias']);
- }
- /**
- * @return ActiveQuery
- */
- public function getCommentsAll(): ActiveQuery
- {
- return $this->hasMany(Comments::class,['comment_pull_id'=>"uid"]);
- }
- public static function getMainView(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[33,]]);
- }
- public static function getPartnersNews(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[34,]]);
- }
- public static function getMainOfWeek(){
- return self::find()->joinWith("topics t")->andWhere(["t.id"=>[35,]]);
- }
- public function getPublishedAt(){
- if($this->dt_pub<date("Y-m-d H:i:s",strtotime("-1 day"))){
- return date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))));
- } else {
- $diff = ceil((time() - strtotime($this->dt_pub))/60); //В минутах
- if($diff<=60){
- return Transliterator::plural($diff,['минуту','минуты', 'минут'],true,'только что','минуту')." назад";
- } else {
- $diff = (int)floor($diff/60);
- return Transliterator::plural($diff,['час','часа', 'часов'],true,'только что','час')." назад";
- }
- }
- }
- public function renderBody()
- {
- $post = $this;
- return \Yii::$app->cache->getOrSet("post-body-".$this->id,function () use ($post){
- $body = $post->text;
- $body = self::processInjects($body);
- $body = self::processSingleImg($body);
- return $body;
- },600);
- }
- public static function processInjects($text)
- {
- $re = '/##news_(.*)##/mU';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $post = News::findOne(['uid'=>ArrayHelper::getValue($matches,1)]);
- if($post instanceof News) {
- return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
- } else {
- return "";
- }
- },$text);
- return $res;
- }
- public static function processSingleImg($text)
- {
- $re = '/(<p.*>)(<img.*>)(<\/p>)/mU';
- $res = preg_replace_callback($re,function (array $matches): string
- {
- $img = ArrayHelper::getValue($matches,2);
- $pic= "<div class='picture-cont-16x9'>
- <picture class='w-100'>
- {$img}
- </picture>
- </div>
- ";
- return ArrayHelper::getValue($matches,1).$pic.ArrayHelper::getValue($matches,3);
- },$text);
- return $res;
- }
- public static function find()
- {
- return parent::find()->orderBy(["dt_pub"=>SORT_DESC]);
- }
- }
|