<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp   "&#160;">
<!ENTITY lsaquo   "&#8249;">
<!ENTITY rsaquo   "&#8250;">
<!ENTITY laquo  "&#171;">
<!ENTITY raquo  "&#187;">
<!ENTITY copy   "&#169;">
]>
<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:ouc="http://omniupdate.com/XSL/Variables"
				exclude-result-prefixes="xs ou ouc"
				expand-text="yes">

	<!-- get a single profile XML -->
	<!-- has option to add custom xsl attribute to exclude it from the xml output of the file -->
	<xsl:function name="ou:get-profile">
		<xsl:param name="document" />

		<xsl:variable name="email" select="$document/profile/ouc:div[@label='email']/text()" />
		<xsl:variable name="image" select="$document/profile/ouc:div[@label='image']/img[@src!='']" />
		<xsl:variable name="xml-url" select="$ou:faculty-directory-data-url" />
		<xsl:variable name="feed-type" select="ou:pcf-param('feed-type')" />

		<xsl:if test="$feed-type = 'faculty' and not(doc-available($ou:faculty-directory-data-url))">
			{error(QName('http://www.omniupdate.com', 'error'), concat('The external xml: "',$ou:faculty-directory-data-url , '" is not accessible.'))}
		</xsl:if>

		<xsl:variable name="display">
			<xsl:choose>
				<xsl:when test="$feed-type = 'faculty' and not(doc($xml-url)/tacomaccDirectory/employees/employee[ou:textual-content(lower-case(@id))=ou:textual-content(lower-case($email))])">
					<!-- set display to false if email not found in external feed -->
					<xsl:text>false</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="if($document/profile/ouc:div[@label='display']/text() = 'true') then 'true' else 'false'" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:element name="{$profile-node}">
			<xsl:attribute name="href" select="concat($dirname, replace($ou:filename, $profile-ext, $extension))" />
			<xsl:attribute name="display" select="$display" />
			<xsl:attribute name="type" select="$feed-type" />

			<xsl:choose>
				<xsl:when test="$feed-type = 'faculty'">
					<email>{$email}</email>
					<image><xsl:apply-templates select="$image" /></image>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="$document/profile/ouc:div[not(@xml = 'exclude')]">
						<xsl:element name="{translate(./@label, ' ', '_')}"><xsl:apply-templates select="node()[not(self::ouc:multiedit)]" /></xsl:element>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>

		</xsl:element>

	</xsl:function>

	<!-- template for getting all the profiles from staging or production depending on what parameters you pass in -->
	<xsl:template name="get-all-profiles">
		<xsl:param name="current-path" select="concat($root,$ou:dirname)" /> <!-- current path -->
		<xsl:param name="skip" select="$ou:stagingpath"/> <!-- what file to skip -->
		<xsl:param name="type" select="'staging'"/> <!-- determine if it is done from staging or production -->
		<xsl:param name="language" select="'php'" /> <!-- determine what server side language the aggregation will be using -->
		<xsl:variable name="skipping" select="tokenize($skip, '/')[last()]" /> <!-- get the word 'index.pcf' or 'default.pcf' -->
		<xsl:if test="not(/document/element()[name() = $profile-node])">
			<xsl:choose>
				<!-- aggregate via server side side code -->
				<xsl:when test="$type = 'production'">
					<xsl:variable name="query-string" select="concat('?dir=', $ou:dirname, '&#38;ext=', $profile-ext, '&#38;node=', $profile-node)" /> <!-- determine the query string -->
					<xsl:variable name="request-location" select="concat($domain, '/_resources/directory')"/> <!-- determine the request location string -->
					<xsl:choose>
						<xsl:when test="$language = 'php'">
							<!-- path to php script -->
							<xsl:variable name="php-loc" select="concat($request-location, '/php/profiles.php', $query-string)" />
							<xsl:if test="not($is-pub)">
								<query-path><xsl:value-of select="$php-loc" /></query-path> <!-- output the query path for debugging -->
							</xsl:if>
							<xsl:copy-of select="document($php-loc)" />
						</xsl:when>
						<xsl:when test="$language = 'csharp'">
							<!-- path to c# script -->
							<xsl:variable name="c-loc" select="concat($request-location, '/cs/profiles.ashx', $query-string)" />
							<xsl:if test="not($is-pub)">
								<query-path><xsl:value-of select="$c-loc" /></query-path> <!-- output the query path for debugging -->
							</xsl:if>
							<xsl:copy-of select="document($c-loc)" />
						</xsl:when>
						<xsl:otherwise>
							<p>Invalid server side language. Please verify/configure <xsl:value-of select="$language" /> is the language you want to use.</p>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<!-- aggregate via XSL -->
				<xsl:otherwise>

					<xsl:variable name="internal-xml">
						<xsl:element name="{$profile-node}s">
							<xsl:call-template name="profiles-with-xsl">
								<xsl:with-param name="current-path" select="replace($current-path, '/$', '')"/> <!-- current path -->
								<xsl:with-param name="skipping" select="$skipping"/> <!-- current path -->
							</xsl:call-template>
						</xsl:element>
					</xsl:variable>
					<profiles>
						<xsl:for-each select="$internal-xml/profiles/profile">
							<xsl:apply-templates select="."/>
						</xsl:for-each>		
					</profiles>
					<xsl:choose>
						<xsl:when test="ou:pcf-param('feed-type') = 'faculty'">
							<xsl:variable name="external-xml">
								<xsl:choose>
									<xsl:when test="doc-available($ou:faculty-directory-data-url)">
										<xsl:copy-of select="doc($ou:faculty-directory-data-url)/tacomaccDirectory/employees"/>
									</xsl:when>
									<xsl:otherwise>
										{error(QName('http://www.omniupdate.com', 'error'), concat('The external xml: "',$ou:faculty-directory-data-url , '" is not accessible.'))}
									</xsl:otherwise>
								</xsl:choose>
							</xsl:variable>



						</xsl:when>
						<xsl:otherwise>							
						</xsl:otherwise>
					</xsl:choose>

				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>


	</xsl:template>

	<xsl:template name="build-internal-profile">
		<xsl:param name="internal-profile" />
		<xsl:param name="external-profile" />

		<profile href="{$internal-profile/@href}" display="true" type="faculty">
			<email>{$internal-profile/email}</email>
			<image><xsl:apply-templates select="$internal-profile/image/node()" /></image>

			<xsl:variable name="name-parts" select="tokenize($external-profile/displayName, ',')" />

			<xsl:choose>
				<xsl:when test="count($name-parts) = 1">
					<firstname></firstname>
					<lastname>{ou:textual-content($name-parts[1])}</lastname>
				</xsl:when>
				<xsl:otherwise>
					<firstname>{ou:textual-content($name-parts[2])}</firstname>
					<lastname>{ou:textual-content($name-parts[1])}</lastname>
				</xsl:otherwise>
			</xsl:choose>

			<positions>
				<xsl:for-each-group select="$external-profile/positions/position" group-by="text()">
					<xsl:sequence select="."/>
				</xsl:for-each-group>
			</positions>
			<showPhoto>{$external-profile/showPhoto}</showPhoto>
			<employeeType>{$external-profile/employeeType}</employeeType>
			<resumeUri>{$external-profile/resumeUri}</resumeUri>
			<department>{$external-profile/department}</department>
			<office>{$external-profile/office}</office>
			<room>{$external-profile/room}</room>
			<phone>{$external-profile/phone}</phone>
			<fax>{$external-profile/fax}</fax>
			<xsl:apply-templates select="$external-profile/classSchedule" />
			<safeZoneMember>{$external-profile/safeZoneMember}</safeZoneMember>
			<xsl:apply-templates select="$external-profile/officeHours" />
		</profile>
	</xsl:template>

	<xsl:template match="employee" mode="external-profile">
		<profile display="true" type="faculty" only-exists-external="true">
			<email>{@id}</email>
			<image></image>

			<xsl:variable name="name-parts" select="tokenize(displayName, ',')" />

			<xsl:choose>
				<xsl:when test="count($name-parts) = 1">
					<firstname></firstname>
					<lastname>{ou:textual-content($name-parts[1])}</lastname>
				</xsl:when>
				<xsl:otherwise>
					<firstname>{ou:textual-content($name-parts[2])}</firstname>
					<lastname>{ou:textual-content($name-parts[1])}</lastname>
				</xsl:otherwise>
			</xsl:choose>

			<positions>
				<xsl:for-each-group select="positions/position" group-by="text()">
					<xsl:sequence select="."/>
				</xsl:for-each-group>
			</positions>
			<showPhoto>{showPhoto}</showPhoto>
			<employeeType>{employeeType}</employeeType>
			<resumeUri>{resumeUri}</resumeUri>
			<department>{department}</department>
			<office>{office}</office>
			<room>{room}</room>
			<phone>{phone}</phone>
			<fax>{fax}</fax>
			<xsl:apply-templates select="classSchedule" />
			<safeZoneMember>{safeZoneMember}</safeZoneMember>
			<xsl:apply-templates select="officeHours" />
		</profile>
	</xsl:template>

	<!-- template to grab all the `profile-ext` xml files from production with XSL -->
	<xsl:template name="profiles-with-xsl">
		<xsl:param name="current-path" /> <!-- current path -->
		<xsl:param name="skipping" /> <!-- what file to skip -->
		<!-- calculate the current path to the production server and replace the root with the domain -->
		<xsl:variable name="domain-dir" select="concat(replace($current-path, $root, $domain), '/')" />
		<xsl:for-each select="doc($current-path)/list/file[contains(text(),'.pcf') and not(contains(text(), $skipping))]">
			<xsl:variable name="file-path" select="concat($domain-dir, replace(text(), 'pcf', $profile-ext))" />
			<xsl:if test="doc-available($file-path)">
				<xsl:copy-of select="doc($file-path)/profile[@type='faculty' or @type='office']" />
			</xsl:if>
		</xsl:for-each> 
		<!-- get all directories -->
		<xsl:for-each select="doc($current-path)/list/directory">
			<xsl:call-template name="profiles-with-xsl">
				<xsl:with-param name="current-path" select="concat($current-path,'/',text())" />
				<xsl:with-param name="skipping" select="$skipping"/> 
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>

</xsl:stylesheet>
