'\models\News', ]; /** * @var int cache lifetime, `0` - for not use cache */ public $cacheExpire = 180; /** * @var array default channel options */ public $channelOptions = []; /** * @var string default route to render Turbo-pages feed (use "/" - for root) */ public $turboRoute = "/rss/turbo"; /** * @var string the module version */ private $version = "1.0.4"; /** * @var integer, priority of initialization */ private $priority = 5; /** * {@inheritdoc} */ public function init() { parent::init(); // Set version of current module $this->setVersion($this->version); // Set priority of current module $this->setPriority($this->priority); // Process and normalize route for frontend $this->turboRoute = self::normalizeRoute($this->turboRoute); // $this->bootstrap(Yii::$app); } /** * {@inheritdoc} */ public function dashboardNavItems($createLink = false) { $items = [ 'label' => $this->name, 'icon' => 'fa fa-fw fa-rocket', 'url' => [$this->routePrefix . '/'. $this->id], 'active' => (in_array(\Yii::$app->controller->module->id, [$this->id]) && Yii::$app->controller->id == 'list'), ]; return $items; } /** * {@inheritdoc} */ public function bootstrap($app) { parent::bootstrap($app); if (isset(Yii::$app->params["turbo.supportModels"])) $this->supportModels = Yii::$app->params["turbo.supportModels"]; if (isset(Yii::$app->params["turbo.cacheExpire"])) $this->cacheExpire = Yii::$app->params["turbo.cacheExpire"]; if (isset(Yii::$app->params["turbo.channelOptions"])) $this->channelOptions = Yii::$app->params["turbo.channelOptions"]; if (isset(Yii::$app->params["turbo.turboRoute"])) $this->turboRoute = Yii::$app->params["turbo.turboRoute"]; if (!isset($this->supportModels)) throw new InvalidConfigException("Required module property `supportModels` isn't set."); if (!isset($this->cacheExpire)) throw new InvalidConfigException("Required module property `cacheExpire` isn't set."); if (!isset($this->channelOptions)) throw new InvalidConfigException("Required module property `channelOptions` isn't set."); if (!isset($this->turboRoute)) throw new InvalidConfigException("Required module property `turboRoute` isn't set."); if (!is_array($this->supportModels)) throw new InvalidConfigException("Module property `supportModels` must be array."); if (!is_array($this->channelOptions)) throw new InvalidConfigException("Module property `channelOptions` must be array."); if (!is_integer($this->cacheExpire)) throw new InvalidConfigException("Module property `cacheExpire` must be integer."); if (!is_string($this->turboRoute)) throw new InvalidConfigException("Module property `turboRoute` must be a string."); // Add route to pass turbo-pages in frontend $turboRoute = $this->turboRoute; if (empty($turboRoute) || $turboRoute == "/") { $app->getUrlManager()->addRules([ [ 'pattern' => '/rss', 'route' => '/rss/default', 'suffix' => '.xml' ], '/turbo.xml' => '/rss/default' ], true); } else if (is_string($turboRoute)) { $app->getUrlManager()->addRules([ [ 'pattern' => $turboRoute . '/feed', 'route' => 'rss/default', 'suffix' => '.xml' ], $turboRoute . '/feed.xml' => 'rss/default' ], true); $app->getUrlManager()->addRules([ [ 'pattern' => $turboRoute . '/turbo', 'route' => 'rss/default/turbo', 'suffix' => '.xml' ], $turboRoute . '/feed.xml' => 'rss/default' ], true); $app->getUrlManager()->addRules([ [ 'pattern' => $turboRoute . '/dzen', 'route' => 'rss/default/dzen', 'suffix' => '.xml' ], $turboRoute . '/dzen.xml' => 'rss/default' ], true); $app->getUrlManager()->addRules([ [ 'pattern' => $turboRoute . '/ria', 'route' => 'rss/default/ria', 'suffix' => '.xml' ], $turboRoute . '/ria.xml' => 'rss/default' ], true); } // echo $turboRoute; // Attach to events of create/change/remove of models for the subsequent clearing cache of feeds if (!($app instanceof \yii\console\Application)) { if ($cache = $app->getCache()) { if (is_array($models = $this->supportModels)) { foreach ($models as $name => $class) { if (class_exists($class)) { $model = new $class(); \yii\base\Event::on($class, $model::EVENT_AFTER_INSERT, function ($event) use ($cache) { $cache->delete(md5('yandex-turbo')); }); \yii\base\Event::on($class, $model::EVENT_AFTER_UPDATE, function ($event) use ($cache) { $cache->delete(md5('yandex-dzen')); }); \yii\base\Event::on($class, $model::EVENT_AFTER_DELETE, function ($event) use ($cache) { $cache->delete(md5('yandex-ria')); }); } } } } } } /** * Generate current RSS-feed URL * * @return null|string */ public function getFeedURL() { $url = null; $turboRoute = $this->turboRoute; if (empty($turboRoute) || $turboRoute == "/") { $url = Url::to('/turbo.xml', true); } else { $url = Url::to($turboRoute . '/feed.xml', true); } return $url; } /** * Get items for building a Yandex.Turbo pages * * @return array */ public function getTurboItems() { $items = []; if (is_array($models = $this->supportModels)) { foreach ($models as $name => $class) { // If class of model exist if (class_exists($class)) { $model = new $class(); $newsDataProvider = new \yii\data\ActiveDataProvider( [ "query"=>\app\models\News::find()->andwhere(['active'=>'Y'])->andWhere(['between', 'news.dt_pub', date("Y-m-d H:i:00",time()-2678400*1 ) , date("Y-m-d H:i:00")])->orderBy(["dt_pub"=>SORT_DESC])->limit(200), "pagination" =>[ "pageSize"=>200 ], ] ); // If module is loaded if (1) { $append = []; foreach ($newsDataProvider->getModels() as $item) { $turbo = true; if( stripos( $item->text, 'text, '##_gallery-') !== false ) $turbo = false; //Сюжеты $story = new \app\models\base\Story(); $stn = $story->getForNews($item->id); $tstory = null; if( count($stn) > 0 ){ $tstory = $stn[0]->url?:null; } // Рубрики $topic = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->one(); $tcategory = null; $stopnews = false; $riatop = false; if($topic instanceof \app\models\base\NewsTopic){ $tcategory = $topic->title; } $atopics = []; $topics = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->all(); foreach( $topics as $aa ){ $atopics[] = ['title'=>$aa->title,'url'=>$aa->getUrl(true)]; if( $aa->id == 109 ) $riatop = true; if( $aa->id == 137 ) $riatop = true; } if( $item->show_author == 'Y' ) { $author = Authors::getAuthor( $item->author ); $authorName = $author->name; }else{ $authorName = 'ИА Амител'; } // $stopnews = false; $append[] = [ 'url' => $item->getUrl(), 'name' => (isset($item->name)) ? $item->name : null, 'title' => (isset($item->title)) ? $item->title : null, 'image' => ($item->getPreview()) ? $item->getPreview()->getUrl(2,'jpg',true) : null, 'description' => (isset($item->lid)) ? trim($item->lid) : ((isset($item->description)) ? trim($item->description) : null), 'content' => (isset($item->text)) ? self::prepare($item->text, $item) : null, 'newscontent' => (isset($item->text)) ? self::prepare($item->text, $item) : null, 'stext' => (isset($item->text)) ? self::prepare2($item->text, $item) : null, 'updated_at' => (isset($item->dt_pub)) ? $item->dt_pub : null, 'status' => $turbo, // отключение турбо 'export_rss' => $item->export_rss, 'type' => $item->type, 'story' => null, //$tstory, 'category'=> $tcategory, 'topics'=>$atopics, 'uid' => $item->uid, 'author'=>$authorName, 'top'=>($item->top == 'Y' || $item->NH == 'Y' || $item->NH == 'F'), 'riatop'=>$riatop, 'stopnews' =>$stopnews ]; }; //print_r($append); $items = ArrayHelper::merge($items, $append); } } } } return $items; } /** * Get items for building a Yandex.Turbo pages * * @return array */ public function getTopicsItems($gtopic) { $items = []; if (is_array($models = $this->supportModels)) { foreach ($models as $name => $class) { // If class of model exist if (class_exists($class)) { $model = new $class(); $newsDataProvider = new \yii\data\ActiveDataProvider( [ "query"=>\app\models\News::find()->from(new Expression("news FORCE INDEX (top,calendar) , news_topic_relation t USE index(news_id)")) ->andwhere(['active'=>'Y'])->andWhere(['between', 'news.dt_pub', "2023-06-27 12:00:00" , date("Y-m-d H:i:00")])->orderBy(["dt_pub"=>SORT_DESC]) ->andWhere(new Expression('t.news_id=id')) ->andWhere(['t.topic_id'=>$gtopic]) ->limit(200), "pagination" =>[ "pageSize"=>200 ], ] ); // If module is loaded if (1) { $append = []; foreach ($newsDataProvider->getModels() as $item) { $turbo = true; if( stripos( $item->text, 'text, '##_gallery-') !== false ) $turbo = false; //Сюжеты $story = new \app\models\base\Story(); $stn = $story->getForNews($item->id); $tstory = null; if( count($stn) > 0 ){ $tstory = $stn[0]->url?:null; } // Рубрики $topic = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->one(); $tcategory = null; if($topic instanceof \app\models\base\NewsTopic){ $tcategory = $topic->title; } $atopics = []; $topics = $item->getTopics()->andWhere(['show'=>"Y","active"=>1])->all(); $stopnews = false; foreach( $topics as $aa ){ $atopics[] = ['title'=>$aa->title,'url'=>$aa->getUrl(true)]; } if( $item->show_author == 'Y' ) { $author = Authors::getAuthor( $item->author ); $authorName = $author->name; }else{ $authorName = 'ИА Амител'; } $re = '|]+>(\d+)<\/div>|is'; $text = preg_replace($re, '', $item->text ); $append[] = [ 'url' => $item->getUrl(), 'name' => (isset($item->name)) ? $item->name : null, 'title' => (isset($item->title)) ? $item->title : null, 'image' => ($item->getPreview()) ? $item->getPreview()->getUrl(2,'jpg',true) : null, 'description' => (isset($item->lid)) ? trim($item->lid) : ((isset($item->description)) ? trim($item->description) : null), 'content' => (isset($item->text)) ? self::prepare($text, $item) : null, 'newscontent' => (isset($item->text)) ? self::prepare($text, $item) : null, 'stext' => (isset($item->text)) ? self::prepare2($text, $item) : null, 'updated_at' => (isset($item->dt_pub)) ? $item->dt_pub : null, 'status' => $turbo, // отключение турбо 'export_rss' => $item->export_rss, 'type' => $item->type, 'story' => null, //$tstory, 'category'=> $tcategory, 'topics'=>$atopics, 'uid' => $item->uid, 'author'=>$authorName, 'top'=>($item->top == 'Y' || $item->NH == 'Y' || $item->NH == 'F'), 'stopnews' =>$stopnews ]; }; //print_r($append); $items = ArrayHelper::merge($items, $append); } } } } return $items; } public function prepare( $text, $item ) { // $f = News::find()->andWhere(['id'=>$item->id])->one(); $text = $this->processInjects($text); $text = $this->processGalleriesInjects($text); $text = preg_replace('|src="/([^/])|im','src="https://www.amic.ru/${1}',$text); $text = preg_replace('|href="/([^/])|im','href="https://www.amic.ru/${1}',$text); $text = preg_replace('/[\x00-\x09\x0B-\x0C\x0E-\x1F]/', '', $text); $text = preg_replace("/.+<\/span>/i",'',$text); $text = str_replace('­','',$text); $text = str_replace(' ',' ',$text); $text = str_replace('

',' ',$text); $text = preg_replace('#(.*?)#is', '', $text); //$text = News::processInjects($text); // var_dump($f); //$text = $f->renderBody(); // $f->renderBody(); // $text = CutLastInject( $text, $a['id'] ); // $text = ShowInject( $text ); // $text = str_replace( 'id="inject"', '', $text ); // $text = ShowGalleryWidgetm( $text ); $text = strip_tags( $text,'



  • ' ); $text = str_replace('

    ',' ',$text); $text = preg_replace('#\s(id)="[^"]+"#', '', $text); return $text; } public function prepare2( $text, $item ) { // $f = News::find()->andWhere(['id'=>$item->id])->one(); //$text = $this->processInjects($text); if( $item->embed_url != '' ){ $text = ''.$text; } $text = $this->processGalleriesInjects($text); $text = preg_replace('|src="/([^/])|im','src="https://www.amic.ru/${1}',$text); $text = preg_replace('|href="/([^/])|im','href="https://www.amic.ru/${1}',$text); $text = preg_replace('/[\x00-\x09\x0B-\x0C\x0E-\x1F]/', '', $text); $text = preg_replace("/.+<\/span>/i",'',$text); $text = str_replace('­','',$text); $text = str_replace(' ',' ',$text); $text = str_replace('

    ',' ',$text); $text = preg_replace('#(.*?)#is', '', $text); //$text = News::processInjects($text); // var_dump($f); //$text = $f->renderBody(); // $f->renderBody(); // $text = CutLastInject( $text, $a['id'] ); // $text = ShowInject( $text ); // $text = str_replace( 'id="inject"', '', $text ); // $text = ShowGalleryWidgetm( $text ); $re = '|]+>(\s+)<\/div>|is'; $text = preg_replace($re, '', $text ); $text = strip_tags( $text,'



    • ' ); $text = str_replace('

      ',' ',$text); $text = preg_replace('#\s(id)="[^"]+"#', '', $text); return $text; } public 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("@app/views/news/view/inject",["post"=>$post]); } else { return ""; } },$text); if( strlen($res) != strlen($text) ) return $res; $re = "|]+>.[^<]*|U"; $res = preg_replace_callback($re,function (array $matches): string { $xml = simplexml_load_string( str_replace( ' ', '', ArrayHelper::getValue($matches,0) ), 'SimpleXMLElement', LIBXML_NOCDATA ); if( $xml ){ $attributes =$xml->attributes(); if( $attributes['class'] == 'insinject' || $attributes['id'] == "inject" ){ $r = preg_match( "/^https?:\/\/(.*)\/(.*)\/(\d+)\/$/i", $attributes['url'], $aa ); $id = 0; if( $r && ($aa[1] == 'www.amic.ru' || $aa[1] == Yii::$app->request->serverName) && $aa[3]*1 > 0 ){ // old style inject $id = $aa[3]*1; } $r = preg_match( "/^https?:\/\/(.*)\/.*(\d+)\/?$/iU", $attributes['url'], $aa ); if( $r && ( $aa[1] == Yii::$app->request->serverName || $aa[1] == 'www.amic.ru' ) && $aa[2]*1 > 0 ){ // new style inject $id = $aa[2]*1; } if( $id ){ //$attributes['type'] to do $post = News::findOne(['id'=>$id]); if($post instanceof News) { if( $attributes['type'] == 1 ){ return \Yii::$app->view->render("@app/views/news/view/inject1",["post"=>$post]); }else if( $attributes['type'] == 2 ){ return \Yii::$app->view->render("@app/views/news/view/inject2",["post"=>$post]); }else{ return \Yii::$app->view->render("@app/views/news/view/inject",["post"=>$post]); } } else { return "Битый инжект"; } } } } return ArrayHelper::getValue($matches,0); },$text); return $res; } public function processGalleriesInjects(string $body) { $re = '/##_gallery-(\d+)##/mU'; $res = preg_replace_callback($re,function (array $matches): string { $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]); if($gallery instanceof \app\models\front\Gallery) { return \Yii::$app->view->render("@app/views/news/view/inject-gallery",["gallery"=>$gallery]); } else { return ""; } },$body); return $res; } }