add file and image type for custom field

This commit is contained in:
chiu 2020-05-30 22:20:10 +08:00
parent fa7d56437d
commit 8b3361360b
13 changed files with 199 additions and 9 deletions

View File

@ -18,3 +18,11 @@ input[type=checkbox]:focus {
justify-content: center;
}
}
.file-selector,.ask-question .controls >.file-selector{
display: inline-flex;
flex-wrap: wrap;
justify-content: center;
}
.filename{
word-break: break-all;
}

View File

@ -98,8 +98,32 @@ class Admin::AsksController < OrbitAdminController
def update
locale = I18n.locale
ask_question_param = params.require(:ask_question).permit!
@ask_question.update_attributes(ask_question_param)
temp_params = params.require(:ask_question).permit!
all_to_save = []
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
ask_setting.custom_fields.select{|k,v| v['type']=='file' || v['type']=='image'}.each do |k,v|
file = temp_params['custom_values'][k]
if !file.blank?
if v['type']=='image'
all_to_save += [[k,AskImage.new(file: file,ask_question_id: @ask_question.id)]]
else
all_to_save += [[k,AskFile.new(file: file,ask_question_id: @ask_question.id)]]
end
else
temp_params['custom_values'][k] = @ask_question.custom_values[k]
end
end
flag = @ask_question.save
if flag
all_to_save.each do |to_save|
flag = flag && to_save[1].save
temp_params['custom_values'][to_save[0]] = [temp_params['custom_values'][to_save[0]].original_filename ,to_save[1].file.url]
end
end
@ask_question.update_attributes(temp_params)
if @ask_question.send_email?
build_email(@ask_question)
end

View File

@ -118,7 +118,6 @@ class AsksController < ActionController::Base
end
def show
ask_question = AskQuestion.where(id: @params['item']).first
puts ask_question.inspect
{'ask_question' => ask_question,'layout_type'=>'show'}
end
def index
@ -161,8 +160,30 @@ class AsksController < ActionController::Base
end
def create
@ask_question = AskQuestion.new(create_params)
if (gotcha_valid? || !AskSetting.first.default_setting['recaptcha'])&& @ask_question.save
@ask_question = AskQuestion.new()
temp_params = create_params
all_to_save = []
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
ask_setting.custom_fields.select{|k,v| v['type']=='file' || v['type']=='image'}.each do |k,v|
file = temp_params['custom_values'][k]
if !file.blank?
if v['type']=='image'
all_to_save += [[k,AskImage.new(file: file,ask_question_id: @ask_question.id)]]
else
all_to_save += [[k,AskFile.new(file: file,ask_question_id: @ask_question.id)]]
end
end
end
flag =(gotcha_valid? || !AskSetting.first.default_setting['recaptcha']) && @ask_question.save
if flag
all_to_save.each do |to_save|
flag = flag && to_save[1].save
temp_params['custom_values'][to_save[0]] = [temp_params['custom_values'][to_save[0]].original_filename ,to_save[1].file.url]
end
end
if flag
@ask_question.update_attributes(temp_params)
build_email(@ask_question)
redirect_to "#{params[:referer_url]}/?method=thank"
else

View File

@ -147,6 +147,22 @@ module Admin::AsksHelper
readonly ? value.collect{|k1,v1| Array(v['options'])[v1.to_i][1][I18n.locale]}.join(', ') : Array(v['options']).select{|index1,option| option['disabled'] != 'true'}.collect do |index1,option|
"#{check_box_tag("#{field_name}[#{index1}]",index1,value[index1]==index1)}#{option[I18n.locale]}"
end.join
when 'file'
file_value = value[0] rescue nil
file_path = value[1] rescue nil
file_required = v['required']=='true' ? 'required="required"' : ''
readonly ? "<a href=\"#{file_path}\" title=\"file\">#{file_value}</a>" : "<div class=\"file-selector\"><label class=\"ui-button\">
<input data-src=\"#{file_path}\" style=\"position: absolute;width:0.00000001em;\" class=\"upload\" name=\"#{field_name}\" #{file_required} type=\"file\" value=\"#{file_value}\"/>
<i class=\"fa fa-file\"></i> #{t('ask.upload_file')}
</label><div class=\"filename\"></div></div>"
when 'image'
file_value = value[0] rescue nil
file_path = value[1] rescue nil
file_required = v['required']=='true' ? 'required="required"' : ''
readonly ? "<img src=\"#{file_path}\" alt=\"#{file_value}\">" : "<div class=\"file-selector\"><label class=\"ui-button\">
<input data-image-src=\"#{file_path}\" style=\"position: absolute;width:0.00000001em;\" accept=\"image/*\" class=\"upload\" name=\"#{field_name}\" #{file_required} type=\"file\" value=\"#{file_value}\"/>
<i class=\"fa fa-photo\"></i> #{t('ask.upload_image')}
</label><div class=\"filename\"></div></div>"
end
rescue => e
debug [e.inspect,e.backtrace]
@ -154,7 +170,7 @@ module Admin::AsksHelper
end
def custom_field_block(k,v={})
set_input_name('ask_setting[custom_fields]')
markups = LIST[:markups].select{|k,v| k != 'member_relationship' && k != 'address'}.collect{|key,val| [t("lists.markups."+key),key]}
markups = LIST[:markups].select{|k,v| k != 'member_relationship' && k != 'address'}.collect{|key,val| [t("lists.markups."+key),key]} +[[t('ask.file_field'),'file'],[t('ask.image_field'),'image']]
multi_lang_tag = multiple_lang_tag(k,'text_field','field',v['field'],{placeholder: t('ask.field_name')})
field_select_tag = field_select_tag(k,'type',markups,v['type'],{:onchange=>'check_available_setting_block(this)'})
key = hidden_field_tag "ask_setting[custom_fields][#{k}][key]",k,{'class' => 'key'}

