(:
    Module: Dictionary menu building for the search interface.
            Returns all available dictionaries and terminology collections.
:)
module namespace dictmenu="http://www.risten.no/shared/dictmenu";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
(: Get ID, coll, and classification ID for all available term collections. :)
(: Availability is checked against user credentials and access permisions. :)
(: Returns an element containing a sequence of empty elements with the following structure:
   
   ...
:)
declare function dictmenu:get-termcolls($user as xs:string) as element()
{
  	let $groups := if ($user = "all") then
  					/termmeta/id/text()
  				  else if ($user = "public") then
  				    /termmeta[public/text() eq "yes"]/id/text()
  				  else
  				    xmldb:get-user-groups($user)
    let $membergroups := count($groups)
    	
   	return
      
      {
        for $r in /termmeta
        let $coll := util:collection-name($r)
        let $collgroup := xmldb:get-group($coll)
        return
          (: we need to lower-case the ID string - groups are always lower-case :)
          if (contains( $groups, lower-case($r/id/text()) ) ) then
            
            (: when the group is different from the Coll. ID, use the group of the collection :)
          else if (contains( $groups, $collgroup ) ) then
            
          else if (contains( $groups, $r/id/text() ) ) then (: case: the user is 'all' or 'public' ->
                                                               $groups = list of id strings :)
            
          else ()
      }
      
};
(: Get ID, and collection for all available dictionaries.                  :)
(: Availability is checked against user credentials and access permisions. :)
(: Returns an element containing a sequence of empty elements with the following structure:
   
   ...
:)
declare function dictmenu:get-dictcolls($user as xs:string) as element()
{
  	let $groups := if ($user = "all") then
  					/dictmeta/id/text()
  				  else if ($user = "public") then
  				    /dictmeta[public/text() eq "yes"]/id/text()
  				  else
  				    xmldb:get-user-groups($user)
    let $membergroups := count($groups)
    	
   	return
      
      {
        for $r in /dictmeta
        let $coll := util:collection-name($r)
        let $collgroup := xmldb:get-group($coll)
        return
          (: we need to lower-case the ID string - groups are always lower-case :)
          if (contains( $groups, lower-case($r/id/text()) ) ) then
            
            (: when the group is different from the Coll. ID, use the group of the collection :)
          else if (contains( $groups, $collgroup ) ) then
            
          else if (contains( $groups, $r/id/text() ) ) then (: case: the user is 'all' or 'public' ->
                                                               $groups = list of id strings :)
            
          else ()
      }
      
};
(: Get ID, and collection for all available classification schemes.        :)
(: Availability is checked against user credentials and access permisions. :)
(: Returns an element containing a sequence of empty elements with the following structure:
   
   ...
:)
declare function dictmenu:get-classcolls($user as xs:string) as element()
{
  	let $groups := if ($user = "all") then
  					/classmeta/id/text()
  				  else if ($user = "public") then
  				    /classmeta[public/text() eq "yes"]/id/text()
  				  else
  				    xmldb:get-user-groups($user)
    let $membergroups := count($groups)
    	
   	return
      
      {
        for $r in /classmeta
        let $coll := util:collection-name($r)
        let $collgroup := xmldb:get-group($coll)
        return
          (: we need to lower-case the ID string - groups are always lower-case :)
          if (contains( $groups, lower-case($r/id/text()) ) ) then
            
            (: when the group is different from the Coll. ID, use the group of the collection :)
          else if (contains( $groups, $collgroup ) ) then
            
          else if (contains( $groups, $r/id/text() ) ) then (: case: the user is 'all' or 'public' ->
                                                               $groups = list of id strings :)
            
          else ()
      }
      
};