<?xml version="1.0" encoding="utf-8"?>
<!-- 
   Copyright (c) 2009- Boreal - Information Strategies, published under the BSD license.
   See http://geoprisma.org/license for the full text of the license.
 --> 
<xsl:stylesheet version="1.0" xmlns:php="http://php.net/xsl"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dyn="http://exslt.org/dynamic"
    xmlns:editfeature_create="http://geoprisma.org/editfeature_create"
    extension-element-prefixes="dyn">

  <xsl:output method="html" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
  <xsl:namespace-alias stylesheet-prefix="php" result-prefix="xsl" />

<!-- Function : printWidgetSource -->
  <xsl:template name="editfeature_create:printWidgetSource">
    <xsl:variable name="WidgetType">
      <xsl:text>editfeature_create</xsl:text>
    </xsl:variable>

    <!-- Superclass JS file of the widget.
         TODO: should be included only once
      -->
    <script>
        <xsl:attribute name="src" >
             <xsl:value-of select="$g_widgets_url"/>
             <xsl:text>/editfeature/EditFeature.js</xsl:text>
        </xsl:attribute>
    </script>

    <script>
        <xsl:attribute name="src" >
             <xsl:value-of select="$g_widgets_url"/>
             <xsl:text>/editfeature/create/Create.js</xsl:text>
        </xsl:attribute>    
    </script>

    <script>            
        Ext.apply(OpenLayers.Control.EditFeature_Create.prototype, {            
            i18n_checkfid_exist_messagebox_title:   <xsl:value-of select="php:functionString('EditFeature_Create::getText','i18n_checkfid_exist_messagebox_title','JS')" />, 
            i18n_checkfid_exist_messagebox_message: <xsl:value-of select="php:functionString('EditFeature_Create::getText','i18n_checkfid_exist_messagebox_message','JS')" />
        });
    </script>

    <script type="text/javascript">
      if (!Ext.isDefined(goEditFeatureActions)) {
          var goEditFeatureActions = {};
      }
    </script>

   </xsl:template>