7
app/models/ask_file.rb Normal file
View File

@ -0,0 +1,7 @@
class AskFile
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :file, AssetUploader
field :ask_question_id
end

9
app/models/ask_image.rb Normal file
View File

@ -0,0 +1,9 @@
class AskImage
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :file, AskImageUploader
field :ask_question_id
field :album_crops,default: []
end

View File

@ -0,0 +1,71 @@
# encoding: utf-8
module CarrierWave
module Uploader
module Versions
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
end
class AskImageUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
# include CarrierWave::ImageScience
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
# storage :file
# storage :s3
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def get_org_url
if have_crop?
model.file.resized.url
else
model.file.url
end
end
def fix_exif_rotation
manipulate! do |img|
img.tap(&:auto_orient)
end
end
process :optimize
version :thumb do
process :pad_process => [200,200]
end
version :theater do
process :limit_process => [1920, 1080]
end
version :mobile do
process :limit_process => [1152, 768]
end
def optimize (*arg)
manipulate! do |img|
return img unless img.mime_type.match /image\/jpeg/
img.strip
img.combine_options do |c|
c.quality "90"
c.depth "24"
c.interlace "plane"
end
img
end
end
def limit_process(w,h)
resize_to_limit(w,h)
end
def pad_process (w,h)
resize_and_pad(w, h, :transparent, 'Center')
end
end

View File

@ -18,6 +18,26 @@
text-align: center;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('input:file').change(function(e){
var fileName = e.target.files[0].name;
$(this).parents(".file-selector").eq(0).find('.filename').html(fileName);
})
$('input:file').each(function(i,v){
var filename = $(v).attr('value')
if (filename){
if ($(v).attr('data-image-src')){
$(v).parents(".file-selector").eq(0).find('.filename').html("<img src=\""+$(v).attr('data-image-src')+"\">"+filename);
}
else{
$(v).parents(".file-selector").eq(0).find('.filename').html("<a href=\""+$(v).attr('data-src')+"\">"+filename);
}
}
})
})
</script>
<div class="input-area">
<div id="ask-asks">
<table class="table">

View File

@ -1,4 +1,4 @@
<%= form_for @ask_question, url: @url, html: { class: 'form-horizontal main-forms previewable' } do |f| %>
<%= form_for @ask_question, url: @url, html: { class: 'form-horizontal main-forms previewable',multipart: true } do |f| %>
<fieldset>
<%= render :partial => 'form', locals: {f: f} %>
</fieldset>

View File

@ -67,6 +67,12 @@
<script type="text/javascript">
var datepicker_fn = $.fn.datepicker
var datepicker = $.datepicker
$(document).ready(function(){
$('input:file').change(function(e){
var fileName = e.target.files[0].name;
$(this).parents(".file-selector").eq(0).find('.filename').html(fileName);
})
})
</script>
<%# javascript_include_tag "lib/bootstrap-datetimepicker" %>
<%# javascript_include_tag "jquery.ui.datepicker.monthyearpicker" %>
@ -74,7 +80,7 @@
<%= javascript_include_tag 'validator' %>
<link href="/assets/ask/ask.css" media="screen" rel="stylesheet">
<div id="new-ask-question" class="ask-question">
<%= form_for @ask_question, url: asks_path, html: {class: 'form-horizontal'} do |f| %>
<%= form_for @ask_question, url: asks_path, html: {class: 'form-horizontal',multipart: true} do |f| %>
<div class="control-group">
<%= f.label nil,t('title'), class: 'control-label required' %>
<div class="controls">

View File

@ -2,6 +2,10 @@ en:
situation: Situation
sex: Sex
ask:
file_field: File Field
image_field: Image Field
upload_file: Upload File
upload_image: Upload Image
no_index_page: There are no page for ask module created at site struture.
thank_text: We will reply as soon as posible,thank you.
is_waiting: Pending

View File

@ -8,6 +8,10 @@ zh_tw:
errors:
verification_failed: 驗證碼錯誤
ask:
file_field: 檔案欄位
image_field: 照片欄位
upload_file: 上傳檔案
upload_image: 上傳圖片
no_index_page: 前台頁面尚未被建立,請前往網站架構設定,謝謝。
thank_text: 我們已經收到您的預約,期待您的蒞臨,謝謝
is_waiting: 待處理

View File

@ -71,7 +71,7 @@
<span>{{widget-title}}</span>
</h3>
<div id="new-ask-question" class="ask-question">
<form action="{{form_url}}" method="post" class="form-horizontal" accept-charset="UTF-8">
<form action="{{form_url}}" enctype="multipart/form-data" method="post" class="form-horizontal" accept-charset="UTF-8">
{{token_tag}}
<div data-level="0" data-list="fields">
<div class="control-group">