<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xs" version="2.0">

    <xsl:output method="text"/>

    <xsl:template name="template">
        <xsl:param name="template"/>
        <xsl:param name="parameters"/>
        <xsl:analyze-string select="$template" regex="\$\{{(\i\c*)\}}" flags="">
            <xsl:matching-substring>
                <xsl:value-of select="$parameters/*[name() = regex-group(1)]"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:template>

    <xsl:template match="/">


        <!-- Création des tags -->
        <xsl:for-each
            select="distinct-values(for $ks in /resultset/row/field[@name='g_keywords' and .!=''] return for $t in tokenize($ks, ',') return lower-case(normalize-space($t)))">
            <xsl:call-template name="template">
                <xsl:with-param name="parameters">
                    <keyword>
                        <xsl:value-of select="."/>
                    </keyword>
                </xsl:with-param>
                <xsl:with-param name="template"><![CDATA[insert into wp_terms (name, slug) values ("${keyword}", "${keyword}");
insert into wp_term_taxonomy (term_id, taxonomy, parent, count) select term_id, 'ngg_tag', 0, 0 from wp_terms where name = "${keyword}";                  
]]></xsl:with-param>
            </xsl:call-template>

        </xsl:for-each>

        <!-- Association des tags aux photos -->
        <xsl:for-each select="/resultset/row[field[@name='g_keywords' and . != '']]">
            <xsl:variable name="row" select="."/>
            <xsl:for-each select="tokenize(field[@name='g_keywords'], ',')">
                <xsl:call-template name="template">
                    <xsl:with-param name="parameters">
                        <keyword>
                            <xsl:value-of select="lower-case(normalize-space(.))"/>
                        </keyword>
                        <gallery-title>
                            <xsl:value-of select="$row/field[@name='g_pathComponent'][2]"/>
                        </gallery-title>
                        <filename>
                            <xsl:value-of select="$row/field[@name='g_pathComponent'][1]"/>
                        </filename>
                        <order>
                            <xsl:value-of select="position()"/>
                        </order>

                    </xsl:with-param>
                    <xsl:with-param name="template"><![CDATA[insert into wp_term_relationships (object_id, term_taxonomy_id, term_order) 
    select pid, term_taxonomy_id, ${order}
        from 
            wp_ngg_gallery g,
            wp_ngg_pictures p,
            wp_terms t,
            wp_term_taxonomy taxo
        where
        	p.galleryid = g.gid
        	and t.term_id = taxo.term_id
        	and g.title = "${gallery-title}"
        	and p.filename = "${filename}"
        	and t.name="${keyword}";
update wp_terms t, wp_term_taxonomy taxo set count = count + 1 where t.term_id = taxo.term_id and t.name="${keyword}";               
]]></xsl:with-param>
                </xsl:call-template>

            </xsl:for-each>
        </xsl:for-each>

        <!-- Mise à jour des photos -->
        <xsl:for-each-group select="/resultset/row" group-by="field[@name='g_pathComponent'][2]">
            <xsl:for-each select="current-group()">
                <xsl:sort select="field[@name='g_orderWeight']" data-type="number"/>
                <xsl:variable name="title"
                    select="if (field[@name='g_title'] != '') then field[@name='g_title'] else field[@name='g_pathComponent'][1]"/>
                <xsl:variable name="description" select="if (field[@name='g_description'] != '') then field[@name='g_description'] else $title"/>
                <xsl:variable name="summary" select="if (field[@name='g_summary'] != '') then field[@name='g_summary'] else $title"/>
                <xsl:call-template name="template">
                    <xsl:with-param name="parameters">
                        <title>
                            <xsl:value-of select="$title"/>
                        </title>
                        <description>
                            <xsl:value-of select="$description"/>
                        </description>
                        <alttext>
                            <xsl:value-of select="$summary"/>
                        </alttext>
                        <gallery-title>
                            <xsl:value-of select="field[@name='g_pathComponent'][2]"/>
                        </gallery-title>
                        <filename>
                            <xsl:value-of select="field[@name='g_pathComponent'][1]"/>
                        </filename>
                        <sort-order>
                            <xsl:value-of select="position()"/>
                        </sort-order>
                    </xsl:with-param>
                    <xsl:with-param name="template"><![CDATA[update
	wp_ngg_gallery g,
	wp_ngg_pictures p
set
	p.image_slug = "${title}",
	p.description = "${description}",
	p.alttext = "${alttext}",
	p.sortorder = ${sort-order}
where
	p.galleryid = g.gid
	and g.title = "${gallery-title}"
	and p.filename = "${filename}";
]]></xsl:with-param>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:for-each-group>
    </xsl:template>


</xsl:stylesheet>

