2017-09-26 19:17:00 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
class ProtocolsController < ApplicationController
|
|
|
|
include RenamingUtil
|
|
|
|
include ProtocolsImporter
|
|
|
|
include ProtocolsExporter
|
2017-01-04 22:04:12 +08:00
|
|
|
include InputSanitizeHelper
|
2017-11-17 03:03:44 +08:00
|
|
|
include ProtocolsIoHelper
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-09-26 19:17:00 +08:00
|
|
|
before_action :check_create_permissions, only: %i(
|
|
|
|
create_new_modal
|
|
|
|
create
|
|
|
|
)
|
|
|
|
before_action :check_clone_permissions, only: [:clone]
|
|
|
|
before_action :check_view_permissions, only: %i(
|
|
|
|
protocol_status_bar
|
|
|
|
updated_at_label
|
|
|
|
preview
|
|
|
|
linked_children
|
|
|
|
linked_children_datatable
|
|
|
|
)
|
|
|
|
before_action :check_view_all_permissions, only: %i(
|
|
|
|
index
|
|
|
|
datatable
|
|
|
|
)
|
2018-02-02 01:41:28 +08:00
|
|
|
# For update_from_parent and update_from_parent_modal we don't need to check
|
|
|
|
# read permission for the parent protocol
|
|
|
|
before_action :check_manage_permissions, only: %i(
|
2017-09-26 19:17:00 +08:00
|
|
|
edit
|
|
|
|
update_metadata
|
|
|
|
update_keywords
|
|
|
|
edit_name_modal
|
|
|
|
edit_keywords_modal
|
|
|
|
edit_authors_modal
|
|
|
|
edit_description_modal
|
|
|
|
unlink
|
|
|
|
unlink_modal
|
|
|
|
revert
|
|
|
|
revert_modal
|
2018-02-02 01:41:28 +08:00
|
|
|
update_from_parent
|
|
|
|
update_from_parent_modal
|
2017-09-26 19:17:00 +08:00
|
|
|
)
|
2018-02-16 01:46:29 +08:00
|
|
|
before_action :check_manage_parent_in_repository_permissions, only: %i(
|
2017-09-26 19:17:00 +08:00
|
|
|
update_parent
|
|
|
|
update_parent_modal
|
|
|
|
)
|
2018-02-19 21:47:36 +08:00
|
|
|
before_action :check_manage_all_in_repository_permissions, only:
|
2018-02-16 19:03:44 +08:00
|
|
|
%i(make_private publish archive)
|
2018-02-19 21:47:36 +08:00
|
|
|
before_action :check_restore_all_in_repository_permissions, only: :restore
|
2017-09-26 19:17:00 +08:00
|
|
|
before_action :check_load_from_repository_views_permissions, only: %i(
|
|
|
|
load_from_repository_modal
|
|
|
|
load_from_repository_datatable
|
|
|
|
)
|
2016-07-21 19:11:15 +08:00
|
|
|
before_action :check_load_from_repository_permissions, only: [
|
|
|
|
:load_from_repository
|
|
|
|
]
|
|
|
|
before_action :check_load_from_file_permissions, only: [
|
|
|
|
:load_from_file
|
|
|
|
]
|
2017-09-26 19:17:00 +08:00
|
|
|
before_action :check_copy_to_repository_permissions, only: %i(
|
|
|
|
copy_to_repository
|
|
|
|
copy_to_repository_modal
|
|
|
|
)
|
2018-03-19 23:10:28 +08:00
|
|
|
before_action :check_import_permissions, only: :import
|
|
|
|
before_action :check_export_permissions, only: :export
|
|
|
|
|
2018-03-26 19:07:00 +08:00
|
|
|
before_action :check_protocolsio_import_permissions,
|
|
|
|
only: %i(protocolsio_import_create protocolsio_import_save)
|
2017-09-26 19:17:00 +08:00
|
|
|
|
|
|
|
def index; end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
def datatable
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: ::ProtocolsDatatable.new(
|
|
|
|
view_context,
|
2017-01-24 23:57:14 +08:00
|
|
|
@current_team,
|
2016-07-21 19:11:15 +08:00
|
|
|
@type,
|
|
|
|
current_user
|
|
|
|
)
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-13 17:39:18 +08:00
|
|
|
def preview
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
title: I18n.t('protocols.index.preview.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2016-12-13 17:39:18 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'protocols/index/protocol_preview_modal_body.html.erb',
|
|
|
|
locals: { protocol: @protocol }
|
2017-01-03 17:04:14 +08:00
|
|
|
),
|
|
|
|
footer: render_to_string(
|
|
|
|
partial: 'protocols/index/protocol_preview_modal_footer.html.erb',
|
|
|
|
locals: { protocol: @protocol }
|
2016-12-13 17:39:18 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def linked_children
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-01-04 22:04:12 +08:00
|
|
|
title: I18n.t('protocols.index.linked_children.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2017-09-26 19:17:00 +08:00
|
|
|
html: render_to_string(partial: 'protocols/index/linked_children_modal_body.html.erb',
|
|
|
|
locals: { protocol: @protocol })
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def linked_children_datatable
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: ::ProtocolLinkedChildrenDatatable.new(
|
|
|
|
view_context,
|
|
|
|
@protocol,
|
|
|
|
current_user,
|
|
|
|
self
|
|
|
|
)
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_private
|
2017-09-26 19:17:00 +08:00
|
|
|
move_protocol('make_private')
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish
|
2017-09-26 19:17:00 +08:00
|
|
|
move_protocol('publish')
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archive
|
2017-09-26 19:17:00 +08:00
|
|
|
move_protocol('archive')
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
2017-09-26 19:17:00 +08:00
|
|
|
move_protocol('restore')
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2017-09-26 19:17:00 +08:00
|
|
|
def edit; end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
def update_metadata
|
|
|
|
@protocol.record_timestamps = false
|
|
|
|
@protocol.assign_attributes(metadata_params)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @protocol.save
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
updated_at_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/updated_at_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
),
|
|
|
|
name_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/name_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
),
|
|
|
|
authors_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/authors_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
),
|
|
|
|
description_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/description_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: @protocol.errors,
|
|
|
|
status: :unprocessable_entity
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_keywords
|
|
|
|
respond_to do |format|
|
2016-12-29 22:15:29 +08:00
|
|
|
# sanitize user input
|
2017-11-14 22:04:37 +08:00
|
|
|
if params[:keywords]
|
|
|
|
params[:keywords].collect! do |keyword|
|
|
|
|
escape_input(keyword)
|
|
|
|
end
|
2016-12-29 22:15:29 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
if @protocol.update_keywords(params[:keywords])
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
updated_at_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/updated_at_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
),
|
|
|
|
keywords_label: render_to_string(
|
2017-09-26 19:17:00 +08:00
|
|
|
partial: 'protocols/header/keywords_label.html.erb'
|
2016-07-21 19:11:15 +08:00
|
|
|
)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
|
|
|
format.json { render json: {}, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@protocol = Protocol.new(
|
2017-01-24 23:57:14 +08:00
|
|
|
team: @current_team,
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol_type: Protocol.protocol_types[@type == :public ? :in_repository_public : :in_repository_private],
|
|
|
|
added_by: current_user
|
|
|
|
)
|
|
|
|
@protocol.assign_attributes(create_params)
|
|
|
|
|
|
|
|
ts = Time.now
|
|
|
|
@protocol.record_timestamps = false
|
|
|
|
@protocol.created_at = ts
|
|
|
|
@protocol.updated_at = ts
|
2017-09-26 19:17:00 +08:00
|
|
|
@protocol.published_on = ts if @type == :public
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @protocol.save
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
url: edit_protocol_path(
|
|
|
|
@protocol,
|
2017-01-24 23:57:14 +08:00
|
|
|
team: @current_team,
|
2016-07-21 19:11:15 +08:00
|
|
|
type: @type
|
|
|
|
)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: @protocol.errors,
|
|
|
|
status: :unprocessable_entity
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clone
|
|
|
|
cloned = nil
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
cloned = @original.deep_clone_repository(current_user)
|
|
|
|
rescue Exception
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
if !cloned.nil?
|
2016-07-21 19:11:15 +08:00
|
|
|
flash[:success] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'protocols.index.clone.success_flash',
|
2016-07-21 19:11:15 +08:00
|
|
|
original: @original.name,
|
|
|
|
new: cloned.name
|
|
|
|
)
|
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
else
|
|
|
|
flash[:error] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'protocols.index.clone.error_flash',
|
2016-07-21 19:11:15 +08:00
|
|
|
original: @original.name
|
|
|
|
)
|
|
|
|
flash.keep(:error)
|
|
|
|
format.json { render json: {}, status: :bad_request }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to_repository
|
2018-02-02 01:41:28 +08:00
|
|
|
link_protocols = params[:link] &&
|
2018-02-16 18:12:30 +08:00
|
|
|
can_manage_protocol_in_module?(@protocol) &&
|
|
|
|
can_create_protocols_in_repository?(@protocol.team)
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
@new = @protocol.copy_to_repository(
|
|
|
|
copy_to_repository_params[:name],
|
|
|
|
copy_to_repository_params[:protocol_type],
|
|
|
|
link_protocols,
|
|
|
|
current_user
|
|
|
|
)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
message: t('my_modules.protocols.copy_to_repository_modal.error_400')
|
2016-07-21 19:11:15 +08:00
|
|
|
},
|
|
|
|
status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
elsif @new.invalid?
|
|
|
|
# Render errors
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: @new.errors,
|
|
|
|
status: :unprocessable_entity
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
|
|
|
# Everything good, render 200
|
|
|
|
format.json { render json: { refresh: link_protocols }, status: :ok }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlink
|
|
|
|
respond_to do |format|
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
@protocol.unlink
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
message: t('my_modules.protocols.unlink_error')
|
2016-07-21 19:11:15 +08:00
|
|
|
},
|
|
|
|
status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
|
|
|
# Everything good, display flash & render 200
|
|
|
|
flash[:success] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'my_modules.protocols.unlink_flash'
|
2016-07-21 19:11:15 +08:00
|
|
|
)
|
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert
|
|
|
|
respond_to do |format|
|
2016-10-06 22:13:35 +08:00
|
|
|
if @protocol.can_destroy?
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
# Revert is basically update from parent
|
|
|
|
@protocol.update_from_parent(current_user)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-10-06 22:13:35 +08:00
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
message: t('my_modules.protocols.revert_error')
|
|
|
|
},
|
|
|
|
status: :bad_request
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# Everything good, display flash & render 200
|
|
|
|
flash[:success] = t(
|
|
|
|
'my_modules.protocols.revert_flash'
|
|
|
|
)
|
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2016-10-06 22:13:35 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
message: t('my_modules.protocols.revert_error_locked')
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_parent
|
|
|
|
respond_to do |format|
|
2016-10-06 22:13:35 +08:00
|
|
|
if @protocol.parent.can_destroy?
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
@protocol.update_parent(current_user)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-10-06 22:13:35 +08:00
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-10-06 22:13:35 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
message: t('my_modules.protocols.update_parent_error')
|
2016-10-06 22:13:35 +08:00
|
|
|
},
|
|
|
|
status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-10-06 22:13:35 +08:00
|
|
|
else
|
2017-01-03 23:35:25 +08:00
|
|
|
# Everything good, record activity, display flash & render 200
|
|
|
|
Activity.create(
|
|
|
|
type_of: :revert_protocol,
|
|
|
|
project: @protocol.my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: @protocol.my_module.experiment,
|
2017-01-03 23:35:25 +08:00
|
|
|
my_module: @protocol.my_module,
|
|
|
|
user: current_user,
|
|
|
|
message: I18n.t(
|
|
|
|
'activities.revert_protocol',
|
|
|
|
user: current_user.full_name,
|
|
|
|
protocol: @protocol.name
|
|
|
|
)
|
2016-11-02 16:18:06 +08:00
|
|
|
)
|
2016-10-06 22:13:35 +08:00
|
|
|
flash[:success] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'my_modules.protocols.update_parent_flash'
|
2016-10-06 22:13:35 +08:00
|
|
|
)
|
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2016-10-06 22:13:35 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
message: t('my_modules.protocols.update_parent_error_locked')
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_from_parent
|
|
|
|
respond_to do |format|
|
2016-10-06 22:13:35 +08:00
|
|
|
if @protocol.can_destroy?
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
@protocol.update_from_parent(current_user)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-10-06 22:13:35 +08:00
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-10-06 22:13:35 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
message: t('my_modules.protocols.update_from_parent_error')
|
2016-10-06 22:13:35 +08:00
|
|
|
},
|
|
|
|
status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-10-06 22:13:35 +08:00
|
|
|
else
|
|
|
|
# Everything good, display flash & render 200
|
|
|
|
flash[:success] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'my_modules.protocols.update_from_parent_flash'
|
2016-10-06 22:13:35 +08:00
|
|
|
)
|
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2016-10-06 22:13:35 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
message: t('my_modules.protocols.update_from_parent_error_locked')
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_repository
|
|
|
|
respond_to do |format|
|
2016-10-06 22:13:35 +08:00
|
|
|
if @protocol.can_destroy?
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
@protocol.load_from_repository(@source, current_user)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-10-06 22:13:35 +08:00
|
|
|
if transaction_error
|
|
|
|
# Bad request error
|
2016-12-21 23:52:15 +08:00
|
|
|
format.json do
|
2016-10-06 22:13:35 +08:00
|
|
|
render json: {
|
2016-12-21 23:52:15 +08:00
|
|
|
message: t('my_modules.protocols.load_from_repository_error')
|
2016-10-06 22:13:35 +08:00
|
|
|
},
|
|
|
|
status: :bad_request
|
2016-12-21 23:52:15 +08:00
|
|
|
end
|
2016-10-06 22:13:35 +08:00
|
|
|
else
|
2017-01-03 23:35:25 +08:00
|
|
|
# Everything good, record activity, display flash & render 200
|
|
|
|
Activity.create(
|
|
|
|
type_of: :load_protocol_from_repository,
|
|
|
|
project: @protocol.my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: @protocol.my_module.experiment,
|
2017-01-03 23:35:25 +08:00
|
|
|
my_module: @protocol.my_module,
|
|
|
|
user: current_user,
|
|
|
|
message: I18n.t(
|
|
|
|
'activities.load_protocol_from_repository',
|
|
|
|
user: current_user.full_name,
|
|
|
|
protocol: @source.name
|
|
|
|
)
|
2016-11-02 16:18:06 +08:00
|
|
|
)
|
2016-12-21 23:52:15 +08:00
|
|
|
flash[:success] = t('my_modules.protocols.load_from_repository_flash')
|
2016-10-06 22:13:35 +08:00
|
|
|
flash.keep(:success)
|
|
|
|
format.json { render json: {}, status: :ok }
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2016-10-06 22:13:35 +08:00
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
message: t('my_modules.protocols.load_from_repository_error_locked')
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_file
|
|
|
|
# This is actually very similar to import
|
|
|
|
respond_to do |format|
|
2016-10-06 22:13:35 +08:00
|
|
|
if @protocol.can_destroy?
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
2017-03-22 23:24:48 +08:00
|
|
|
import_into_existing(
|
|
|
|
@protocol, @protocol_json, current_user, current_team
|
|
|
|
)
|
2016-10-06 22:13:35 +08:00
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-10-06 22:13:35 +08:00
|
|
|
if transaction_error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-10-06 22:13:35 +08:00
|
|
|
render json: { status: :error }, status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-10-06 22:13:35 +08:00
|
|
|
else
|
2017-01-03 23:35:25 +08:00
|
|
|
# Everything good, record activity, display flash & render 200
|
|
|
|
Activity.create(
|
|
|
|
type_of: :load_protocol_from_file,
|
|
|
|
project: @protocol.my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: @protocol.my_module.experiment,
|
2017-01-03 23:35:25 +08:00
|
|
|
my_module: @protocol.my_module,
|
|
|
|
user: current_user,
|
|
|
|
message: I18n.t(
|
|
|
|
'activities.load_protocol_from_file',
|
|
|
|
user: current_user.full_name,
|
|
|
|
protocol: @protocol_json[:name]
|
|
|
|
)
|
2016-11-02 16:18:06 +08:00
|
|
|
)
|
2016-10-06 22:13:35 +08:00
|
|
|
flash[:success] = t(
|
2017-09-26 19:17:00 +08:00
|
|
|
'my_modules.protocols.load_from_file_flash'
|
2016-10-06 22:13:35 +08:00
|
|
|
)
|
|
|
|
flash.keep(:success)
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-10-06 22:13:35 +08:00
|
|
|
render json: { status: :ok }, status: :ok
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-10-06 22:13:35 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2016-10-06 22:13:35 +08:00
|
|
|
format.json do
|
|
|
|
render json: { status: :locked }, status: :bad_request
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import
|
|
|
|
protocol = nil
|
|
|
|
respond_to do |format|
|
|
|
|
transaction_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
2017-01-24 23:57:14 +08:00
|
|
|
protocol = import_new_protocol(@protocol_json, @team, @type, current_user)
|
2016-07-21 19:11:15 +08:00
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-11 22:50:11 +08:00
|
|
|
p_name =
|
|
|
|
if @protocol_json['name'].present? && !@protocol_json['name'].empty?
|
|
|
|
escape_input(@protocol_json['name'])
|
|
|
|
else
|
|
|
|
t('protocols.index.no_protocol_name')
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
if transaction_error
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: { name: p_name, status: :bad_request }, status: :bad_request
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
name: p_name, new_name: protocol.name, status: :ok
|
|
|
|
},
|
|
|
|
status: :ok
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-09-22 22:02:56 +08:00
|
|
|
|
2017-09-25 19:53:32 +08:00
|
|
|
def protocolsio_import_create
|
|
|
|
@protocolsio_too_big = false
|
2017-11-14 19:02:17 +08:00
|
|
|
@protocolsio_invalid_file = false
|
2018-02-07 18:17:53 +08:00
|
|
|
@protocolsio_no_file = false
|
|
|
|
if params[:json_file].nil?
|
|
|
|
@protocolsio_no_file = true
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
end
|
|
|
|
return 0 # return 0 stops the rest of the controller code from executing
|
|
|
|
end
|
2017-11-14 19:02:17 +08:00
|
|
|
extension = File.extname(params[:json_file].path)
|
2017-09-25 19:53:32 +08:00
|
|
|
file_size = File.size(params[:json_file].path)
|
2017-11-14 19:02:17 +08:00
|
|
|
if extension != '.txt' && extension != '.json'
|
|
|
|
@protocolsio_invalid_file = true
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
end
|
|
|
|
return 0 # return 0 stops the rest of the controller code from executing
|
|
|
|
end
|
2018-06-19 20:15:14 +08:00
|
|
|
if file_size > Rails.configuration.x.file_max_size_mb.megabytes
|
2017-09-25 19:53:32 +08:00
|
|
|
@protocolsio_too_big = true
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
# if file is too big, default to the js.erb file,
|
|
|
|
# named the same as this controller
|
|
|
|
# where a javascript alert is called
|
|
|
|
end
|
2017-11-14 19:02:17 +08:00
|
|
|
return 0 # return 0 stops the rest of the controller code from executing
|
2017-09-25 19:53:32 +08:00
|
|
|
end
|
|
|
|
json_file_contents = File.read(params[:json_file].path)
|
|
|
|
json_file_contents.gsub! '\"', "'"
|
|
|
|
# escaped double quotes too stressfull, html works with single quotes too
|
|
|
|
# json double quotes dont get escaped since they dont match \"
|
2017-11-14 19:02:17 +08:00
|
|
|
unless valid_protocol_json(json_file_contents)
|
|
|
|
@protocolsio_invalid_file = true
|
|
|
|
respond_to do |format|
|
|
|
|
format.js {}
|
|
|
|
end
|
|
|
|
return 0 # return 0 stops the rest of the controller code from executing
|
|
|
|
end
|
2017-09-25 19:53:32 +08:00
|
|
|
@json_object = JSON.parse(json_file_contents)
|
2018-02-21 23:17:53 +08:00
|
|
|
unless step_hash_null?(@json_object['steps'])
|
|
|
|
@json_object['steps'] = protocols_io_guid_reorder_step_json(
|
|
|
|
@json_object['steps']
|
|
|
|
)
|
|
|
|
end
|
2018-01-24 16:31:43 +08:00
|
|
|
|
2017-09-25 19:53:32 +08:00
|
|
|
@protocol = Protocol.new
|
2017-09-22 17:56:50 +08:00
|
|
|
respond_to do |format|
|
2017-09-25 19:53:32 +08:00
|
|
|
format.js {} # go to the js.erb file named the same as this controller,
|
|
|
|
# where a preview modal is rendered,
|
|
|
|
# and some modals get closed and opened
|
2017-09-22 17:56:50 +08:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 19:53:32 +08:00
|
|
|
|
|
|
|
def protocolsio_import_save
|
|
|
|
@json_object = JSON.parse(params['json_object'])
|
2017-09-26 19:17:00 +08:00
|
|
|
@db_json = {}
|
2017-11-17 09:43:32 +08:00
|
|
|
@toolong = false
|
|
|
|
@db_json['name'] = pio_eval_title_len(
|
2018-02-21 23:17:53 +08:00
|
|
|
sanitize_input(not_null(params['protocol']['name']))
|
2017-11-17 09:43:32 +08:00
|
|
|
)
|
2017-09-25 19:53:32 +08:00
|
|
|
# since scinote only has description field, and protocols.io has many others
|
|
|
|
# ,here i am putting everything important from protocols.io into description
|
2017-11-17 09:43:32 +08:00
|
|
|
@db_json['authors'] = pio_eval_title_len(
|
2018-02-21 23:17:53 +08:00
|
|
|
sanitize_input(not_null(params['protocol']['authors']))
|
2017-11-17 09:43:32 +08:00
|
|
|
)
|
|
|
|
@db_json['created_at'] = pio_eval_title_len(
|
2018-02-21 23:17:53 +08:00
|
|
|
sanitize_input(not_null(params['protocol']['created_at']))
|
2017-11-17 09:43:32 +08:00
|
|
|
)
|
|
|
|
@db_json['updated_at'] = pio_eval_title_len(
|
2018-02-21 23:17:53 +08:00
|
|
|
sanitize_input(not_null(params['protocol']['last_modified']))
|
2017-11-17 09:43:32 +08:00
|
|
|
)
|
2017-10-25 01:24:53 +08:00
|
|
|
@db_json['steps'] = {}
|
2018-02-21 23:17:53 +08:00
|
|
|
|
|
|
|
unless step_hash_null?(@json_object['steps'])
|
|
|
|
@db_json['steps'] = protocols_io_fill_step(
|
|
|
|
@json_object, @db_json['steps']
|
|
|
|
)
|
|
|
|
end
|
2017-09-26 19:17:00 +08:00
|
|
|
protocol = nil
|
|
|
|
respond_to do |format|
|
|
|
|
transaction_error = false
|
|
|
|
@protocolsio_general_error = false
|
|
|
|
Protocol.transaction do
|
|
|
|
begin
|
|
|
|
protocol = import_new_protocol(
|
|
|
|
@db_json, current_team, params[:type].to_sym, current_user
|
|
|
|
)
|
|
|
|
rescue Exception
|
|
|
|
transaction_error = true
|
|
|
|
raise ActiveRecord:: Rollback
|
2017-09-25 19:53:32 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2017-09-26 19:17:00 +08:00
|
|
|
p_name =
|
2017-10-08 04:13:31 +08:00
|
|
|
if @db_json['name'].present?
|
2017-09-26 19:17:00 +08:00
|
|
|
escape_input(@db_json['name'])
|
|
|
|
else
|
|
|
|
t('protocols.index.no_protocol_name')
|
2017-09-25 19:53:32 +08:00
|
|
|
end
|
2017-09-26 19:17:00 +08:00
|
|
|
if transaction_error
|
|
|
|
@protocolsio_general_error = true
|
2017-10-08 04:13:31 +08:00
|
|
|
# General something went wrong, upload to db failed error
|
2017-09-26 19:17:00 +08:00
|
|
|
# format.json {
|
|
|
|
# render json: { name: p_name, status: :bad_request },
|
|
|
|
# status: :bad_request
|
|
|
|
# }
|
2017-09-19 21:02:16 +08:00
|
|
|
else
|
2017-09-26 19:17:00 +08:00
|
|
|
@protocolsio_general_error = false
|
|
|
|
format.json do
|
|
|
|
render json:
|
2017-11-17 09:43:32 +08:00
|
|
|
{ name: @db_json['name'], new_name: @db_json['name'], status: :ok },
|
2017-09-26 19:17:00 +08:00
|
|
|
status: :ok
|
|
|
|
end
|
2017-09-19 21:02:16 +08:00
|
|
|
end
|
2017-09-26 19:17:00 +08:00
|
|
|
format.js {}
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def export
|
2016-12-09 06:21:13 +08:00
|
|
|
# Make a zip output stream and send it to the client
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
2016-12-12 22:33:21 +08:00
|
|
|
format.html do
|
2016-12-09 06:21:13 +08:00
|
|
|
z_output_stream = Zip::OutputStream.write_buffer do |ostream|
|
2016-12-12 22:33:21 +08:00
|
|
|
ostream.put_next_entry('scinote.xml')
|
|
|
|
ostream.print(generate_envelope_xml(@protocols))
|
|
|
|
ostream.put_next_entry('scinote.xsd')
|
2016-12-09 06:21:13 +08:00
|
|
|
ostream.print(generate_envelope_xsd)
|
2016-12-12 22:33:21 +08:00
|
|
|
ostream.put_next_entry('eln.xsd')
|
2016-12-09 06:21:13 +08:00
|
|
|
ostream.print(generate_eln_xsd)
|
2016-12-12 22:33:21 +08:00
|
|
|
|
|
|
|
# Create folder and xml file for each protocol and populate it
|
|
|
|
@protocols.each do |protocol|
|
|
|
|
protocol_dir = get_guid(protocol.id).to_s
|
|
|
|
ostream.put_next_entry("#{protocol_dir}/eln.xml")
|
|
|
|
ostream.print(generate_protocol_xml(protocol))
|
|
|
|
# Add assets to protocol folder
|
2017-01-09 23:19:44 +08:00
|
|
|
next if protocol.steps.count <= 0
|
|
|
|
protocol.steps.order(:id).each do |step|
|
|
|
|
step_guid = get_guid(step.id)
|
|
|
|
step_dir = "#{protocol_dir}/#{step_guid}"
|
2017-11-21 23:30:48 +08:00
|
|
|
if step.assets.exists?
|
|
|
|
step.assets.order(:id).each do |asset|
|
2018-03-25 19:36:24 +08:00
|
|
|
next unless asset.file.exists?
|
2017-11-21 23:30:48 +08:00
|
|
|
asset_guid = get_guid(asset.id)
|
|
|
|
asset_file_name = asset_guid.to_s +
|
|
|
|
File.extname(asset.file_file_name).to_s
|
|
|
|
ostream.put_next_entry("#{step_dir}/#{asset_file_name}")
|
|
|
|
input_file = asset.open
|
|
|
|
ostream.print(input_file.read)
|
|
|
|
input_file.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if step.tiny_mce_assets.exists?
|
|
|
|
step.tiny_mce_assets.order(:id).each do |tiny_mce_asset|
|
|
|
|
asset_guid = get_guid(tiny_mce_asset.id)
|
|
|
|
asset_file_name =
|
|
|
|
"rte-#{asset_guid.to_s +
|
|
|
|
File.extname(tiny_mce_asset.image_file_name).to_s}"
|
|
|
|
|
|
|
|
ostream.put_next_entry("#{step_dir}/#{asset_file_name}")
|
|
|
|
input_file = tiny_mce_asset.open
|
|
|
|
ostream.print(input_file.read)
|
|
|
|
input_file.close
|
|
|
|
end
|
2016-12-12 22:33:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-09 06:21:13 +08:00
|
|
|
end
|
|
|
|
z_output_stream.rewind
|
2016-12-12 22:33:21 +08:00
|
|
|
|
|
|
|
protocol_name = get_protocol_name(@protocols[0])
|
|
|
|
|
|
|
|
# Now generate filename of the archive and send file to user
|
|
|
|
if @protocols.count == 1
|
|
|
|
# Try to construct an OS-safe file name
|
|
|
|
file_name = 'protocol.eln'
|
|
|
|
unless protocol_name.nil?
|
2017-01-09 23:19:44 +08:00
|
|
|
escaped_name = protocol_name.gsub(/[^0-9a-zA-Z\-.,_]/i, '_')
|
|
|
|
.downcase[0..Constants::NAME_MAX_LENGTH]
|
2016-12-12 22:33:21 +08:00
|
|
|
file_name = escaped_name + '.eln' unless escaped_name.empty?
|
|
|
|
end
|
|
|
|
elsif @protocols.length > 1
|
|
|
|
file_name = 'protocols.eln'
|
|
|
|
end
|
|
|
|
send_data(z_output_stream.read, filename: file_name)
|
2016-12-09 06:21:13 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlink_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
title: t('my_modules.protocols.confirm_link_update_modal.unlink_title'),
|
|
|
|
message: t('my_modules.protocols.confirm_link_update_modal.unlink_message'),
|
|
|
|
btn_text: t('my_modules.protocols.confirm_link_update_modal.unlink_btn_text'),
|
2016-07-21 19:11:15 +08:00
|
|
|
url: unlink_protocol_path(@protocol)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revert_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
title: t('my_modules.protocols.confirm_link_update_modal.revert_title'),
|
|
|
|
message: t('my_modules.protocols.confirm_link_update_modal.revert_message'),
|
|
|
|
btn_text: t('my_modules.protocols.confirm_link_update_modal.revert_btn_text'),
|
2016-07-21 19:11:15 +08:00
|
|
|
url: revert_protocol_path(@protocol)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_parent_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
title: t('my_modules.protocols.confirm_link_update_modal.update_parent_title'),
|
|
|
|
message: t('my_modules.protocols.confirm_link_update_modal.update_parent_message'),
|
|
|
|
btn_text: t('general.update'),
|
2016-07-21 19:11:15 +08:00
|
|
|
url: update_parent_protocol_path(@protocol)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_from_parent_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-09-26 19:17:00 +08:00
|
|
|
title: t('my_modules.protocols.confirm_link_update_modal.update_self_title'),
|
|
|
|
message: t('my_modules.protocols.confirm_link_update_modal.update_self_message'),
|
|
|
|
btn_text: t('general.update'),
|
2016-07-21 19:11:15 +08:00
|
|
|
url: update_from_parent_protocol_path(@protocol)
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_repository_datatable
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
2017-09-26 19:17:00 +08:00
|
|
|
@type = (params[:type] || 'public').to_sym
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: ::LoadFromRepositoryProtocolsDatatable.new(
|
|
|
|
view_context,
|
2017-01-24 23:57:14 +08:00
|
|
|
@protocol.team,
|
2016-07-21 19:11:15 +08:00
|
|
|
@type,
|
|
|
|
current_user
|
|
|
|
)
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_from_repository_modal
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "my_modules/protocols/load_from_repository_modal_body.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to_repository_modal
|
|
|
|
@new = Protocol.new
|
|
|
|
@original = Protocol.find(params[:id])
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "my_modules/protocols/copy_to_repository_modal_body.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def protocol_status_bar
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "my_modules/protocols/protocol_status_bar.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at_label
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/header/updated_at_label.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_new_modal
|
|
|
|
@new_protocol = Protocol.new
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/index/create_new_modal_body.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_name_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-01-04 22:04:12 +08:00
|
|
|
title: I18n.t('protocols.header.edit_name_modal.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2017-11-03 23:28:15 +08:00
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/header/edit_name_modal_body.html.erb"
|
|
|
|
})
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_keywords_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-01-04 22:04:12 +08:00
|
|
|
title: I18n.t('protocols.header.edit_keywords_modal.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2017-11-03 23:28:15 +08:00
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/header/edit_keywords_modal_body.html.erb"
|
|
|
|
}),
|
2017-01-24 23:57:14 +08:00
|
|
|
keywords: @protocol.team.protocol_keywords_list
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_authors_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-01-04 22:04:12 +08:00
|
|
|
title: I18n.t('protocols.header.edit_authors_modal.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2017-11-03 23:28:15 +08:00
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/header/edit_authors_modal_body.html.erb"
|
|
|
|
})
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_description_modal
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
2017-01-04 22:04:12 +08:00
|
|
|
title: I18n.t('protocols.header.edit_description_modal.title',
|
2017-01-11 22:50:11 +08:00
|
|
|
protocol: escape_input(@protocol.name)),
|
2016-07-21 19:11:15 +08:00
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/header/edit_description_modal_body.html.erb"
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-14 19:02:17 +08:00
|
|
|
def valid_protocol_json(json)
|
|
|
|
JSON.parse(json)
|
|
|
|
return true
|
|
|
|
rescue JSON::ParserError => e
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def move_protocol(action)
|
|
|
|
rollbacked = false
|
|
|
|
results = []
|
|
|
|
begin
|
|
|
|
Protocol.transaction do
|
|
|
|
@protocols.find_each do |protocol|
|
|
|
|
result = {
|
|
|
|
name: protocol.name
|
|
|
|
}
|
|
|
|
|
|
|
|
success = protocol.method(action).call(current_user)
|
|
|
|
|
|
|
|
# Try renaming protocol
|
|
|
|
unless success
|
|
|
|
rename_record(protocol, :name)
|
|
|
|
success = protocol.method(action).call(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
result[:new_name] = protocol.name
|
|
|
|
result[:type] = protocol.protocol_type
|
|
|
|
result[:success] = success
|
|
|
|
results << result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
rollbacked = true
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
2017-09-26 19:17:00 +08:00
|
|
|
if rollbacked
|
|
|
|
format.json do
|
|
|
|
render json: {}, status: :bad_request
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json do
|
2016-07-21 19:11:15 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
|
|
|
partial: "protocols/index/results_modal_body.html.erb",
|
|
|
|
locals: { results: results, en_action: "#{action}_results" }
|
|
|
|
})
|
|
|
|
}
|
2017-09-26 19:17:00 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
def load_team_and_type
|
|
|
|
@current_team = current_team
|
2016-07-21 19:11:15 +08:00
|
|
|
# :public, :private or :archive
|
2017-09-26 19:17:00 +08:00
|
|
|
@type = (params[:type] || 'public').to_sym
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_all_permissions
|
2017-01-24 23:57:14 +08:00
|
|
|
load_team_and_type
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2017-11-27 18:24:41 +08:00
|
|
|
render_403 unless can_read_team?(@current_team)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_permissions
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
2018-02-02 01:41:28 +08:00
|
|
|
unless @protocol.present? &&
|
|
|
|
(can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@protocol))
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to { |f| f.json { render json: {}, status: :unauthorized } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_create_permissions
|
2017-01-24 23:57:14 +08:00
|
|
|
load_team_and_type
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2018-01-05 22:15:50 +08:00
|
|
|
if !can_create_protocols_in_repository?(@current_team) || @type == :archive
|
2016-07-21 19:11:15 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_clone_permissions
|
2017-01-24 23:57:14 +08:00
|
|
|
load_team_and_type
|
2016-07-21 19:11:15 +08:00
|
|
|
@original = Protocol.find_by_id(params[:id])
|
|
|
|
|
|
|
|
if @original.blank? ||
|
2018-02-02 01:41:28 +08:00
|
|
|
!can_clone_protocol_in_repository?(@original) || @type == :archive
|
2016-07-21 19:11:15 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-19 21:47:36 +08:00
|
|
|
def check_manage_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
2018-02-16 01:46:29 +08:00
|
|
|
render_403 unless @protocol.present? &&
|
2018-02-19 21:47:36 +08:00
|
|
|
(can_manage_protocol_in_module?(@protocol) ||
|
|
|
|
can_manage_protocol_in_repository?(@protocol))
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2018-02-16 01:46:29 +08:00
|
|
|
def check_manage_parent_in_repository_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
2018-02-02 01:41:28 +08:00
|
|
|
render_403 unless @protocol.present? &&
|
2018-02-16 01:46:29 +08:00
|
|
|
can_read_protocol_in_module?(@protocol) &&
|
2018-02-16 18:12:30 +08:00
|
|
|
can_manage_protocol_in_repository?(@protocol.parent)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2018-02-19 21:47:36 +08:00
|
|
|
def check_manage_all_in_repository_permissions
|
2018-02-16 19:03:44 +08:00
|
|
|
@protocols = Protocol.where(id: params[:protocol_ids])
|
|
|
|
@protocols.find_each do |protocol|
|
|
|
|
unless can_manage_protocol_in_repository?(protocol)
|
|
|
|
respond_to { |f| f.json { render json: {}, status: :unauthorized } }
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2018-02-19 21:47:36 +08:00
|
|
|
def check_restore_all_in_repository_permissions
|
2018-02-16 19:03:44 +08:00
|
|
|
@protocols = Protocol.where(id: params[:protocol_ids])
|
|
|
|
@protocols.find_each do |protocol|
|
2018-02-19 21:47:36 +08:00
|
|
|
unless can_restore_protocol_in_repository?(protocol)
|
2018-02-16 19:03:44 +08:00
|
|
|
respond_to { |f| f.json { render json: {}, status: :unauthorized } }
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_load_from_repository_views_permissions
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
|
|
|
|
2018-02-02 01:41:28 +08:00
|
|
|
render_403 if @protocol.blank? || !can_read_protocol_in_module?(@protocol)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_load_from_repository_permissions
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
|
|
|
@source = Protocol.find_by_id(params[:source_id])
|
|
|
|
|
2018-02-02 01:41:28 +08:00
|
|
|
render_403 unless @protocol.present? && @source.present? &&
|
|
|
|
(can_manage_protocol_in_module?(@protocol) ||
|
|
|
|
can_read_protocol_in_repository?(@source))
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_load_from_file_permissions
|
|
|
|
@protocol_json = params[:protocol]
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
|
|
|
@my_module = @protocol.my_module
|
|
|
|
|
|
|
|
if @protocol_json.blank? ||
|
2017-09-26 19:17:00 +08:00
|
|
|
@protocol.blank? ||
|
|
|
|
@my_module.blank? ||
|
2018-02-02 01:41:28 +08:00
|
|
|
!can_manage_protocol_in_module?(@protocol)
|
2016-07-21 19:11:15 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_copy_to_repository_permissions
|
|
|
|
@protocol = Protocol.find_by_id(params[:id])
|
|
|
|
@my_module = @protocol.my_module
|
|
|
|
|
2018-02-02 01:41:28 +08:00
|
|
|
render_403 unless @my_module.present? &&
|
2018-02-06 01:55:37 +08:00
|
|
|
(can_read_protocol_in_module?(@protocol) ||
|
|
|
|
can_create_protocols_in_repository?(@protocol.team))
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_import_permissions
|
|
|
|
@protocol_json = params[:protocol]
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.find(params[:team_id])
|
2016-07-21 19:11:15 +08:00
|
|
|
@type = params[:type] ? params[:type].to_sym : nil
|
2018-01-23 18:06:33 +08:00
|
|
|
unless @protocol_json.present? && @team.present? &&
|
|
|
|
(@type == :public || @type == :private) &&
|
|
|
|
can_create_protocols_in_repository?(@team)
|
2016-07-21 19:11:15 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_export_permissions
|
|
|
|
@protocols = Protocol.where(id: params[:protocol_ids])
|
2017-12-06 02:51:44 +08:00
|
|
|
render_403 if @protocols.blank?
|
|
|
|
@protocols.each do |p|
|
2018-02-02 01:41:28 +08:00
|
|
|
render_403 unless can_read_protocol_in_module?(p) ||
|
|
|
|
can_read_protocol_in_repository?(p)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to_repository_params
|
|
|
|
params.require(:protocol).permit(:name, :protocol_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
params.require(:protocol).permit(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def metadata_params
|
|
|
|
params.require(:protocol).permit(:name, :authors, :description)
|
|
|
|
end
|
2018-03-19 23:10:28 +08:00
|
|
|
|
2018-03-26 19:07:00 +08:00
|
|
|
def check_protocolsio_import_permissions
|
2018-03-19 23:10:28 +08:00
|
|
|
render_403 unless can_create_protocols_in_repository?(current_team)
|
|
|
|
end
|
2016-08-12 22:31:40 +08:00
|
|
|
end
|