# this script gives you the present RTC in a readable form in HTML
# call the script in a job and write its return value into the blob of a file
# copyright by Jetaido GmbH (kt@jetaido.com)
# version 1.2 (2009-03-05) with counter of files
# parameters
#   lang:       language, in which the overview should be generated
# return value
#               the function returns a html formatted string 

proc rtcDokuJob {lang} {

    # define whether you also want the job to count files
    # if 'yes' the job will take longer
    set countFiles yes

    # the variable rv contains the return value of the procedure
    set rv "<p>[localize notice]</p>\n"
    append rv "<h2>[localize file_formats]</h2>\n"
    
    # iterate through all available object classes (file formats), write them and save their attributes in usedAttributes
    array set usedAttributes {}
    foreach i [lsort [objClass list]] {
        append rv "<h3>[objClass withName $i get title.$lang] ($i)</h3>\n"
        append rv "<p><strong>[localize type]</strong>: [localize [objClass withName $i get objType]]<br />\n"
        if {$countFiles == "yes"} {
            append rv "<strong>[localize totalnumber]</strong>: [llength [obj where objClass $i]] ([llength [obj where objClass $i state released]] [localize released])<br />\n"
        }
        foreach j [lsort [objClass withName $i get attributes]] {
            append rv "<strong>[attribute withName $j get title.$lang]</strong> ($j) - [attribute withName $j get helpText.$lang] - [localize [attribute withName $j get type]]<br />\n"
            # the value "1" is arbitrary. we only want to store in usedAttributes that this attribute is used at least once.
            set usedAttributes($j) "1"
        }
        append rv "</p>\n"
    }

    append rv "<h2>[localize unused_formats]</h2>\n"
    # iterate through ALL available attributes
    foreach j [lsort [attribute list]] {
        # if the current attribute is not included in usedAttributes...
        if {![info exist usedAttributes($j)]} {
            # ...write it out.
            append rv "<strong>[attribute withName $j get title.$lang]</strong> ($j) - [attribute withName $j get helpText.$lang] - [localize [attribute withName $j get type]]<br />\n"
        }
    }
   
    # list all unused file formats
    # iterate through all available object classes
    foreach i [objClass list] {
        # check, if there are any objects of that class 
        if {[llength [obj where objClass $i]] == 0} {
            # if not, write it out
            append rv "<strong>[objClass withName $i get title.$lang] ($i)</strong><br />\n"
        }
    }
    
    append rv "<p></p>\n"

    # list all existing jobs
    append rv "<h2>[localize jobs]</h2>\n"
    foreach i [job list] {
        append rv "<p><strong>[job withName $i get title]</strong> ($i) - [job withName $i get comment]</p>\n"
    }

    return $rv
}

safeInterp alias rtcDokuJob rtcDokuJob
 
proc localize {string} {
# function, that returns a localized version of a string
# parameters
#   string:     key, that identifies the string to return
#               possible keys are all the first values of the array local
#   lang:       language, in which the localized string has to be returned
#               possible values are en (english) and de (german)
#               (received via upvar)
# return value
#               the function returns a localized string

    upvar lang lang

    switch $lang {
        "en" {
            array set local {
                # english localized versions of the string
                # the first word works as key, the second is the localized string to return
                document Document
                file_formats "File Formats"
                generic Resource
                image Image
                jobs "Jobs"
                notice "If you want to update the content of this page, you have to run the job 'rtcDokuJob'."
                publication Folder
                released released
                template "Layout File"
                totalnumber "Total Number"
                type Type
                unused_formats "Unused Formats and Fields"
                string "String"
                text "Text"
                date "Date"
                enum "Selection"
                multienum "Multiple Selection"
                html "HTML"
                signature "Signature"
                linklist "Link list"
            }
        }

        "de" {
            array set local {
                # german localized versions of the string
                # the first word works as key, the second is the localized string to return
                document Dokument
                file_formats "Dateiformate"
                generic Ressource
                image Bild
                jobs "Jobs"
                notice "Wenn Sie den Inhalt dieser Seite aktualisieren moechten muessen Sie den Job 'rtcDokuJob' ausfuehren."
                publication Ordner
                released freigegeben
                template Layoutdatei
                totalnumber "Gesamtzahl"
                type Typ
                unused_formats "Nicht verwendete Formate und Felder"
                string "Zeichenkette"
                text "Text"
                date "Datum"
                enum "Auswahl"
                multienum "Mehrfachauswahl"
                html "HTML"
                signature "Unterschrift"
                linklist "Linkliste"
           }
        }
    }
    return $local($string)
}
