<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet>
<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:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:ouc="http://omniupdate.com/XSL/Variables"

	exclude-result-prefixes="xs ou ouc">
	
	<!-- This call returns all tags that have been created in the account. -->
	<xsl:function name="ou:get-all-tags">
		<xsl:copy-of select="doc('ou:/Tag/GetAllTags')/tags" />
	</xsl:function>
	
	<!-- This call returns the tags that have been manually applied to a particular page. This does not include any fixed tags that are enforced at the directory level. -->
	<xsl:function name="ou:get-tags">
		<xsl:param name="page-path" />
		<xsl:param name="site" />
		<xsl:copy-of select="doc('ou:/Tag/GetTags?site=' || $site || '&amp;path=' || encode-for-uri($page-path))/tags" />
	</xsl:function>
	
	<!-- This call returns the tags that have been applied to a particular directory through Tag Access Settings. -->
	<xsl:function name="ou:get-fixed-tags">
		<xsl:param name="dir" />
		<xsl:param name="site" />
		<xsl:copy-of select="doc('ou:/Tag/GetFixedTags?site=' || $site || '&amp;path=' || encode-for-uri($dir))/tags" />
	</xsl:function>
	
	<!-- This call returns the combination of the manually-applied tags and fixed tags for a given file (essentially combining GetTags and GetFixedTags for a given file). -->
	<xsl:function name="ou:get-combined-tags">
		<xsl:param name="page-path" />
		<xsl:param name="site" />
		<xsl:copy-of select="doc('ou:/Tag/GetCombinedTags?site=' || $site || '&amp;path=' || encode-for-uri($page-path))/tags" />
	</xsl:function>
	
	<!-- This call returns any collections that a given tag belongs to. -->
	<xsl:function name="ou:get-collection-tags">
		<xsl:param name="tag" />
		<xsl:copy-of select="doc('ou:/Tag/GetParents?tag=' || $tag)/tags" />
	</xsl:function>
	
	<!-- This call returns any children of a given collection. -->
	<xsl:function name="ou:get-children-tags">
		<xsl:param name="collection" />
		<xsl:copy-of select="doc('ou:/Tag/GetChildren?tag=' || encode-for-uri($collection))/tags" />
	</xsl:function>
	
	<!-- This call returns any file that contains all of the listed tags. -->
	<xsl:function name="ou:get-files-with-all-tags">
		<xsl:param name="tags" />
		<xsl:variable name="tag-string">
			<xsl:for-each select="tokenize(string-join($tags, ','), ',')">
				<xsl:value-of select="'&amp;tag=' || encode-for-uri(.)" />
			</xsl:for-each>
		</xsl:variable>
		<xsl:copy-of select="doc('ou:/Tag/GetFilesWithAllTags?site=' || $ou:site || $tag-string)/pages" />
	</xsl:function>
	
	<!-- This call returns any file that contains no tags. -->
	<xsl:function name="ou:get-files-with-no-tags">
			<xsl:param name="site" />
<!-- 			<xsl:variable name="dir_list">
				<xsl:copy-of select="doc('/var/staging/oucampus/usao/www')/list"></xsl:copy-of>
			</xsl:variable> -->
<!-- 			DIR list<xsl:copy-of select="$dir_list"></xsl:copy-of>END DIR LIST -->
<!-- 			<xsl:for-each select="$dir_list/list/directory">
				DIR:<xsl:value-of select="."></xsl:value-of>
			</xsl:for-each> -->
		<xsl:variable name="one"><file>sentinalFL</file></xsl:variable>
