diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index a4e5571db..8e042556a 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -643,14 +643,25 @@ class ProtocolsController < ApplicationController def protocolsio_import_save @json_object = JSON.parse(params['json_object']) @db_json = {} - @db_json['name'] = sanitize_input(params['protocol']['name']) + @toolong = false + @db_json['name'] = pio_eval_title_len( + sanitize_input(params['protocol']['name']) + ) # since scinote only has description field, and protocols.io has many others # ,here i am putting everything important from protocols.io into description - @db_json['authors'] = sanitize_input(params['protocol']['authors']) - @db_json['created_at'] = sanitize_input(params['protocol']['created_at']) - @db_json['updated_at'] = sanitize_input(params['protocol']['last_modified']) + @db_json['authors'] = pio_eval_title_len( + sanitize_input(params['protocol']['authors']) + ) + @db_json['created_at'] = pio_eval_title_len( + sanitize_input(params['protocol']['created_at']) + ) + @db_json['updated_at'] = pio_eval_title_len( + sanitize_input(params['protocol']['last_modified']) + ) @db_json['steps'] = {} - @db_json['steps'] = protocols_io_fill_step(@json_object, @db_json['steps']) + @db_json['steps'] = protocols_io_fill_step( + @json_object, @db_json['steps'] + ) protocol = nil respond_to do |format| transaction_error = false @@ -682,7 +693,7 @@ class ProtocolsController < ApplicationController @protocolsio_general_error = false format.json do render json: - { name: p_name, new_name: protocol.name, status: :ok }, + { name: @db_json['name'], new_name: @db_json['name'], status: :ok }, status: :ok end end @@ -946,7 +957,7 @@ class ProtocolsController < ApplicationController # pio_stp_x means protocols io step (id of component) parser def pio_stp_1(iterating_key) # protocols io description parser br = '
' - append = br + sanitize_input(iterating_key) + br if iterating_key.present? + append = br + pio_eval_s_desc_len(sanitize_input(iterating_key)) + br if iterating_key.present? if iterating_key.blank? append = t('protocols.protocols_io_import.comp_append.missing_desc') end @@ -954,7 +965,7 @@ class ProtocolsController < ApplicationController end def pio_stp_6(iterating_key) # protocols io section(title) parser - return sanitize_input(iterating_key) if iterating_key.present? + return pio_eval_title_len(sanitize_input(iterating_key)) if iterating_key.present? t('protocols.protocols_io_import.comp_append.missing_step') end @@ -962,7 +973,7 @@ class ProtocolsController < ApplicationController if iterating_key.present? append = t('protocols.protocols_io_import.comp_append.expected_result') + - sanitize_input(iterating_key) + '
' + pio_eval_s_safe_expctres_len(sanitize_input(iterating_key)) + '
' return append end '' @@ -1012,9 +1023,9 @@ class ProtocolsController < ApplicationController iterating_key['os_name'] && iterating_key['os_version'] append = t('protocols.protocols_io_import.comp_append.command.title') + - sanitize_input(iterating_key['name']) + + pio_eval_s_cmd_len(sanitize_input(iterating_key['name'])) + t('protocols.protocols_io_import.comp_append.command.desc') + - sanitize_input(iterating_key['description']) + + pio_eval_s_cmd_desc_len(sanitize_input(iterating_key['description'])) + t('protocols.protocols_io_import.comp_append.command.os') + sanitize_input(iterating_key['os_name']) + ' , ' + iterating_key['os_version'] @@ -1050,7 +1061,7 @@ class ProtocolsController < ApplicationController t( 'protocols.protocols_io_import.comp_append.safety_infor.title' ) + - sanitize_input(iterating_key['body']) + + pio_eval_s_safe_expctres_len(sanitize_input(iterating_key['body'])) + t('protocols.protocols_io_import.comp_append.general_link') + sanitize_input(iterating_key['link']) return append @@ -1066,7 +1077,9 @@ class ProtocolsController < ApplicationController description_string = if json_hash['description'].present? '' + t('protocols.protocols_io_import.preview.prot_desc') + - '' + sanitize_input(json_hash['description'].html_safe) + '' + pio_eval_p_desc_len( + sanitize_input(json_hash['description']).html_safe + ) else '' + t('protocols.protocols_io_import.preview.prot_desc') + '' + t('protocols.protocols_io_import.comp_append.missing_desc') @@ -1076,26 +1089,25 @@ class ProtocolsController < ApplicationController if e == 'created_on' && json_hash[e].present? new_e = '' + e.humanize + '' description_string += - new_e.to_s + ': ' + - sanitize_input(params['protocol']['created_at'].to_s) + '
' + new_e.to_s + ': ' + pio_eval_p_pbldate_len( + sanitize_input(params['protocol']['created_at'].to_s) + '
' + ) elsif e == 'tags' && json_hash[e].any? && json_hash[e] != '' new_e = '' + e.humanize + '' description_string += new_e.to_s + ': ' + tags_length_checker = '' json_hash[e].each do |tag| - description_string += + tags_length_checker += sanitize_input(tag['tag_name']) + ' , ' end + description_string += pio_eval_p_keywords_tags_len(tags_length_checker) description_string += '
' - # Since protocols description field doesnt show html,i just remove it - # because its even messier (using Sanitize) - # what this does is basically appends "FIELD NAME: "+" FIELD VALUE" - # to description for various fields elsif json_hash[e].present? new_e = '' + e.humanize + '' description_string += new_e.to_s + ': ' + - sanitize_input(json_hash[e].html_safe) + '
' + eval_prot_desc(sanitize_input(json_hash[e]).html_safe, e) + '
' end end description_string diff --git a/app/helpers/protocols_io_helper.rb b/app/helpers/protocols_io_helper.rb index 2a6e280b8..3f44386a7 100644 --- a/app/helpers/protocols_io_helper.rb +++ b/app/helpers/protocols_io_helper.rb @@ -42,10 +42,36 @@ module ProtocolsIoHelper string_without_tables end + def eval_prot_desc(text, attribute_name) + case attribute_name + when 'before_start' + pio_eval_p_bfrandsafe_len(text) + when 'warning' + pio_eval_p_bfrandsafe_len(text) + when 'guidelines' + pio_eval_p_guid_len(text) + when 'publish_date' + pio_eval_p_pbldate_len(text) + when 'vendor_name' + pio_eval_p_misc_vnd_link_len(text) + when 'vendor_link' + pio_eval_p_misc_vnd_link_len(text) + when 'keywords' + pio_eval_p_keywords_tags_len(text) + when 'link' + pio_eval_p_misc_vnd_link_len(text) + else + '' + end + # ( before_start warning guidelines publish_date + # vendor_name vendor_link keywords link ) + end + def pio_eval_title_len(tekst) tekst += ' ' if tekst.length <= 1 if tekst.length > 250 tekst = tekst[0..195] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -59,6 +85,7 @@ module ProtocolsIoHelper def pio_eval_p_desc_len(tekst) if tekst.length - 4000 > 10000 tekst = tekst[0..13940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -66,6 +93,7 @@ module ProtocolsIoHelper def pio_eval_p_guid_len(tekst) if tekst.length - 2000 > 10000 tekst = tekst[0..11940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -73,6 +101,7 @@ module ProtocolsIoHelper def pio_eval_p_bfrandsafe_len(tekst) if tekst.length - 2000 > 7000 tekst = tekst[0..8940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -82,18 +111,23 @@ module ProtocolsIoHelper def pio_eval_p_misc_vnd_link_len(tekst) if tekst.length > 250 tekst = tekst[0..190] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end def pio_eval_p_pbldate_len(tekst) - tekst = tekst[0..120] if tekst.length > 120 + if tekst.length > 120 + tekst = tekst[0..120] + @toolong = true + end tekst end def pio_eval_p_keywords_tags_len(tekst) if tekst.length > 1000 tekst = tekst[0..940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -101,6 +135,7 @@ module ProtocolsIoHelper def pio_eval_s_desc_len(tekst) if tekst.length - 4000 > 20000 tekst = tekst[0..23940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -108,6 +143,7 @@ module ProtocolsIoHelper def pio_eval_s_cmd_desc_len(tekst) if tekst.length - 1000 > 2500 tekst = tekst[0..3440] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -115,6 +151,7 @@ module ProtocolsIoHelper def pio_eval_s_cmd_len(tekst) if tekst.length - 1000 > 3000 tekst = tekst[0..3940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end @@ -122,8 +159,8 @@ module ProtocolsIoHelper def pio_eval_s_safe_expctres_len(tekst) if tekst.length - 2000 > 5000 tekst = tekst[0..6940] + t('protocols.protocols_io_import.too_long') + @toolong = true end tekst end - end diff --git a/app/utilities/protocols_importer.rb b/app/utilities/protocols_importer.rb index 879a7d1ed..d79cd94fe 100644 --- a/app/utilities/protocols_importer.rb +++ b/app/utilities/protocols_importer.rb @@ -17,7 +17,6 @@ module ProtocolsImporter if protocol.invalid? then rename_record(protocol, :name) end - # Okay, now save the protocol protocol.save! diff --git a/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb b/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb index 9a35e186a..53edf815f 100644 --- a/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb +++ b/app/views/protocols/import_export/_import_json_protocol_p_desc.html.erb @@ -21,43 +21,52 @@ <% if json_object['description'].present? %> <% prot_info_string += (json_object['description']) %> <%= t('protocols.protocols_io_import.preview.prot_desc') %> - <%= pio_eval_p_desc_len(sanitize_input(string_html_table_remove(json_object['description'])).html_safe) %>
+ <% description = pio_eval_p_desc_len(sanitize_input(string_html_table_remove(json_object['description']))) %> + <%= description.html_safe %>
<% end %> <% if json_object['before_start'].present? %> <% prot_info_string += (json_object['before_start']) %> <%= t('protocols.protocols_io_import.preview.b_s_p') %> - <%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['before_start'])).html_safe) %>
+ <% before_start = pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['before_start']))) %> + <%= before_start.html_safe %>
<% end %> <% if json_object['warning'].present? %> <% prot_info_string += (json_object['warning']) %> <%= t('protocols.protocols_io_import.preview.warn') %> - <%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['warning'])).html_safe) %>
+ <% sft_warning = pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['warning']))) %> + <%= sft_warning.html_safe%>
<% end %> <% if json_object['guidelines'].present? %> <% prot_info_string += (json_object['guidelines']) %> <%= t('protocols.protocols_io_import.preview.guideln') %> - <%= pio_eval_p_guid_len(sanitize_input(string_html_table_remove(json_object['guidelines'])).html_safe) %>
+ <% guidelines = pio_eval_p_guid_len(sanitize_input(string_html_table_remove(json_object['guidelines']))) %> + <%= guidelines.html_safe %>
<% end %> <% if json_object['manuscript_citation'].present? %> <% prot_info_string += (json_object['manuscript_citation']) %> <%= t('protocols.protocols_io_import.preview.manu_cit') %> - <%= sanitize_input(string_html_table_remove(json_object['manuscript_citation']).html_safe) %>
+ <% manu_cit = sanitize_input(string_html_table_remove(json_object['manuscript_citation'])) %> + <%= manu_cit.html_safe %>
<% end %> <% if json_object['publish_date'].present? %> <%= t('protocols.protocols_io_import.preview.pbl_date') %> - <%= pio_eval_p_pbldate_len(sanitize_input(json_object['publish_date'])) %>
+ <% pbl_date = pio_eval_p_pbldate_len(sanitize_input(json_object['publish_date'])) %> + <%= pbl_date.html_safe %>
<% end %> <% if json_object['vendor_name'].present? %> <%= t('protocols.protocols_io_import.preview.vnd_name') %> - <%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_name'])) %>
+ <% vnd_name = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_name'])) %> + <%= vnd_name.html_safe %>
<% end %> <% if json_object['vendor_link'].present? %> <%= t('protocols.protocols_io_import.preview.vnd_link') %> - <%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_link']).html_safe) %>
+ <% vnd_link = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_link'])) %> + <%= vnd_link.html_safe %>
<% end %> <% if json_object['keywords'].present? %> <%= t('protocols.protocols_io_import.preview.key_wrd') %> - <%= pio_eval_p_keywords_tags_len(sanitize_input(json_object['keywords'])) %>
+ <% keywords = pio_eval_p_keywords_tags_len(sanitize_input(json_object['keywords'])) %> + <%= keywords.html_safe %>
<% end %> <% if json_object['tags'].present? %> <%= t('protocols.protocols_io_import.preview.tags') %> @@ -65,11 +74,13 @@ <% json_object['tags'].each do |tag| %> <% string_of_tags += tag['tag_name']+' , ' %>
<% end %> - <%= pio_eval_p_keywords_tags_len(sanitize_input(string_of_tags))%> + <% tags = pio_eval_p_keywords_tags_len(sanitize_input(string_of_tags))%> + <%= tags.html_safe %> <% end %> <% if json_object['link'].present? %> <%= t('protocols.protocols_io_import.preview.p_link') %> - <%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['link']).html_safe) %>
+ <% link = pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['link']).html_safe) %> + <%= link.html_safe %>
<% end %> <% tables, garbage = protocolsio_string_to_table_element(prot_info_string) %> <% if tables.present? %> 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 1b1bf7415..7feb62b8f 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 @@ -41,11 +41,13 @@ when '1' %> <% step_info_string += (key['data']) %>
<%= t('protocols.protocols_io_import.preview.strng_s_desc') %> - <%=pio_eval_s_desc_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%>
+ <% s_desc = pio_eval_s_desc_len(sanitize_input(string_html_table_remove(key['data'])))%> + <%= s_desc.html_safe %>
<% when '17' %> <% step_info_string += (key['data']) %>
<%= t('protocols.protocols_io_import.preview.s_exp_res') %> - <%=pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%> + <% s_exp_res = pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['data'])))%> + <%= s_exp_res.html_safe %>
<% end %> <% elsif key && whitelist_complex.include?(key['component_type_id']) %> @@ -77,9 +79,11 @@ <% step_info_string += (key['source_data']['description']) %>
<%= key['name']+': ' %> - <%= pio_eval_s_cmd_len(sanitize_input(key['source_data']['name']).html_safe) %> + <% s_cmd = pio_eval_s_cmd_len(sanitize_input(key['source_data']['name'])) %> + <%= s_cmd.html_safe %>
<%= t('protocols.protocols_io_import.preview.s_desc') %> - <%= pio_eval_s_cmd_desc_len(sanitize_input(string_html_table_remove(key['source_data']['description'])).html_safe) %> + <% s_cmd_desc = pio_eval_s_cmd_desc_len(sanitize_input(string_html_table_remove(key['source_data']['description']))) %> + <% s_cmd_desc.html_safe %>
<%= t('protocols.protocols_io_import.preview.os') %> <%= sanitize_input(key['source_data']['os_name'].html_safe)+ ' , '+sanitize_input(key['source_data']['os_version'].html_safe) %> @@ -97,7 +101,8 @@ <% step_info_string += key['source_data']['body'] %>
<%= key['name']+': ' %> - <%= pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['source_data']['body'])).html_safe) %> + <% s_safety = pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['source_data']['body']))) %> + <%= s_safety.html_safe %>
<%= t('protocols.protocols_io_import.preview.s_link') %> <%= sanitize_input(key['source_data']['link'].html_safe) %> <% when '20'%> diff --git a/app/views/protocols/protocolsio_import_save.js.erb b/app/views/protocols/protocolsio_import_save.js.erb index 1250b7816..ca236b378 100644 --- a/app/views/protocols/protocolsio_import_save.js.erb +++ b/app/views/protocols/protocolsio_import_save.js.erb @@ -1,7 +1,10 @@ <% if @protocolsio_general_error %> -$('#modal-import-json-protocol-preview').modal('hide'); + $('#modal-import-json-protocol-preview').modal('hide'); alert(' <%= t('my_modules.protocols.load_from_file_protocol_general_error', max: Constants::NAME_MAX_LENGTH, min: Constants::NAME_MIN_LENGTH) %>'); +<% elsif @toolong%> + $('#modal-import-json-protocol-preview').modal('hide'); + HelperModule.flashAlertMsg(' <%= t('protocols.index.import_results.message_warn_truncated')%>', 'success'); <% else %> $('#modal-import-json-protocol-preview').modal('hide'); $('#protocols_io_form').trigger("reset"); diff --git a/config/locales/en.yml b/config/locales/en.yml index bbd841374..14010fbfa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1385,7 +1385,7 @@ en: manager: "Protocol management" edit: "Edit protocol" protocols_io_import: - too_long: "... Your text is too long. Please shorten it to 300 words." + too_long: "... Your text is too long. Please shorten it." import_description_notice: "The protocols description is listed below under \"Protocol info\"." preview: prot_desc: "Protocol Description: " @@ -1565,6 +1565,7 @@ en: message_failed: "Failed to import %{nr} protocol/s." message_ok: "Successfully imported %{nr} protocol/s." message_ok_pio: "Successfully imported protocol from protocols.io file." + message_warn_truncated: "Some fields have been shortened due to being too long." row_success: "Imported" row_renamed: "Imported & renamed" row_failed: "Failed"