Class ::xowf::test_item::Renaming_form_loader (public)

 ::nx::Class ::xowf::test_item::Renaming_form_loader[i]

Defined in packages/xowf/tcl/test-item-procs.tcl

Form loader that renames "generic" form-field-names as provided by the test-item form-field classes (@answer@) into names based on the form name, such that multiple of these form names can be processed together without name clashes. - answer_attributes - answer_for_form - answers_for_form - form_name_based_attribute_stem - name_to_question_obj_dict - rename_attributes

Testcases:
No testcase defined.
Source code:
    :method map_form_constraints {form_constraints oldName newName} {
      #
      # Rename form constraints starting with $oldName into $newName.
      # Handle as well "answer=$oldName" form constraint properties.
      #
      return [lmap f $form_constraints {
        #:msg check?'$f'
        if {[string match "${oldName}*" $f]} {
          regsub $oldName $f $newName f
          if {[string match "*answer=$oldName*" $f]} {
            regsub answer=$oldName $f answer=$newName f
            #:log "MAP VALUE=answer=$oldName => answer=$newName "
          }
        }
        set f
      }]
    }

    :public method form_name_based_attribute_stem {formName} {
      #
      # Produce from the provided 'formName' an attribute stem for the
      # input fields of this form.
      #
      set strippedName [lindex [split $formName :] end]
      regsub -all -- {[-]} $strippedName _ stem
      return ${stem}_
    }

    :public method name_to_question_obj_dict {question_objs} {
      #
      # Produce a dict for attribute name to question_obj
      #
      set nameToQuestionObjDict {}
      foreach o $question_objs {
        dict set nameToQuestionObjDict [:form_name_based_attribute_stem [$o name]] $o
      }
      return $nameToQuestionObjDict
    }

    :public method answer_attributes {instance_attributes} {
      #
      # Return all form-loader specific attributes from
      # instance_attributes.
      #
      set result ""
      foreach key [lsort [dict keys $instance_attributes]] {
        if {[string match *_ $key]} {
          lappend result $key [dict get $instance_attributes $key]
        }
      }
      return $result
    }

    :public method answer_for_form {formName instance_attributes} {
      #
      # Return answer for the provided formName from
      # instance_attributes of a single object.
      #
      set result ""
      set stem [:form_name_based_attribute_stem $formName]
      set answerAttributes [:answer_attributes $instance_attributes]
      #ns_log notice "answer_for_form\ninstance_attributes $instance_attributes"
      if {[dict exists $answerAttributes $stem]} {
        set value [dict get $answerAttributes $stem]
        if {$value ne ""} {
          lappend result $value
        }
      }
      return $result
    }

    :public method answers_for_form {formName answers} {
      #
      # Return a list of dicts for the provided formName from the
      # answers (as returned from [answer_manager get_answer_attributes ...]).
      #
      set stem [:form_name_based_attribute_stem $formName]
      set result ""
      foreach answer $answers {
        set value answer_for_form
        set answerAttributes [dict get $answer answerAttributes]
        if {[dict exists $answerAttributes $stem]} {
          set value [dict get $answerAttributes $stem]
          if {$value ne ""} {
            lappend result [list item [dict get $answer item] value $value]
          }
        }
      }
      return $result
    }

    :public method rename_attributes {form_obj:object} {
      #
      # Perform attribute renaming in the provided form_obj and return
      # this form_obj. In essence, this changes the generic "@answer@"
      # value in the form and in the form constraints to a name based
      # on the form name.
      #

      set form [$form_obj get_property -name form]
      set fc   [$form_obj get_property -name form_constraints]

      #
      # Map "answer" to a generic name "@answer@" in the form and in
      # the form constraints.
      #
      set newName [:form_name_based_attribute_stem [$form_obj name]]
      #ns_log notice "renaming form loader: MAP '[$form_obj name]' -> '$newName'"

      regsub -all -- {@answer} $form @$newName form
      set fc [:map_form_constraints $fc "answer" $newName]
      set disabled_fc [lmap f $fc {
        if {[string match "$newName*" $f]} { append f ,disabled=true }
        set f
      }]

      lappend fc @cr_fields:hidden
      lappend disabled_fc @cr_fields:hidden
      #:msg fc=$fc

      $form_obj set_property -new 1 form $form
      $form_obj set_property -new 1 form_constraints $fc
      $form_obj set_property -new 1 disabled_form_constraints $disabled_fc

      #ns_log notice "RENAMED form $form\n$fc\n$disabled_fc"
      return $form_obj
    }

    # :method -deprecated get_form_object {ctx:object form_name} {
    #   #
    #   # Return the form object based on the provided form name. This
    #   # function performs attribute renaming on the returned form
    #   # object.
    #   #
    #   set form_id [$ctx default_load_form_id $form_name]
    #   set obj [$ctx object]
    #   set form_obj [::xowiki::FormPage get_instance_from_db -item_id $form_id]
    #   return [:rename_attributes $form_obj]
    # }
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: