diff --git a/app/controllers/panel/ask/back_end/asK_requests_controller.rb b/app/controllers/panel/ask/back_end/asK_requests_controller.rb new file mode 100644 index 0000000..5f7d3a4 --- /dev/null +++ b/app/controllers/panel/ask/back_end/asK_requests_controller.rb @@ -0,0 +1,45 @@ +# encoding: utf-8 + +class Panel::Ask::BackEnd::AskRequestsController < OrbitBackendController + include AdminHelper + #include OrbitControllerLib::DivisionForDisable + + #before_filter :for_app_manager + skip_before_filter :verify_authenticity_token + open_for_visitor :only => [:transferred_request] + + def transferred_request + app_title = request.fullpath.split('/')[2] + module_app ||= ModuleApp.first(conditions: {:key => app_title} ) + + if request.headers["HTTP_X_AUTH_TOKEN"] == '3kjlfksjDFJ' + if !module_app.categories.where("title.en" => "Transferred Question").any? + ask_category = module_app.categories.build + ask_category.title_translations = { "zh_tw" => "轉介的問題", "en" => "Transferred Question" } + ask_category.save! + end + + module_app.categories.enabled + category = module_app.categories.where("title.en" => "Transferred Question").first + + @transferred_question = AskQuestion.new( + name: params['name'], + identity: params['identity'], + email: params['email'], + phone: params['phone'], + tax: params['tax'], + title: params['title'], + content: params['content'], + category_id: category.id) + + @transferred_question.ask_reply = AskReply.new + @transferred_question.ask_reply.update_attributes(status: "待處理 (轉)") + @transferred_question.save + + render :nothing => true + else + render :status => 500 + end + end +end + diff --git a/app/controllers/panel/ask/back_end/ask_acknowledgements_controller.rb b/app/controllers/panel/ask/back_end/ask_acknowledgements_controller.rb index 58f2ef4..8debc5e 100644 --- a/app/controllers/panel/ask/back_end/ask_acknowledgements_controller.rb +++ b/app/controllers/panel/ask/back_end/ask_acknowledgements_controller.rb @@ -1,13 +1,14 @@ class Panel::Ask::BackEnd::AskAcknowledgementsController < OrbitBackendController - include AdminHelper - include OrbitControllerLib::DivisionForDisable + #include AdminHelper + #include OrbitControllerLib::DivisionForDisable - before_filter :for_app_manager + #before_filter :for_app_manager + open_for_manager - def initialize - super - @app_title = 'ask_acknowledgement' - end + #def initialize + # super + # @app_title = 'ask_acknowledgement' + #end def index @ask_acknowledgement = AskAcknowledgement.first || AskAcknowledgement.create diff --git a/app/controllers/panel/ask/back_end/ask_admins_controller.rb b/app/controllers/panel/ask/back_end/ask_admins_controller.rb index b6994da..8a657d4 100644 --- a/app/controllers/panel/ask/back_end/ask_admins_controller.rb +++ b/app/controllers/panel/ask/back_end/ask_admins_controller.rb @@ -1,14 +1,15 @@ class Panel::Ask::BackEnd::AskAdminsController < OrbitBackendController - include AdminHelper - include OrbitControllerLib::DivisionForDisable + #include AdminHelper + #include OrbitControllerLib::DivisionForDisable - before_filter :for_app_manager + #before_filter :for_app_manager + open_for_manager - def initialize - super - @app_title = 'ask_admins' - end + #def initialize + # super + # @app_title = 'ask_admins' + #end def index @ask_admins = AskAdmin.all diff --git a/app/controllers/panel/ask/back_end/ask_questions_controller.rb b/app/controllers/panel/ask/back_end/ask_questions_controller.rb index cf8180c..f15731e 100644 --- a/app/controllers/panel/ask/back_end/ask_questions_controller.rb +++ b/app/controllers/panel/ask/back_end/ask_questions_controller.rb @@ -1,11 +1,11 @@ -require 'csv' - class Panel::Ask::BackEnd::AskQuestionsController < OrbitBackendController + + require 'csv' - def initialize - super - @app_title = 'ask_questions' - end + # def initialize + # super + # @app_title = 'ask_questions' + # end def index @ask_questions = AskQuestion.all @@ -53,10 +53,12 @@ class Panel::Ask::BackEnd::AskQuestionsController < OrbitBackendController Rails.application.config.mongoid.use_activesupport_time_zone = true date_start = "#{params[:export]['start(1i)']}-#{params[:export]['start(2i)']}-#{params[:export]['start(3i)']}" date_end = "#{params[:export]['end(1i)']}-#{params[:export]['end(2i)']}-#{params[:export]['end(3i)']}" - @ask_questions = AskQuestion.includes(:ask_category).includes(:ask_reply).where(:created_at.gte => date_start, :created_at.lte => date_end) + #@ask_questions = AskQuestion.includes(:ask_category).includes(:ask_reply).where(:created_at.gte => date_start, :created_at.lte => date_end) + @ask_questions = AskQuestion.where(:created_at.gte => date_start, :created_at.lte => date_end) csv = CSV.generate do |csv| - csv << [ t('category'), + csv << [ t('status'), + t('category'), AskQuestion.human_attribute_name(:name), AskQuestion.human_attribute_name(:identity), AskQuestion.human_attribute_name(:email), @@ -68,7 +70,8 @@ class Panel::Ask::BackEnd::AskQuestionsController < OrbitBackendController AskReply.human_attribute_name(:comment)] @ask_questions.each do |ask_question| ask_question.ask_reply ||= AskReply.new - csv << [ ask_question.ask_category.name, + csv << [ ask_question.ask_reply.status, + ask_question.category.title, ask_question.name, ask_question.identity, ask_question.email, diff --git a/app/controllers/panel/ask/back_end/ask_replies_controller.rb b/app/controllers/panel/ask/back_end/ask_replies_controller.rb index 86219b7..d392fd9 100644 --- a/app/controllers/panel/ask/back_end/ask_replies_controller.rb +++ b/app/controllers/panel/ask/back_end/ask_replies_controller.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController include AdminHelper @@ -13,19 +14,122 @@ class Panel::Ask::BackEnd::AskRepliesController < OrbitBackendController def create @ask_question = AskQuestion.find(params[:ask_question_id]) @ask_question.ask_reply = AskReply.new(params[:ask_reply]) + @ask_reply = @ask_question.ask_reply @ask_question.save + if @ask_question.ask_reply.send_email? Resque.enqueue(SendAskReplyMail, @ask_reply.ask_question.id) end + + status_stack = [] + new_status = "" + params[:ask_reply][:depts].each do |dns| + # host = dns.split(":").first # local test + + begin + status_stack << send_request(dns, @ask_reply.ask_question, @ask_reply) # local test, switch host to dns if on server + rescue + status_stack << "fail" + end + end if params[:ask_reply][:status] == "轉介其他單位" + + success_count = status_stack.count("success") + failed_count = status_stack.count("fail") + + if failed_count == 0 && success_count > 0 + new_status = "轉介其他單位" + elsif failed_count > 0 && success_count > 0 + new_status = "部份轉介失敗" + elsif failed_count > 0 && success_count == 0 + new_status = "轉介失敗" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "待處理" + new_status = "待處理" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "已處理" + new_status = "已處理" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "轉介其他單位" + new_status = "轉介失敗" + end + + @ask_reply.update_attributes(status: new_status) + redirect_to panel_ask_back_end_ask_questions_path, notice: t('ask.reply_success') end def update + @ask_reply = AskReply.find(params[:id]) @ask_reply.update_attributes(params[:ask_reply]) + if @ask_reply.send_email? Resque.enqueue(SendAskReplyMail, @ask_reply.ask_question.id) end + + status_stack = [] + new_status = "" + params[:ask_reply][:depts].each do |dns| + # host = dns.split(":").first # local test + + begin + status_stack << send_request(dns, @ask_reply.ask_question, @ask_reply) # local test, switch host to dns if on server + rescue + status_stack << "fail" + end + end if params[:ask_reply][:status] == "轉介其他單位" + + success_count = status_stack.count("success") + failed_count = status_stack.count("fail") + + if failed_count == 0 && success_count > 0 + new_status = "轉介其他單位" + elsif failed_count > 0 && success_count > 0 + new_status = "部份轉介失敗" + elsif failed_count > 0 && success_count == 0 + new_status = "轉介失敗" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "待處理" + new_status = "待處理" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "已處理" + new_status = "已處理" + elsif failed_count == 0 && success_count == 0 && @ask_reply.status == "轉介其他單位" + new_status = "轉介失敗" + end + + @ask_reply.update_attributes(status: new_status) + redirect_to panel_ask_back_end_ask_questions_path, notice: t('ask.reply_success') end + + private + + def send_request(dep_dns, current_question, current_reply) + require 'net/http' + require 'uri' + require 'json' + + uri_path = "/panel/ask/back_end/ask_requests/transferred_request" + http = Net::HTTP.new(dep_dns, 80) # port 4000 for local test, switch to port 80 for deployment + req = Net::HTTP::Post.new(uri_path) + + body = { + 'name' => current_question.name, + 'title' => current_question.title, + 'identity' => current_question.identity, + 'email' => current_question.email, + 'phone' => current_question.phone, + 'tax' => current_question.tax, + 'content' => current_question.content + } + + status = "" + req.set_form_data(body) + req.add_field 'X-Auth-Token', '3kjlfksjDFJ' + response = http.request(req) + + if response.code == "200" + status = "success" + else + status = "fail" + end + + return status + end end diff --git a/app/models/ask_reply.rb b/app/models/ask_reply.rb index 54112aa..a5fe235 100644 --- a/app/models/ask_reply.rb +++ b/app/models/ask_reply.rb @@ -6,6 +6,7 @@ class AskReply field :comment, type: String field :status, type: String field :send_email, type: Boolean, detault: false + field :depts, :type => Array belongs_to :ask_question end diff --git a/app/views/panel/ask/back_end/ask_questions/edit.html.erb b/app/views/panel/ask/back_end/ask_questions/edit.html.erb index 7225f69..2a14fd3 100644 --- a/app/views/panel/ask/back_end/ask_questions/edit.html.erb +++ b/app/views/panel/ask/back_end/ask_questions/edit.html.erb @@ -1,3 +1,45 @@ + +
<%= form_for @ask_reply, url: @url, method: @method do |f| %> @@ -32,6 +74,26 @@ ['轉介其他單位', '轉介其他單位'] ] %> + + +
+
+ + <%= check_box("ask_reply", "depts", {:multiple => true}, "www.ga.ntu.edu.tw", nil) %><%= label_tag("www.ga.ntu.edu.tw", "總務處") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "sec.ga.ntu.edu.tw", nil) %><%= label_tag("sec.ga.ntu.edu.tw", "總務長室暨總務處秘書室") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "doc.ga.ntu.edu.tw", nil) %><%= label_tag("doc.ga.ntu.edu.tw", "文書組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "general.ga.ntu.edu.tw", nil) %><%= label_tag("general.ga.ntu.edu.tw", "事務組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "property.ga.ntu.edu.tw", nil) %><%= label_tag("property.ga.ntu.edu.tw", "保管組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "construction.ga.ntu.edu.tw", nil) %><%= label_tag("construction.ga.ntu.edu.tw", "營繕組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "cashier.ga.ntu.edu.tw", nil) %><%= label_tag("cashier.ga.ntu.edu.tw", "出納組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "procurement.ga.ntu.edu.tw", nil) %><%= label_tag("procurement.ga.ntu.edu.tw", "採購組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "fss.ga.ntu.edu.tw", nil) %><%= label_tag("fss.ga.ntu.edu.tw", "經營管理組") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "police.ga.ntu.edu.tw", nil) %><%= label_tag("police.ga.ntu.edu.tw", "駐警隊") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "social.ga.ntu.edu.tw", nil) %><%= label_tag("social.ga.ntu.edu.tw", "社科院總務分處") %> + <%= check_box("ask_reply", "depts", {:multiple => true}, "medicine.ga.ntu.edu.tw", nil) %><%= label_tag("medicine.ga.ntu.edu.tw", "醫學院總務分處") %> +
+
<%= f.label :send_email %><%= f.radio_button :send_email, 1, checked: @ask_reply.send_email? %><%= t('ask.yes') %> diff --git a/app/views/panel/ask/front_end/ask_questions/index.html.erb b/app/views/panel/ask/front_end/ask_questions/index.html.erb index 30ea643..fd928b2 100644 --- a/app/views/panel/ask/front_end/ask_questions/index.html.erb +++ b/app/views/panel/ask/front_end/ask_questions/index.html.erb @@ -84,8 +84,11 @@ $(document).ready(function(){ <%= f.text_area :content, rows: 8, class: 'input-xlarge' %> +
+ <%= f.label t('ask.validate_code'), class: 'control-label required' %>
+ <%= gotcha_error %> <%= gotcha%>
diff --git a/config/locales/en.yml b/config/locales/en.yml index a4aeaca..7efeb89 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,6 +17,8 @@ en: admin: Administrator new_question: New question pending: Pending + validate_code: Verification code + validate_code_note: Please answer the following verification questions. mongoid: attributes: ask_question: diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 0b8109b..d4fd716 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -22,6 +22,8 @@ zh_tw: admin: 管理者 new_question: 新的發問 pending: 待處理 + validate_code: 驗證碼 + validate_code_note: 請回答下列驗證問句。 mongoid: attributes: ask_question: diff --git a/config/routes.rb b/config/routes.rb index df2ffe9..bc33409 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,8 @@ Rails.application.routes.draw do resources :ask_categories resources :ask_acknowledgements resources :ask_admins + + match "ask_requests/transferred_request" => "ask_requests#transferred_request", :as => 'transferred_request', :via => :post end namespace :front_end do