<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:svg="http://www.w3.org/2000/svg">
<!-- $Id: jonshouse.php.xsl,v 1.17 2011/05/24 13:38:09 jbk Exp $
     vim:ts=4
  -->
	<xsl:variable name="gen" select="/jonsbrain/@generated"/>
	<!-- <xsl:variable name="chartlen" select="3600 * 4"/> -->
	<xsl:param name="chartlen" select="3600 * 4" />
	<xsl:param name="includefunction" select="0" />
	<xsl:param name="includealias" select="0" />
	<xsl:param name="displayalias" select="1" />
	<xsl:param name="chartwidth" select="220" />
	<xsl:param name="chartheight" select="120" />
	<xsl:param name="selfurl" />
	<xsl:key name="alias-by-target" match="alias" use="target" />
	<xsl:template match="/">
		<xsl:apply-templates />
	</xsl:template>
	<xsl:template match="jonsbrain">
		<p>Data Read with timestamp: <xsl:value-of select="$gen"/>, Cycle: <xsl:value-of select="/jonsbrain/@cycle"/>, 
		XSLT Version $Id: jonshouse.php.xsl,v 1.17 2011/05/24 13:38:09 jbk Exp $</p>
		<xsl:apply-templates/>
	</xsl:template>
	<xsl:template match="symbols">
		<h1>Symbols (Variable, String, and Function)</h1>
		<table BORDER="1"> 
			<tr>
				<th ROWSPAN="2">Name</th>
				<th ROWSPAN="2">Value/Target</th>
				<th ROWSPAN="2" COLSPAN="3">Cmd</th>
				<th COLSPAN="2">Last Update</th>
				<th COLSPAN="2">Last Change</th>
				<th ROWSPAN="2">Trend</th>
				<th ROWSPAN="2">Hash</th>
			</tr>
			<tr>
				<th>Time</th>
				<th>Cycle</th>
				<th>Time</th>
				<th>Cycle</th>
			</tr>
			<xsl:apply-templates><xsl:sort select="@name"/></xsl:apply-templates>
		</table>
		<hr/>
	</xsl:template>
	<xsl:template match="nextrun|lastrun|update|changed|connected">
		<xsl:choose>
			<xsl:when test=". = 0">
				Forever
			</xsl:when>
			<xsl:when test="$gen = .">
				Now
			</xsl:when>
			<xsl:when test=". > $gen">
				In 
				<xsl:variable name="dy" select="floor((. - $gen) div 86400)"/>
				<xsl:variable name="hr" select="floor(((. - $gen) mod 86400) div 3600)"/>
				<xsl:variable name="mi" select="floor(((. - $gen) mod 3600) div 60)"/>
				<xsl:variable name="se" select="(. - $gen) mod 60"/>
				<xsl:if test="$dy > 0"><xsl:value-of select="$dy"/>d</xsl:if>
				<xsl:if test="$hr > 0"><xsl:value-of select="$hr"/>h</xsl:if>
				<xsl:if test="$mi > 0"><xsl:value-of select="$mi"/>m</xsl:if>
				<xsl:if test="$se > 0"><xsl:value-of select="$se"/>s</xsl:if>
			</xsl:when>
			<xsl:otherwise> 
				<xsl:variable name="dy" select="floor(($gen - .) div 86400)"/>
				<xsl:variable name="hr" select="floor((($gen - .) mod 86400) div 3600)"/>
				<xsl:variable name="mi" select="floor((($gen - .) mod 3600) div 60)"/>
				<xsl:variable name="se" select="($gen - .) mod 60"/>
				<xsl:if test="$dy > 0"><xsl:value-of select="$dy"/>d</xsl:if>
				<xsl:if test="$hr > 0"><xsl:value-of select="$hr"/>h</xsl:if>
				<xsl:if test="$mi > 0"><xsl:value-of select="$mi"/>m</xsl:if>
				<xsl:if test="$se > 0"><xsl:value-of select="$se"/>s</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="variable">
		<xsl:variable name="rows">
			<xsl:choose>
				<xsl:when test="$displayalias > 0">
					<xsl:value-of select="count( . | key('alias-by-target', @name)[1])"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="1"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<form ACTION="{$selfurl}" METHOD="POST">
			<tr BGCOLOR="CYAN">
				<td><xsl:value-of select="@name"/>
					<xsl:apply-templates select="./history"/>
				</td>
				<td ROWSPAN="{$rows}"><input TYPE="hidden" NAME="_var">
						<xsl:attribute name="VALUE"><xsl:value-of select="@name"/></xsl:attribute>
					</input><input TYPE="hidden" NAME="_type" VALUE="var"/><input NAME="_val">
						<xsl:attribute name="VALUE"><xsl:value-of select="./value"/></xsl:attribute>
					</input></td>
				<td ROWSPAN="{$rows}"><input TYPE="Submit" NAME="Change" VALUE="Change"/></td>
				<td><input TYPE="Submit" NAME="Delete" VALUE="Delete"/></td>
				<td ROWSPAN="{$rows}"><input TYPE="Submit" NAME="Purge" VALUE="Purge"/></td>
				<td ROWSPAN="{$rows}"><xsl:apply-templates select="./update"/></td>
				<td ROWSPAN="{$rows}"><xsl:apply-templates select="./updcycle"/></td>
				<td ROWSPAN="{$rows}"><xsl:apply-templates select="./changed"/></td>
				<td ROWSPAN="{$rows}"><xsl:apply-templates select="./chgcycle"/></td>
				<td ROWSPAN="{$rows}">
					<a HREF="jonsimage2.cgi?unit={@name}&amp;duration={$chartlen}&amp;width=780&amp;height=550">
						<xsl:choose>
							<xsl:when test="./update + ./changed = 0">
								N/A
							</xsl:when>
							<xsl:when test="$gen - ./changed > $chartlen">
								<b>Unchanged during graph period</b>
							</xsl:when>
							<xsl:otherwise>
								<img SRC="jonsimage.php?unit={@name}&amp;duration={$chartlen}&amp;width={$chartwidth}&amp;height={$chartheight}" HEIGHT="{$chartheight}" WIDTH="{$chartwidth}" ALT="Graph of {@name}"/>
							</xsl:otherwise>
						</xsl:choose>
					</a>
				</td>
				<td><xsl:value-of select="./hash"/></td>
			</tr>
		</form>
		<xsl:if test="$displayalias > 0">
			<xsl:apply-templates select="key('alias-by-target', @name)" mode="alias-in-variable" />
		</xsl:if>
	</xsl:template>
	<xsl:template match="history">
		<span class="history">
			History
			<svg:svg width="50" height="50" version="1.1">
			<svg:circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
			</svg:svg>
			<span class="historydata">
				Length: <xsl:value-of select="@length"/><br/>
				<xsl:apply-templates select="./historyitem"/>
			</span>
		</span>
	</xsl:template>
	<xsl:template match="historyitem">
		Value: <xsl:value-of select="./value"/><br/>
		Update: <xsl:apply-templates select="./update"/><br/>
	</xsl:template>
	<xsl:template match="strvariable">
		<xsl:variable name="rows">
			<xsl:choose>
				<xsl:when test="$displayalias > 0">
					<xsl:value-of select="count( . | key('alias-by-target', @name)[1])"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="1"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<form ACTION="{$selfurl}" METHOD="POST">
			<tr BGCOLOR="YELLOW">
				<td><xsl:value-of select="@name"/></td>
				<td><input TYPE="hidden" NAME="_var">
						<xsl:attribute name="VALUE"><xsl:value-of select="@name"/></xsl:attribute>
					</input><input TYPE="hidden" NAME="_type" VALUE="str"/><input NAME="_val">
						<xsl:attribute name="VALUE"><xsl:value-of select="./value"/></xsl:attribute>
					</input></td>
				<td ROWSPAN="{$rows}"><input TYPE="Submit" NAME="Change" VALUE="Change"/></td>
				<td><input TYPE="Submit" NAME="Delete" VALUE="Delete"/></td>
				<td ROWSPAN="{$rows}"><input TYPE="Submit" NAME="Purge" VALUE="Purge"/></td>
				<td><xsl:apply-templates select="./update"/></td>
				<td><xsl:apply-templates select="./updcycle"/></td>
				<td><xsl:apply-templates select="./changed"/></td>
				<td><xsl:apply-templates select="./chgcycle"/></td>
				<td>N/A</td>
				<td><xsl:value-of select="./hash"/></td>
			</tr>
		</form>
	</xsl:template>
	<xsl:template match="function">
	    <xsl:if test="$includefunction > 0">
			<tr BGCOLOR="RED">
				<td><xsl:value-of select="@name"/></td>
				<td><xsl:value-of select="./type"/></td>
				<td COLSPAN="8"></td>
				<td><xsl:value-of select="./hash"/></td>
			</tr>
		</xsl:if>
	</xsl:template>
	<xsl:template match="alias">
		<xsl:if test="$includealias > 0">
        	<form ACTION="{$selfurl}" METHOD="POST">
				<tr BGCOLOR="GREEN">
					<td><xsl:value-of select="@name"/></td>
					<td><input TYPE="hidden" NAME="_var">
        	                <xsl:attribute name="VALUE"><xsl:value-of select="@name"/></xsl:attribute>
            	        </input><input TYPE="hidden" NAME="_type" VALUE="alias"/><input NAME="_val">
                	        <xsl:attribute name="VALUE"><xsl:value-of select="./target"/></xsl:attribute>
	                    </input></td>
					<td><input TYPE="Submit" NAME="Change" VALUE="Change"/></td>
					<td><input TYPE="Submit" NAME="Delete" VALUE="Delete"/></td>
					<td></td>
					<td><xsl:apply-templates select="./update"/></td>
					<td><xsl:apply-templates select="./updcycle"/></td>
					<td><xsl:apply-templates select="./changed"/></td>
					<td><xsl:apply-templates select="./chgcycle"/></td>
					<td>N/A</td>
					<td><xsl:value-of select="./hash"/></td>
				</tr>
			</form>
		</xsl:if>
	</xsl:template>
	<xsl:template match="alias" mode="alias-in-variable">
		<form ACTION="{$selfurl}" METHOD="POST">
			<tr BGCOLOR="GREEN">
				<td><xsl:value-of select="@name"/></td>
				<td><input TYPE="hidden" NAME="_var">
                            <xsl:attribute name="VALUE"><xsl:value-of select="@name"/></xsl:attribute>
                        </input><input TYPE="hidden" NAME="_type" VALUE="alias"/>
                        <input TYPE="Submit" NAME="Delete" VALUE="Delete"/></td>
				<td><xsl:value-of select="./hash"/></td>
			</tr>
		</form>
	</xsl:template>
	<xsl:template match="streams">
		<h1>Streams (File, Driver, and Network)</h1>
		<table BORDER="1">
			<tr>
				<th rowspan="2">Name</th>
				<th rowspan="2">Comment</th>
				<th rowspan="2">Handle</th>
				<th rowspan="2">Connected</th>
				<th colspan="2">Last Run</th>
				<th rowspan="2">Next Run</th>
				<th colspan="2">Transactions</th>
				<th colspan="2">Bytes</th>
				<th rowspan="2">Type</th>
				<th rowspan="2">Cmd</th>
			</tr>
			<tr>
				<th>Time</th>
				<th>Cycle</th>
				<th>In</th>
				<th>Out</th>
				<th>In</th>
				<th>Out</th>
			</tr>
			<xsl:apply-templates/>
		</table>
		<hr/>
	</xsl:template>
	<xsl:template match="stream">
		<tr>
			<td><xsl:value-of select="@name"/></td>
			<td><xsl:value-of select="./comment"/></td>
			<td><xsl:value-of select="./handle"/></td>
			<td><xsl:apply-templates select="./connected"/></td>
			<td><xsl:apply-templates select="./lastrun"/></td>
			<td><xsl:value-of select="./cycle"/></td>
			<td><xsl:apply-templates select="./nextrun"/></td>
			<td><xsl:value-of select="./transactionsin"/></td>
			<td><xsl:value-of select="./transactionsout"/></td>
			<td><xsl:value-of select="format-number(./bytesin,'###,###,###,###,###')"/></td>
			<td><xsl:value-of select="format-number(./bytesout,'###,###,###,###,###')"/></td>
			<td><xsl:value-of select="./type"/></td>
			<td>
				<form ACTION="{$selfurl}" METHOD="POST">
					<input TYPE="hidden" NAME="_streamname">
						<xsl:attribute name="VALUE"><xsl:value-of select="@name"/></xsl:attribute>
					</input>
					<input TYPE="hidden" NAME="_type" VALUE="stream"/>
					<input TYPE="Submit" NAME="Disconnect" VALUE="Disconnect"/>
				</form>
			</td>
			</tr>
	</xsl:template>
</xsl:stylesheet>


