2023-05-10 17:15:28 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Toolbars
|
|
|
|
class LabelTemplatesService
|
|
|
|
attr_reader :current_user
|
|
|
|
|
|
|
|
include Canaid::Helpers::PermissionsHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
def initialize(current_user, label_template_ids: [])
|
|
|
|
@current_user = current_user
|
|
|
|
@label_templates = LabelTemplate.where(id: label_template_ids)
|
|
|
|
|
|
|
|
@single = @label_templates.length == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def actions
|
2023-05-16 18:27:28 +08:00
|
|
|
return [] if @label_templates.none?
|
2023-05-10 17:15:28 +08:00
|
|
|
|
|
|
|
[
|
2023-05-16 18:27:28 +08:00
|
|
|
edit_action,
|
2023-05-10 17:15:28 +08:00
|
|
|
duplicate_action,
|
|
|
|
set_as_default_action,
|
|
|
|
delete_action
|
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-05-16 18:27:28 +08:00
|
|
|
def any_fluics?
|
|
|
|
@label_templates.any? { |lt| lt.type == 'FluicsLabelTemplate' }
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_action
|
|
|
|
return unless @single
|
|
|
|
|
2023-06-06 17:42:03 +08:00
|
|
|
return unless can_manage_label_templates?(current_user.current_team)
|
|
|
|
|
2023-05-16 18:27:28 +08:00
|
|
|
{
|
|
|
|
name: 'edit',
|
|
|
|
label: I18n.t('label_templates.index.toolbar.edit'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-edit',
|
2023-05-16 18:27:28 +08:00
|
|
|
path: label_template_path(@label_templates.first),
|
|
|
|
type: :link
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-05-10 17:15:28 +08:00
|
|
|
def set_as_default_action
|
|
|
|
return unless @single
|
|
|
|
|
|
|
|
return unless can_manage_label_templates?(current_user.current_team)
|
|
|
|
|
|
|
|
return if @label_templates.first.default
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'set_as_default',
|
2023-05-16 18:27:28 +08:00
|
|
|
label: I18n.t("label_templates.index.toolbar.set_#{@label_templates.first.type}_default"),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-dashboard',
|
2023-05-10 17:15:28 +08:00
|
|
|
button_id: 'setZplDefaultLabelTemplate',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def duplicate_action
|
2023-05-16 18:27:28 +08:00
|
|
|
return if any_fluics?
|
|
|
|
|
2023-05-10 17:15:28 +08:00
|
|
|
return unless can_manage_label_templates?(current_user.current_team)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'duplicate',
|
|
|
|
label: I18n.t('label_templates.index.toolbar.duplicate'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-duplicate',
|
2023-05-10 17:15:28 +08:00
|
|
|
button_id: 'duplicateLabelTemplate',
|
|
|
|
path: duplicate_label_templates_path,
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_action
|
2023-05-16 18:27:28 +08:00
|
|
|
return if any_fluics?
|
|
|
|
|
2023-05-10 17:15:28 +08:00
|
|
|
return unless can_manage_label_templates?(current_user.current_team)
|
|
|
|
|
|
|
|
return unless @label_templates.none?(&:default)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: 'delete',
|
|
|
|
label: I18n.t('label_templates.index.toolbar.delete'),
|
2023-06-08 14:33:37 +08:00
|
|
|
icon: 'sn-icon sn-icon-delete',
|
2023-05-10 17:15:28 +08:00
|
|
|
button_id: 'deleteLabelTemplate',
|
|
|
|
type: :legacy
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|