Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ProjectsController < ApplicationController
- skip_before_filter CAS::Filter, :only => [:us, :international, :show]
- skip_before_filter AuthenticationFilter, :only => [:us, :international, :show]
- before_filter :find_project, :only => [:show, :update, :edit, :destroy, :reopen, :close, :leader_search, :update_leader]
- before_filter :user_stamp, :only => [:create, :update]
- before_filter :get_countries, :only => [:new, :edit, :update, :create]
- search_action_for 'sp_project', :method => :reopen_search, :order => 'name', :conditions => "project_status = 'closed'"
- auto_complete_for :sp_project, :name
- caches_action :us, :international
- def index
- SpProject.with_scope(:find => { :conditions => sp_user.scope(partner) }) do
- unless params[:include_closed]
- conditions = ["project_status = 'open'"]
- end
- @projects = SpProject.find(:all, :conditions => conditions, :order => 'name', :include => [:pd, :apd, :opd])
- end
- @partner = partner || my_region
- render :action => :index
- end
- def new()
- @project = SpProject.new
- @project.primary_partner ||= my_region
- end
- def create
- @project = SpProject.new(params[:project])
- unless @project.save
- render(:action => :new)
- else
- get_coordinates(@project)
- clear_cache(@project)
- go_to_index "Your project as been created successfully"
- end
- end
- def update_leader
- @project.update_attribute_with_validation_skipping(params[:role]+'_id', params[:leader_id])
- flash[:notice] = "Your project as been updated successfully"
- # If we are assigning a leader, we'd better give them permission too.
- if l = @project.send(params[:role].to_sym)
- ssm_id = l.user.id
- leader_user = SpUser.find_by_ssm_id(ssm_id)
- unless leader_user.is_a?(SpNationalCoordinator) || leader_user.is_a?(SpRegionalCoordinator)
- leader_user.destroy unless leader_user.is_a?(NilClass)
- SpDirector.create!(:ssm_id => ssm_id,
- :created_at => Time.now,
- :created_by_id => user.id)
- end
- end
- clear_cache(@project)
- close_window
- end
- def update
- unless @project.update_attributes(params[:project])
- render(:action => :edit)
- else
- get_coordinates(@project)
- clear_cache(@project)
- go_to_index
- end
- end
- def edit
- end
- def destroy
- @project.destroy
- go_to_index("Your project as been deleted successfully")
- end
- def show
- respond_to do |format|
- format.html
- format.xml {render :xml => @project.to_xml(:only => [:name, :start_date,
- :end_date, :weeks,
- :job, :description,
- :display_location,
- :student_cost, :primary_partner,
- :url, :url_title],
- :methods => [:pd_name, :apd_name,
- :pd_email, :apd_email,
- :primary_focus_name,
- :regional_info],
- :include => [:ministry_focuses])}
- end
- end
- def reopen
- @project.open!
- go_to_index("#{@project.name} has been re-opened.")
- end
- def close
- @project.close!
- go_to_index("#{@project.name} has been closed.")
- end
- def international
- @projects = SpProject.find(:all, :conditions => "country <> 'USA' AND project_status = 'open'", :order => 'name')
- respond_to do |format|
- format.html
- format.xml do
- @headers["Content-Type"] = "text/xml; charset=utf-8"
- render :action => 'international_map_feed', :layout => false
- end
- end
- end
- def merge
- ids = params[:projects] || []
- # raise ids.inspect
- unless ids.size > 1
- flash[:notice] = "You must choose at least 2 projects to merge, exactly 1 \
- of which must have a status of \"open\""
- end
- ids.map!(&:to_i)
- open_projects = SpProject.find(:all, :conditions => ["id IN(?) and project_status = 'open'", ids])
- if open_projects.size == 1
- open_id = open_projects.first.id
- closed_projects = SpProject.find(:all, :conditions => ["id IN(?) and project_status = 'closed'", ids])
- closed_projects.each do |project|
- closed_id = project.id
- # update all applicants associated with this project to point to the open project
- ['preference1_id','preference2_id','preference3_id','preference4_id',
- 'preference5_id','project_id','current_project_queue_id'].each do |column|
- SpProject.connection.update("update #{SpApplication.table_name} set #{column} = #{open_id} where #{column} = #{closed_id}")
- end
- project.destroy
- end
- else
- flash[:notice] = "Please choose exactly 1 open project, and as many closed \
- projects as you like."
- end
- index
- end
- def us
- @projects = SpProject.find(:all, :conditions => "country = 'USA' AND project_status = 'open'", :order => 'name')
- respond_to do |format|
- format.html
- format.xml do
- @headers["Content-Type"] = "text/xml; charset=utf-8"
- render :action => 'us_map_feed', :layout => false
- end
- end
- end
- protected
- def go_to_index(msg = nil)
- flash[:notice] = msg
- redirect_to(:action => :index)
- end
- def find_project
- @project = SpProject.find(params[:id])
- end
- def get_countries
- @countries = Country.find(:all, :order => :country)
- end
- def clear_cache(project)
- expire_fragment(%r{projects;.*})
- end
- def get_coordinates(p)
- key = 'ABQIAAAA3_Rt6DOXqoqzxOdrpwwtvhSTzVfmYDnwpEGk65AEA3VA32K1ZBTjPtznyT3qg_teDdJYQqkNfMwI7w'
- q = p.city
- q += ','+p.state if p.state
- q += ','+p.country
- q.gsub!(' ','+')
- gg = GoogleGeocode.new key
- begin
- location = gg.locate q
- p.latitude = location.coordinates[0]
- p.longitude = location.coordinates[1]
- # We need to make sure that that no 2 projects have exactly the same
- # coordinates. If they do, they will overlap on the flash map and
- # you won't be able to click on one of them.
- while SpProject.find(:first, :conditions => ['latitude = ? and longitude = ?', p.latitude, p.longitude])
- p.longitude += rand(4).to_f/10 - 0.2 # move it over a little.
- p.latitude += rand(4).to_f/10 - 0.2
- end
- p.save
- rescue GoogleGeocode::AddressError => e;
- rescue
- end;
- end
- end
Add Comment
Please, Sign In to add comment