2023-04-26 17:20:10 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Toolbars
|
|
|
|
class ProjectsService
|
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2023-11-10 20:34:36 +08:00
|
|
|
def initialize(current_user, items: [])
|
2023-04-26 17:20:10 +08:00
|
|
|
@current_user = current_user
|
2023-11-10 20:34:36 +08:00
|
|
|
project_ids = items.select { |i| i['type'] == 'projects' }.map { |i| i['id'] }
|
|
|
|
project_folder_ids = items.select { |i| i['type'] == 'project_folders' }.map { |i| i['id'] }
|
|
|
|
|
2023-04-26 17:20:10 +08:00
|
|
|
@projects = current_user.current_team.projects.where(id: project_ids)
|
|
|
|
@project_folders = current_user.current_team.project_folders.where(id: project_folder_ids)
|
|
|
|
|
|
|
|
@items = @projects + @project_folders
|
|
|
|
|
|
|
|
@single = @items.length == 1
|
|
|
|
|
|
|
|
@item_type = if project_ids.blank? && project_folder_ids.blank?
|
|
|
|
:none
|
|
|
|
elsif project_ids.present? && project_folder_ids.present?
|
|
|
|
:any
|
|
|
|
elsif project_folder_ids.present?
|
|
|
|
:project_folder
|
|
|
|
else
|
|
|
|
:project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions
|
|
|
|
return [] if @item_type == :none
|
|
|
|
|
|
|
|
[
|
2023-05-04 21:52:46 +08:00
|
|
|
restore_action,
|
2023-05-04 17:29:19 +08:00
|
|
|
edit_action,
|
|
|
|
access_action,
|
|
|
|
move_action,
|
|
|
|
export_action,
|
|
|
|
archive_action,
|
|
|
|
comments_action,
|
2023-05-04 20:59:16 +08:00
|
|
|
activities_action,
|
|
|
|
delete_folder_action
|
2023-04-26 17:20:10 +08:00
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def edit_action
|
2023-04-26 17:20:10 +08:00
|
|
|
return unless @single
|
|
|
|
|
2023-05-04 21:52:46 +08:00
|
|
|
if @items.first.is_a?(Project)
|
|
|
|
project = @items.first
|
|
|
|
|
|
|
|
return unless can_manage_project?(project)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'edit',
|
|
|
|
label: I18n.t('projects.index.edit_option'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-edit',
|
2023-05-04 21:52:46 +08:00
|
|
|
button_class: 'edit-btn',
|
|
|
|
path: edit_project_path(project),
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-05-04 21:52:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
project_folder = @items.first
|
|
|
|
|
|
|
|
return unless can_create_project_folders?(project_folder.team)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'edit',
|
|
|
|
label: I18n.t('projects.index.edit_option'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-edit',
|
2023-05-04 21:52:46 +08:00
|
|
|
button_class: 'edit-btn',
|
|
|
|
path: edit_project_folder_path(project_folder),
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-05-04 21:52:46 +08:00
|
|
|
}
|
|
|
|
end
|
2023-04-26 17:20:10 +08:00
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def access_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
return unless @item_type == :project
|
|
|
|
|
|
|
|
project = @items.first
|
|
|
|
|
2023-08-24 00:05:20 +08:00
|
|
|
return unless can_manage_team?(project.team) || can_read_project?(project)
|
2023-05-04 17:29:19 +08:00
|
|
|
|
|
|
|
path = if can_manage_project_users?(project)
|
|
|
|
edit_access_permissions_project_path(project)
|
|
|
|
else
|
|
|
|
access_permissions_project_path(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'access',
|
|
|
|
label: I18n.t('general.access'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-project-member-access',
|
2023-05-04 17:29:19 +08:00
|
|
|
path: path,
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-05-04 17:29:19 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_action
|
2023-08-24 00:05:20 +08:00
|
|
|
return unless can_manage_team?(@items.first.team) &&
|
|
|
|
@items.all? { |item| item.is_a?(Project) ? can_read_project?(item) : true }
|
2023-04-26 17:20:10 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
name: 'move',
|
|
|
|
label: I18n.t('projects.index.move_button'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-move',
|
2023-04-26 17:20:10 +08:00
|
|
|
path: move_to_modal_project_folders_path,
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def export_action
|
|
|
|
return unless @items.all? { |item| item.is_a?(Project) ? can_export_project?(item) : true }
|
2023-04-26 17:20:10 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
name: 'export',
|
|
|
|
label: I18n.t('projects.export_projects.export_button'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-export',
|
2023-05-04 17:29:19 +08:00
|
|
|
path: export_projects_modal_team_path(@items.first.team),
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def archive_action
|
|
|
|
return unless @items.all? do |item|
|
2023-05-04 20:59:16 +08:00
|
|
|
item.is_a?(Project) && can_archive_project?(item)
|
2023-04-26 17:20:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'archive',
|
|
|
|
label: I18n.t('projects.index.archive_button'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-archive',
|
2023-04-26 17:20:10 +08:00
|
|
|
path: archive_group_projects_path,
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit,
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def restore_action
|
|
|
|
return unless @items.all? do |item|
|
2023-05-04 20:59:16 +08:00
|
|
|
item.is_a?(Project) && can_restore_project?(item)
|
2023-04-26 17:20:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'restore',
|
|
|
|
label: I18n.t('projects.index.restore_button'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-restore',
|
2023-04-26 17:20:10 +08:00
|
|
|
button_class: 'restore-projects-btn',
|
|
|
|
path: restore_group_projects_path,
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def delete_folder_action
|
|
|
|
return unless @items.all? do |item|
|
2023-05-04 20:59:16 +08:00
|
|
|
item.is_a?(ProjectFolder) && can_delete_project_folder?(item)
|
2023-04-26 17:20:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'delete_folders',
|
|
|
|
label: I18n.t('general.delete'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-delete',
|
2023-05-04 20:59:16 +08:00
|
|
|
path: destroy_modal_project_folders_path(project_folder_ids: @items.map(&:id)),
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def comments_action
|
2023-04-26 17:20:10 +08:00
|
|
|
return unless @single
|
|
|
|
|
|
|
|
return unless @item_type == :project
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
project = @items.first
|
2023-04-26 17:20:10 +08:00
|
|
|
|
|
|
|
return unless can_read_project?(project)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'comments',
|
|
|
|
label: I18n.t('Comments'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-comments',
|
2023-11-10 20:34:36 +08:00
|
|
|
type: :emit
|
2023-04-26 17:20:10 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
def activities_action
|
2023-04-26 17:20:10 +08:00
|
|
|
return unless @single
|
|
|
|
|
|
|
|
return unless @item_type == :project
|
|
|
|
|
2023-05-04 17:29:19 +08:00
|
|
|
project = @items.first
|
2023-04-26 17:20:10 +08:00
|
|
|
|
|
|
|
return unless can_read_project?(project)
|
|
|
|
|
|
|
|
activity_url_params = Activity.url_search_query({ subjects: { Project: [project] } })
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'activities',
|
|
|
|
label: I18n.t('nav.label.activities'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-activities',
|
2023-04-26 17:20:10 +08:00
|
|
|
button_class: 'project-activities-btn',
|
|
|
|
path: "/global_activities?#{activity_url_params}",
|
|
|
|
type: :link
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|