tools_controller.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. require "rest_client"
  2. require "uri"
  3. require "json"
  4. class ToolsController < ApplicationController
  5. before_filter :set_user
  6. before_filter :set_action_page
  7. before_filter :create_newsletter_subscription, only: [:email, :call]
  8. before_filter :create_partner_subscription, only: [:email, :call, :petition, :message_congress]
  9. after_filter :deliver_thanks_message, only: [:email, :call, :petition, :message_congress]
  10. skip_after_filter :deliver_thanks_message, if: :signature_has_errors
  11. # See https://github.com/EFForg/action-center-platform/wiki/Deployment-Notes#csrf-protection
  12. skip_before_filter :verify_authenticity_token
  13. before_filter :verify_request_origin, except: :email
  14. def call
  15. ahoy.track "Action",
  16. { type: "action", actionType: "call", actionPageId: params[:action_id] },
  17. action_page: @action_page
  18. @name = current_user.try :name
  19. if params[:update_user_data] == "yes"
  20. update_user_data(call_params.with_indifferent_access)
  21. end
  22. CallTool.campaign_call(params[:call_campaign_id],
  23. phone: params[:phone],
  24. location: params[:location],
  25. user_id: @user.try(:id),
  26. action_id: @action_page.to_param,
  27. callback_url: root_url)
  28. render json: {}, status: 200
  29. end
  30. # GET /tools/social_buttons_count
  31. def social_buttons_count
  32. render "application/error.html.erb", status: 500
  33. end
  34. # POST /tools/petition
  35. #
  36. # A form is posted here via ajax when a user signs a petition
  37. def petition
  38. @user ||= User.find_or_initialize_by(email: params[:signature][:email])
  39. @email = params[:signature][:email]
  40. @name = params[:signature][:first_name]
  41. @action_page = Petition.find(params[:signature][:petition_id]).action_page
  42. @signature = Signature.new(signature_params.merge(user_id: @user.id))
  43. @signature.country_code = "US" if @signature.zipcode.present?
  44. if @signature.country_code == "US" && !Rails.application.secrets.smarty_streets_id.nil?
  45. if city_state = SmartyStreets.get_city_state(@signature.zipcode)
  46. @signature.city = city_state["city"]
  47. @signature.state = city_state["state"]
  48. end
  49. end
  50. if @signature.save
  51. # You will only get here if you are not logged in. Subscribe does not show for logged in users,
  52. # since they are presented that option at signup.
  53. if params[:subscribe] == "1"
  54. @user.attributes = signature_params.slice(
  55. :email, :first_name, :last_name, :city, :state, :street_address,
  56. :zipcode, :country_code, :phone
  57. )
  58. @source = "action center petition :: " + @action_page.title
  59. @user.subscribe!(opt_in = true, source = @source)
  60. end
  61. if params[:update_user_data]
  62. update_user_data(signature_params.with_indifferent_access)
  63. end
  64. ahoy.track "Action",
  65. { type: "action", actionType: "signature", actionPageId: @action_page.id },
  66. action_page: @action_page
  67. respond_to do |format|
  68. format.json { render json: { success: true }, status: 200 }
  69. format.html do
  70. begin
  71. url = URI.parse(request.referrer)
  72. url.query = [url.query.presence, "thankyou=1"].join("&")
  73. redirect_to url.to_s
  74. rescue
  75. redirect_to welcome_index_path
  76. end
  77. end
  78. end
  79. else
  80. render json: { errors: @signature.errors.to_json }, status: 200
  81. end
  82. end
  83. def tweet
  84. ahoy.track "Action",
  85. { type: "action", actionType: "tweet", actionPageId: params[:action_id] },
  86. action_page: @action_page
  87. render json: { success: true }, status: 200
  88. end
  89. def message_congress
  90. @user ||= User.find_or_initialize_by(email: params[:email])
  91. update_user_data(email_params.with_indifferent_access) if params[:update_user_data] == "true"
  92. ahoy.track "Action",
  93. { type: "action", actionType: "congress_message", actionPageId: params[:action_id] },
  94. action_page: @action_page
  95. # You will only get here if you are not logged in. Subscribe does not show for logged in users,
  96. # since they are presented that option at signup.
  97. if params[:subscribe] == "true"
  98. @user.attributes = email_params.slice(
  99. :first_name, :last_name, :city, :state, :street_address, :zipcode
  100. )
  101. @source = "action center congress message :: " + @action_page.title
  102. @user.subscribe!(opt_in = true, source = @source)
  103. end
  104. @name = email_params[:first_name] # for deliver_thanks_message
  105. render json: { success: true }, status: 200
  106. end
  107. def email
  108. unless (@user and @user.events.emails.find_by_action_page_id(params[:action_id])) or params.include? :dnt
  109. ahoy.track "Action",
  110. { type: "action", actionType: "email", actionPageId: params[:action_id] },
  111. action_page: @action_page
  112. end
  113. if params[:service] == "copy"
  114. @actionPage = @action_page
  115. render "email_target"
  116. else
  117. redirect_to @action_page.email_campaign.service_uri(params[:service])
  118. end
  119. end
  120. # GET /tools/reps
  121. #
  122. # This endpoint is hit by the js for tweet actions.
  123. # It renders json containing html markup for presentation on the view
  124. def reps
  125. @reps = CongressMember.lookup(street: params[:street_address], zipcode: params[:zipcode])
  126. if @reps.present?
  127. update_user_data(params.slice(:street_address, :zipcode)) if params[:update_user_data] == "true"
  128. render json: { content: render_to_string(partial: "action_page/reps") }, status: 200
  129. else
  130. render json: { error: "No representatives found" }, status: 200
  131. end
  132. end
  133. # GET /tools/reps_raw
  134. #
  135. # This endpoint is hit by the js for email/congress message actions to lookup what legislators
  136. # should be emailed based on the input long/lat or zipcode
  137. def reps_raw
  138. @reps = CongressMember.lookup(street: params[:street_address], zipcode: params[:zipcode])
  139. if @reps.present?
  140. render json: @reps, status: 200
  141. else
  142. render json: { error: "No representatives found" }, status: 200
  143. end
  144. end
  145. private
  146. def set_user
  147. @user = current_user
  148. end
  149. def set_action_page
  150. @action_page ||= ActionPage.find_by_id(params[:action_id])
  151. end
  152. def deliver_thanks_message
  153. @action_page ||= ActionPage.find(params[:action_id])
  154. @email ||= current_user.try(:email) || params[:email]
  155. UserMailer.thanks_message(@email, @action_page, user: @user, name: @name).deliver_now if @email
  156. end
  157. def create_newsletter_subscription
  158. if params[:subscription] && EmailValidator.valid?(params[:subscription][:email])
  159. source = "action center #{@action_page.class.name.downcase} :: " + @action_page.title
  160. params[:subscription][:opt_in] = true
  161. params[:subscription][:source] = source
  162. CiviCRM::subscribe params[:subscription]
  163. end
  164. end
  165. def create_partner_subscription
  166. return unless @action_page
  167. @action_page.partners.each do |partner|
  168. if params["#{partner.code}_subscribe"] == "1"
  169. Subscription.create!(partner_signup_params.merge(partner: partner))
  170. end
  171. end
  172. end
  173. def signature_has_errors
  174. !@signature.nil? and @signature.errors.count > 0
  175. end
  176. def partner_signup_params
  177. if params[:signature].present?
  178. params.require(:signature).permit(:first_name, :last_name, :email)
  179. else
  180. # Partner signup params might come through the main form or a nested subscription form.
  181. params.merge(params[:subscription] || {}).permit(:first_name, :last_name, :email)
  182. end
  183. end
  184. def signature_params
  185. params.require(:signature).permit(
  186. :first_name, :last_name, :email, :petition_id, :user_id,
  187. :street_address, :city, :state, :country_code, :zipcode, :anonymous,
  188. affiliations_attributes: [
  189. :id, :institution_id, :affiliation_type_id
  190. ]
  191. )
  192. end
  193. def call_params
  194. params.permit(:phone, :zipcode, :street_address, :action_id, :call_campaign_id)
  195. end
  196. def email_params
  197. params.permit(:first_name, :last_name, :street_address, :city, :state, :zipcode)
  198. end
  199. end