scinote-web/app/controllers/projects_controller.rb

358 lines
11 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class ProjectsController < ApplicationController
include SampleActions
2016-07-21 19:11:15 +08:00
include RenamingUtil
include TeamsHelper
include InputSanitizeHelper
2016-02-12 23:52:43 +08:00
before_action :load_vars, only: %i(show edit update
notifications reports
samples experiment_archive
delete_samples samples_index)
before_action :load_projects_tree, only: %i(sidebar show samples archive
2018-06-01 21:05:12 +08:00
experiment_archive)
before_action :check_view_permissions, only: %i(show reports notifications
samples experiment_archive
samples_index)
before_action :check_create_permissions, only: %i(new create)
before_action :check_manage_permissions, only: :edit
before_action :set_inline_name_editing, only: %i(show experiment_archive)
2016-02-12 23:52:43 +08:00
# except parameter could be used but it is not working.
2018-03-22 18:41:33 +08:00
layout 'fluid'
2016-02-12 23:52:43 +08:00
def index
respond_to do |format|
format.json do
@current_team = current_team if current_team
@current_team ||= current_user.teams.first
@projects = ProjectsOverviewService
.new(@current_team, current_user, params)
.project_cards
render json: {
html: render_to_string(
partial: 'projects/index/team_projects.html.erb',
locals: { projects: @projects }
),
2018-11-08 16:43:32 +08:00
count: @projects.size
}
end
format.html do
current_team_switch(Team.find_by_id(params[:team])) if params[:team]
@teams = current_user.teams
# New project for create new project modal
@project = Project.new
2018-12-10 22:42:05 +08:00
if current_team
view_state =
current_team.current_view_state(current_user)
@current_filter = view_state.state.dig('projects', 'filter')
@current_sort = view_state.state.dig('projects', 'cards', 'sort')
end
load_projects_tree
end
2016-10-12 15:30:55 +08:00
end
end
2016-10-12 15:30:55 +08:00
def index_dt
@draw = params[:draw].to_i
respond_to do |format|
format.json do
@current_team = current_team if current_team
@current_team ||= current_user.teams.first
@projects = ProjectsOverviewService
.new(@current_team, current_user, params)
.projects_datatable
end
end
2016-02-12 23:52:43 +08:00
end
def sidebar
2020-02-27 23:04:44 +08:00
current_task ||= current_task || nil
current_experiment ||= current_experiment || current_task&.experiment || nil
current_project ||= current_experiment&.project || current_task&.experiment&.project || nil
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'shared/sidebar/projects.html.erb',
2020-02-27 23:04:44 +08:00
locals: {
current_project: current_project,
current_experiment: current_experiment,
current_task: current_task
},
formats: :html
)
}
end
end
end
2016-02-12 23:52:43 +08:00
def new
@project = Project.new
end
def create
@project = Project.new(project_params)
2016-10-12 18:56:44 +08:00
@project.created_by = current_user
2016-02-12 23:52:43 +08:00
@project.last_modified_by = current_user
if current_team.id == project_params[:team_id].to_i &&
2016-10-12 18:56:44 +08:00
@project.save
2016-02-12 23:52:43 +08:00
# Create user-project association
up = UserProject.new(
role: :owner,
user: current_user,
project: @project
)
up.save
2019-03-08 00:26:42 +08:00
log_activity(:create_project)
2016-02-12 23:52:43 +08:00
message = t('projects.create.success_flash', name: escape_input(@project.name))
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json {
render json: { message: message }, status: :ok
2016-02-12 23:52:43 +08:00
}
end
else
respond_to do |format|
format.json {
render json: @project.errors, status: :unprocessable_entity
}
end
end
end
def edit
respond_to do |format|
format.json {
render json: {
html: render_to_string({
partial: "edit.html.erb",
locals: { project: @project }
}),
title: t('projects.index.modal_edit_project.modal_title',
project: escape_input(@project.name))
2016-02-12 23:52:43 +08:00
}
}
end
end
def update
return_error = false
flash_error = t('projects.update.error_flash', name: escape_input(@project.name))
2016-02-12 23:52:43 +08:00
# Check archive permissions if archiving/restoring
if project_params.include? :archived
if (project_params[:archived] == 'true' &&
!can_archive_project?(@project)) ||
(project_params[:archived] == 'false' &&
!can_restore_project?(@project))
2016-02-12 23:52:43 +08:00
return_error = true
is_archive = project_params[:archived] == 'true' ? 'archive' : 'restore'
flash_error =
t("projects.#{is_archive}.error_flash", name: escape_input(@project.name))
2016-02-12 23:52:43 +08:00
end
elsif !can_manage_project?(@project)
render_403 && return
2016-02-12 23:52:43 +08:00
end
message_renamed = nil
message_visibility = nil
if (project_params.include? :name) &&
(project_params[:name] != @project.name)
2019-03-08 00:26:42 +08:00
message_renamed = true
2016-02-12 23:52:43 +08:00
end
if (project_params.include? :visibility) &&
(project_params[:visibility] != @project.visibility)
2019-03-08 00:26:42 +08:00
message_visibility = if project_params[:visibility] == 'visible'
t('projects.activity.visibility_visible')
else
t('projects.activity.visibility_hidden')
end
2016-02-12 23:52:43 +08:00
end
@project.last_modified_by = current_user
if !return_error && @project.update(project_params)
2016-02-12 23:52:43 +08:00
# Add activities if needed
log_activity(:change_project_visibility, visibility: message_visibility) if message_visibility.present?
log_activity(:rename_project) if message_renamed.present?
log_activity(:archive_project) if project_params[:archived] == 'true'
log_activity(:restore_project) if project_params[:archived] == 'false'
2016-02-12 23:52:43 +08:00
flash_success = t('projects.update.success_flash', name: escape_input(@project.name))
if project_params[:archived] == 'true'
flash_success = t('projects.archive.success_flash', name: escape_input(@project.name))
elsif project_params[:archived] == 'false'
flash_success = t('projects.restore.success_flash', name: escape_input(@project.name))
end
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.html do
2016-02-12 23:52:43 +08:00
# Redirect URL for archive view is different as for other views.
if project_params[:archived] == 'false'
2016-02-12 23:52:43 +08:00
# The project should be restored
unless @project.archived
@project.restore(current_user)
end
elsif @project.archived
2016-02-12 23:52:43 +08:00
# The project should be archived
@project.archive(current_user)
2016-02-12 23:52:43 +08:00
end
redirect_to projects_path
2016-02-12 23:52:43 +08:00
flash[:success] = flash_success
end
format.json do
2016-02-12 23:52:43 +08:00
render json: {
status: :ok,
message: flash_success
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
else
return_error = true
end
if return_error
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.html do
2016-02-12 23:52:43 +08:00
flash[:error] = flash_error
# Redirect URL for archive view is different as for other views.
if URI(request.referer).path == projects_archive_path
redirect_to projects_archive_path
else
redirect_to projects_path
end
end
format.json do
render json: { message: flash_error, errors: @project.errors },
status: :unprocessable_entity
end
2016-02-12 23:52:43 +08:00
end
end
end
def show
# save experiments order
if params[:sort]
@project.experiments_order = params[:sort].to_s
@project.save
end
2016-02-12 23:52:43 +08:00
# This is the "info" view
2018-08-28 20:51:05 +08:00
current_team_switch(@project.team)
@current_sort = @project.experiments_order || :new
2016-02-12 23:52:43 +08:00
end
def notifications
@modules = @project
.assigned_modules(current_user)
.order(due_date: :desc)
respond_to do |format|
#format.html
format.json {
render :json => {
:html => render_to_string({
:partial => "notifications.html.erb"
})
}
}
end
end
def samples
@samples_index_link = samples_index_project_path(@project, format: :json)
@team = @project.team
2016-02-12 23:52:43 +08:00
end
def experiment_archive
current_team_switch(@project.team)
2016-02-12 23:52:43 +08:00
end
def samples_index
@team = @project.team
@user = current_user
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.html
format.json do
render json: ::SampleDatatable.new(view_context,
@team,
@project,
nil,
nil,
@user)
end
2016-02-12 23:52:43 +08:00
end
end
def dt_state_load
respond_to do |format|
format.json do
render json: {
state: current_team.current_view_state(current_user)
.state.dig('projects', 'table')
}
end
end
end
2016-02-12 23:52:43 +08:00
private
def project_params
params.require(:project).permit(:name, :team_id, :visibility, :archived)
2016-02-12 23:52:43 +08:00
end
def load_vars
@project = Project.find_by_id(params[:id])
unless @project
render_404
end
end
def load_projects_tree
# Switch to correct team
current_team_switch(@project.team) unless @project.nil? || @project.new_record?
2018-03-22 18:41:33 +08:00
if current_user.teams.any?
@current_team = current_team if current_team
@current_team ||= current_user.teams.first
@current_sort ||= 'new'
@projects_tree = current_user.projects_tree(@current_team, 'atoz')
else
@projects_tree = []
end
end
2016-02-12 23:52:43 +08:00
def check_view_permissions
2018-01-25 19:00:30 +08:00
render_403 unless can_read_project?(@project)
2016-02-12 23:52:43 +08:00
end
2017-12-08 00:08:41 +08:00
def check_create_permissions
2018-01-05 22:15:50 +08:00
render_403 unless can_create_projects?(current_team)
2017-12-08 00:08:41 +08:00
end
def check_manage_permissions
render_403 unless can_manage_project?(@project)
2016-02-12 23:52:43 +08:00
end
2019-03-08 00:26:42 +08:00
def set_inline_name_editing
return unless can_manage_project?(@project)
@inline_editable_title_config = {
name: 'title',
params_group: 'project',
item_id: @project.id,
field_to_udpate: 'name',
path_to_update: project_path(@project)
}
end
2019-03-21 04:34:47 +08:00
def log_activity(type_of, message_items = {})
message_items = { project: @project.id }.merge(message_items)
2019-03-08 00:26:42 +08:00
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @project,
team: @project.team,
project: @project,
2019-03-21 04:34:47 +08:00
message_items: message_items)
2019-03-08 00:26:42 +08:00
end
2016-02-12 23:52:43 +08:00
end