2020-12-01 16:59:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ProjectFoldersController < ApplicationController
|
2020-12-03 19:53:19 +08:00
|
|
|
include InputSanitizeHelper
|
2021-01-12 17:51:17 +08:00
|
|
|
include ProjectsHelper
|
2021-01-06 22:01:39 +08:00
|
|
|
include ProjectFoldersHelper
|
2020-12-03 19:53:19 +08:00
|
|
|
|
2020-12-01 16:59:08 +08:00
|
|
|
before_action :load_current_folder, only: %i(new)
|
2020-12-29 05:49:52 +08:00
|
|
|
before_action :load_project_folder, only: %i(edit update)
|
2020-12-01 16:59:08 +08:00
|
|
|
before_action :check_create_permissions, only: %i(new create)
|
2020-12-29 05:49:52 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(archive move_to)
|
2020-12-01 16:59:08 +08:00
|
|
|
|
|
|
|
def new
|
2021-01-12 17:51:17 +08:00
|
|
|
@project_folder =
|
|
|
|
current_team.project_folders.new(parent_folder: @current_folder, archived: projects_view_mode_archived?)
|
2020-12-01 16:59:08 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'projects/index/modals/new_project_folder.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-12-03 19:53:19 +08:00
|
|
|
project_folder = current_team.project_folders.new(project_folders_params)
|
2020-12-01 16:59:08 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2020-12-03 19:53:19 +08:00
|
|
|
if project_folder.save
|
2020-12-14 22:54:18 +08:00
|
|
|
log_activity(:create_project_folder, project_folder, project_folder: project_folder.id)
|
2020-12-03 19:53:19 +08:00
|
|
|
message = t('projects.index.modal_new_project_folder.success_flash',
|
|
|
|
name: escape_input(project_folder.name))
|
|
|
|
render json: { message: message }
|
2020-12-01 16:59:08 +08:00
|
|
|
else
|
2020-12-03 19:53:19 +08:00
|
|
|
render json: project_folder.errors,
|
2020-12-01 16:59:08 +08:00
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-04 16:54:23 +08:00
|
|
|
def move_to
|
2021-01-06 22:01:39 +08:00
|
|
|
destination_folder =
|
|
|
|
if move_params[:destination_folder_id] == 'root_folder'
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
current_team.project_folders.find(move_params[:destination_folder_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2020-11-04 16:54:23 +08:00
|
|
|
move_projects(destination_folder)
|
|
|
|
move_folders(destination_folder)
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: { flash: I18n.t('projects.move.success_flash') } }
|
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
Rails.logger.error e.backtrace.join("\n")
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: { flash: I18n.t('projects.move.error_flash') }, status: :bad_request }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-06 22:01:39 +08:00
|
|
|
def move_to_modal
|
2021-01-08 00:31:27 +08:00
|
|
|
view_state = current_team.current_view_state(current_user)
|
2021-01-12 17:51:17 +08:00
|
|
|
@current_sort = view_state.state.dig('projects', projects_view_mode, 'sort') || 'atoz'
|
2021-01-08 00:31:27 +08:00
|
|
|
|
2021-01-06 22:01:39 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'projects/index/modals/move_to_modal_contents.html.erb',
|
|
|
|
locals: { items_label: items_label(params[:items]) })
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-12-14 22:54:18 +08:00
|
|
|
def edit
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'projects/index/modals/edit_folder_contents.html.erb',
|
2020-12-29 05:49:52 +08:00
|
|
|
locals: { folder: @project_folder })
|
2020-12-01 21:51:26 +08:00
|
|
|
|
2020-12-14 22:54:18 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-12-29 05:49:52 +08:00
|
|
|
if @project_folder.update(project_folders_params)
|
|
|
|
log_activity(:rename_project_folder, @project_folder, project_folder: @project_folder.id)
|
|
|
|
render json: { message: t('projects.update.success_flash', name: escape_input(@project_folder.name)) }
|
2020-12-14 22:54:18 +08:00
|
|
|
else
|
2020-12-29 05:49:52 +08:00
|
|
|
render json: { message: t('projects.update.error_flash', name: escape_input(@project_folder.name)),
|
|
|
|
errors: @project_folder.errors }, status: :unprocessable_entity
|
2020-12-14 22:54:18 +08:00
|
|
|
end
|
2020-12-01 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def archive
|
|
|
|
folder = current_team.project_folders.find(params[:id])
|
|
|
|
|
2020-12-14 22:54:18 +08:00
|
|
|
log_activity(:archive_project_folder, folder, project_folder: folder.id)
|
2020-12-01 21:51:26 +08:00
|
|
|
end
|
|
|
|
|
2020-12-01 16:59:08 +08:00
|
|
|
private
|
|
|
|
|
2020-12-29 05:49:52 +08:00
|
|
|
def load_project_folder
|
|
|
|
@project_folder = current_team.project_folders.find_by(id: params[:id])
|
2020-12-14 22:54:18 +08:00
|
|
|
|
2020-12-29 05:49:52 +08:00
|
|
|
render_404 unless @project_folder
|
2020-12-14 22:54:18 +08:00
|
|
|
end
|
|
|
|
|
2020-12-01 16:59:08 +08:00
|
|
|
def load_current_folder
|
2020-12-03 19:53:19 +08:00
|
|
|
if params[:project_folder_id].present?
|
2020-12-01 16:59:08 +08:00
|
|
|
@current_folder = current_team.project_folders.find_by(id: params[:project_folder_id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_folders_params
|
2021-01-12 17:51:17 +08:00
|
|
|
params.require(:project_folder).permit(:name, :parent_folder_id, :archived)
|
2020-12-01 16:59:08 +08:00
|
|
|
end
|
|
|
|
|
2020-11-04 16:54:23 +08:00
|
|
|
def move_params
|
2021-01-06 22:01:39 +08:00
|
|
|
parsed_params = ActionController::Parameters.new(
|
|
|
|
movables: JSON.parse(params[:movables]),
|
|
|
|
destination_folder_id: params[:destination_folder_id]
|
|
|
|
)
|
|
|
|
parsed_params.require(:destination_folder_id)
|
|
|
|
parsed_params.require(:movables)
|
|
|
|
parsed_params.permit(:destination_folder_id, movables: %i(id type))
|
2020-11-04 16:54:23 +08:00
|
|
|
end
|
|
|
|
|
2020-12-01 16:59:08 +08:00
|
|
|
def check_create_permissions
|
|
|
|
render_403 unless can_create_project_folders?(current_team)
|
2020-12-03 20:04:55 +08:00
|
|
|
end
|
2020-12-03 19:50:19 +08:00
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_update_team?(current_team)
|
|
|
|
end
|
|
|
|
|
2020-11-04 16:54:23 +08:00
|
|
|
def move_projects(destination_folder)
|
|
|
|
project_ids = move_params[:movables].collect { |movable| movable[:id] if movable[:type] == 'project' }.compact
|
|
|
|
return if project_ids.blank?
|
|
|
|
|
2020-12-04 19:58:23 +08:00
|
|
|
current_team.projects.where(id: project_ids).each do |project|
|
|
|
|
source_folder_name = project.project_folder&.name || I18n.t('global_activities.root_folder_level')
|
|
|
|
project.update!(project_folder: destination_folder)
|
|
|
|
destination_folder_name = project.project_folder&.name || I18n.t('global_activities.root_folder_level')
|
2020-12-01 21:51:26 +08:00
|
|
|
|
2020-12-14 22:54:18 +08:00
|
|
|
log_activity(:move_project, project, project: project.id,
|
2020-12-04 19:58:23 +08:00
|
|
|
destination_folder: destination_folder_name,
|
2020-12-14 22:54:18 +08:00
|
|
|
source_folder: source_folder_name)
|
2020-12-01 21:51:26 +08:00
|
|
|
end
|
2020-11-04 16:54:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def move_folders(destination_folder)
|
|
|
|
folder_ids = move_params[:movables].collect { |movable| movable[:id] if movable[:type] == 'project_folder' }.compact
|
|
|
|
return if folder_ids.blank?
|
|
|
|
|
2020-12-04 19:58:23 +08:00
|
|
|
current_team.project_folders.where(id: folder_ids).each do |folder|
|
|
|
|
source_folder_name = folder.parent_folder&.name || I18n.t('global_activities.root_folder_level')
|
|
|
|
folder.update!(parent_folder: destination_folder)
|
|
|
|
destination_folder_name = folder.parent_folder&.name || I18n.t('global_activities.root_folder_level')
|
2020-12-01 21:51:26 +08:00
|
|
|
|
2020-12-14 22:54:18 +08:00
|
|
|
log_activity(:move_project_folder, folder, project_folder: folder.id,
|
2020-12-04 19:58:23 +08:00
|
|
|
destination_folder: destination_folder_name,
|
2020-12-14 22:54:18 +08:00
|
|
|
source_folder: source_folder_name)
|
2020-12-01 21:51:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_activity(type_of, subject, items = {})
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of, owner: current_user, team: subject.team, subject: subject, message_items: items)
|
2020-12-01 16:59:08 +08:00
|
|
|
end
|
|
|
|
end
|