2019-05-21 15:19:44 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-12 16:52:43 +01:00
|
|
|
module ApplicationHelper
|
2017-01-13 11:34:10 +01:00
|
|
|
include ActionView::Helpers::AssetTagHelper
|
|
|
|
include ActionView::Helpers::UrlHelper
|
2017-05-25 16:28:06 +02:00
|
|
|
include InputSanitizeHelper
|
2017-01-13 11:34:10 +01:00
|
|
|
|
2016-07-29 15:47:41 +02:00
|
|
|
def module_page?
|
2020-04-22 17:55:22 +02:00
|
|
|
controller_name == 'my_modules' ||
|
|
|
|
controller_name == 'my_module_repositories'
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
2016-07-29 15:47:41 +02:00
|
|
|
def experiment_page?
|
|
|
|
controller_name == 'experiments'
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
|
|
|
|
2016-07-29 15:47:41 +02:00
|
|
|
def project_page?
|
2018-05-08 16:33:42 +02:00
|
|
|
controller_name == 'projects' &&
|
|
|
|
action_name.in?(%w(show experiment_archive))
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_projects_page?
|
|
|
|
controller_name == 'projects' && action_name.in?(%w(index archive))
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|
2016-12-14 13:36:10 +01:00
|
|
|
|
|
|
|
def display_tooltip(message, len = Constants::NAME_TRUNCATION_LENGTH)
|
2018-05-16 15:08:50 +02:00
|
|
|
return '' unless message
|
2019-05-21 15:19:44 +02:00
|
|
|
|
2017-06-19 15:30:38 +02:00
|
|
|
if message.strip.length > len
|
2017-01-02 22:27:12 +01:00
|
|
|
sanitize_input("<div class='modal-tooltip'> \
|
|
|
|
#{truncate(message.strip, length: len)} \
|
|
|
|
<span class='modal-tooltiptext'> \
|
|
|
|
#{message.strip}</span></div>")
|
2016-12-14 13:36:10 +01:00
|
|
|
else
|
|
|
|
truncate(message.strip, length: len)
|
|
|
|
end
|
|
|
|
end
|
2016-12-14 17:05:00 +01:00
|
|
|
|
2017-05-18 14:21:00 +02:00
|
|
|
def module_repository_page?
|
|
|
|
controller_name == 'my_modules' && !@repository.nil?
|
|
|
|
end
|
|
|
|
|
2021-01-12 15:26:45 +01:00
|
|
|
def displayable_flash_type?(type)
|
2021-01-19 17:19:40 +01:00
|
|
|
%w(success warning alert error notice).include?(type)
|
2021-01-12 15:26:45 +01:00
|
|
|
end
|
|
|
|
|
2021-01-04 15:11:24 +01:00
|
|
|
def flash_alert_class(type)
|
|
|
|
case type
|
|
|
|
when 'success'
|
|
|
|
'alert-success'
|
|
|
|
when 'warning'
|
|
|
|
'alert-warning'
|
2021-01-19 17:19:40 +01:00
|
|
|
when 'error', 'alert'
|
2021-01-04 15:11:24 +01:00
|
|
|
'alert-danger'
|
|
|
|
else
|
|
|
|
'alert-info'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def flash_icon_class(type)
|
|
|
|
case type
|
|
|
|
when 'error', 'warning'
|
|
|
|
'fa-exclamation-triangle'
|
|
|
|
else
|
|
|
|
'fa-check-circle'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 08:42:16 +02:00
|
|
|
def smart_annotation_notification(options = {})
|
|
|
|
title = options.fetch(:title) { :title_must_be_present }
|
|
|
|
message = options.fetch(:message) { :message_must_be_present }
|
|
|
|
new_text = options.fetch(:new_text) { :new_text_must_be_present }
|
|
|
|
old_text = options[:old_text] || ''
|
2017-04-04 09:19:43 +02:00
|
|
|
sa_user = /\[\@(.*?)~([0-9a-zA-Z]+)\]/
|
2017-04-10 11:21:28 +02:00
|
|
|
# fetch user ids from the previous text
|
2017-04-06 08:42:16 +02:00
|
|
|
old_user_ids = []
|
|
|
|
old_text.gsub(sa_user) do |el|
|
|
|
|
match = el.match(sa_user)
|
|
|
|
old_user_ids << match[2].base62_decode
|
|
|
|
end
|
2017-04-06 15:08:03 +02:00
|
|
|
# fetch user ids from the new text
|
2017-04-06 08:42:16 +02:00
|
|
|
new_user_ids = []
|
|
|
|
new_text.gsub(sa_user) do |el|
|
2017-04-04 09:19:43 +02:00
|
|
|
match = el.match(sa_user)
|
2017-04-06 08:42:16 +02:00
|
|
|
new_user_ids << match[2].base62_decode
|
2017-04-04 09:19:43 +02:00
|
|
|
end
|
2017-04-06 15:08:03 +02:00
|
|
|
# check if the user has been already mentioned
|
2017-04-06 08:42:16 +02:00
|
|
|
annotated_users = []
|
|
|
|
new_user_ids.each do |el|
|
|
|
|
annotated_users << el unless old_user_ids.include?(el)
|
|
|
|
end
|
2017-04-06 15:08:03 +02:00
|
|
|
# restrict the list of ids and generate notification
|
2017-04-04 09:19:43 +02:00
|
|
|
annotated_users.uniq.each do |user_id|
|
|
|
|
target_user = User.find_by_id(user_id)
|
|
|
|
next unless target_user
|
2019-05-21 15:19:44 +02:00
|
|
|
|
2017-04-04 09:19:43 +02:00
|
|
|
generate_annotation_notification(target_user, title, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_annotation_notification(target_user, title, message)
|
|
|
|
notification = Notification.create(
|
|
|
|
type_of: :assignment,
|
2017-05-11 11:36:47 +02:00
|
|
|
title: sanitize_input(title),
|
|
|
|
message: sanitize_input(message)
|
2017-04-04 09:19:43 +02:00
|
|
|
)
|
2019-05-21 15:19:44 +02:00
|
|
|
UserNotification.create(notification: notification, user: target_user) if target_user.assignments_notification
|
2017-04-04 09:19:43 +02:00
|
|
|
end
|
|
|
|
|
2022-03-29 12:09:33 +02:00
|
|
|
def custom_link_open_new_tab(text)
|
|
|
|
text.gsub(/\<a /, '<a target=_blank ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def smart_annotation_parser(text, team = nil, base64_encoded_imgs = false, preview_repository = false)
|
2017-05-03 15:30:08 +02:00
|
|
|
# sometimes happens that the "team" param gets wrong data: "{nil, []}"
|
|
|
|
# so we have to check if the "team" param is kind of Team object
|
2022-08-10 14:48:47 +02:00
|
|
|
team = nil unless team.is_a?(Team)
|
|
|
|
new_text = smart_annotation_filter_resources(text, team, preview_repository: preview_repository)
|
|
|
|
smart_annotation_filter_users(new_text, team, base64_encoded_imgs: base64_encoded_imgs)
|
2017-01-24 10:51:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check if text have smart annotations of resources
|
|
|
|
# and outputs a link to resource
|
2022-08-10 14:48:47 +02:00
|
|
|
def smart_annotation_filter_resources(text, team, preview_repository: false)
|
2019-07-01 10:14:16 +02:00
|
|
|
user = if !defined?(current_user) && @user
|
|
|
|
@user
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
2022-08-10 14:48:47 +02:00
|
|
|
team ||= defined?(current_team) ? current_team : nil
|
2022-03-29 12:09:33 +02:00
|
|
|
SmartAnnotations::TagToHtml.new(user, team, text, preview_repository).html
|
2017-01-24 10:51:12 +01:00
|
|
|
end
|
2017-01-10 17:05:32 +01:00
|
|
|
|
2017-01-24 10:51:12 +01:00
|
|
|
# Check if text have smart annotations of users
|
|
|
|
# and outputs a popover with user information
|
2022-08-10 14:48:47 +02:00
|
|
|
def smart_annotation_filter_users(text, team, base64_encoded_imgs: false)
|
2017-01-11 14:56:47 +01:00
|
|
|
sa_user = /\[\@(.*?)~([0-9a-zA-Z]+)\]/
|
2017-01-24 10:51:12 +01:00
|
|
|
new_text = text.gsub(sa_user) do |el|
|
2017-01-10 17:05:32 +01:00
|
|
|
match = el.match(sa_user)
|
2017-01-11 14:56:47 +01:00
|
|
|
user = User.find_by_id(match[2].base62_decode)
|
2017-05-26 18:09:29 +02:00
|
|
|
next unless user
|
2019-05-21 15:19:44 +02:00
|
|
|
|
2019-12-05 13:27:17 +01:00
|
|
|
popover_for_user_name(user, team, false, false, base64_encoded_imgs)
|
2017-01-10 17:05:32 +01:00
|
|
|
end
|
|
|
|
new_text
|
2017-01-09 17:16:39 +01:00
|
|
|
end
|
2017-04-03 15:10:00 +02:00
|
|
|
|
2017-04-04 15:22:41 +02:00
|
|
|
# Generate smart annotation link for one user object
|
2019-02-27 16:02:53 +01:00
|
|
|
def popover_for_user_name(user,
|
|
|
|
team = nil,
|
|
|
|
skip_user_status = false,
|
2019-12-05 13:27:17 +01:00
|
|
|
skip_avatar = false,
|
|
|
|
base64_encoded_imgs = false)
|
2017-05-26 18:09:29 +02:00
|
|
|
user_still_in_team = user.teams.include?(team)
|
|
|
|
|
2017-04-03 15:10:00 +02:00
|
|
|
user_description = %(<div class='col-xs-4'>
|
2019-12-05 13:27:17 +01:00
|
|
|
<img src='#{user_avatar_absolute_url(user, :thumb, base64_encoded_imgs)}'
|
2017-05-03 10:50:43 +02:00
|
|
|
alt='thumb'></div><div class='col-xs-8'>
|
2017-04-03 15:10:00 +02:00
|
|
|
<div class='row'><div class='col-xs-9 text-left'><h5>
|
|
|
|
#{user.full_name}</h5></div><div class='col-xs-3 text-right'>
|
2018-07-09 13:13:44 +02:00
|
|
|
<span class='fas fa-times' aria-hidden='true'></span>
|
2017-04-03 15:10:00 +02:00
|
|
|
</div></div><div class='row'><div class='col-xs-12'>
|
2017-04-04 15:22:41 +02:00
|
|
|
<p class='silver'>#{user.email}</p>)
|
2017-05-26 18:09:29 +02:00
|
|
|
if user_still_in_team
|
2022-09-16 16:08:36 +02:00
|
|
|
user_team_assignment = user.user_assignments.find_by(assignable: team)
|
2017-04-04 15:22:41 +02:00
|
|
|
user_description += %(<p>
|
2017-05-26 18:09:29 +02:00
|
|
|
#{I18n.t('atwho.users.popover_html',
|
2022-09-16 16:08:36 +02:00
|
|
|
role: user_team_assignment.user_role.name.capitalize,
|
|
|
|
team: user_team_assignment.assignable.name,
|
|
|
|
time: I18n.l(user_team_assignment.created_at, format: :full_date))}
|
2017-04-04 15:22:41 +02:00
|
|
|
</p></div></div></div>)
|
|
|
|
else
|
|
|
|
user_description += %(<p></p></div></div></div>)
|
|
|
|
end
|
2017-05-09 14:55:23 +02:00
|
|
|
|
2017-05-26 18:09:29 +02:00
|
|
|
user_name = user.full_name
|
2018-10-22 14:32:10 +02:00
|
|
|
|
2019-02-27 16:02:53 +01:00
|
|
|
html = if skip_avatar
|
|
|
|
''
|
|
|
|
else
|
2019-12-05 13:27:17 +01:00
|
|
|
raw("<span class=\"global-avatar-container smart-annotation\">" \
|
|
|
|
"<img src='#{user_avatar_absolute_url(user, :icon_small, base64_encoded_imgs)}'" \
|
|
|
|
"alt='avatar' class='atwho-user-img-popover'" \
|
|
|
|
" ref='#{'missing-img' unless user.avatar.attached?}'></span>")
|
2019-02-27 16:02:53 +01:00
|
|
|
end
|
|
|
|
|
2018-11-26 15:17:18 +01:00
|
|
|
html =
|
2019-02-27 16:02:53 +01:00
|
|
|
raw(html) +
|
2017-05-26 18:09:29 +02:00
|
|
|
raw('<a onClick="$(this).popover(\'show\')" ' \
|
2018-11-26 15:17:18 +01:00
|
|
|
'class="atwho-user-popover" data-container="body" ' \
|
|
|
|
'data-html="true" tabindex="0" data-trigger="focus" ' \
|
|
|
|
'data-placement="top" data-toggle="popover" data-content="') +
|
2017-05-26 18:09:29 +02:00
|
|
|
raw(user_description) + raw('" >') + user_name + raw('</a>')
|
2018-11-26 15:17:18 +01:00
|
|
|
|
2019-05-21 15:19:44 +02:00
|
|
|
html << " #{I18n.t('atwho.res.removed')}" unless skip_user_status || user_still_in_team
|
|
|
|
html = '<span class="atwho-user-container">' + html + '</span>'
|
2018-11-26 15:17:18 +01:00
|
|
|
html
|
2017-04-03 15:10:00 +02:00
|
|
|
end
|
2017-05-03 10:50:43 +02:00
|
|
|
|
2019-05-21 15:19:44 +02:00
|
|
|
# No more dirty hack
|
2019-12-05 13:27:17 +01:00
|
|
|
def user_avatar_absolute_url(user, style, base64_encoded_imgs = false)
|
2019-10-22 12:27:13 +02:00
|
|
|
avatar_link = user.avatar_variant(style)
|
|
|
|
if user.avatar.attached?
|
2019-12-05 13:27:17 +01:00
|
|
|
return user.convert_variant_to_base64(avatar_link) if base64_encoded_imgs
|
2017-06-27 11:26:32 +02:00
|
|
|
|
2019-10-22 12:27:13 +02:00
|
|
|
avatar_link.processed.service_url(expires_in: Constants::URL_LONG_EXPIRE_TIME)
|
2021-03-08 14:39:58 +01:00
|
|
|
elsif base64_encoded_imgs
|
|
|
|
file_path = Rails.root.join('app', 'assets', *avatar_link.split('/'))
|
|
|
|
encoded_data =
|
|
|
|
File.open(file_path) do |file|
|
|
|
|
Base64.strict_encode64(file.read)
|
|
|
|
end
|
|
|
|
"data:#{avatar_link.split('.').last};base64,#{encoded_data}"
|
2019-10-22 12:27:13 +02:00
|
|
|
else
|
|
|
|
avatar_link
|
|
|
|
end
|
2019-10-22 09:54:36 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
2021-05-06 17:33:58 +02:00
|
|
|
'icon_small/missing.png'
|
2017-06-27 11:26:32 +02:00
|
|
|
end
|
2019-05-10 16:25:18 +02:00
|
|
|
|
|
|
|
def wopi_enabled?
|
|
|
|
ENV['WOPI_ENABLED'] == 'true'
|
|
|
|
end
|
2020-11-04 13:08:40 +01:00
|
|
|
|
|
|
|
# Check whether the wopi file can be edited and return appropriate response
|
|
|
|
def wopi_file_edit_button_status(asset)
|
|
|
|
file_ext = asset.file_name.split('.').last
|
|
|
|
if Constants::WOPI_EDITABLE_FORMATS.include?(file_ext)
|
|
|
|
edit_supported = true
|
|
|
|
title = ''
|
|
|
|
else
|
|
|
|
edit_supported = false
|
|
|
|
title = if Constants::FILE_TEXT_FORMATS.include?(file_ext)
|
|
|
|
I18n.t('assets.wopi_supported_text_formats_title')
|
|
|
|
elsif Constants::FILE_TABLE_FORMATS.include?(file_ext)
|
|
|
|
I18n.t('assets.wopi_supported_table_formats_title')
|
|
|
|
else
|
|
|
|
I18n.t('assets.wopi_supported_presentation_formats_title')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return edit_supported, title
|
|
|
|
end
|
2016-02-12 16:52:43 +01:00
|
|
|
end
|