diff --git a/app/assets/stylesheets/custom_field.css b/app/assets/stylesheets/custom_field.css index 1a627c1..e9ef739 100644 --- a/app/assets/stylesheets/custom_field.css +++ b/app/assets/stylesheets/custom_field.css @@ -17,4 +17,12 @@ input[type=checkbox]:focus { .ask-question .control-group{ justify-content: center; } +} +.file-selector,.ask-question .controls >.file-selector{ + display: inline-flex; + flex-wrap: wrap; + justify-content: center; +} +.filename{ + word-break: break-all; } \ No newline at end of file diff --git a/app/controllers/admin/asks_controller.rb b/app/controllers/admin/asks_controller.rb index b8f18dc..917c79e 100644 --- a/app/controllers/admin/asks_controller.rb +++ b/app/controllers/admin/asks_controller.rb @@ -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 diff --git a/app/controllers/asks_controller.rb b/app/controllers/asks_controller.rb index 1c3cf96..490325f 100644 --- a/app/controllers/asks_controller.rb +++ b/app/controllers/asks_controller.rb @@ -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 diff --git a/app/helpers/admin/asks_helper.rb b/app/helpers/admin/asks_helper.rb index 641713a..c5dd961 100644 --- a/app/helpers/admin/asks_helper.rb +++ b/app/helpers/admin/asks_helper.rb @@ -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 ? "#{file_value}" : "
" + when 'image' + file_value = value[0] rescue nil + file_path = value[1] rescue nil + file_required = v['required']=='true' ? 'required="required"' : '' + readonly ? "\"#{file_value}\"" : "
" 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'} diff --git a/app/models/ask_file.rb b/app/models/ask_file.rb new file mode 100644 index 0000000..c2af899 --- /dev/null +++ b/app/models/ask_file.rb @@ -0,0 +1,7 @@ +class AskFile + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + field :ask_question_id +end \ No newline at end of file diff --git a/app/models/ask_image.rb b/app/models/ask_image.rb new file mode 100644 index 0000000..913dfa9 --- /dev/null +++ b/app/models/ask_image.rb @@ -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 \ No newline at end of file diff --git a/app/uploaders/ask_image_uploader.rb b/app/uploaders/ask_image_uploader.rb new file mode 100644 index 0000000..b6bd482 --- /dev/null +++ b/app/uploaders/ask_image_uploader.rb @@ -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 + diff --git a/app/views/admin/asks/_form.html.erb b/app/views/admin/asks/_form.html.erb index 6abe0d4..a8d321e 100644 --- a/app/views/admin/asks/_form.html.erb +++ b/app/views/admin/asks/_form.html.erb @@ -18,6 +18,26 @@ text-align: center; } +
diff --git a/app/views/admin/asks/edit.html.erb b/app/views/admin/asks/edit.html.erb index 3675252..a0311e1 100644 --- a/app/views/admin/asks/edit.html.erb +++ b/app/views/admin/asks/edit.html.erb @@ -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| %>
<%= render :partial => 'form', locals: {f: f} %>
diff --git a/app/views/asks/index.html.erb b/app/views/asks/index.html.erb index 285d121..4df2aae 100644 --- a/app/views/asks/index.html.erb +++ b/app/views/asks/index.html.erb @@ -67,6 +67,12 @@ <%# javascript_include_tag "lib/bootstrap-datetimepicker" %> <%# javascript_include_tag "jquery.ui.datepicker.monthyearpicker" %> @@ -74,7 +80,7 @@ <%= javascript_include_tag 'validator' %>
- <%= 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| %>
<%= f.label nil,t('title'), class: 'control-label required' %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 40d1c17..199ff62 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index acdc07a..656a5e3 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -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: 待處理 diff --git a/modules/ask/_ask_widget_form.html.erb b/modules/ask/_ask_widget_form.html.erb index 04e2573..1d1a38d 100644 --- a/modules/ask/_ask_widget_form.html.erb +++ b/modules/ask/_ask_widget_form.html.erb @@ -71,7 +71,7 @@ {{widget-title}}
-
+ {{token_tag}}