<?php
/*
 * to do
 * https://dzen.ru/help/news/export-content/export.html
 * https://yandex.ru/dev/turbo/doc/rss/simple-rss.html
 *
 */
use yii\helpers\Html;
Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
header('Content-Type: application/rss+xml; charset=utf-8');

?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL ?>
<rss version="2.0"
    xmlns:yandex="http://news.yandex.ru"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:turbo="http://turbo.yandex.ru"
>
    <channel>
<?php if (isset($channel['title'])) : ?>
        <title><?= $channel['title']; ?></title>
<?php endif; ?>
<?php if (isset($channel['link'])) : ?>
        <link><?= Html::encode($channel['link']); ?></link>
<?php endif; ?>
<?php if (isset($channel['description'])) : ?>
        <description><?= $channel['description']; ?></description>
<?php endif; ?>
<?php if (isset($channel['language'])) : ?>
        <language><?= $channel['language']; ?></language>
<?php endif; ?>
<?php if (isset($channel['analytics'])) : ?>
        <turbo:analytics><?= $channel['analytics']; ?></turbo:analytics>
<?php endif; ?>
<?php if (isset($channel['adnetwork'])) : ?>
        <turbo:adNetwork><?= $channel['adnetwork']; ?></turbo:adNetwork>
<?php endif; ?>
<?php foreach ($items as $item): ?>
<?php if ( $item['export_rss'] & 1 ) continue; ?>
<?php if (isset($item['url']) ) : ?>
        <item turbo="<?= ($item['status']) ? 'true' : 'false'; ?>">
            <link><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></link>
<?php if (isset($item['title']) ) : ?>
            <title><![CDATA[<?= $item['title']; ?>]]></title>
<?php endif; ?>
<?php if (isset($item['description']) ) : ?>
            <description>
            <![CDATA[
            <?= $item['description']; ?>
            ]]>
            </description>
<?php endif; ?>
            <pubDate><?= date( "r", strtotime( $item['updated_at'] ) ); ?></pubDate>
<?php if (isset($item['newscontent']) ) : ?>
            <yandex:full-text>
            <![CDATA[
				<?/*<h1><?= ($item['title']) ? $item['title'] : ''; ?></h1>*/?>
				<p><?= ($item['description']) ? $item['description'] : ''; ?></p>
            <?= $item['newscontent']; ?>
            ]]>
            </yandex:full-text>
<?php endif; ?>
<?php if ($item['status'] && isset($item['content'])) : ?>
            <turbo:content>
            <![CDATA[
                <header>
                    <?/*<h1><?= ($item['title']) ? $item['title'] : ''; ?></h1>*/?>
                    <p><?= ($item['description']) ? $item['description'] : ''; ?></p>
                </header>
				<figure data-turbo-ad-id="second_ad_1444026-44"></figure>
<?php if (isset($item['image'])) :?>
                <figure>
                    <img src="<?= $item['image']; ?>" />
<?php if (isset($item['image_title'])) :?>
                    <figcaption><?= $item['image_title']; ?></figcaption>
<?php endif; ?>
                </figure>
<?php endif; ?>
                <?= $item['content']; ?>
               <figure data-turbo-ad-id="second_ad_1444026-43"></figure>
            ]]>
            </turbo:content>
<?php endif; ?>
<?php if (isset($item['image'])) :?>
<?
				$r = explode(".", $item['image']);
				if( is_array($r) ){
					if( array_pop($r) == 'webp' ){
						$mtype = "image/webp";
					}else{
						$mtype = "image/jpeg";
					}
				}
?>
			<enclosure url="<?=$item['image']?>" type="<?=$mtype?>" />
<?php endif; ?>
<?
	   preg_match_all('/src="([^"]+\.(jpg|png)).*"/i', $item['content'], $ax );
       foreach ( $ax[1] as $aa ){
            if( !preg_match( "/(\/manager\/|small)/", $aa) ){
				$r = explode(".", $aa);
				if( is_array($r) ){
					if( array_pop($r) == 'png' ){
						$mtype = "image/png";
					}else{
						$mtype = "image/jpeg";
					}
				}
    ?>
			<enclosure url="<?=$aa?>" type="<?=$mtype?>" />
    <?
            }
        }
		if( count( $item['topics'] ) > 0 ){
?>
			<metrics>
                <yandex schema_identifier="<?=$item['uid']?>">
                    <breadcrumblist>
<?
			foreach ( $item['topics'] as $topics ){
?>
						<breadcrumb url="<?=$topics['url']?>" text='<?=str_replace("'",'`',$topics['title'])?>'/>
<?
			}
?>
					</breadcrumblist>
                </yandex>
            </metrics>
<?
		}
        switch( $item['type'] ){
			case 2:
			case 6:
                    // статья
                    $ganre =  "article";
			case 7:
			case 5:
                    // статья
                    $ganre =  "article";
			default:
                    $ganre = "message";
                    break;
		}


?>
		<yandex:genre><?=$ganre?></yandex:genre>
<?php if (isset($item['story'])) : ?>
        <yandex:theme_tags><?=$item['story'];?></yandex:theme_tags>

<?php endif; ?>
<?php if (isset($item['category'])) : ?>
        <category><?=$item['category'];?></category>
<?php endif; ?>
	<author><?=$item['author'];?></author>
        </item>
<?php endif; ?><?php endforeach; ?>
    </channel>
</rss>
