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-09-22 17:07:08 +08:00
|
|
|
before_action :check_feature_enabled, except: :index
|
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)
|
2023-01-16 17:23:34 +08:00
|
|
|
before_action :load_label_template, only: %i(show set_default update template_tags)
|
2022-07-26 19:52:40 +08:00
|
|
|
|
|
|
|
layout 'fluid'
|
|
|
|
|
2022-09-22 17:07:08 +08:00
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: @label_templates, each_serializer: LabelTemplateSerializer, user: current_user
|
|
|
|
end
|
|
|
|
format.html do
|
|
|
|
unless LabelTemplate.enabled?
|
|
|
|
render :promo
|
|
|
|
return
|
|
|
|
end
|
|
|
|
render 'index'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-07-26 19:52:40 +08:00
|
|
|
|
2022-07-27 16:10:32 +08:00
|
|
|
def datatable
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: ::LabelTemplateDatatable.new(
|
|
|
|
view_context,
|
|
|
|
@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-22 23:38:52 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
label_template = ZebraLabelTemplate.default
|
|
|
|
label_template.team = current_team
|
2022-09-15 17:20:20 +08:00
|
|
|
label_template.created_by = current_user
|
|
|
|
label_template.last_modified_by = current_user
|
2022-08-22 23:38:52 +08:00
|
|
|
label_template.save!
|
|
|
|
log_activity(:label_template_created, label_template)
|
|
|
|
redirect_to label_template_path(label_template, new_label: true)
|
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
|
|
|
flash[:error] = I18n.t('errors.general')
|
|
|
|
redirect_to label_templates_path
|
2022-08-04 19:36:14 +08:00
|
|
|
end
|
|
|
|
|
2022-08-04 16:51:30 +08:00
|
|
|
def update
|
2022-08-22 23:38:52 +08:00
|
|
|
@label_template.transaction do
|
|
|
|
@label_template.update!(label_template_params)
|
|
|
|
log_activity(:label_template_edited, @label_template)
|
2022-08-04 16:51:30 +08:00
|
|
|
end
|
2022-08-22 23:38:52 +08:00
|
|
|
render json: @label_template, serializer: LabelTemplateSerializer, user: current_user
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: { error: @label_template.errors.messages }, status: :unprocessable_entity
|
2022-08-04 16:51:30 +08:00
|
|
|
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
|
2022-08-18 19:50:04 +08:00
|
|
|
new_template.created_by = current_user
|
|
|
|
new_template.last_modified_by = current_user
|
2022-07-28 20:37:59 +08:00
|
|
|
new_template.name = template.name + '(1)'
|
|
|
|
new_template.save!
|
2022-08-22 23:38:52 +08:00
|
|
|
log_activity(
|
|
|
|
:label_template_copied,
|
|
|
|
new_template,
|
|
|
|
message_items: { label_template_new: new_template.id, label_template_original: template.id }
|
|
|
|
)
|
2022-07-28 20:37:59 +08:00
|
|
|
end
|
|
|
|
render json: { message: I18n.t('label_templates.index.templates_duplicated',
|
|
|
|
count: params[:selected_ids].length) }
|
|
|
|
end
|
2022-08-22 23:38:52 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
|
|
|
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
2022-07-28 20:37:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
ActiveRecord::Base.transaction do
|
2022-08-22 23:38:52 +08:00
|
|
|
LabelTemplate.where(team_id: current_team.id, id: params[:selected_ids]).each do |template|
|
|
|
|
log_activity(:label_template_deleted, template)
|
|
|
|
template.destroy!
|
|
|
|
end
|
2022-07-28 20:37:59 +08:00
|
|
|
render json: { message: I18n.t('label_templates.index.templates_deleted') }
|
|
|
|
end
|
2022-08-22 23:38:52 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
2022-07-28 20:37:59 +08:00
|
|
|
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
|
2022-08-22 23:38:52 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
Rails.logger.error(e.backtrace.join("\n"))
|
2022-07-28 20:37:59 +08:00
|
|
|
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
|
2023-01-16 17:23:34 +08:00
|
|
|
render json: LabelTemplates::TagService.new(current_team, @label_template).tags
|
2022-08-10 17:35:38 +08:00
|
|
|
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-10-24 21:50:10 +08:00
|
|
|
|
|
|
|
# only render last generated label image
|
|
|
|
payload = service.generate_zpl_preview!.split.last
|
2022-08-12 05:05:57 +08:00
|
|
|
|
|
|
|
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
|
2022-09-22 21:24:30 +08:00
|
|
|
render json: { error: service.error }, status: :unprocessable_entity
|
2022-08-12 05:05:57 +08:00
|
|
|
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') }
|
2022-09-27 04:58:09 +08:00
|
|
|
rescue StandardError => e
|
2022-08-29 15:52:54 +08:00
|
|
|
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
|
2022-10-11 16:58:37 +08:00
|
|
|
params.require(:label_template).permit(:name, :description, :content, :width_mm, :height_mm, :unit, :density)
|
2022-08-04 16:51:30 +08:00
|
|
|
end
|
2022-08-22 23:38:52 +08:00
|
|
|
|
|
|
|
def log_activity(type_of, label_template = @label_template, message_items: {})
|
|
|
|
message_items = { label_template: label_template.id } if message_items.blank?
|
|
|
|
message_items[:type] = I18n.t("label_templates.types.#{label_template.class.name.underscore}")
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: label_template,
|
|
|
|
team: label_template.team,
|
|
|
|
message_items: message_items)
|
|
|
|
end
|
2022-07-26 19:52:40 +08:00
|
|
|
end
|