Guest User

Untitled

a guest
Feb 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. class ProjectsController < ApplicationController
  2. skip_before_filter CAS::Filter, :only => [:us, :international, :show]
  3. skip_before_filter AuthenticationFilter, :only => [:us, :international, :show]
  4. before_filter :find_project, :only => [:show, :update, :edit, :destroy, :reopen, :close, :leader_search, :update_leader]
  5. before_filter :user_stamp, :only => [:create, :update]
  6. before_filter :get_countries, :only => [:new, :edit, :update, :create]
  7. search_action_for 'sp_project', :method => :reopen_search, :order => 'name', :conditions => "project_status = 'closed'"
  8. auto_complete_for :sp_project, :name
  9.  
  10. caches_action :us, :international
  11.  
  12. def index
  13. SpProject.with_scope(:find => { :conditions => sp_user.scope(partner) }) do
  14. unless params[:include_closed]
  15. conditions = ["project_status = 'open'"]
  16. end
  17. @projects = SpProject.find(:all, :conditions => conditions, :order => 'name', :include => [:pd, :apd, :opd])
  18. end
  19. @partner = partner || my_region
  20. render :action => :index
  21. end
  22.  
  23. def new()
  24. @project = SpProject.new
  25. @project.primary_partner ||= my_region
  26. end
  27.  
  28. def create
  29. @project = SpProject.new(params[:project])
  30. unless @project.save
  31. render(:action => :new)
  32. else
  33. get_coordinates(@project)
  34. clear_cache(@project)
  35. go_to_index "Your project as been created successfully"
  36. end
  37. end
  38.  
  39. def update_leader
  40. @project.update_attribute_with_validation_skipping(params[:role]+'_id', params[:leader_id])
  41. flash[:notice] = "Your project as been updated successfully"
  42. # If we are assigning a leader, we'd better give them permission too.
  43. if l = @project.send(params[:role].to_sym)
  44. ssm_id = l.user.id
  45. leader_user = SpUser.find_by_ssm_id(ssm_id)
  46. unless leader_user.is_a?(SpNationalCoordinator) || leader_user.is_a?(SpRegionalCoordinator)
  47. leader_user.destroy unless leader_user.is_a?(NilClass)
  48. SpDirector.create!(:ssm_id => ssm_id,
  49. :created_at => Time.now,
  50. :created_by_id => user.id)
  51. end
  52. end
  53. clear_cache(@project)
  54. close_window
  55. end
  56.  
  57. def update
  58. unless @project.update_attributes(params[:project])
  59. render(:action => :edit)
  60. else
  61. get_coordinates(@project)
  62. clear_cache(@project)
  63. go_to_index
  64. end
  65. end
  66.  
  67. def edit
  68. end
  69.  
  70. def destroy
  71. @project.destroy
  72. go_to_index("Your project as been deleted successfully")
  73. end
  74.  
  75. def show
  76. respond_to do |format|
  77. format.html
  78. format.xml {render :xml => @project.to_xml(:only => [:name, :start_date,
  79. :end_date, :weeks,
  80. :job, :description,
  81. :display_location,
  82. :student_cost, :primary_partner,
  83. :url, :url_title],
  84. :methods => [:pd_name, :apd_name,
  85. :pd_email, :apd_email,
  86. :primary_focus_name,
  87. :regional_info],
  88. :include => [:ministry_focuses])}
  89. end
  90. end
  91.  
  92. def reopen
  93. @project.open!
  94. go_to_index("#{@project.name} has been re-opened.")
  95. end
  96.  
  97. def close
  98. @project.close!
  99. go_to_index("#{@project.name} has been closed.")
  100. end
  101.  
  102. def international
  103. @projects = SpProject.find(:all, :conditions => "country <> 'USA' AND project_status = 'open'", :order => 'name')
  104. respond_to do |format|
  105. format.html
  106. format.xml do
  107. @headers["Content-Type"] = "text/xml; charset=utf-8"
  108. render :action => 'international_map_feed', :layout => false
  109. end
  110. end
  111. end
  112.  
  113. def merge
  114. ids = params[:projects] || []
  115. # raise ids.inspect
  116. unless ids.size > 1
  117. flash[:notice] = "You must choose at least 2 projects to merge, exactly 1 \
  118. of which must have a status of \"open\""
  119. end
  120. ids.map!(&:to_i)
  121. open_projects = SpProject.find(:all, :conditions => ["id IN(?) and project_status = 'open'", ids])
  122. if open_projects.size == 1
  123. open_id = open_projects.first.id
  124. closed_projects = SpProject.find(:all, :conditions => ["id IN(?) and project_status = 'closed'", ids])
  125. closed_projects.each do |project|
  126. closed_id = project.id
  127. # update all applicants associated with this project to point to the open project
  128. ['preference1_id','preference2_id','preference3_id','preference4_id',
  129. 'preference5_id','project_id','current_project_queue_id'].each do |column|
  130. SpProject.connection.update("update #{SpApplication.table_name} set #{column} = #{open_id} where #{column} = #{closed_id}")
  131. end
  132. project.destroy
  133. end
  134. else
  135. flash[:notice] = "Please choose exactly 1 open project, and as many closed \
  136. projects as you like."
  137. end
  138. index
  139. end
  140.  
  141. def us
  142. @projects = SpProject.find(:all, :conditions => "country = 'USA' AND project_status = 'open'", :order => 'name')
  143. respond_to do |format|
  144. format.html
  145. format.xml do
  146. @headers["Content-Type"] = "text/xml; charset=utf-8"
  147. render :action => 'us_map_feed', :layout => false
  148. end
  149. end
  150. end
  151.  
  152. protected
  153. def go_to_index(msg = nil)
  154. flash[:notice] = msg
  155. redirect_to(:action => :index)
  156. end
  157.  
  158. def find_project
  159. @project = SpProject.find(params[:id])
  160. end
  161.  
  162. def get_countries
  163. @countries = Country.find(:all, :order => :country)
  164. end
  165.  
  166. def clear_cache(project)
  167. expire_fragment(%r{projects;.*})
  168. end
  169.  
  170. def get_coordinates(p)
  171. key = 'ABQIAAAA3_Rt6DOXqoqzxOdrpwwtvhSTzVfmYDnwpEGk65AEA3VA32K1ZBTjPtznyT3qg_teDdJYQqkNfMwI7w'
  172. q = p.city
  173. q += ','+p.state if p.state
  174. q += ','+p.country
  175. q.gsub!(' ','+')
  176. gg = GoogleGeocode.new key
  177. begin
  178. location = gg.locate q
  179. p.latitude = location.coordinates[0]
  180. p.longitude = location.coordinates[1]
  181. # We need to make sure that that no 2 projects have exactly the same
  182. # coordinates. If they do, they will overlap on the flash map and
  183. # you won't be able to click on one of them.
  184. while SpProject.find(:first, :conditions => ['latitude = ? and longitude = ?', p.latitude, p.longitude])
  185. p.longitude += rand(4).to_f/10 - 0.2 # move it over a little.
  186. p.latitude += rand(4).to_f/10 - 0.2
  187. end
  188. p.save
  189. rescue GoogleGeocode::AddressError => e;
  190. rescue
  191. end;
  192. end
  193. end
Add Comment
Please, Sign In to add comment