ask/app/controllers/asks_controller.rb

412 lines
17 KiB
Ruby
Raw Normal View History

2022-03-23 13:39:28 +00:00
class AsksController < ApplicationController
helper Admin::AsksHelper
2020-04-30 05:18:02 +00:00
include Admin::AsksHelper
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormOptionsHelper
helper ActionView::Helpers::UrlHelper
include ActionController::RequestForgeryProtection
2022-03-24 05:27:55 +00:00
include RuCaptcha::ViewHelpers
2020-04-30 05:18:02 +00:00
def widget
subpart = OrbitHelper.get_current_widget
case subpart.widget_type
when /.*_form/
2021-08-24 06:33:33 +00:00
read_more_page = Page.where(id: subpart.read_more_page_id).first || Page.where(:module => 'ask').first
referer_url = read_more_page ? "/#{I18n.locale}#{read_more_page.url}" : "/#{I18n.locale}/asks"
2020-04-30 05:18:02 +00:00
module_app = ModuleApp.where(:key => "ask").first
2020-07-04 13:36:32 +00:00
categories = Array(Category.find(OrbitHelper.widget_categories)) rescue ['all']
2020-04-30 05:18:02 +00:00
categories = module_app.categories if categories.include? 'all'
tags = module_app.tags
ask_question = AskQuestion.new
locale = I18n.locale
2020-07-04 13:36:32 +00:00
ask_settings = AskCategorySetting.where(:category_id.in=> categories.collect{|v| v.id.to_s}).to_a
2022-03-24 03:56:25 +00:00
if ask_settings.count != categories.count
2020-07-04 13:36:32 +00:00
ask_settings = [AskSetting.first]+ask_settings
2020-04-30 05:18:02 +00:00
end
2020-07-04 13:36:32 +00:00
ask_setting = AskCategorySetting.where(category_id: categories.first.id.to_s).first
if ask_setting.nil?
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
2022-03-09 17:00:05 +00:00
@default_ask_setting = ask_setting
else
@default_ask_setting = AskSetting.first
2020-04-30 05:18:02 +00:00
end
2020-07-04 13:36:32 +00:00
defalt_fields = cal_form_from_setting(ask_setting,categories)
2020-04-30 05:18:02 +00:00
request = OrbitHelper.request
csrf_value = OrbitHelper.request.session[:_csrf_token] || SecureRandom.base64(32)
2020-07-04 13:36:32 +00:00
token_tag = hidden_field_tag('authenticity_token',csrf_value)
2021-08-24 06:33:33 +00:00
switch_form = cal_switch_form(ask_settings,categories,token_tag,referer_url)
2020-04-30 05:18:02 +00:00
{
'fields' => defalt_fields,
'extras'=>{
2020-07-04 13:36:32 +00:00
'switch_form' => switch_form
2020-04-30 05:18:02 +00:00
}
}
else
data_count = OrbitHelper.widget_data_count.to_i
if data_count != 0
ask_questions = AskQuestion.where(situation: 'is_published').page(1).per(data_count)
else
ask_questions = AskQuestion.where(situation: 'is_published')
end
2020-07-04 13:36:32 +00:00
ask_index_page = OrbitHelper.widget_more_url rescue nil
2020-04-30 05:18:02 +00:00
ask_questions = ask_questions.collect do |v|
url = ((ask_index_page+"?item=#{v.id}") rescue "javascript:alert('#{t('ask.no_index_page')}')")
{'td'=> [
{'content' =>v.category.title},
{'content' => link_to(v.title,"#{url}",title: v.title)}
]
}
end
{
'ask_questions' => ask_questions,
2020-07-04 13:36:32 +00:00
'th' => [{'td'=>t('categories')},{'td'=>t('title')}],
'extras' => {'more_title'=>I18n.t('ask.more_title'),
'more_href'=>ask_index_page}
2020-04-30 05:18:02 +00:00
}
end
end
def get_layout_type
@params = OrbitHelper.params
page = Page.where(url:@params['url']).first
@layout_type = 'index'
if page.methods.include? 'select_option_items'.to_sym
ModuleApp.all.select{|tmp| tmp.key.to_s=='ask'}.each do |modile_app|
@show_option_items = modile_app.show_option_items rescue nil
end
page.select_option_items.each do |select_option_item|
if !(@show_option_items.nil?) && select_option_item.field_name == @show_option_items.keys.first.to_s
value = YAML.load(select_option_item.value)
2022-03-23 13:39:28 +00:00
I18n.with_locale(:en) do
2020-04-30 05:18:02 +00:00
if value[locale] == t('ask.widget.index')
@layout_type = 'index'
elsif value[locale] == t('ask.is_published')
@layout_type = 'published_index'
end
end
end
end
end
end
2014-10-02 06:00:35 +00:00
def initialize
super
@app_title = 'ask'
2022-03-24 05:27:55 +00:00
self.request = OrbitHelper.request
2014-10-02 06:00:35 +00:00
end
2020-04-30 05:18:02 +00:00
def show
ask_question = AskQuestion.where(id: @params['item']).first
2020-07-04 13:36:32 +00:00
ask_setting = AskCategorySetting.where(category_id: ask_question.category_id.to_s).first
ask_setting = AskSetting.first if ask_setting.nil?
{'ask_question' => ask_question,'layout_type'=>'show','ask_setting'=>ask_setting}
2020-04-30 05:18:02 +00:00
end
2014-10-02 06:00:35 +00:00
def index
2020-04-30 05:18:02 +00:00
@params = OrbitHelper.params
2021-08-24 06:33:33 +00:00
case ((@params['method'] rescue params['method']) rescue nil)
when 'thank'
render :thank and return {}
when 'sorry'
render :sorry and return {}
end
referer_url = OrbitHelper.request.fullpath
2020-04-30 05:18:02 +00:00
if @params['item'].to_s.empty?
get_layout_type
else
@layout_type = 'show'
end
if @layout_type == 'index'
module_app = ModuleApp.where(:key => "ask").first
categories = Array(Category.find(OrbitHelper.page_categories)) rescue ['all']
2020-07-04 13:36:32 +00:00
categories = module_app.categories if categories.include? 'all'
ask_settings = AskCategorySetting.where(:category_id.in=> categories.collect{|v| v.id.to_s}).to_a
2022-03-24 03:56:25 +00:00
if ask_settings.count != categories.count
2020-07-04 13:36:32 +00:00
ask_settings = [AskSetting.first]+ask_settings
end
ask_setting = AskCategorySetting.where(category_id: categories.first.id.to_s).first
if ask_setting.nil?
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
2022-03-09 17:00:05 +00:00
@default_ask_setting = ask_setting
else
@default_ask_setting = AskSetting.first
2020-07-04 13:36:32 +00:00
end
csrf_value = OrbitHelper.request.session[:_csrf_token] || SecureRandom.base64(32)
token_tag = hidden_field_tag('authenticity_token',csrf_value)
2021-08-24 06:33:33 +00:00
switch_form = cal_switch_form(ask_settings,categories,token_tag,referer_url)
2020-04-30 05:18:02 +00:00
tags = module_app.tags
2022-03-09 17:00:05 +00:00
ask_question = AskQuestion.new(id: nil)
2020-04-30 05:18:02 +00:00
{
'layout_type' => 'index',
'ask_question' => ask_question,
'categories' => categories,
'tags' => tags,
2020-07-04 13:36:32 +00:00
'module_app' => module_app,
'switch_form' => switch_form,
2022-03-09 17:00:05 +00:00
'ask_setting' => ask_setting,
'default_ask_setting' => @default_ask_setting
2020-04-30 05:18:02 +00:00
}
elsif @layout_type == 'published_index'
page_number = OrbitHelper.page_number.to_i
page_number = 1 if page_number == 0
page_data_count = OrbitHelper.page_data_count.to_i
if page_data_count != 0
ask_questions = AskQuestion.where(situation: 'is_published').page(page_number).per(page_data_count)
else
ask_questions = AskQuestion.where(situation: 'is_published')
end
{
'layout_type' => 'published_index',
'ask_questions' => ask_questions,
'url' => @params['url']
}
else
show
end
2014-10-02 06:00:35 +00:00
end
def create
2021-11-18 06:32:20 +00:00
if !params[:referer_url].blank? && !params[:referer_url].to_s.start_with?("/")
render :file => "#{Rails.root}/app/views/errors/403.html", :layout => false, :status => 403 and return
end
@ask_question = AskQuestion.new()
temp_params = create_params
all_to_save = []
2020-07-04 13:36:32 +00:00
ask_setting = AskCategorySetting.where(category_id: params['ask_question']['category_id']).first
if ask_setting.nil?
ask_setting = AskSetting.first
ask_setting = AskSetting.create() if ask_setting.nil?
end
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
2022-03-24 03:56:25 +00:00
flag = !ask_setting.default_setting['recaptcha'] || gotcha_valid?
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)
2014-10-02 06:00:35 +00:00
build_email(@ask_question)
2021-08-24 06:33:33 +00:00
redirect_to "#{params[:referer_url]}?method=thank"
2014-10-02 06:00:35 +00:00
else
2021-08-24 06:33:33 +00:00
redirect_to "#{params[:referer_url]}?method=sorry"
2014-10-02 06:00:35 +00:00
end
end
def thank
acknowledgement = AskAcknowledgement.last
{
"acknowledgement" => acknowledgement
}
end
def sorry
{}
end
def build_email(email_er)
2020-04-30 05:18:02 +00:00
email = Email.new
email.save
email_er.email_id = email.id
email_er.save
2014-10-02 06:00:35 +00:00
2020-04-30 05:18:02 +00:00
@group_mail = email_er.email_address
@mail_sentdate = DateTime.now
2014-10-02 06:00:35 +00:00
2022-03-23 13:39:28 +00:00
site = current_site rescue Site.first
mail_from = site.title_translations[site.default_locale]
2020-05-13 09:34:42 +00:00
2020-04-30 05:18:02 +00:00
email_er.email.update_attributes(
2022-03-23 13:39:28 +00:00
:mail_lang=> site.default_locale,
2020-04-30 05:18:02 +00:00
:create_user=>(current_user rescue nil),
:mail_sentdate=>@mail_sentdate,
:module_app=>@module_app,
:mail_to=>@group_mail,
2020-05-13 09:34:42 +00:00
:mail_subject=>mail_from+" #{t('ask.new_question')}",
2020-04-30 05:18:02 +00:00
:template=>'asks/email',
:template_data=>{
# "title" => email_er.title,
"name" => email_er.name,
"identity" => email_er[:identity],
"mail" => email_er.mail,
"phone" => email_er.phone,
# "fax" => email_er.fax,
# "content" => email_er.content
"appointment" => email_er.appointment
}
)
2014-10-02 06:00:35 +00:00
2021-02-23 08:12:39 +00:00
email_er.email.deliver
2014-10-02 06:00:35 +00:00
end
def create_params
params.require(:ask_question).permit!
end
2020-07-04 13:36:32 +00:00
private
def cal_form_from_setting(ask_setting,categories,show_categories=false,filter_fields=nil)
disp_fields_infos = AskSetting.get_disp_fields_infos
if filter_fields
disp_fields_infos = disp_fields_infos.select{|field, info| filter_fields.include?(field)}
end
2020-07-04 13:36:32 +00:00
tmp = 'mongoid.attributes.ask_question'
defalt_fields = [{'style_html'=>'','label'=>ask_label(t('title'),'true'),'content'=>text_field_tag('ask_question[title]',nil,placeholder: t('title'),required: true)}]
tmp_type = (ask_setting.class == AskSetting) ? 0 : 1
tmp_ask_cat_id = (tmp_type == 1) ? ask_setting.id.to_s : ""
disp_fields_infos.each do |field, info|
if field.include?("default@")
if tmp_type == 0
k = field[8..-1]
v = ask_setting.custom_fields[k]
if v
defalt_fields << {'label'=>ask_label(v['field'][I18n.locale],v['required']),'content'=>show_on_front(k,v)}
end
end
elsif field.include?("custom@") && tmp_ask_cat_id != ""
if tmp_type == 1 && field.include?(tmp_ask_cat_id)
k = field[8+tmp_ask_cat_id.length..-1]
v = ask_setting.custom_fields[k]
if v
defalt_fields << {'label'=>ask_label(v['field'][I18n.locale],v['required']),'content'=>show_on_front(k,v)}
end
end
2020-07-04 13:36:32 +00:00
else
if field == 'ask_category_id'
if show_categories || ask_setting.default_setting[field]
if categories.count > 1
defalt_fields = defalt_fields + [{'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>select_tag('ask_question[category_id]', options_for_select(categories.collect{|t| [ t.title, t.id ]}))}]
else
defalt_fields = defalt_fields + [{'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("ask_category_id"),'true'),'content'=>"<span>#{(categories[0].title rescue '')}</span>"+hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))}]
end
else
defalt_fields = defalt_fields + [{'style_html'=>'display: none;','label'=>'','content'=>hidden_field_tag('ask_question[category_id]', (categories[0].id.to_s rescue ''))}]
end
else
if ask_setting.default_setting[field] || field == "usage_rule"
required = ['phone','appointment'].exclude?(field) ? 'true' : 'false'
f1 = {'style_html'=>'','label'=>ask_label(@default_ask_setting.field_name("#{field}"),required)}
field_name = "ask_question[#{field}]"
case field
when 'name'
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;", "fv-messages" => "必填欄位;"},required: true,placeholder:@default_ask_setting.field_name("#{field}"))
when 'sex'
f1['content'] = "<label class=\"radio-inline\">
#{radio_button_tag(field_name,'male')}#{I18n.t('users.male')}
</label>
<label class=\"radio-inline\">
#{radio_button_tag(field_name,'female')}#{I18n.t('users.female')}
</label>"
when 'mail'
f1['content'] = text_field_tag(field_name,nil,data: {"fv-validation" => "required;check_email;", "fv-messages" => "必填欄位;Email不正確;"},required: true,placeholder:@default_ask_setting.field_name("#{field}"))
when 'phone'
f1['content'] = text_field_tag(field_name,nil,placeholder:@default_ask_setting.field_name("#{field}"))
when 'appointment'
f1['content'] = "<div class=\"default_picker\">#{text_field_tag(field_name,nil,placeholder:@default_ask_setting.field_name("#{field}"))}</div>"
when 'recaptcha'
2022-03-24 05:27:55 +00:00
f1['content'] = "#{gotcha_error}#{gotcha}"
when 'usage_rule'
next if ask_setting.usage_rule.blank?
f1['content'] = ask_setting.usage_rule
when 'agree_show'
f1['content'] = check_box_tag('ask_question[agree_show]')
when 'agree_usage'
f1['content'] = check_box_tag('ask_question[agree_usage]')
end
defalt_fields << f1
end
end
2020-07-04 13:36:32 +00:00
end
end
defalt_fields
end
def cal_html(fields,token_tag,form_id,referer_url,is_hidden=true)
2020-07-04 13:36:32 +00:00
form_url = "/#{I18n.locale.to_s}/asks"
submit_tag = submit_tag(t('submit'), :class=> 'btn btn-primary', :id => 'button-mail')
close_tag = button_tag(t('cancel'), type: 'reset', :class=> 'btn')
tmp = fields.collect do |field|
style_html = field['style_html']
label = field['label']
content = field['content']
"<div class=\"control-group\" style=\"#{style_html}\">
#{label}
<div class=\"controls\">
#{content}
</div>
</div>"
end.join
"<form id=\"#{form_id}\" action=\"#{form_url}\" enctype=\"multipart/form-data\" method=\"post\" class=\"form-horizontal\" accept-charset=\"UTF-8\" style=\"#{is_hidden ? 'display: none;' : ''}\">
2020-07-04 13:36:32 +00:00
#{token_tag}
<div data-level=\"0\" data-list=\"fields\">
#{tmp}
</div>
<div class=\"form-actions\">
<input type=\"hidden\" name=\"referer_url\" value=\"#{referer_url}\">
#{submit_tag}
#{close_tag}
</div>
</form>"
end
def script_text
"<script type=\"text/javascript\">
$(document).ready(function(){
$('select[name=\"ask_question[category_id]\"]').change(function(){
var category_id = $(this).val()
$(this).parents('.ask-question').eq(0).find('select[name=\"ask_question[category_id]\"]').val(category_id)
var id
if ($('form#'+category_id).length != 0){
id = category_id
}
else{
id = 'ask-default-form'
}
if (id != $(this).parents('form').eq(0).attr('id')){
var self = $(this)
var old_controls = self.parents('form').eq(0).find('.control-group')
2022-03-09 17:00:05 +00:00
var old_inputs = old_controls.children('label').map(function(){
2020-07-04 13:36:32 +00:00
return $.trim($(this).text().replace(/\\\*/g,''));
}).get()
var new_controls = self.parents('.ask-question').eq(0).find('form#'+id).find('.control-group')
2022-03-09 17:00:05 +00:00
var new_inputs = new_controls.children('label').map(function(){
2020-07-04 13:36:32 +00:00
return $.trim($(this).text().replace(/\\\*/g,''));
}).get()
$(new_inputs).each(function(i,v){
var i0 = old_inputs.indexOf(v)
if (i0 != -1 && new_controls.eq(i).find('select#ask_question_category_id').length==0){
2022-03-09 17:00:05 +00:00
var new_ = new_controls.eq(i).find('.controls').eq(0),
old_ = old_controls.eq(i0).find('.controls').eq(0);
old_.clone().replaceAll(new_);
2020-07-04 13:36:32 +00:00
}
})
self.parents('.ask-question').eq(0).find('form').hide()
self.parents('.ask-question').eq(0).find('form#'+id).show()
}
})
})
</script>"
end
2021-08-24 06:33:33 +00:00
def cal_switch_form(ask_settings,categories,token_tag,referer_url)
2020-07-04 13:36:32 +00:00
switch_form = ''
default_idx = 0
if categories.count != 0
2022-03-23 13:39:28 +00:00
default_idx = ask_settings[1..-1].to_a.map{|a| a.category_id.to_s}.index(categories[0].id.to_s)
default_idx = default_idx.nil? ? 0 : default_idx + 1
2020-07-04 13:36:32 +00:00
end
switch_form = script_text + ask_settings.collect.with_index do |ask_setting,i|
fields = cal_form_from_setting(ask_setting,categories,true)
is_hidden = (i != default_idx)
if ask_setting.class == AskSetting
form_id = 'ask-default-form'
else
form_id = ask_setting.category_id
end
cal_html(fields,token_tag,form_id,referer_url,is_hidden)
end.join
2020-07-04 13:36:32 +00:00
switch_form
end
2014-10-02 06:00:35 +00:00
end