profile_controller.rb
4.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class ProfileController < PublicController
needs_profile
before_filter :check_access_to_profile, :except => [:join, :refuse_join, :refuse_for_now, :index]
before_filter :store_before_join, :only => [:join]
before_filter :login_required, :only => [:join, :refuse_join, :leave]
helper TagsHelper
def index
@tags = profile.article_tags
unless profile.display_info_to?(user)
profile.visible? ? private_profile : invisible_profile
end
end
def tags
@tags_cache_key = "tags_profile_#{profile.id.to_s}"
if is_cache_expired?(@tags_cache_key, true)
@tags = profile.article_tags
end
end
def tag
@tag = params[:id]
@tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_#{profile.id.to_s}_page_#{params[:npage]}"
if is_cache_expired?(@tag_cache_key, true)
@tagged = profile.find_tagged_with(@tag).paginate(:per_page => 20, :page => params[:npage])
end
end
def communities
if is_cache_expired?(profile.communities_cache_key(params))
@communities = profile.communities.paginate(:per_page => per_page, :page => params[:npage])
end
end
def enterprises
@enterprises = profile.enterprises
end
def friends
if is_cache_expired?(profile.friends_cache_key(params))
@friends = profile.friends.paginate(:per_page => per_page, :page => params[:npage])
end
end
def members
if is_cache_expired?(profile.members_cache_key(params))
@members = profile.members.paginate(:per_page => members_per_page, :page => params[:npage])
end
end
def favorite_enterprises
@favorite_enterprises = profile.favorite_enterprises
end
def sitemap
@articles = profile.top_level_articles
end
def join
@wizard = params[:wizard]
if request.post? && params[:confirmation]
profile.add_member(current_user.person)
flash[:notice] = _('%s administrator still needs to accept you as member.') % profile.name if profile.closed?
if @wizard
redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
else
redirect_to_before_join
end
else
store_location(request.referer)
if current_user.person.memberships.include?(profile)
flash[:notice] = _('You are already a member of "%s"') % profile.name
redirect_back_or_default profile.url
return
end
if request.xhr?
render :layout => false
end
end
end
def leave
@wizard = params[:wizard]
if request.post? && params[:confirmation]
profile.remove_member(current_user.person)
if @wizard
redirect_to :controller => 'search', :action => 'assets', :asset => 'communities', :wizard => true
else
redirect_back_or_default profile.url
end
else
store_location(request.referer)
if request.xhr?
render :layout => false
end
end
end
def refuse_join
p = current_user.person
p.refused_communities << profile
p.save
redirect_to profile.url
end
def refuse_for_now
session[:no_asking] ||= []
session[:no_asking].shift if session[:no_asking].size >= 10
session[:no_asking] << profile.id
render :text => '', :layout => false
end
def unblock
profile.unblock
redirect_to :controller => 'profile', :action => 'index'
end
protected
def check_access_to_profile
unless profile.display_info_to?(user)
redirect_to :action => 'index'
end
end
def store_before_join
session[:before_join] = request.referer unless logged_in?
end
def redirect_to_before_join
back = session[:before_join]
if back
session[:before_join] = nil
redirect_to back
else
redirect_back_or_default profile.url
end
end
def private_profile
if profile.person?
@action = :add_friend
@message = _("The content here is available to %s's friends only." % profile.short_name)
else
@action = :join
@message = _('The contents in this community is available to members only.')
end
@no_design_blocks = true
end
def invisible_profile
render_access_denied(_("Sorry, this profile was defined as private by its owner. You'll not be able to view content here unless the profile owner adds adds you."), _("Oops ... you cannot go ahead here"))
end
def per_page
Noosfero::Constants::PROFILE_PER_PAGE
end
def members_per_page
20
end
end