<?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:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:georss="http://www.georss.org/georss">
    <channel>
<?php if (isset($channel['title'])) : ?>
        <title><?= $channel['title']; ?></title>
<?php endif; ?>
<?php if (isset($channel['clink'])) : ?>
        <link><?= Html::encode($channel['clink']); ?></link>
<?php endif; ?>
<description>Вопросы ответы amic.ru</description>
<?php if (isset($channel['language'])) : ?>
        <language><?= $channel['language']; ?></language>
<?php endif; ?>
    <image>
      <url>https://www.amic.ru/img/amic-logo.svg</url>
      <link><?= Html::encode($channel['link']); ?></link>
      <title><?= $channel['title']; ?></title>
    </image>
<?php foreach ($items as $item): ?>
<?php if (isset($item['url']) ) : ?>
        <item>
            <link><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></link>
            <guid><?= rtrim(Html::encode($channel['link']),'/'); ?><?= Html::encode($item['url']); ?></guid>
<?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['stext']) ) : ?>
            <content:encoded>
            <![CDATA[
				<?/*<h1><?= ($item['title']) ? $item['title'] : ''; ?></h1>*/?>
				<p><?= ($item['description']) ? $item['description'] : ''; ?></p>
            <?= $item['stext']; ?>
            ]]>
            </content:encoded>
<?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?>" />
    <?
            }
        }
        switch( $item['type'] ){
			case 2:
			case 6:
                    // статья
                    $ganre =  "article";
			case 7:
			case 5:
                    // статья
                    $ganre =  "article";
			default:
                    $ganre = "message";
                    break;
		}


?>

<?php endif; ?>
<?php if (isset($item['category'])) : ?>
        <category><?=$item['category'];?></category>
<?php endif; ?>
<?php if ( $item['export_rss'] & 2 ) : ?>
        <category>native-draft</category>
<?php else: ?>
        <category>native-yes</category>
<?php endif; ?>
        </item>
<?php endforeach; ?>
    </channel>
</rss>
