Newer
Older
dmpopidor / app / views / branded / shared / dynamic_form / _form.html.erb
<%# locals: { f, current_fragment, schema, question_id, readonly, classname, template_locale, source } %>
<% sub_schemas_ids = schema.sub_schemas_ids %>
<% data = current_fragment.data unless current_fragment.nil? %>
<% additional_info = current_fragment.additional_info unless current_fragment.nil? %>
<% validations = additional_info.present? && additional_info['validations'].present? ? additional_info["validations"] : nil %>

<% dmp_fragments = current_fragment.present? ? current_fragment.get_dmp_fragments() : [] %>
<% schema_properties = schema.schema["properties"]%>
<% required_fields = schema.schema["required"] %>
<% schema_properties.each do |key, prop| %>
  <% value = data[key] unless data.nil? %>
  <% field_name = defined?(form_prefix) ? "#{form_prefix}[#{key}]" : key %>
  <% field_id = current_fragment.present? ? "fragment_#{key}_#{SecureRandom.uuid}" : "new_fragment_#{key}_#{SecureRandom.uuid}"%>
  <% validation = validations.nil? ? "none" : validations[key] %>
  <% required = required_fields.include?(key)%>
  <% label = prop["label@#{template_locale}"]%>
  <% ttip = prop["tooltip@#{template_locale}"] %>
  <% example = prop["example@#{template_locale}"] ? "ex: #{prop["example@#{template_locale}"]}": nil %>
  <% default_value = prop["default@#{template_locale}"] %>
  <% const = prop["const@#{template_locale}"]%>

  <%# Constant values are displayed as readonly text field %>
  <% if prop["hidden"].present?%>
    <% value = const.present? ? const : value %>
    <%= create_hidden_field(f, value, field_name) %>
    
    <% next %>
  <% end %>

  <%# Constant values are displayed as readonly text field %>
  <% if const.present?%>
    <%= create_const_field(f, const, field_name, label) %>
    
    <% next %>
  <% end %>

  <%# ############################### %>
  <%# REGISTRIES %>
  <%# ############################### %>
  <% if prop['registry_id'].present? && prop['inputType'] == 'dropdown' %>
    <% overridable = prop["overridable"] unless prop["overridable"].nil? %>
    <% registryValues = RegistryValue.where(registry_id: prop['registry_id'])%>
    <% if prop['schema_id'].present? && prop["type"].eql?('object') %>
      <%# Complex registries (value is an object) %>
      <% value = dmp_fragments.find(value["dbid"]) unless value.nil? %>
      <%= create_single_complex_registry_field(f, value, form_prefix, key, label, field_id, registryValues, template_locale, current_fragment.id, prop['schema_id'], readonly: readonly, validation: validation, ttip: ttip, default_value: default_value, overridable: overridable) %>
    <% elsif prop["type"].eql?('array') && prop["items"]['schema_id'].present?%>
      <%# Complex registries that can be selected multiple times %>
      <% 
        sub_schema = @schemas.find_by(id: prop['items']['schema_id'])
        values = []
        if data.present? && data[key].present?
          values = dmp_fragments.where(id: data[key].map { |d| d["dbid"] })
        end
        table_header = prop["table_header@#{template_locale}"] ? prop["table_header@#{template_locale}"] : prop["table_header"]
      %>
      <%= create_multiple_complex_registry_field(f, values, form_prefix, key, label, field_id, registryValues, template_locale, current_fragment.id, prop['items']['schema_id'], readonly: readonly, validation: validation, ttip: ttip, default_value: default_value, overridable: overridable) %>
    <% elsif prop["type"].eql?('string')%>
      <%# Simple registries (value in a string) %>
      <%= create_simple_registry_field(f, value, form_prefix, key, label, field_id, registryValues, template_locale, readonly: readonly, validation: validation, ttip: ttip, default_value: default_value, overridable: overridable) %>
    <% elsif prop["type"].eql?('array')%>
      <%# Select 'multiple' from simple registry where the user can choose multiple options %>
      <%= create_simple_registry_field(f, value, form_prefix, key, label, field_id, registryValues, template_locale, readonly: readonly, multiple: true, validation: validation, ttip: ttip, default_value: default_value, overridable: overridable) %>
    <% end %>

    <% next %>
  <% end %>

  <%# ############################### %>
  <%# SUB FRAGMENTS %>
  <%# ############################### %>
  <%# Properties with schema_id %>
  <% if prop['type'].eql?('object') && prop["schema_id"].present? %>
    <% sub_schema = @schemas.find_by(id: prop["schema_id"]) %>
    <%# Display Fragment selector %>
    <% if prop["inputType"].eql?("pickOrCreate") && source.eql?("form") %>
      <% 
        values = dmp_fragments.where(madmp_schema_id: prop["schema_id"])
        value = dmp_fragments.find(value["dbid"]) unless value.nil?
      %>
      <%= render(partial: 'shared/dynamic_form/fields/fragment_select_field', locals: {
        f: f,
        select_values: values,
        selected_value: value,
        readonly: readonly,
        parent_id: current_fragment.id,
        schema: sub_schema,
        required: required,
        template_locale: template_locale,
        property_name: key,
        field_id: field_id, field_name: "#{f.object_name}[#{field_name}]",
        validation: validation,
        field_label: label, ttip: ttip, answer_id: nil 
        }
      ) %>
    <% else %> 
      <%# Display sub form %>
      <div class="col-md-12">
        <%
          sub_schema = @schemas.find_by(id: prop["schema_id"])
          sub_fragment = nil
          if data.present? && data[key].present? 
            sub_fragment = dmp_fragments.find(data[key]["dbid"])
          end
        %>
        <fieldset class="sub-fragment fragment-<%= sub_fragment.id unless sub_fragment.nil? %>">
          <legend class="sub-fragment" data-toggle="tooltip" data-original-title="<%= ttip %>"><%= label %></legend>
          <%= render(partial: 'shared/dynamic_form/form', locals: { 
                f: f, 
                current_fragment: sub_fragment,
                schema: sub_schema,
                question_id: question_id,
                readonly: readonly,
                classname: sub_schema.classname,
                form_prefix: field_name,
                template_locale: template_locale,
                source: source
              }) %>
        </fieldset>
      </div>
    <% end %>

    <% next %>
  <% end %>

  <%# Sub Fragments list %>
  <% if prop['type'].eql?('array') && 
        prop['items']['type'] == 'object' && 
        prop['items']['schema_id'].present? %>
    <% sub_schema = @schemas.find_by(id: prop['items']['schema_id']) %>
    <% 
      values = []
      if data.present? && data[key].present?
        values = dmp_fragments.where(id: data[key].map { |d| d["dbid"] })
      end
      table_header = prop["table_header@#{template_locale}"] ? prop["table_header@#{template_locale}"] : prop["table_header"]
    %>
    <% if sub_schema.classname.eql?("contributor") %>
      <% persons = dmp_fragments.where(classname: "person") %>
      <%= render(partial: 'shared/dynamic_form/fields/contributor/contributor_field', locals: {
        f: f,
        field_label: label,
        contributor_schema: sub_schema,
        persons: persons, 
        contributors: values,
        current_fragment: current_fragment,
        property_name: key,
        readonly: readonly,
        template_locale: template_locale,
        ttip: ttip
        }
      ) %>
    <% else %>
      <%= render(partial: 'shared/dynamic_form/fields/fragment_list_field', locals: { 
        field_values: values, 
        readonly: readonly,
        parent_id: current_fragment.id,
        schema: sub_schema,
        table_header: table_header,
        template_locale: template_locale,
        property_name: key,
        field_label: label, ttip: ttip, answer_id: nil 
        }
      ) %>
    <% end %>
    <% next %>
  <% end %>


  <%# ############################### %>
  <%# ARRAY FIELDS %>
  <%# ############################### %>
  <% if prop['type'].eql?('array') %>
    <%# Input field associated with 'Add' and 'Delete' icons %>
      <%= render(partial: 'shared/dynamic_form/fields/multiple_field', locals: { 
        f: f, 
        field_values: value,
        readonly: readonly,
        field_label: label,
        field_properties: prop['items'],
        field_name: field_name, answer_id: nil,
        ttip: ttip
        }
      ) %>

    <% next %>
  <% end %>


  <%# ############################### %>
  <%# SIMPLE FIELDS %>
  <%# ############################### %>

  <%# String Field %>
  <% if prop['type'].eql?('string') %>
    <%# Text Area %>
    <% if prop['inputType'].present? && prop['inputType'].eql?('textarea') %>
      <%= create_textarea_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, example: example, ttip: ttip, default_value: default_value) %>
    <% elsif prop['format'].nil? %>
      <%# Text Field %>
      <%= create_text_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, ttip: ttip, example: example, default_value: default_value) %>
    <% elsif prop['format'] == 'date' %>
      <%# Date Field %>
      <%= create_date_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, ttip: ttip, example: example, default_value: default_value) %>
    <% elsif prop['format'] == 'uri' %>
      <%# URL Field %>
      <%= create_url_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, ttip: ttip, example: example, default_value: default_value) %>
    <% elsif prop['format'] == 'email' %>
      <%# Email Field %>
      <%= create_email_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, example: example, default_value: default_value) %>
    <% end %>

    <% next %>
  <% end %>
  <%# Number Field %>
  <% if prop['type'].eql?('integer') || prop['type'].eql?('number') %>
    <%= create_number_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation, ttip: ttip) %>

    <% next %>
  <% end %>
  <%# Boolean Field %>
  <% if prop['type'].eql?('boolean') %>
    <%= create_checkbox_field(f, value, field_name, label, field_id, required: required, readonly: readonly, validation: validation) %>

    <% next %>
  <% end %>
<% end %>