Class ::xowiki::includelet::form-usages

::xowiki::includelet::form-usages[i] create ... \
           [ -__decoration (default "plain") ] \
           [ -parameter_declaration (default " {-form_item_id:integer,1..n} {-form} {-parent_id} {-package_ids ""} {-orderby "_raw_last_modified,desc"} {-view_field _name} {-publish_status "all"} {-field_names} {-hidden_field_names "_last_modified"} {-extra_form_constraints ""} {-inherit_from_forms ""} {-category_id} {-unless} {-where} {-extra_where_clause {}} {-csv true} {-voting_form} {-voting_form_form ""} {-voting_form_anon_instances "t"} {-generate} {-with_form_link true} {-with_categories} {-wf} {-bulk_actions ""} {-buttons "edit delete"} {-renderer ""} {-return_url} {-date_format} {-with_checkboxes:boolean false} ") ]

Show usages of the specified form.
Documented Parameters:
return_url
When provided and NOT empty, use the value as return_url. When provided and empty, do NOT set a return URL. When NOT provided, set the calling page as return_url.
date_format
Date format used for modification date. Might be "pretty-age" or a format string like "%Y-%m-%d %T".
extra_where_clause
a plain SQL clause that will be appended to the where clause retrieving the entries.
where
filter those entries where the instance attribute condition expressed by this flag does not match.
unless
filter those entries where the instance attribute condition expressed by this flag matches.
Defined in packages/xowiki/tcl/includelet-procs.tcl

Class Relations

  • class: ::xowiki::IncludeletClass[i]
  • superclass: ::xowiki::Includelet[i]
::xowiki::IncludeletClass create ::xowiki::includelet::form-usages \
     -superclass ::xowiki::Includelet

