2022-07-26 19:52:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class LabelTemplatesController < ApplicationController
|
2022-07-27 16:10:32 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2022-08-02 20:51:18 +08:00
|
|
|
before_action :check_feature_enabled
|
2022-08-12 05:05:57 +08:00
|
|
|
before_action :check_view_permissions, except: %i(create duplicate set_default delete update)
|
2022-08-04 19:36:14 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(create duplicate set_default delete update)
|
2022-07-27 16:10:32 +08:00
|
|
|
before_action :load_label_templates, only: %i(index datatable)
|
2022-08-24 19:55:54 +08:00
|
|
|
before_action :load_label_template, only: %i(show set_default update)
|
2022-07-26 19:52:40 +08:00
|
|
|
|
|
|
|
layout 'fluid'
|
|
|
|
|
|
|
|
def index; end
|
|
|
|
|
2022-07-27 16:10:32 +08:00
|
|
|
def datatable
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: ::LabelTemplateDatatable.new(
|
|
|
|
view_context,
|
|
|
|
can_manage_label_templates?(current_team),
|
|
|
|
@label_templates
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-04 16:51:30 +08:00
|
|
|
def show
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: @label_template, serializer: LabelTemplateSerializer, user: current_user }
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
2022-07-28 20:37:59 +08:00
|
|
|
|
2022-08-04 19:36:14 +08:00
|
|
|
def create
|
2022-08-08 16:06:00 +08:00
|
|
|
label_template = ZebraLabelTemplate.default.save!
|
2022-08-04 19:36:14 +08:00
|
|
|
|
|
|
|
redirect_to label_template_path(label_template, new_label: true)
|
|
|
|
end
|
|
|
|
|
2022-08-04 16:51:30 +08:00
|
|
|
def update
|
|
|
|
if @label_template.update(label_template_params)
|
|
|
|
render json: @label_template, serializer: LabelTemplateSerializer, user: current_user
|
|
|
|
else
|
|
|
|
render json: { error: @label_template.errors.messages }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2022-07-28 20:37:59 +08:00
|
|
|
|
|
|
|
def duplicate
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each do |template|
|
|
|
|
new_template = template.dup
|
|
|
|
new_template.default = false
|
|
|
|
new_template.name = template.name + '(1)'
|
|
|
|
new_template.save!
|
|
|
|
end
|
|
|
|
render json: { message: I18n.t('label_templates.index.templates_duplicated',
|
|
|
|
count: params[:selected_ids].length) }
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each(&:destroy!)
|
|
|
|
render json: { message: I18n.t('label_templates.index.templates_deleted') }
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotDestroyed => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
LabelTemplate.find_by(team_id: current_team.id,
|
2022-08-08 16:06:00 +08:00
|
|
|
type: @label_template.type,
|
2022-07-28 20:37:59 +08:00
|
|
|
default: true)&.update!(default: false)
|
|
|
|
@label_template.update!(default: true)
|
|
|
|
render json: { message: I18n.t('label_templates.index.template_set_as_default') }
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
2022-07-27 16:10:32 +08:00
|
|
|
end
|
|
|
|
|
2022-08-10 17:35:38 +08:00
|
|
|
def template_tags
|
|
|
|
render json: LabelTemplates::TagService.new(current_team).tags
|
|
|
|
end
|
|
|
|
|
2022-08-12 05:05:57 +08:00
|
|
|
def zpl_preview
|
2022-08-24 19:55:54 +08:00
|
|
|
service = LabelTemplatesPreviewService.new(params, current_user)
|
2022-08-12 05:05:57 +08:00
|
|
|
payload = service.generate_zpl_preview!
|
|
|
|
|
|
|
|
if service.error.blank?
|
2022-08-24 19:55:54 +08:00
|
|
|
render json: { base64_preview: payload }
|
2022-08-12 05:05:57 +08:00
|
|
|
else
|
|
|
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-29 15:52:54 +08:00
|
|
|
def sync_fluics_templates
|
|
|
|
sync_service = LabelPrinters::Fluics::SyncService.new(current_user, current_team)
|
|
|
|
sync_service.sync_templates!
|
|
|
|
render json: { message: t('label_templates.fluics.sync.success') }
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { error: t('label_templates.fluics.sync.error') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2022-07-26 19:52:40 +08:00
|
|
|
private
|
|
|
|
|
2022-08-02 20:51:18 +08:00
|
|
|
def check_feature_enabled
|
|
|
|
render :promo unless LabelTemplate.enabled?
|
|
|
|
end
|
|
|
|
|
2022-07-26 19:52:40 +08:00
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_view_label_templates?(current_team)
|
|
|
|
end
|
2022-07-27 16:10:32 +08:00
|
|
|
|
2022-07-28 20:37:59 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_label_templates?(current_team)
|
|
|
|
end
|
|
|
|
|
2022-07-27 16:10:32 +08:00
|
|
|
def load_label_templates
|
|
|
|
@label_templates = LabelTemplate.where(team_id: current_team.id)
|
|
|
|
end
|
2022-07-28 20:37:59 +08:00
|
|
|
|
|
|
|
def load_label_template
|
|
|
|
@label_template = LabelTemplate.where(team_id: current_team.id).find(params[:id])
|
|
|
|
end
|
2022-08-04 16:51:30 +08:00
|
|
|
|
|
|
|
def label_template_params
|
|
|
|
params.require(:label_template).permit(:name, :description, :content)
|
|
|
|
end
|
2022-07-26 19:52:40 +08:00
|
|
|
end
|