<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY amp   "&#38;">
<!ENTITY copy   "&#169;">
<!ENTITY gt   "&#62;">
<!ENTITY hellip "&#8230;">
<!ENTITY laquo  "&#171;">
<!ENTITY lsaquo   "&#8249;">
<!ENTITY lsquo   "&#8216;">
<!ENTITY lt   "&#60;">
<!ENTITY nbsp   "&#160;">
<!ENTITY quot   "&#34;">
<!ENTITY raquo  "&#187;">
<!ENTITY rsaquo   "&#8250;">
<!ENTITY rsquo   "&#8217;">
]>


<xsl:stylesheet version="3.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:ou="http://omniupdate.com/XSL/Variables"
	xmlns:fn="http://omniupdate.com/XSL/Functions"
	xmlns:ouc="http://omniupdate.com/XSL/Variables"
	exclude-result-prefixes="ou xsl xs fn ouc"
	expand-text="yes">
	
	<xsl:import href="../common.xsl"/>
	
	<xsl:output method="html" version="4.0"/>
	
	<xsl:template name="template-headcode"/>
	<xsl:template name="template-footcode"/>
	
	<xsl:param name="output"/>
	<xsl:param name="title" select="ou:pcf-param('title') => normalize-space()"/>
	<xsl:param name="description" select="ou:pcf-param('description') => normalize-space()"/>
	
	<xsl:variable name="search-root" select="if(ou:pcf-param('search-root') != '') then normalize-space(ou:pcf-param('search-root')) else '/'" />
	<xsl:variable name="filters">
		<!-- location filter is used so that only blog posts that are located in a given folder and its subfolders are returned -->
		<xsl:text>[starts-with(@href,'{replace($search-root, '/[^/]+$', '/')}')]</xsl:text>
		<xsl:if test="ou:pcf-param('post-author')!=''">
			<xsl:text>[author/text()='{ou:pcf-param('post-author')}']</xsl:text>
		</xsl:if>
		<xsl:if test="string-length(ou:pcf-param('post-year'))=4">
			<xsl:text>[contains(pubDate/text(), '{ou:pcf-param('post-year')}')]</xsl:text>
		</xsl:if>
		
		<xsl:if test="$tags!=''">
			<xsl:variable name="logical-operator" select="if(ou:pcf-param('filter-option') = 'all') then ' and ' else ' or '" />
			
			<xsl:text>[</xsl:text>
			<xsl:for-each select="tokenize($tags, ',')">
				<xsl:choose>
					<xsl:when test="contains(., '''')">
						<xsl:value-of select="'tags/tag=&quot;' || .=>normalize-space()=>replace('&quot;', '') || '&quot;'" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="'tags/tag=''' || normalize-space(.) || ''''" />
					</xsl:otherwise>
				</xsl:choose>
				<xsl:if test="position() != last()">
					<xsl:value-of select="$logical-operator" />
				</xsl:if>
			</xsl:for-each>
			<xsl:text>]</xsl:text>
		</xsl:if>
	</xsl:variable>
	<xsl:variable name="dmc-output" as="node()*">
		<xsl:call-template name="dmc">
			<xsl:with-param name="options">
				<datasource>{ou:pcf-param('dsn')}</datasource>
				<xpath>items/item{$filters}</xpath>
				<type>rss</type>
				<sort>date(pubDate) desc</sort>
				<title>{$title}</title>
				<link>{$domain}</link>
				<description>{$description}</description>
			</xsl:with-param>
			
			<xsl:with-param name="script-name">blogs</xsl:with-param>
			<xsl:with-param name="debug" select="false()" />
		</xsl:call-template>
	</xsl:variable>
	
	<xsl:template name="rss-feed">
		<xsl:choose>
			<xsl:when test="$is-pub">
				<xsl:choose>
					<xsl:when test="$server-type = 'php'">
						<xsl:copy-of select="$dmc-output"/>
					</xsl:when>
					<xsl:when test="$server-type = 'csharp'">
						<xsl:text disable-output-escaping="yes">&lt;%@ Page Language="C#" %&gt;</xsl:text>
						<xsl:text disable-output-escaping="yes" expand-text="no">&lt;% </xsl:text>
						OUC.BlogsDMC blogs = new OUC.BlogsDMC();
						<xsl:text disable-output-escaping="yes" expand-text="no">%&gt;</xsl:text>
						<xsl:value-of select="$dmc-output" disable-output-escaping="yes"/>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$dmc-output" disable-output-escaping="yes"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="/document">
		<xsl:text disable-output-escaping="yes" expand-text="no">&lt;!doctype html&gt;</xsl:text>
		<xsl:next-match/>
	</xsl:template>
	<xsl:template match="/document[$output = 'rss']">
		<xsl:call-template name="rss-feed"/>
	</xsl:template>
	
	<xsl:template name="page-content">
		<main class="main-container" id="top">
			<div class="main-content" id="content">
				<section class="section section-news" aria-label="">
					<div class="section-container">
						<div class="layout layout-sidebar">
							<div class="layout-col">
								<div class="news-module">
									<h1 class="news-module-title">RSS Feed Items</h1>
								</div>
							</div>
						</div>
					</div>
				</section>
				<div class="container-inner">
					<div id="news-tabContent" class="menu-tabs-container tab-content">
						<h2>Overview</h2>
						<p>A quick overview of the RSS feed is displayed below.</p>
						<p>If you are interested in previewing the actual XML file, use the preview dropdown toggle in the top right portion of the CMS to view the actual XML document that is produced.</p>
						<xsl:apply-templates select="$dmc-output" mode="dmc-output"/> <!-- grab the "debug info" link -->
					</div>
				</div>
			</div>
		</main>
	</xsl:template>
	
	<xsl:mode name="dmc-output" on-no-match="shallow-copy"/>
	<xsl:template match="text()[. => starts-with('&lt;')]" mode="dmc-output">
		<xsl:try>
			<xsl:apply-templates select="parse-xml(.)" mode="feed"/>
			
			<xsl:catch>
				<pre>
					<xsl:value-of select="." />
				</pre>
			</xsl:catch>
		</xsl:try>
	</xsl:template>
	
	<xsl:mode name="feed" on-no-match="shallow-skip"/>
	<xsl:template match="channel" mode="feed">
		<h2>Feed Information</h2>
		<xsl:apply-templates select="description|title" mode="#current"/>
		
		<h2>Feed Items</h2>
		<xsl:choose>
			<xsl:when test="item">
				<ul>
					<xsl:apply-templates select="item" mode="#current"/>
				</ul>
			</xsl:when>
			<xsl:otherwise>
				<p>No items match your filters. This RSS feed will not display any items until new articles that match the given filters are created.</p>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="channel/element()[name() = ('title', 'description')]" mode="feed">
		<xsl:choose>
			<xsl:when test="normalize-space(.)">
				<h3><span style="background-color: black; color: white; padding: .2em .3em;">{upper-case(name())}</span> {.}</h3>
			</xsl:when>
			<xsl:otherwise>
				<h3><span style="background-color: red; color: white; padding: .2em .3em;">Error</span> Missing {upper-case(name())}</h3>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="channel/item" mode="feed">
		<li><a href="{link}" target="_blank" style="display:inline">{title}<br/>{pubDate}</a></li>
	</xsl:template>
</xsl:stylesheet>