<!-- Function : printWidgetExecution -->
  <xsl:template name="editfeature_create:printWidgetExecution">
    <xsl:param name="pWidgetName" />
    <xsl:param name="pMapName" />

    <xsl:variable name="WidgetType">
      <xsl:text>editfeature_create</xsl:text>
    </xsl:variable>

    <!-- This widget supports this servicetype only-->
    <xsl:variable name="pServiceType">
      <xsl:text>featureserver</xsl:text>
    </xsl:variable>

    <!-- get draw mode  -->
    <xsl:variable name="pDrawMode">
      <xsl:value-of select="$drawmode" />
    </xsl:variable>

    <!-- javascript START -->
    <script type="text/javascript">
      <!-- Quick reference to the Map object -->
      oMap = objGPWidget<xsl:value-of select="$pMapName"/>;

      <!-- widget OPTIONS object used to create the widget class -->
      var objWidgetOptions;

      <xsl:for-each select="./resources/resource">
        <!-- ResourceName -->
        <xsl:variable name="pResourceName">
          <xsl:value-of select="." />
        </xsl:variable>

        <!-- Use the DataStoreName to validate that the resource has a valid
             service type
          -->
        <!-- DataStoreName -->
        <xsl:variable name="pDataStoreName">
          <xsl:call-template name="getDataStoreInfoFromWidget">
            <xsl:with-param name="pWidgetName" select="$pWidgetName" />
            <xsl:with-param name="pServiceType" select="$pServiceType" />
            <xsl:with-param name="pNodeName"><xsl:text>name</xsl:text></xsl:with-param>
            <xsl:with-param name="pResourceName" select="$pResourceName"/>
          </xsl:call-template>
        </xsl:variable>

        <!-- DataStoreText -->
        <xsl:variable name="pDataStoreText">
          <xsl:call-template name="getDataStoreInfoFromWidget">
            <xsl:with-param name="pWidgetName" select="$pWidgetName" />
            <xsl:with-param name="pServiceType" select="$pServiceType" />
            <xsl:with-param name="pNodeName"><xsl:text>params/text</xsl:text></xsl:with-param>
            <xsl:with-param name="pResourceName" select="$pResourceName"/>
          </xsl:call-template>
        </xsl:variable>

        <!-- If a DataStoreName was returned, that means the resource has
             a valid service type.
          -->
        <xsl:if test="$pDataStoreName!=''">
          <!-- get resource 'iconClsCreate' option, if any -->
          <xsl:variable name="iconClsCreate">
            <xsl:value-of select="/geoprisma/resources/resource[./name = $pResourceName]/options/iconClsCreate"/>
          </xsl:variable>

          <!-- get resource 'tooltipTextCreate' option, i18n supported -->
          <xsl:variable name="tooltipTextCreate">
            <xsl:for-each select="/geoprisma/resources/resource[./name = $pResourceName]/options/tooltipTextCreate"><xsl:call-template name="getText" /></xsl:for-each>
          </xsl:variable>

          objWidgetOptions =
          <xsl:for-each select="../../options">
            <xsl:call-template name="getObjectFromNode">
              <xsl:with-param name="pNode" select="." />
            </xsl:call-template>
          </xsl:for-each>;

          if(objWidgetOptions['featurepanel']) {
              delete objWidgetOptions['featurepanel'];
          }

          objWidgetOptions['resource'] = '<xsl:value-of select="$pResourceName" />';
          objWidgetOptions['id'] = '<xsl:value-of select="$pWidgetName" /><xsl:value-of select="$pResourceName" />';

          <!-- resourceOptions -->
          objWidgetOptions['resourceOptions'] =
          <xsl:for-each select="/geoprisma/resources/resource[./name = $pResourceName]/options">
            <xsl:call-template name="getObjectFromNode">
              <xsl:with-param name="pNode" select="." />
            </xsl:call-template>
          </xsl:for-each>;

          <!-- 'selectorMethod' resource option set as Function  -->
          <xsl:if test="/geoprisma/resources/resource[./name = $pResourceName]/options/selectorMethod">
            objWidgetOptions['resourceOptions'].selectorMethod = <xsl:value-of select="/geoprisma/resources/resource[./name = $pResourceName]/options/selectorMethod" />;
	  </xsl:if>

          <!-- tooltip text of the button -->
          var strTooltipText =
          <xsl:choose>
            <!-- if resource had a tooltipTextCreate option, then use it -->
            <xsl:when test="$tooltipTextCreate != ''">
              <xsl:value-of select="$tooltipTextCreate" />;
	    </xsl:when>
	    <xsl:otherwise>
              <!-- check the 'text' property in :
                   1) the widget options/text
                   2) the datastore params/text
                   if none was provided, use the resource name instead
                -->
              <xsl:choose>
                <xsl:when test="text">
                  '<xsl:value-of select="text"/>'
	        </xsl:when>
	        <xsl:otherwise>
                  <xsl:choose>
                    <xsl:when test="$pDataStoreText != ''">
                      <xsl:value-of select="$pDataStoreText"/>
	            </xsl:when>
	            <xsl:otherwise>
                      '<xsl:value-of select="$pResourceName"/>'
	            </xsl:otherwise>
	          </xsl:choose>
	        </xsl:otherwise>
	      </xsl:choose>;
              <xsl:variable name="geometrytype">
                <xsl:value-of select='../../options/geometrytype'/>   
              </xsl:variable>
              <xsl:variable name="functionString">
                <xsl:text>php:functionString('EditFeature_Create::getText','i18n_geometry_type_</xsl:text>
                <xsl:value-of select="$geometrytype" />
                <xsl:text>','JS')</xsl:text>            
              </xsl:variable>                
              strTooltipText += " - " + <xsl:value-of select="dyn:evaluate($functionString)"/> ;
              strTooltipText = <xsl:value-of select="php:functionString('EditFeature_Create::getText','i18n_create_button_tooltip','JS')"/> + ' - ' + strTooltipText;
	    </xsl:otherwise>
	  </xsl:choose>

          <!-- Widget object creation  -->
          var objGPWidget<xsl:value-of select="$pWidgetName" /><xsl:value-of select="$pResourceName" /> = new OpenLayers.Control.EditFeature_Create(objWidgetOptions);

          <!-- GeoExt.Action object for the geoexttoolbar-->
          action = new GeoExt.Action({
            id: '<xsl:value-of select="$pWidgetName" /><xsl:value-of select="$pResourceName" />',
          <xsl:choose>
            <xsl:when test="$iconClsCreate != ''">
            iconCls: '<xsl:value-of select="$iconClsCreate" />',
	    </xsl:when>
	    <xsl:otherwise>
            iconCls: 'editfeature_create',
	    </xsl:otherwise>
	  </xsl:choose>
            control: objGPWidget<xsl:value-of select="$pWidgetName" /><xsl:value-of select="$pResourceName" />,
            map: oMap,
            // button options
            toggleGroup: oMap.id,
            allowDepress: false,
            tooltip: strTooltipText,
            // check item options
            group: oMap.id
          });

          <!-- Push action in goEditFeatureActions['resource'] array  -->
          if (!goEditFeatureActions['<xsl:value-of select="$pResourceName" />'])
          {
              goEditFeatureActions['<xsl:value-of select="$pResourceName" />'] = [];
          }
          goEditFeatureActions['<xsl:value-of select="$pResourceName" />'].push(action);

        </xsl:if>
      </xsl:for-each><!-- end of for-each resources -->
    </script>
    <!-- javascript END -->
  </xsl:template>

</xsl:stylesheet>