<!-- do NOT modify  -->
		<xsl:variable name="two"><directory>SentinalDir</directory></xsl:variable>
		FUNCTION OUTPUT start <xsl:copy-of select="ou:create-file-list('/var/staging/oucampus/usao/www', $one, $two, '0')"></xsl:copy-of> FUNTION OUTPUT end
			
	</xsl:function>
	<xsl:function name="ou:create-file-list">
		<xsl:param name="next_dir"></xsl:param>
		<xsl:param name="file_list"></xsl:param>
		<xsl:param name="dir_list"></xsl:param>
		<xsl:param name="times_recursed"></xsl:param>
		<xsl:choose>
		<xsl:when test="$next_dir='SentinalDir'">
			<!-- return file list -->
			<xsl:copy-of select="$file_list"></xsl:copy-of>	
			TIMES RECURSED <xsl:copy-of select="$times_recursed"></xsl:copy-of>
		</xsl:when>
		<xsl:otherwise>
			
				<xsl:variable name="curr_list">
					<xsl:copy-of select="doc($next_dir)/list"></xsl:copy-of>
				</xsl:variable>
				
				<xsl:variable name="curr_fl">
					<xsl:for-each select="$curr_list/list/file">
						<file>
							<xsl:choose>
								<xsl:when test="$next_dir='/var/staging/oucampus/usao/www'">/<xsl:value-of select="."></xsl:value-of></xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="replace($next_dir, '/var/staging/oucampus/usao/www',  '')"></xsl:value-of>/<xsl:value-of select="."></xsl:value-of>
								</xsl:otherwise>
							
								</xsl:choose>
						</file>
					</xsl:for-each>
				</xsl:variable>
				<xsl:variable name="curr_dl">
					<xsl:for-each select="$curr_list/list/directory">
						<directory>
							<xsl:value-of select="$next_dir"></xsl:value-of>/<xsl:value-of select="."></xsl:value-of>
						</directory>
					</xsl:for-each>

				</xsl:variable>

				<xsl:variable name="next_dl">
					<xsl:copy-of select="$dir_list"></xsl:copy-of>
					<xsl:copy-of select="$curr_dl"></xsl:copy-of>
				</xsl:variable>
				<xsl:variable name="next_fl">
					<xsl:copy-of select="$file_list"></xsl:copy-of>
					<xsl:copy-of select="$curr_fl"></xsl:copy-of>
				</xsl:variable>

				<xsl:variable name="next_node" select='$next_dl/directory[count($next_dl/directory)]/text()'></xsl:variable>
				<xsl:variable name="next_dl_min_one">
					<xsl:for-each select="$next_dl/directory">
					<xsl:if test='position()!=count($next_dl/directory)'>
						<xsl:copy-of select="."></xsl:copy-of>
						</xsl:if>
					</xsl:for-each>
				</xsl:variable>
				
				<xsl:variable name="times_recursed_next" select="string(number($times_recursed)+1)"></xsl:variable>
				<xsl:copy-of select="ou:create-file-list($next_node ,$next_fl, $next_dl_min_one, $times_recursed_next)"></xsl:copy-of>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	
	<!-- This call returns any file that contains one or more of the listed tags. -->
	<xsl:function name="ou:get-files-with-any-tags">
		<xsl:param name="tags" />
		<xsl:param name="site" />
		<xsl:variable name="tag-string">
			<xsl:for-each select="tokenize(string-join($tags, ','), ',')">
				<xsl:value-of select="'&amp;tag=' || encode-for-uri(.)" />
			</xsl:for-each>
		</xsl:variable>
		<xsl:copy-of select="doc('ou:/Tag/GetFilesWithAnyTags?site=' || $site || $tag-string)/pages" />
	</xsl:function>
	
	<!-- outputs parent directory path without trailing '/': /path/to/parent -->
	<xsl:function name="ou:parent-path-no-slash">
		<xsl:param name="path" as="xs:string?" />
		<xsl:variable name="token-path" select="tokenize(substring($path, 2), '/')[if(substring($path,string-length($path)) = '/') then position() != last() else position()]" />
		<xsl:variable name="new-path" select="concat('/', string-join(remove($token-path, count($token-path)), '/'))"/>
		<xsl:value-of select="$new-path" />
	</xsl:function>
	
	<!-- function to help with listing tags -->
	<xsl:function name="ou:list-tags">
		<xsl:param name="tag-group" />
		<xsl:param name="helper-text" />
		<xsl:param name="type" />
		<xsl:choose>
			<xsl:when test="$tag-group/tag">
				<ul>
					<xsl:for-each select="$tag-group/tag">
						<xsl:sort select="name => lower-case()" />
						<li>
							<strong><xsl:value-of select="name" /></strong> - 
							<xsl:value-of select="disabled" /> - <xsl:value-of select="children" />
						</li>
					</xsl:for-each>
				</ul>
			</xsl:when>
			<xsl:otherwise>
				<ul>
					<li>No <xsl:value-of select="$helper-text" /> Tags for this <xsl:value-of select="$type" /></li>
				</ul>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	
</xsl:stylesheet>