2023-05-17 20:12:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Toolbars
|
|
|
|
class ProtocolsService
|
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
include ActionView::Helpers::AssetUrlHelper
|
|
|
|
|
|
|
|
def initialize(current_user, protocol_ids: [])
|
|
|
|
@current_user = current_user
|
|
|
|
@protocols = Protocol.where(id: protocol_ids)
|
|
|
|
|
|
|
|
@single = @protocols.length == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions
|
|
|
|
return [] if @protocols.none?
|
|
|
|
|
|
|
|
[
|
|
|
|
restore_action,
|
|
|
|
versions_action,
|
|
|
|
duplicate_action,
|
|
|
|
access_action,
|
|
|
|
export_action,
|
|
|
|
archive_action
|
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def versions_action
|
|
|
|
return unless @single
|
|
|
|
|
2023-05-29 17:23:14 +08:00
|
|
|
return unless can_read_protocol_in_repository?(@protocols.first)
|
|
|
|
|
2023-05-17 20:12:40 +08:00
|
|
|
{
|
|
|
|
name: 'versions',
|
|
|
|
label: I18n.t('protocols.index.toolbar.versions'),
|
2023-06-08 23:33:50 +08:00
|
|
|
icon: 'sn-icon sn-icon-versions',
|
2023-05-17 20:12:40 +08:00
|
|
|
button_id: 'protocolVersions',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
protocol = @protocols.first.latest_published_version_or_self
|
|
|
|
return unless can_clone_protocol_in_repository?(protocol)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'duplicate',
|
|
|
|
label: I18n.t('protocols.index.toolbar.duplicate'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-duplicate',
|
2023-05-17 20:12:40 +08:00
|
|
|
path: clone_protocols_path,
|
|
|
|
type: :request,
|
|
|
|
request_method: :post
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
protocol = @protocols.first
|
|
|
|
|
|
|
|
path = if can_manage_protocol_users?(protocol)
|
|
|
|
edit_access_permissions_protocol_path(protocol)
|
|
|
|
else
|
|
|
|
access_permissions_protocol_path(protocol)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'access',
|
|
|
|
label: I18n.t('protocols.index.toolbar.access'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-project-member-access',
|
2023-05-17 20:12:40 +08:00
|
|
|
button_class: 'access-btn',
|
|
|
|
path: path,
|
|
|
|
type: 'remote-modal'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_action
|
|
|
|
return unless @single
|
|
|
|
|
2023-05-29 17:23:14 +08:00
|
|
|
return unless can_read_protocol_in_repository?(@protocols.first)
|
|
|
|
|
2023-05-17 20:12:40 +08:00
|
|
|
{
|
|
|
|
name: 'export',
|
|
|
|
label: I18n.t('protocols.index.toolbar.export'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-export',
|
2023-05-17 20:12:40 +08:00
|
|
|
path: export_protocols_path(protocol_ids: @protocols.pluck(:id)),
|
|
|
|
type: :download
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def archive_action
|
|
|
|
return unless @protocols.all? { |p| can_archive_protocol_in_repository?(p) }
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'archive',
|
|
|
|
label: I18n.t('protocols.index.toolbar.archive'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-archive',
|
2023-05-17 20:12:40 +08:00
|
|
|
path: archive_protocols_path,
|
|
|
|
type: :request,
|
|
|
|
request_method: :post
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore_action
|
|
|
|
return unless @protocols.all? { |p| can_restore_protocol_in_repository?(p) }
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'archive',
|
|
|
|
label: I18n.t('protocols.index.toolbar.restore'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-restore',
|
2023-05-17 20:12:40 +08:00
|
|
|
path: restore_protocols_path,
|
|
|
|
type: :request,
|
|
|
|
request_method: :post
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|