News.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\models;
  3. use app\helpers\Transliterator;
  4. use app\models\base\Comments;
  5. use app\models\base\Image;
  6. use app\models\base\OldNews;
  7. use yii\db\ActiveQuery;
  8. use yii\helpers\ArrayHelper;
  9. use yii\helpers\Inflector;
  10. /**
  11. * @property string $url
  12. * @property $mainPic
  13. * @property Image $preview
  14. * @property Image $image
  15. * @property OldNews $old
  16. * @property string $publishedAt
  17. * @property Comments[] $commentsAll
  18. */
  19. class News extends \app\models\base\News
  20. {
  21. public function getUrl():string
  22. {
  23. if(count($this->topics)>0){
  24. $topic = Transliterator::toUrl($this->topics[0]->title)."/";
  25. } else{
  26. $topic = "";
  27. }
  28. switch ($this->type){
  29. case 6:
  30. return "https://old.amic.ru/long/{$this->id}/";
  31. break;
  32. default:
  33. return "/news/".$topic.Transliterator::toUrl($this->title)."-".$this->id;
  34. }
  35. }
  36. public function getMainPic(){
  37. }
  38. /**
  39. * @return Image
  40. */
  41. public function getPreview($type="webp"):Image{
  42. return Image::findOne($this->id,$type);
  43. }
  44. /**
  45. * @return Image
  46. */
  47. public function getImage($type="webp"):Image
  48. {
  49. return Image::findOne($this->id,$type);
  50. }
  51. public function getOld():ActiveQuery
  52. {
  53. return $this->hasOne(OldNews::class,['alias'=>'alias']);
  54. }
  55. /**
  56. * @return ActiveQuery
  57. */
  58. public function getCommentsAll(): ActiveQuery
  59. {
  60. return $this->hasMany(Comments::class,['comment_pull_id'=>"uid"]);
  61. }
  62. public static function getMainView(){
  63. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[33,]]);
  64. }
  65. public static function getPartnersNews(){
  66. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[34,]]);
  67. }
  68. public static function getMainOfWeek(){
  69. return self::find()->joinWith("topics t")->andWhere(["t.id"=>[35,]]);
  70. }
  71. public function getPublishedAt(){
  72. if($this->dt_pub<date("Y-m-d H:i:s",strtotime("-1 day"))){
  73. return date("d",strtotime($this->dt_pub))." ".mb_strtolower(Transliterator::month(date("n",strtotime($this->dt_pub))));
  74. } else {
  75. $diff = ceil((time() - strtotime($this->dt_pub))/60); //В минутах
  76. if($diff<=60){
  77. return Transliterator::plural($diff,['минуту','минуты', 'минут'],true,'только что','минуту')." назад";
  78. } else {
  79. $diff = (int)floor($diff/60);
  80. return Transliterator::plural($diff,['час','часа', 'часов'],true,'только что','час')." назад";
  81. }
  82. }
  83. }
  84. public function renderBody()
  85. {
  86. $post = $this;
  87. return \Yii::$app->cache->getOrSet("post-body-".$this->id,function () use ($post){
  88. $body = $post->text;
  89. $body = self::processInjects($body);
  90. $body = self::processSingleImg($body);
  91. return $body;
  92. },600);
  93. }
  94. public static function processInjects($text)
  95. {
  96. $re = '/##news_(.*)##/mU';
  97. $res = preg_replace_callback($re,function (array $matches): string
  98. {
  99. $post = News::findOne(['uid'=>ArrayHelper::getValue($matches,1)]);
  100. if($post instanceof News) {
  101. return \Yii::$app->view->render("/news/view/inject",["post"=>$post]);
  102. } else {
  103. return "";
  104. }
  105. },$text);
  106. return $res;
  107. }
  108. public static function processSingleImg($text)
  109. {
  110. $re = '/(<p.*>)(<img.*>)(<\/p>)/mU';
  111. $res = preg_replace_callback($re,function (array $matches): string
  112. {
  113. $img = ArrayHelper::getValue($matches,2);
  114. $pic= "<div class='picture-cont-16x9'>
  115. <picture class='w-100'>
  116. {$img}
  117. </picture>
  118. </div>
  119. ";
  120. return ArrayHelper::getValue($matches,1).$pic.ArrayHelper::getValue($matches,3);
  121. },$text);
  122. return $res;
  123. }
  124. public static function find()
  125. {
  126. return parent::find()->orderBy(["dt_pub"=>SORT_DESC]);
  127. }
  128. }