<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet>
<!-- 
Implementations Skeletor v3 - 5/10/2014

FUNCTIONS 
Repository of active functions
See GIT for more functions

Variable Dependencies: (see vars xsl)
$serverType

Contributors: Your Name Here
last Updated: Enter Date Here
-->
<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">

	<!--
	ou:includeFile()
	The following function takes in two parameters
	(directory name and file name) and outputs the 
	proper code to include the file on the page.
	-->
	<xsl:function name="ou:includeFile">
		<!-- directory name + file name -->
		<xsl:param name="fullpath" />
		<!-- on publish, it will output the proper SSI code, but on staging we require the omni div tag -->
		<xsl:choose>
			<xsl:when test="$ou:action = 'pub'">
				<xsl:copy-of select="ou:ssi($fullpath)" />
			</xsl:when>
			<xsl:otherwise>
				<ouc:div label="{$fullpath}" path="{$fullpath}" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	
	<xsl:function name="ou:ssi">
		<xsl:param name="fullpath"/>
		<xsl:comment>#include virtual="<xsl:value-of select="$fullpath" />" </xsl:comment>
		<!--<xsl:processing-instruction name="php"> include($_SERVER['DOCUMENT_ROOT'] . "<xsl:value-of select="$fullpath" />"); ?</xsl:processing-instruction>-->
	</xsl:function>
		
	<!-- 
	PCF PARAMS
	An extremely useful function for getting page properties without needing to type the full xpath.
	How to use:
	The pcf has a parameter, name="pagetype". To get the value and store it in an XSL param : 
	<xsl:param name="pagetype" select="ou:pcfparam('pagetype')"/> 
	Use wherever you need it
	-->
	<xsl:param name="pcfparams" select="/document/descendant::parameter"/> <!-- save all page properties in a variable -->

	<xsl:function name="ou:pcfparam">
		<xsl:param name="name"/>
		<xsl:variable name="parameter" select="$pcfparams[@name=$name]"/>
		<xsl:choose>
			<xsl:when test="$parameter/@type = 'select' or $parameter/@type = 'radio'">
				<xsl:value-of select="$parameter/option[@selected = 'true']/@value"/>
			</xsl:when>
			<xsl:when test="$parameter/@type = 'checkbox'">
				<xsl:copy-of select="$parameter/option[@selected = 'true']/@value"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$parameter/text()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
		
	<!-- 
	ASSIGN VARIABLE
	Concisely assign fallback variables to prevent unexpected errors in development and post implementation.
	
	How to use:
	<xsl:param name="galleryType" select="ou:assignVariable('galleryType','PrettyPhoto')"/>	
	<xsl:variable name="navfile" select="ou:assignVariable('page-nav',$ou:navigation,$local-nav)"/>
	
	Third parameter may be a string or variable. The first parameter must always be a string, as required by the pcfparam function.
	
	Second version requires overwriteDirectory variable, which is typically defined in vars xsl.
	
	<xsl:variable name="overwriteDirectory" select="ou:assignVariable('overwriteDirectory','no')"/> 
	
	
	Note: requires the function ou:pcfparams
	-->
	<xsl:function name="ou:assignVariable"> <!-- test if page property has value, give default value if none -->
		<xsl:param name="var"/>
		<xsl:param name="fallback"/>
		<xsl:variable name="pcf-var" select="ou:pcfparam($var)"/>
		<xsl:choose>	
			<xsl:when test="string-length($pcf-var) > 0"><xsl:value-of select="$pcf-var"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
		</xsl:choose>	
	</xsl:function>		
	
	<xsl:function name="ou:assignVariable"> <!-- pcf - dir var - fallback precedence, also tests if there is a value for each -->
		<xsl:param name="var"/>
		<xsl:param name="dir-var"/>
		<xsl:param name="fallback"/>
		<xsl:variable name="overwriteDirectory" select="ou:pcfparam('overwriteDirectory')"/>
		<xsl:variable name="pcf-var" select="ou:pcfparam($var)"/>
		<xsl:choose>	
			<xsl:when test="string-length($pcf-var) > 0 and $overwriteDirectory='yes'"><xsl:value-of select="$pcf-var"/></xsl:when>
			<xsl:when test="string-length($dir-var) > 0 and $dir-var!='[auto]'" ><xsl:value-of select="$dir-var"/></xsl:when> 
			<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
		</xsl:choose>	
	</xsl:function>
		
	<!-- 
	TEST VARIABLE
	A simpler version of Assign Variable, if you are not dealing with page properties.
	-->		
	<xsl:function name="ou:testVariable"> <!-- test if variable has value, give default value if none -->
		<xsl:param name="var"/>
		<xsl:param name="fallback"/>
		<xsl:choose>	
			<xsl:when test="string-length($var) > 0"><xsl:value-of select="$var"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="$fallback"/></xsl:otherwise>
		</xsl:choose>	
	</xsl:function>	
		
	<!--
	FIND PREVIOUS DIRECTORY
	Used by breadcrumbs xsl.
	-->
	<xsl:function name="ou:findPrevDir"> <!-- outputs parent directory path with trailing '/': /path/to/parent/ -->
		<xsl:param name="path" />
		<xsl:variable name="tokenPath" select="tokenize(substring($path, 2), '/')[if(substring($path,string-length($path)) = '/') then position() != last() else position()]" />
		<xsl:variable name="newPath" select="concat('/', string-join(remove($tokenPath, count($tokenPath)), '/') ,'/')"/>
		<xsl:value-of select="if($newPath = '//') then '/' else $newPath" />
	</xsl:function>
	
	<!--
	OUC MULTIEDIT
	Manually create multiedit button so that users can access multiedit, if no multiedit nodes are being copied (ie, value-of is used)
	-->		
	<xsl:function name="ou:multieditButton"> <!-- call function to activate the multiedit button in edit mode -->
		<xsl:if test="$ou:action='edt'">
			<ouc:div label="multiedit" group="everyone" button="hide"><ouc:multiedit /></ouc:div>
		</xsl:if>
	</xsl:function>

	<!--
	HAS CLASS
	boolean that returns whether an element contains a class, much like JQuery's hasClass function
	-->
	<xsl:function name="ou:hasClass">
		<xsl:param name="element"/>
		<xsl:param name="classname"/>
		<xsl:value-of select="boolean($element/@class) and contains(concat(' ', $element/@class, ' '), concat(' ', $classname, ' '))"/>
	</xsl:function>	
	
	<!--
	GET CURRENT FOLDER
	-->
	<xsl:function name="ou:current_folder">
		<xsl:param name="string"/>
		<xsl:variable name="chars" select="tokenize(substring-after($string,'/'), '/')"/>
		<xsl:for-each select="$chars">
			<xsl:if test="position()=last()">
				<xsl:variable name="result"><xsl:value-of select="."/></xsl:variable>
				<!-- <xsl:value-of select="ou:capital(replace($result,'-',' '))" /> -->
				<!-- copy the when statement for more cleaning options -->
				<xsl:choose>
					<xsl:when test="contains($result,'_')">
						<xsl:value-of select="ou:clean($result,'_')"/>
					</xsl:when>
					<xsl:when test="contains($result,'-')">
						<xsl:value-of select="ou:clean($result,'-')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="ou:capital($result)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</xsl:for-each>
	</xsl:function>
	
	<!--
	CAPITALIZE THE FIRST LETTER OF EVERY WORD
	-->
	<xsl:function name="ou:capital">
		<xsl:param name="string"/>
		<xsl:variable name="chars" select="tokenize($string, ' ')"/>
		<xsl:for-each select="$chars">
			<xsl:variable name="key"><xsl:value-of select="."/></xsl:variable>
			<xsl:variable name="firstletter1"><xsl:value-of select="upper-case(substring($key,1,1))" /></xsl:variable>
			<xsl:variable name="rest1"><xsl:value-of select="lower-case(substring($key,2))" /></xsl:variable>
			<xsl:variable name="result"><xsl:value-of select="concat($firstletter1,$rest1,' ')" /> </xsl:variable>
			<xsl:value-of select="$result" />
		</xsl:for-each>
	</xsl:function>
	
	<!--
	Clean content by replacing content with space
	-->
	<xsl:function name="ou:clean">
		<xsl:param name="string"/>
		<xsl:param name="deliminator"/>
		<xsl:variable name="chars" select="tokenize($string,$deliminator)"/>
		<xsl:for-each select="$chars">
			<xsl:variable name="key"><xsl:value-of select="."/></xsl:variable>
			<xsl:variable name="firstletter1"><xsl:value-of select="upper-case(substring($key,1,1))" /></xsl:variable>
			<xsl:variable name="rest1"><xsl:value-of select="lower-case(substring($key,2))" /></xsl:variable>
			<xsl:variable name="result"><xsl:value-of select="concat($firstletter1,$rest1,' ')" /> </xsl:variable>
			<xsl:value-of select="$result" />
		</xsl:for-each>
	</xsl:function>
	
	<xsl:function name="ou:get-iso-date-time">
		<xsl:param name="the-date"/>
		<!-- calculate date -->
		<xsl:variable name="date" select="substring-before($the-date, ' ')"/>
		<xsl:variable name="M" select="substring-before($date, '/')"/>
		<xsl:variable name="D-Y" select="substring-after($date, '/')"/>
		<xsl:variable name="D" select="substring-before($D-Y, '/')"/>
		<xsl:variable name="Y" select="substring-after($D-Y, '/')"/>
		<!-- calculate time -->
		<xsl:variable name="time-ampm" select="substring-after($the-date, ' ')"/>
		<xsl:variable name="time" select="substring-before($time-ampm, ' ')"/>
		<xsl:variable name="ampm" select="substring-after($time-ampm, ' ')"/>
		<xsl:variable name="h" select="substring-before($time, ':')"/>
		<xsl:variable name="m-s" select="substring-after($time, ':')"/>
		<xsl:variable name="m" select="substring-before($m-s, ':')"/>
		<xsl:variable name="s" select="substring-after($m-s, ':')"/>
		
		<xsl:variable name="hh">
			<xsl:choose>
				<xsl:when test="$ampm = 'PM'">
					<xsl:value-of select="format-number(number($h) + 12, '00')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="format-number(number($h), '00')"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="concat($Y, '-', $M, '-', $D, 'T', $hh, ':', $m, ':', $s)"/> 
	</xsl:function>
	
</xsl:stylesheet>
