diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 0f110a58b..f60c19650 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -640,7 +640,8 @@ class ProtocolsController < ApplicationController return 0 # return 0 stops the rest of the controller code from executing end @json_object = JSON.parse(json_file_contents) - + @json_object['steps'] = step_hash_not_null(@json_object['steps']) + byebug @json_object['steps'] = protocols_io_guid_reorder_step_json( @json_object['steps'] ) @@ -672,6 +673,8 @@ class ProtocolsController < ApplicationController sanitize_input(params['protocol']['last_modified']) ) @db_json['steps'] = {} + @json_object['steps'] = step_hash_not_null(@json_object['steps']) + byebug @db_json['steps'] = protocols_io_fill_step( @json_object, @db_json['steps'] ) diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index cafb7c94b..3ea053bd2 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -103,6 +103,8 @@ module ProtocolsIoHelper @toolong = true end text + else + '' end end @@ -129,6 +131,8 @@ module ProtocolsIoHelper @remaining -= text.length - reserved end text + else + '' end end @@ -146,10 +150,59 @@ module ProtocolsIoHelper Nokogiri::HTML::DocumentFragment.parse(text).to_html end + def step_hash_not_null(step_json) + is_null_check = false + if step_json.blank? + is_null_check = true + elsif step_json[0].blank? + is_null_check = true + elsif step_json[0]['components'].blank? + is_null_check = true + elsif step_json[0]['components'][0].blank? + is_null_check = true + elsif step_json[0]['components'][0]['component_type_id'].blank? + is_null_check = true + else + is_null_check = false + end + if is_null_check + return generate_null_step_skeleton + else + step_json + end + end + + # Creates dummy info for when empty steps json is sent, or + # hash structure is modified + def generate_null_step_skeleton + json_string = [ + { + "guid": 0, + "previous_guid": nil, + "components": + [ + { + "component_type_id": '1', + "name": 'Description', + "data": '', + "data_id": nil + }, + { + "component_type_id": '6', + "name": 'Section', + "data": 'Step', + "data_id": '0' + } + ] + } + ] + json_string + end # Images are allowed in: # Step: description, expected result # Protocol description : description before_start warning # guidelines manuscript_citation + def prepare_for_view( attribute_text1, size, table = 'no_table', image_allowed = false ) @@ -314,8 +367,10 @@ module ProtocolsIoHelper end def protocols_io_guid_reorder_step_json(unordered_step_json) - base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } + return '' if unordered_step_json.nil? number_of_steps = unordered_step_json.size + return unordered_step_json if number_of_steps == 1 + base_step = unordered_step_json.find { |step| step['previous_guid'].nil? } step_order = [] step_counter = 0 step_order[step_counter] = base_step diff --git a/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb b/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb index 4af0850ff..6f67da96a 100644 --- a/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_preview_modal.html.erb @@ -30,7 +30,7 @@ - <%= f.text_field :name, :value => pio_eval_title_len(sanitize_input(@json_object['protocol_name'])), class: + <%= f.text_field :name, :value => pio_eval_title_len(sanitize_input(not_null(@json_object['protocol_name']))), class: "form-control" %>
@@ -52,14 +52,14 @@
- <% display_created_at=Time.at(@json_object['created_on'].to_i) %> + <% display_created_at=Time.at(not_null(@json_object['created_on']).to_i) %> <%= f.text_field :created_at, :value => display_created_at.to_s, readonly: true, class: "form-control" %>
- <% display_last_modified=Time.at(@json_object['last_modified'].to_i) %> + <% display_last_modified=Time.at(not_null(@json_object['last_modified']).to_i) %> <%= f.text_field :last_modified, :value => display_last_modified.to_s,readonly: true, class: "form-control" %> diff --git a/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb b/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb index 2b4eff332..55b93f3a8 100644 --- a/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_s_desc.html.erb @@ -40,13 +40,13 @@
<% case key['component_type_id'] when '1' %> - <% step_info_string += (key['data']) %> + <% step_info_string += not_null(key['data']) %>
<%= t('protocols.protocols_io_import.preview.strng_s_desc') %> <%= prepare_for_view(key['data'],ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,'table',true).html_safe %>
<% when '17' %> - <% step_info_string += (key['data']) %> + <% step_info_string += not_null(key['data']) %>
<%= t('protocols.protocols_io_import.preview.s_exp_res') %> <%= prepare_for_view(key['data'],ProtocolsIoHelper::PIO_ELEMENT_RESERVED_LENGTH_SMALL,'table',true).html_safe %>