123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- /**
- * @var $this \yii\web\View
- * @var $model \app\models\front\News
- */
- use yii\helpers\ArrayHelper;
- use app\modules\ldjson\jsonLD;
- use yii\web\UrlManager;
- use app\models\Authors;
- use app\models\base\Image;
- use \app\models\news;
- use yii\helpers\Html;
- use yii\helpers\Url;
- use magyarandras\AMPConverter\Converter;
- use yii\web\View;
- use app\components\amp\AMPDiv;
- use app\components\amp\AMPAhref;
- use app\components\amp\AMPSpan;
- use app\components\amp\AMPP;
- setlocale( LC_ALL, 'ru_RU.UTF-8' );
- $preview = $model->image;
- $retina = $preview->getUrl(Image::SIZE_HD,"jpg");
- $normal = $preview->getUrl(Image::SIZE_1040x586,"webp");
- $bad = $preview->getUrl(Image::SIZE_680x383,'jpg');
- //$bad = $bad?$bad:'/images/default.jpg';
- if( $normal === false ){
- $normal = $bad;
- }
- if( $retina === false ){
- $retina = $normal;
- }
- $normal .= "?v=".$model->rev;
- $retina .= "?v=".$model->rev;
- $bad .= "?v=".$model->rev;
- $picture = $normal;
- if( $model->show_author == 'Y' )
- {
- $author = Authors::getAuthor( $model->author );
- $authorName = $author->name;
- }else{
- $authorName = 'ИА Амител';
- }
- $title = Html::encode( ( trim($model->meta_title) == '' )?$model->title:$model->meta_title );
- $this->title = $title;
- $this->registerLinkTag([ 'rel' => 'canonical', 'href' => $model->getUrl(true)], 'canonical');
- $lid = Html::encode( ($model->lid && trim($model->lid) <> '')?$model->lid:mb_substr( trim($model->text) ,0,150)."..." );
- //$ampProducer = new AmpProducer(['model'=>$model]);
- $converter = new Converter([
- 'image_base_url' => 'https://www.amic.ru',
- 'image_timeout' => 5
- ]);
- //Чистка
- $s = preg_replace( '/<meta([^\>]+)>/i', '<p>', $model->text );
- $sa = explode( '+++', $s );
- $s = $sa[0];
- $pattern = array( '/<style[^>]*>[^>]*>/i', '/<\/?font[^>]*>/i', '/sp_eventWasSet="on"/i' );
- $s = preg_replace( $pattern, '', $s );
- $s = preg_replace( '/<lj([^\>]+)>/i', '<p>', $s );
- $s = preg_replace( '/<\/lj>/i', '</p>', $s );
- $s = preg_replace( '/<asid([^\>]*)>/i', '<p>', $s );
- $s = preg_replace( '/<\/asid>/i', '</p>', $s );
- $s = preg_replace( '/<br([^\>]*)>/i', '<br>', $s );
- $s = preg_replace( '/<em([^\>]*)>/i', '<i>', $s );
- $s = preg_replace( '/<\/em>/i', '</i>', $s );
- $s = preg_replace( '/<hc([^\>]+)>/i', '<p>', $s );
- $s = preg_replace( '/<\/hc>/i', '</p>', $s );
- $s = preg_replace( '/<col([^\>]+)>/i', '', $s );
- $s = preg_replace( '/<\/col>/i', '', $s );
- $s = preg_replace( '/<st1([^\>]+)>/i', '', $s );
- $s = preg_replace( '/<\/st1>/i', '', $s );
- $s = preg_replace( '/<form([^\>]+)>/i', '<p>', $s );
- $s = preg_replace( '/<\/form>/i', '</p>', $s );
- $s = preg_replace( '/<noindex>/i', '<p>', $s );
- $s = preg_replace( '/<\/noindex>/i', '</p>', $s );
- $s = preg_replace( '/<pl>/i', '', $s );
- $s = preg_replace( '/<\/pl>/i', '', $s );
- $s = preg_replace( '/<index>/i', '<p>', $s );
- $s = preg_replace( '/<\/index>/i', '</p>', $s );
- $s = preg_replace( '/<hypernews>/i', '<p>', $s );
- $s = preg_replace( '/<\/hypernews>/i', '</p>', $s );
- $s = preg_replace( '/<content>/i', '<p>', $s );
- $s = preg_replace( '/<\/content>/i', '</p>', $s );
- $s = preg_replace( '/<tspan([^\>]+)>/i', '<span>', $s );
- $s = preg_replace( '/<\/tspan>/i', '</span>', $s );
- $s = preg_replace( '/<text([^\>]+)>/i', '<p>', $s );
- $s = preg_replace( '/<\/text>/i', '</p>', $s );
- $s = preg_replace( '/\<metricconverter([^\>]*)\>/iux', 'span', $s );
- $s = preg_replace( '/metricconverter/iu', 'span', $s );
- $s = preg_replace( '/<pre([^\>]+)>/ix', '', $s );
- $s = preg_replace( '/<\/pre>/i', '', $s );
- $s = preg_replace( '/<tmp([^\>]*)>/ix', '', $s );
- $s = preg_replace( '/<\/tmp>/i', '', $s );
- $s = preg_replace( '/<nowiki([^\>]*)>/ix', '', $s );
- $s = preg_replace( '/<\/nowiki>/i', '', $s );
- $s = preg_replace( '/<foreach([^\>]+)>/ix', ' ', $s );
- $s = preg_replace( '/<\/foreach>/i', ' ', $s );
- $s = preg_replace( '/<link([^\>]+)>/ix', '', $s );
- $s = preg_replace( '/<\/link>/i', '', $s );
- $s = str_replace( '<h ', '<h3 ', $s );
- $s = str_replace( '/h>', '/h3> ', $s );
- $s = str_replace( 'xmlns:fn', 'uri ', $s );
- $s = str_replace( 'publib/gimage.php?image=/', '', $s );
- //
- $converter->loadDefaultConverters();
- $converter->addConverter(new AMPDiv());
- $converter->addConverter(new AMPSpan());
- $converter->addConverter(new AMPAhref());
- $converter->addConverter(new AMPP());
- $content = $converter->convert($s);
- $content = str_replace( 'src="/', 'src="https://www.amic.ru/', $content ); //??
- $content = str_replace( 'http://', 'https://', $content );
- $content = str_replace('<div> </div>','', $content);
- $amp_scripts = $converter->getScripts();
- $yurl = $model->getYoutubeEmbedLink();
- $shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
- $longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
- $youtube_key = false;
- if (preg_match($longUrlRegex, $yurl, $matches)) {
- $youtube_key = $matches[count($matches) - 1];
- }
- if (preg_match($shortUrlRegex, $yurl, $matches)) {
- $youtube_key = $matches[count($matches) - 1];
- }
- if( $youtube_key ){
- $amp_scripts[] ='<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>';
- }
- $re = '/##_gallery-(\d+)##/mU';
- if( preg_match($re, $content) ){
- $content = processGalleriesInjects($content, $model);
- $amp_scripts[] ='<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>';
- }
- $this->context->scripts = '';
- foreach( array_unique($amp_scripts) as $p_scripts ){
- $this->context->scripts .= $p_scripts."\n";
- }
- $text = trim($content);
- $text = str_replace(["\n","\r"," "],'',$text);
- $text = str_replace([" "],' ',$text);
- $text = addslashes( strip_tags( $text ) );
- ?>
- <!-- Schema.org -->
- <script type="application/ld+json">
- {
- "@context": "http://schema.org",
- "@type": "NewsArticle",
- "mainEntityOfPage": {
- "@type": "WebPage",
- "@id": "<?= $model->getUrl(true) ?>"
- },
- "identifier":{"@type":"PropertyValue","propertyID":"WebPage","value":"<?= $model->uid?>"},
- "headline": "<?= $title ?>",
- "description": "<?= $lid ?>",
- <?php if ($picture) :?>
- "image": {
- "@type": "ImageObject",
- "representativeOfPage": "true",
- "url": "<?= $picture; ?>",
- "width": 1040,
- "height": 586
- },
- <?php endif ;?>
- "dateCreated": "<?= date(DATE_RSS, strtotime($model->dt_cr)); ?>",
- "dateModified": "<?= date(DATE_RSS, strtotime($model->dt_upd)); ?>",
- "datePublished": "<?= date(DATE_RSS, strtotime($model->dt_pub)); ?>",
- "author": {
- "@type": "Person",
- "name": "<?=$authorName ?>"
- },
- "publisher": {
- "@type": "Organization",
- "name": "<?= Yii::$app->params['orgname'] ?>",
- "logo": {
- "@type": "ImageObject",
- "url": "<?= Url::to('/img/amic-logo.svg', 'https') ?>",
- "width": 91,
- "height": 28
- }
- }
- }
- </script>
- <!-- !Schema.org -->
- <div class="block px3 pt2 mb2" data-newsId="<?= $model->id; ?>">
- <time class="pubdate block bold my1"><?= strftime('%e %B %Y, %H:%M', strtotime($model->dt_pub)); ?>, <?=$authorName?></time>
- <h1 class="mb1 px3"><?= $model->title ?></h1>
- <?php if (!empty($picture) && $model->photo_include == 'Y') : ?>
- <div class="amp-main-img">
- <amp-img class="amp-main-img my3" alt="<?= \yii\helpers\Html::encode($model->title) ?>" src="<?= $picture; ?>" width="1040"
- height="586"
- layout="responsive"></amp-img>
- <div class="amp-img-title">
- <?= $model->photo_title; ?>
- </div>
- </div>
- <?php endif; ?>
- <?php if (!empty($model->embed_url)) : ?>
- <div class="amp-main-img">
- <amp-youtube data-videoid="<?= $youtube_key?>" width="358" height="204" layout="responsive" >
- <amp-img src="<?= $picture?>" placeholder layout="fill" alt="<?= $model->photo_title; ?>"/>
- </amp-youtube>
- </div>
- <?php endif; ?>
- <div class="amp-preview"><?= $model->lid; ?></div>
- <div class="amp-news-content-text">
- <?= $content ?>
- </div>
- <a href="<?= $model->getUrl($schema=true) ?>" class="amp-button">Читайте полную версию на сайте</a>
- <section class="text">
- <?=($model->inscription == 2)?'<span class="advertisement">На правах рекламы</span>':''?>
- <?=($model->inscription == 1)?'<span class="advertisement">На правах рекламы</span><br><span class="med_advertisement">Имеются противопоказания, необходима консультация специалиста</span>':''?>
- <?=($model->inscription == 3)?'<span class="advertisement">Партнёр amic.ru</span>':''?>
- </section>
- <?=$this->render('@app/views/_etc/banners/amp1')?>
- </div>
- <?
- function processGalleriesInjects(string $body, News $post)
- {
- $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("/news/view/amp-gallery",["gallery"=>$gallery]);
- } else {
- return "";
- }
- },$body);
- return $res;
- }
|