Added the functions and put in limitations from my validations files, changed controller name, added some locals

This commit is contained in:
Zanz2 2017-11-16 20:03:44 +01:00
parent 42f2d7e685
commit c44465817d
6 changed files with 150 additions and 63 deletions

View file

@ -4,7 +4,7 @@ class ProtocolsController < ApplicationController
include ProtocolsImporter
include ProtocolsExporter
include InputSanitizeHelper
include ProtocolsIoTableHelper
include ProtocolsIoHelper
before_action :check_create_permissions, only: %i(
create_new_modal

View file

@ -0,0 +1,129 @@
module ProtocolsIoHelper
def protocolsio_string_to_table_element(description_string)
string_without_tables = string_html_table_remove(description_string)
table_regex = %r{<table\b[^>]*>(.*?)<\/table>}m
tr_regex = %r{<tr\b[^>]*>(.*?)<\/tr>}m
td_regex = %r{<td\b[^>]*>(.*?)<\/td>}m
tables = {}
table_strings = description_string.scan(table_regex)
table_strings.each_with_index do |table, table_counter|
tables[table_counter.to_s] = {}
tr_strings = table[0].scan(tr_regex)
contents = {}
contents['data'] = []
tr_strings.each_with_index do |tr, tr_counter|
td_strings = tr[0].scan(td_regex)
contents['data'][tr_counter] = []
td_strings.each do |td|
td_stripped = ActionController::Base.helpers.strip_tags(td[0])
contents['data'][tr_counter].push(td_stripped)
end
end
tables[table_counter.to_s]['contents'] = Base64.encode64(
contents.to_s.sub('=>', ':')
)
tables[table_counter.to_s]['name'] = nil
end
# return string_without_tables, tables
return tables, string_without_tables
end
def string_html_table_remove(description_string)
description_string.remove!("\n", "\t", "\r", "\f")
table_whole_regex = %r{(<table\b[^>]*>.*?<\/table>)}m
table_pattern_array = description_string.scan(table_whole_regex)
string_without_tables = description_string
table_pattern_array.each do |table_pattern|
string_without_tables = string_without_tables.gsub(
table_pattern[0],
t('protocols.protocols_io_import.comp_append.table_moved').html_safe
)
end
string_without_tables
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')
end
tekst
end
# I put .length - number there instead of just adding it to right number
# so that later if i need to change this implementation, i can use it as
# a refference to what will always be allowed and where it can get cut
# (in pio_eval_p_desc_len, it could get cut from indexes 4000 to 10000)
# These can easily be adjusted if more room for an attribute is needed.
# (Subtract from one, add to another)
def pio_eval_p_desc_len(tekst)
if tekst.length - 4000 > 10000
tekst = tekst[0..13940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_p_guid_len(tekst)
if tekst.length - 2000 > 10000
tekst = tekst[0..11940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_p_bfrandsafe_len(tekst)
if tekst.length - 2000 > 7000
tekst = tekst[0..8940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
# I am almost certain the 2 methods below this comment will never get called
# But just incase someon adds huge urls or weird date format, i added them
def pio_eval_p_misc_vnd_link_len(tekst)
if tekst.length > 250
tekst = tekst[0..190] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_p_pbldate_len(tekst)
tekst = tekst[0..120] if tekst.length > 120
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')
end
tekst
end
def pio_eval_s_desc_len(tekst)
if tekst.length - 4000 > 20000
tekst = tekst[0..23940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_s_cmd_desc_len(tekst)
if tekst.length - 1000 > 2500
tekst = tekst[0..3440] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_s_cmd_len(tekst)
if tekst.length - 1000 > 3000
tekst = tekst[0..3940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
def pio_eval_s_safe_expctres_len(tekst)
if tekst.length - 2000 > 5000
tekst = tekst[0..6940] + t('protocols.protocols_io_import.too_long')
end
tekst
end
end

View file

@ -1,44 +0,0 @@
module ProtocolsIoTableHelper
def protocolsio_string_to_table_element(description_string)
string_without_tables = string_html_table_remove(description_string)
table_regex = %r{<table\b[^>]*>(.*?)<\/table>}m
tr_regex = %r{<tr\b[^>]*>(.*?)<\/tr>}m
td_regex = %r{<td\b[^>]*>(.*?)<\/td>}m
tables = {}
table_strings = description_string.scan(table_regex)
table_strings.each_with_index do |table, table_counter|
tables[table_counter.to_s] = {}
tr_strings = table[0].scan(tr_regex)
contents = {}
contents['data'] = []
tr_strings.each_with_index do |tr, tr_counter|
td_strings = tr[0].scan(td_regex)
contents['data'][tr_counter] = []
td_strings.each do |td|
td_stripped = ActionController::Base.helpers.strip_tags(td[0])
contents['data'][tr_counter].push(td_stripped)
end
end
tables[table_counter.to_s]['contents'] = Base64.encode64(
contents.to_s.sub('=>', ':')
)
tables[table_counter.to_s]['name'] = nil
end
# return string_without_tables, tables
return tables, string_without_tables
end
def string_html_table_remove(description_string)
description_string.remove!("\n", "\t", "\r", "\f")
table_whole_regex = %r{(<table\b[^>]*>.*?<\/table>)}m
table_pattern_array = description_string.scan(table_whole_regex)
string_without_tables = description_string
table_pattern_array.each do |table_pattern|
string_without_tables = string_without_tables.gsub(
table_pattern[0],
t('protocols.protocols_io_import.comp_append.table_moved').html_safe
)
end
string_without_tables
end
end

View file

@ -21,22 +21,22 @@
<% if json_object['description'].present? %>
<% prot_info_string += (json_object['description']) %>
<strong><%= t('protocols.protocols_io_import.preview.prot_desc') %></strong>
<%= sanitize_input(string_html_table_remove(json_object['description']).html_safe) %><br>
<%= pio_eval_p_desc_len(sanitize_input(string_html_table_remove(json_object['description'])).html_safe) %><br>
<% end %>
<% if json_object['before_start'].present? %>
<% prot_info_string += (json_object['before_start']) %>
<strong><%= t('protocols.protocols_io_import.preview.b_s_p') %></strong>
<%= sanitize_input(string_html_table_remove(json_object['before_start']).html_safe) %><br>
<%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['before_start'])).html_safe) %><br>
<% end %>
<% if json_object['warning'].present? %>
<% prot_info_string += (json_object['warning']) %>
<strong><%= t('protocols.protocols_io_import.preview.warn') %></strong>
<%= sanitize_input(string_html_table_remove(json_object['warning']).html_safe) %><br>
<%= pio_eval_p_bfrandsafe_len(sanitize_input(string_html_table_remove(json_object['warning'])).html_safe) %><br>
<% end %>
<% if json_object['guidelines'].present? %>
<% prot_info_string += (json_object['guidelines']) %>
<strong><%= t('protocols.protocols_io_import.preview.guideln') %></strong>
<%= sanitize_input(string_html_table_remove(json_object['guidelines']).html_safe) %><br>
<%= pio_eval_p_guid_len(sanitize_input(string_html_table_remove(json_object['guidelines'])).html_safe) %><br>
<% end %>
<% if json_object['manuscript_citation'].present? %>
<% prot_info_string += (json_object['manuscript_citation']) %>
@ -45,29 +45,31 @@
<% end %>
<% if json_object['publish_date'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.pbl_date') %></strong>
<%= sanitize_input(json_object['publish_date']) %><br>
<%= pio_eval_p_pbldate_len(sanitize_input(json_object['publish_date'])) %><br>
<% end %>
<% if json_object['vendor_name'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.vnd_name') %></strong>
<%= sanitize_input(json_object['vendor_name']) %><br>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_name'])) %><br>
<% end %>
<% if json_object['vendor_link'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.vnd_link') %></strong>
<%= sanitize_input(json_object['vendor_link'].html_safe) %><br>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['vendor_link']).html_safe) %><br>
<% end %>
<% if json_object['keywords'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.key_wrd') %></strong>
<%= sanitize_input(json_object['keywords']) %><br>
<%= pio_eval_p_keywords_tags_len(sanitize_input(json_object['keywords'])) %><br>
<% end %>
<% if json_object['tags'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.tags') %></strong>
<% string_of_tags='' %>
<% json_object['tags'].each do |tag| %>
<%= sanitize_input(tag['tag_name'])+' , ' %><br>
<% string_of_tags += tag['tag_name']+' , ' %><br>
<% end %>
<%= pio_eval_p_keywords_tags_len(sanitize_input(string_of_tags))%>
<% end %>
<% if json_object['link'].present? %>
<strong><%= t('protocols.protocols_io_import.preview.p_link') %></strong>
<%= sanitize_input(json_object['link'].html_safe) %><br>
<%= pio_eval_p_misc_vnd_link_len(sanitize_input(json_object['link']).html_safe) %><br>
<% end %>
<% tables, garbage = protocolsio_string_to_table_element(prot_info_string) %>
<% if tables.present? %>

View file

@ -13,12 +13,11 @@
&nbsp; &nbsp;
<span class="step-panel-collapse-link" data-toggle="collapse">
<span class="glyphicon collapse-step-icon glyphicon-collapse-up"></span>
<% title = nil %>
<% step['components'].each do |key1,value1| #finding section (title of step) %>
<% key1 = value1 if value1.class == Hash %>
<% if(key1['component_type_id']=='6') %>
<% title = sanitize_input(key1['data']) if key1['data'].present? %>
<% title = pio_eval_title_len(sanitize_input(key1['data'])) if key1['data'].present? %>
<% end %>
<% end %>
<% title ||=t('protocols.protocols_io_import.comp_append.missing_step') %>
@ -42,11 +41,11 @@
when '1' %>
<% step_info_string += (key['data']) %>
<br><strong><%= t('protocols.protocols_io_import.preview.strng_s_desc') %></strong>
<%=sanitize_input(string_html_table_remove(key['data']).html_safe)%><br>
<%=pio_eval_s_desc_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%><br>
<% when '17' %>
<% step_info_string += (key['data']) %>
<br><strong><%= t('protocols.protocols_io_import.preview.s_exp_res') %></strong>
<%=sanitize_input(string_html_table_remove(key['data']).html_safe)%>
<%=pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['data'])).html_safe)%>
<br>
<% end %>
<% elsif key && whitelist_complex.include?(key['component_type_id']) %>
@ -75,12 +74,12 @@
<br><%= t('protocols.protocols_io_import.preview.s_link') %>
<%= sanitize_input(key['source_data']['link'].html_safe) %>
<% when '15'%>
<% step_info_string += key['source_data']['description'] %>
<% step_info_string += (key['source_data']['description']) %>
<br>
<strong><%= key['name']+': ' %></strong>
<%= sanitize_input(key['source_data']['name'].html_safe) %>
<%= pio_eval_s_cmd_len(sanitize_input(key['source_data']['name']).html_safe) %>
<br><%= t('protocols.protocols_io_import.preview.s_desc') %>
<%= sanitize_input(string_html_table_remove(key['source_data']['description']).html_safe) %>
<%= pio_eval_s_cmd_desc_len(sanitize_input(string_html_table_remove(key['source_data']['description'])).html_safe) %>
<br><%= 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) %>
@ -98,7 +97,7 @@
<% step_info_string += key['source_data']['body'] %>
<br>
<strong><%= key['name']+': ' %></strong>
<%= sanitize_input(string_html_table_remove(key['source_data']['body']).html_safe) %>
<%= pio_eval_s_safe_expctres_len(sanitize_input(string_html_table_remove(key['source_data']['body'])).html_safe) %>
<br><%= t('protocols.protocols_io_import.preview.s_link') %>
<%= sanitize_input(key['source_data']['link'].html_safe) %>
<% when '20'%>

View file

@ -1385,6 +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."
import_description_notice: "The protocols description is listed below under \"Protocol info\"."
preview:
prot_desc: "Protocol Description: "