view.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @var $this \yii\web\View
  4. * @var $model \app\models\front\News
  5. */
  6. use yii\helpers\ArrayHelper;
  7. use app\modules\ldjson\jsonLD;
  8. use yii\web\UrlManager;
  9. use app\models\Authors;
  10. use app\models\base\Image;
  11. use \app\models\news;
  12. use yii\helpers\Html;
  13. use yii\helpers\Url;
  14. use magyarandras\AMPConverter\Converter;
  15. use yii\web\View;
  16. use app\components\amp\AMPDiv;
  17. use app\components\amp\AMPAhref;
  18. use app\components\amp\AMPSpan;
  19. use app\components\amp\AMPP;
  20. setlocale( LC_ALL, 'ru_RU.UTF-8' );
  21. $preview = $model->image;
  22. $retina = $preview->getUrl(Image::SIZE_HD,"jpg");
  23. $normal = $preview->getUrl(Image::SIZE_1040x586,"webp");
  24. $bad = $preview->getUrl(Image::SIZE_680x383,'jpg');
  25. //$bad = $bad?$bad:'/images/default.jpg';
  26. if( $normal === false ){
  27. $normal = $bad;
  28. }
  29. if( $retina === false ){
  30. $retina = $normal;
  31. }
  32. $normal .= "?v=".$model->rev;
  33. $retina .= "?v=".$model->rev;
  34. $bad .= "?v=".$model->rev;
  35. $picture = $normal;
  36. if( $model->show_author == 'Y' )
  37. {
  38. $author = Authors::getAuthor( $model->author );
  39. $authorName = $author->name;
  40. }else{
  41. $authorName = 'ИА Амител';
  42. }
  43. $title = Html::encode( ( trim($model->meta_title) == '' )?$model->title:$model->meta_title );
  44. $this->title = $title;
  45. $this->registerLinkTag([ 'rel' => 'canonical', 'href' => $model->getUrl(true)], 'canonical');
  46. $lid = Html::encode( ($model->lid && trim($model->lid) <> '')?$model->lid:mb_substr( trim($model->text) ,0,150)."..." );
  47. //$ampProducer = new AmpProducer(['model'=>$model]);
  48. $converter = new Converter([
  49. 'image_base_url' => 'https://www.amic.ru',
  50. 'image_timeout' => 5
  51. ]);
  52. //Чистка
  53. $s = preg_replace( '/<meta([^\>]+)>/i', '<p>', $model->text );
  54. $sa = explode( '+++', $s );
  55. $s = $sa[0];
  56. $pattern = array( '/<style[^>]*>[^>]*>/i', '/<\/?font[^>]*>/i', '/sp_eventWasSet="on"/i' );
  57. $s = preg_replace( $pattern, '', $s );
  58. $s = preg_replace( '/<lj([^\>]+)>/i', '<p>', $s );
  59. $s = preg_replace( '/<\/lj>/i', '</p>', $s );
  60. $s = preg_replace( '/<asid([^\>]*)>/i', '<p>', $s );
  61. $s = preg_replace( '/<\/asid>/i', '</p>', $s );
  62. $s = preg_replace( '/<br([^\>]*)>/i', '<br>', $s );
  63. $s = preg_replace( '/<em([^\>]*)>/i', '<i>', $s );
  64. $s = preg_replace( '/<\/em>/i', '</i>', $s );
  65. $s = preg_replace( '/<hc([^\>]+)>/i', '<p>', $s );
  66. $s = preg_replace( '/<\/hc>/i', '</p>', $s );
  67. $s = preg_replace( '/<col([^\>]+)>/i', '', $s );
  68. $s = preg_replace( '/<\/col>/i', '', $s );
  69. $s = preg_replace( '/<st1([^\>]+)>/i', '', $s );
  70. $s = preg_replace( '/<\/st1>/i', '', $s );
  71. $s = preg_replace( '/<form([^\>]+)>/i', '<p>', $s );
  72. $s = preg_replace( '/<\/form>/i', '</p>', $s );
  73. $s = preg_replace( '/<noindex>/i', '<p>', $s );
  74. $s = preg_replace( '/<\/noindex>/i', '</p>', $s );
  75. $s = preg_replace( '/<pl>/i', '', $s );
  76. $s = preg_replace( '/<\/pl>/i', '', $s );
  77. $s = preg_replace( '/<index>/i', '<p>', $s );
  78. $s = preg_replace( '/<\/index>/i', '</p>', $s );
  79. $s = preg_replace( '/<hypernews>/i', '<p>', $s );
  80. $s = preg_replace( '/<\/hypernews>/i', '</p>', $s );
  81. $s = preg_replace( '/<content>/i', '<p>', $s );
  82. $s = preg_replace( '/<\/content>/i', '</p>', $s );
  83. $s = preg_replace( '/<tspan([^\>]+)>/i', '<span>', $s );
  84. $s = preg_replace( '/<\/tspan>/i', '</span>', $s );
  85. $s = preg_replace( '/<text([^\>]+)>/i', '<p>', $s );
  86. $s = preg_replace( '/<\/text>/i', '</p>', $s );
  87. $s = preg_replace( '/\<metricconverter([^\>]*)\>/iux', 'span', $s );
  88. $s = preg_replace( '/metricconverter/iu', 'span', $s );
  89. $s = preg_replace( '/<pre([^\>]+)>/ix', '', $s );
  90. $s = preg_replace( '/<\/pre>/i', '', $s );
  91. $s = preg_replace( '/<tmp([^\>]*)>/ix', '', $s );
  92. $s = preg_replace( '/<\/tmp>/i', '', $s );
  93. $s = preg_replace( '/<nowiki([^\>]*)>/ix', '', $s );
  94. $s = preg_replace( '/<\/nowiki>/i', '', $s );
  95. $s = preg_replace( '/<foreach([^\>]+)>/ix', ' ', $s );
  96. $s = preg_replace( '/<\/foreach>/i', ' ', $s );
  97. $s = preg_replace( '/<link([^\>]+)>/ix', '', $s );
  98. $s = preg_replace( '/<\/link>/i', '', $s );
  99. $s = str_replace( '<h ', '<h3 ', $s );
  100. $s = str_replace( '/h>', '/h3> ', $s );
  101. $s = str_replace( 'xmlns:fn', 'uri ', $s );
  102. $s = str_replace( 'publib/gimage.php?image=/', '', $s );
  103. //
  104. $converter->loadDefaultConverters();
  105. $converter->addConverter(new AMPDiv());
  106. $converter->addConverter(new AMPSpan());
  107. $converter->addConverter(new AMPAhref());
  108. $converter->addConverter(new AMPP());
  109. $content = $converter->convert($s);
  110. $content = str_replace( 'src="/', 'src="https://www.amic.ru/', $content ); //??
  111. $content = str_replace( 'http://', 'https://', $content );
  112. $content = str_replace('<div>&nbsp;</div>','', $content);
  113. $amp_scripts = $converter->getScripts();
  114. $yurl = $model->getYoutubeEmbedLink();
  115. $shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
  116. $longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
  117. $youtube_key = false;
  118. if (preg_match($longUrlRegex, $yurl, $matches)) {
  119. $youtube_key = $matches[count($matches) - 1];
  120. }
  121. if (preg_match($shortUrlRegex, $yurl, $matches)) {
  122. $youtube_key = $matches[count($matches) - 1];
  123. }
  124. if( $youtube_key ){
  125. $amp_scripts[] ='<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>';
  126. }
  127. $re = '/##_gallery-(\d+)##/mU';
  128. if( preg_match($re, $content) ){
  129. $content = processGalleriesInjects($content, $model);
  130. $amp_scripts[] ='<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>';
  131. }
  132. $this->context->scripts = '';
  133. foreach( array_unique($amp_scripts) as $p_scripts ){
  134. $this->context->scripts .= $p_scripts."\n";
  135. }
  136. $text = trim($content);
  137. $text = str_replace(["\n","\r","&nbsp;"],'',$text);
  138. $text = str_replace(["&nbsp;"],' ',$text);
  139. $text = addslashes( strip_tags( $text ) );
  140. ?>
  141. <!-- Schema.org -->
  142. <script type="application/ld+json">
  143. {
  144. "@context": "http://schema.org",
  145. "@type": "NewsArticle",
  146. "mainEntityOfPage": {
  147. "@type": "WebPage",
  148. "@id": "<?= $model->getUrl(true) ?>"
  149. },
  150. "identifier":{"@type":"PropertyValue","propertyID":"WebPage","value":"<?= $model->uid?>"},
  151. "headline": "<?= $title ?>",
  152. "description": "<?= $lid ?>",
  153. <?php if ($picture) :?>
  154. "image": {
  155. "@type": "ImageObject",
  156. "representativeOfPage": "true",
  157. "url": "<?= $picture; ?>",
  158. "width": 1040,
  159. "height": 586
  160. },
  161. <?php endif ;?>
  162. "dateCreated": "<?= date(DATE_RSS, strtotime($model->dt_cr)); ?>",
  163. "dateModified": "<?= date(DATE_RSS, strtotime($model->dt_upd)); ?>",
  164. "datePublished": "<?= date(DATE_RSS, strtotime($model->dt_pub)); ?>",
  165. "author": {
  166. "@type": "Person",
  167. "name": "<?=$authorName ?>"
  168. },
  169. "publisher": {
  170. "@type": "Organization",
  171. "name": "<?= Yii::$app->params['orgname'] ?>",
  172. "logo": {
  173. "@type": "ImageObject",
  174. "url": "<?= Url::to('/img/amic-logo.svg', 'https') ?>",
  175. "width": 91,
  176. "height": 28
  177. }
  178. }
  179. }
  180. </script>
  181. <!-- !Schema.org -->
  182. <div class="block px3 pt2 mb2" data-newsId="<?= $model->id; ?>">
  183. <time class="pubdate block bold my1"><?= strftime('%e %B %Y, %H:%M', strtotime($model->dt_pub)); ?>, <?=$authorName?></time>
  184. <h1 class="mb1 px3"><?= $model->title ?></h1>
  185. <?php if (!empty($picture) && $model->photo_include == 'Y') : ?>
  186. <div class="amp-main-img">
  187. <amp-img class="amp-main-img my3" alt="<?= \yii\helpers\Html::encode($model->title) ?>" src="<?= $picture; ?>" width="1040"
  188. height="586"
  189. layout="responsive"></amp-img>
  190. <div class="amp-img-title">
  191. <?= $model->photo_title; ?>
  192. </div>
  193. </div>
  194. <?php endif; ?>
  195. <?php if (!empty($model->embed_url)) : ?>
  196. <div class="amp-main-img">
  197. <amp-youtube data-videoid="<?= $youtube_key?>" width="358" height="204" layout="responsive" >
  198. <amp-img src="<?= $picture?>" placeholder layout="fill" alt="<?= $model->photo_title; ?>"/>
  199. </amp-youtube>
  200. </div>
  201. <?php endif; ?>
  202. <div class="amp-preview"><?= $model->lid; ?></div>
  203. <div class="amp-news-content-text">
  204. <?= $content ?>
  205. </div>
  206. <a href="<?= $model->getUrl($schema=true) ?>" class="amp-button">Читайте полную версию на сайте</a>
  207. <section class="text">
  208. <?=($model->inscription == 2)?'<span class="advertisement">На правах рекламы</span>':''?>
  209. <?=($model->inscription == 1)?'<span class="advertisement">На правах рекламы</span><br><span class="med_advertisement">Имеются противопоказания, необходима консультация специалиста</span>':''?>
  210. <?=($model->inscription == 3)?'<span class="advertisement">Партнёр amic.ru</span>':''?>
  211. </section>
  212. <?=$this->render('@app/views/_etc/banners/amp1')?>
  213. </div>
  214. <?
  215. function processGalleriesInjects(string $body, News $post)
  216. {
  217. $re = '/##_gallery-(\d+)##/mU';
  218. $res = preg_replace_callback($re,function (array $matches): string
  219. {
  220. $gallery = \app\models\front\Gallery::findOne(['id'=>ArrayHelper::getValue($matches,1)]);
  221. if($gallery instanceof \app\models\front\Gallery) {
  222. return \Yii::$app->view->render("/news/view/amp-gallery",["gallery"=>$gallery]);
  223. } else {
  224. return "";
  225. }
  226. },$body);
  227. return $res;
  228. }