Methods (to be applied on instances)

  • __decoration (setter)

  • generate_voting_form (scripted)

    #:msg "generate_voting anon=$voting_form_anon_instances"
    set form "<form> How do you rate<br />
    <table rules='all' frame='box' cellspacing='1' cellpadding='1' border='0' style='border-style: none;'>
      <tbody>
        <tr>
          <td style='border-style: none;'> </td>
          <td style='border-style: none; text-align: left; width: 150px;'>&nbsp;very good<br /></td>
          <td align='right' style='border-style: none; text-align: right; width: 150px;'>&nbsp;very bad<br /></td>
        </tr> \n"
    
    # We use here the table t1 to preserve sorting etc.
    # The basic assumption is that every line of the table has an instance variable
    # corresponding to the wanted field name. This is guaranteed by the construction
    # in form-usages.
    set count 0
    set table_field_names [list]
    foreach t [$t1 children] {
      incr count
      lappend table_field_names $count
      # In most situations, it seems useful to have just one field in
      # the voting table. If there are multiple, we use a comma to
      # separate the values (looks better than separate columns).
      set field_contents [list]
      foreach __fn $field_names {
        lappend field_contents [$t set $__fn]
      }
      append form "<tr><td>[join $field_contents {, }]</td><td align='center' colspan='2'>@$count@</td></tr>\n"
    }
    
    append form "</tbody></table></form>\n"
    lappend table_field_names _last_modified _creation_user
    
    # Check, of we have a form for editing the generated form. If yes, we will
    # instantiate a form page from it.
    set form_form_id 0
    if {$form_form ne ""} {
      set form_form_id  [::xo::db::CrClass lookup -name $form_form -parent_id [::${:package_id} folder_id]]
    }
    # The normal form requires for rich-text the 2 element list as content
    if {$form_form_id == 0} { set form [list $form text/html] }
    
    set item_id [::xo::db::CrClass lookup -name $form_name -parent_id [::${:package_id} folder_id]]
    if {$item_id == 0} {
    
      if {$form_form_id == 0} {
        set f [::xowiki::Form new  -package_id ${:package_id}  -parent_id [::${:package_id} folder_id]  -name $form_name  -anon_instances $voting_form_anon_instances  -form $form  -form_constraints "@fields:scale,n=7,inline=true @cr_fields:hidden @categories:off\n @table:[join $table_field_names ,]"  ]
      } else {
        set f [::xowiki::FormPage new  -page_template $form_form_id  -package_id ${:package_id}  -parent_id [::${:package_id} folder_id]  -name $form_name]
        $f set_property anon_instances $voting_form_anon_instances
        $f set_property form $form
        $f set_property form_constraints "@fields:scale,n=7,inline=true @cr_fields:hidden @categories:off\n @table:[join $table_field_names ,]"
      }
      $f save_new
      set form_href [$f pretty_link]
      $f destroy
      set action created
    } else {
      ::xo::db::CrClass get_instance_from_db -item_id $item_id
      if {$form_form_id == 0} {
        ::$item_id form $form
      } else {
        ::$item_id set_property form $form
      }
      ::$item_id save
      set form_href [::$item_id pretty_link]
      set action updated
    }
    return "#xowiki.form-$action# <a href='[ns_quotehtml $form_href]'>[ns_quotehtml $form_name]</a>"
  • parameter_declaration (setter)

  • render (scripted)

    :get_parameters
    
    set o ${:__including_page}
    ::xo::Page requireCSS "/resources/acs-templating/lists.css"
    
    if {[info exists return_url]} {
      if {$return_url eq ""} {
        #
        # If provided return_url is empty, NO return_url is set.
        #
        unset return_url
      } else {
        #
        # Use the provided return_url.
        #
      }
    } else {
      #
      # Per default, set the return_url to the current page.
      #
      set return_url [::xo::cc url]?[::xo::cc actual_query]
    }
    
    if {[info exists parent_id]} {
      if {$parent_id eq "self"} {
        set parent_id [${:__including_page} item_id]
      } elseif {$parent_id eq "*"} {
        set query_parent_id $parent_id
        set parent_id [$o parent_id]
      }
    } else {
      set parent_id [$o parent_id]
    }
    if {![info exists query_parent_id]} {
      set query_parent_id $parent_id
    }
    
    if {![info exists form_item_id]} {
      #
      # Resolve forms by name, since we have no id.  The variable
      # "$form" can be actually refer to multiple forms in the usual
      # syntax, therefore, the result has the plural form.
      #
      set form_item_ids [::$package_id instantiate_forms  -parent_id $parent_id  -default_lang [$o lang]  -forms $form  ]
      if {$form_item_ids eq ""} {
        return -code error "could not load form '$form' (default-language [$o lang])"
      }
    } else {
      set form_item_ids $form_item_id
    }
    
    set form_constraints $extra_form_constraints\n
    
    set inherit_form_ids {}
    if {$inherit_from_forms ne ""} {
      foreach inherit_form $inherit_from_forms {
        set inherit_form_id [::$package_id instantiate_forms  -parent_id [$o parent_id]  -default_lang [$o lang]  -forms $inherit_form]
        if {$inherit_form_id ne ""} {
          if {[::$inherit_form_id istype ::xowiki::FormPage]} {
            set p [::$inherit_form_id property form_constraints]
          } else {
            set p [::$inherit_form_id form_constraints]
          }
          append form_constraints $p\n
          lappend inherit_form_ids $inherit_form_id
        }
      }
    }
    
    foreach form_item $form_item_ids {
      append form_constraints [$form_item get_form_constraints -trylocal true] \n
    }
    #:log fc=$form_constraints
    
    # load table properties; order_by won't work due to comma, but solve that later (TODO)
    set table_properties [::xowiki::PageInstance get_list_from_form_constraints  -name @table_properties  -form_constraints $form_constraints]
    
    foreach {attr value} $table_properties {
      # All labels of the following switch statement are used as
      # variable names. Take care when adding new labels to avoid
      # overwriting existing variables.
      switch -- $attr {
        orderby {set $attr _[::xowiki::formfield::FormField fc_decode $value]}
        buttons - publish_status - category_id - unless -
        where -   with_categories - with_form_link - csv - view_field -
        voting_form - voting_form_form - voting_form_anon_instances {
          set $attr $value
          #:msg " set $attr $value"
        }
        default {error "unknown table property '$attr' provided"}
      }
    }
    
    if {![info exists field_names]} {
      set fn [::xowiki::PageInstance get_short_spec_from_form_constraints  -name @table  -form_constraints $form_constraints]
      set raw_field_names [split $fn ,]
    } elseif {[string match "*,*" $field_names] } {
      set raw_field_names [split $field_names ,]
    } else {
      set raw_field_names $field_names
    }
    
    if {$raw_field_names eq ""} {
      set raw_field_names {_name _last_modified _creation_user}
    }
    foreach fn $hidden_field_names {
      lappend raw_field_names $fn
    }
    
    #:log raw_field_names=$raw_field_names
    
    #
    # Finally, evaluate conditions in case these are included.
    #
    set field_names [list]
    foreach f $raw_field_names {
      set _ [string trim [::xowiki::formfield::FormField get_single_spec  -object $o -package_id $package_id $f]]
      if {$_ ne ""} {lappend field_names $_}
    }
    
    if {[llength $inherit_form_ids] > 0} {
      set item_ids $inherit_form_ids
    } else {
      set item_ids $form_item_ids
    }
    
    set form_fields ""
    foreach form_item $item_ids {
      set form_field_objs [::xowiki::FormPage get_table_form_fields  -base_item $form_item  -field_names $field_names  -form_constraints $form_constraints  -nls_language [${:__including_page} nls_language]  ]
      #$form_item show_fields $form_field_objs
      foreach f $form_field_objs {
        dict set form_fields [$f name] $f
      }
      #foreach f $form_field_objs {ns_log notice "form <[$form_item name]: field [$f name] label [$f label]"}
    }
    # if {[dict exists $form_fields _creation_user]} {[dict get $form_fields _creation_user] label "By User"}
    
    #
    # TODO: wiki-substitution is just forced in here. Maybe it makes
    # more sense to use it as a default for _text, but we have to
    # check all the nested cases to avoid double-substitutions.
    #
    if {[dict exists $form_fields _text]} {
      [dict get $form_fields _text] set wiki 1
    }
    
    if {[dict exists $form_fields _last_modified] && [info exists date_format]} {
      [dict get $form_fields _last_modified] display_format $date_format
    }
    
    #
    # Create Table widget
    #
    set table_widget [::xowiki::TableWidget create_from_form_fields  -form_field_objs $form_field_objs  -package_id $package_id  -buttons $buttons  -hidden_field_names $hidden_field_names  -bulk_actions $bulk_actions  -renderer $renderer  -orderby $orderby  -with_checkboxes $with_checkboxes]
    #
    # Handling voting_forms
    #
    if {[info exists voting_form]} {
      #
      # If the user provided a voting form name without a language
      # prefix, add one.
      #
      if {![regexp {^..:} $voting_form]} {
        set voting_form [${:__including_page} lang]:$voting_form
      }
      dict set voting_dict voting_form $voting_form
      dict set voting_dict renderer  [list [self] generate_voting_form $voting_form $voting_form_form  $table_widget $field_names $voting_form_anon_instances]
    } else {
      set voting_dict ""
    }
    
    #
    # Compute filter clauses
    #
    set filters [::xowiki::FormPage compute_filter_clauses  {*}[expr {[info exists unless] ? [list -unless $unless] : ""}]  {*}[expr {[info exists where] ? [list -where $where] : ""}]]
    
    #:msg filters=$filters
    
    #
    # Get an ordered composite of the base set (currently including
    # extra_where clause)
    #
    #:log "exists category_id [info exists category_id]"
    if {[info exists with_categories] && [info exists category_id]} {
      append extra_where_clause  [expr {$extra_where_clause ne "" ? " and " : ""}]  [lindex [:category_clause $category_id item_id] 1]
    }
    
    set items [::xowiki::FormPage get_form_entries  -base_item_ids $form_item_ids  -parent_id $query_parent_id  -form_fields $form_field_objs  -publish_status $publish_status  -extra_where_clause $extra_where_clause  -h_where [dict get $filters wc]  -h_unless [dict get $filters uc]  -from_package_ids $package_ids  -package_id $package_id]
    
    #:log "queries done"
    if {[info exists wf]} {
      set wf_link [::$package_id pretty_link -parent_id $parent_id -path_encode false $wf]
    }
    
    set HTML [$table_widget render_page_items_as_table  -form_field_objs $form_field_objs  -return_url [ad_return_url]  -package_id $package_id  -items $items  -init_vars [dict get $filters init_vars]  -view_field $view_field  -buttons $buttons  -include_object_id_attribute [expr {$with_checkboxes || [llength $bulk_actions] > 0}]  -form_item_ids $form_item_ids  -with_form_link $with_form_link  -csv $csv  {*}[expr {[info exists generate] ? [list -generate $generate] : ""}]  -voting_dict $voting_dict  ]
    $table_widget destroy
    return $HTML