bsc_plugin_myprofile_controller.rb
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class BscPluginMyprofileController < MyProfileController
def manage_associated_enterprises
@associated_enterprises = profile.enterprises
@pending_enterprises = profile.enterprise_requests.pending.map(&:enterprise)
end
def search_enterprise
render :text => environment.enterprises.find(:all, :conditions => ["type <> 'BscPlugin::Bsc' AND (LOWER(name) LIKE ? OR LOWER(identifier) LIKE ?)", "%#{params[:q]}%", "%#{params[:q]}%"]).
select { |enterprise| enterprise.bsc.nil? && !profile.already_requested?(enterprise)}.
map {|enterprise| {:id => enterprise.id, :name => enterprise.name} }.
to_json
end
def save_associations
enterprises = [Enterprise.find(params[:q].split(','))].flatten
to_remove = profile.enterprises - enterprises
to_add = enterprises - profile.enterprises
to_remove.each do |enterprise|
enterprise.bsc = nil
enterprise.save!
profile.enterprises.delete(enterprise)
end
to_add.each do |enterprise|
if enterprise.enabled
BscPlugin::AssociateEnterprise.create!(:requestor => user, :target => enterprise, :bsc => profile)
else
enterprise.bsc = profile
enterprise.save!
profile.enterprises << enterprise
end
end
session[:notice] = _('This Bsc associations were saved successfully.')
begin
redirect_to :controller => 'profile_editor'
rescue Exception => ex
session[:notice] = _('This Bsc associations couldn\'t be saved.')
logger.info ex
redirect_to :action => 'manage_associated_enterprises'
end
end
def similar_enterprises
name = params[:name]
city = params[:city]
result = []
if !name.blank?
enterprises = (profile.environment.enterprises - profile.enterprises).select { |enterprise| enterprise.bsc_id.nil? && enterprise.city == city && enterprise.name.downcase.include?(name.downcase)}
result = enterprises.inject(result) {|result, enterprise| result << [enterprise.id, enterprise.name]}
end
render :text => result.to_json
end
def transfer_ownership
role = Profile::Roles.admin(profile.environment.id)
@roles = [role]
if request.post?
person = Person.find(params['q_'+role.key])
profile.admins.map { |admin| profile.remove_admin(admin) }
profile.add_admin(person)
BscPlugin::Mailer.deliver_admin_notification(person, profile)
session[:notice] = _('Enterprise ownership transferred.')
redirect_to :controller => 'profile_editor'
end
end
def create_enterprise
@create_enterprise = CreateEnterprise.new(params[:create_enterprise])
@create_enterprise.requestor = user
@create_enterprise.target = environment
@create_enterprise.bsc_id = profile.id
@create_enterprise.enabled = true
@create_enterprise.validated = false
if request.post? && @create_enterprise.valid?
@create_enterprise.perform
session[:notice] = _('Enterprise was created in association with %s.') % profile.name
redirect_to :controller => 'profile_editor', :profile => @create_enterprise.identifier
end
end
end