Include useful URLs in note responses
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout 'site', :except => :rss
3
4   before_filter :authorize_web
5   before_filter :set_locale
6   before_filter :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment]
7   before_filter :lookup_this_user, :only => [:view, :comments]
8   before_filter :check_database_readable
9   before_filter :check_database_writable, :only => [:new, :edit]
10   before_filter :require_administrator, :only => [:hide, :hidecomment]
11
12   caches_action :list, :layout => false, :unless => :user_specific_list?
13   caches_action :rss, :layout => true
14   caches_action :view, :layout => false
15   cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment]
16
17   def new
18     @title = t 'diary_entry.new.title'
19
20     if params[:diary_entry]
21       @diary_entry = DiaryEntry.new(params[:diary_entry])
22       @diary_entry.user = @user
23
24       if @diary_entry.save
25         default_lang = @user.preferences.where(:k => "diary.default_language").first
26         if default_lang
27           default_lang.v = @diary_entry.language_code
28           default_lang.save!
29         else
30           @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
31         end
32         redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name 
33       else
34         render :action => 'edit'
35       end
36     else
37       default_lang = @user.preferences.where(:k => "diary.default_language").first
38       lang_code = default_lang ? default_lang.v : @user.preferred_language
39       @diary_entry = DiaryEntry.new(:language_code => lang_code)
40       set_map_location
41       render :action => 'edit'
42     end
43   end
44
45   def edit
46     @title= t 'diary_entry.edit.title'
47     @diary_entry = DiaryEntry.find(params[:id])
48
49     if @user != @diary_entry.user
50       redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
51     elsif params[:diary_entry] and @diary_entry.update_attributes(params[:diary_entry])
52       redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
53     end
54
55     set_map_location
56   rescue ActiveRecord::RecordNotFound
57     render :action => "no_such_entry", :status => :not_found
58   end
59
60   def comment
61     @entry = DiaryEntry.find(params[:id])
62     @diary_comment = @entry.comments.build(params[:diary_comment])
63     @diary_comment.user = @user
64     if @diary_comment.save
65       if @diary_comment.user != @entry.user
66         Notifier.diary_comment_notification(@diary_comment).deliver
67       end
68
69       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
70     else
71       render :action => 'view'
72     end
73   end
74
75   def list
76     if params[:display_name]
77       @this_user = User.active.find_by_display_name(params[:display_name])
78
79       if @this_user
80         @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
81         @entry_pages, @entries = paginate(:diary_entries,
82                                           :conditions => { 
83                                             :user_id => @this_user.id,
84                                             :visible => true 
85                                           },
86                                           :order => 'created_at DESC',
87                                           :per_page => 20)
88       else
89         render_unknown_user params[:display_name]
90       end
91     elsif params[:language]
92       @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
93       @entry_pages, @entries = paginate(:diary_entries, :include => :user,
94                                         :conditions => {
95                                           :users => { :status => ["active", "confirmed"] },
96                                           :visible => true,
97                                           :language_code => params[:language]
98                                         },
99                                         :order => 'created_at DESC',
100                                         :per_page => 20)
101     elsif params[:friends]
102       if @user
103         @title = t 'diary_entry.list.title_friends'
104         @entry_pages, @entries = paginate(:diary_entries, :include => :user,
105                                           :conditions => {
106                                             :user_id => @user.friend_users,
107                                             :visible => true
108                                           },
109                                           :order => 'created_at DESC',
110                                           :per_page => 20)
111       else
112           require_user
113           return     
114       end
115     elsif params[:nearby]
116       if @user
117         @title = t 'diary_entry.list.title_nearby'
118         @entry_pages, @entries = paginate(:diary_entries, :include => :user,
119                                           :conditions => {
120                                             :user_id => @user.nearby,
121                                             :visible => true
122                                           },
123                                           :order => 'created_at DESC',
124                                           :per_page => 20)                                        
125       else
126           require_user
127           return     
128       end                                  
129     else
130       @title = t 'diary_entry.list.title'
131       @entry_pages, @entries = paginate(:diary_entries, :include => :user,
132                                         :conditions => {
133                                           :users => { :status => ["active", "confirmed"] },
134                                           :visible => true
135                                         },
136                                         :order => 'created_at DESC',
137                                         :per_page => 20)
138     end
139   end
140
141   def rss
142     @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
143
144     if params[:display_name]
145       user = User.active.find_by_display_name(params[:display_name])
146
147       if user
148         @entries = user.diary_entries.visible
149         @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
150         @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
151         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
152       else
153         render :nothing => true, :status => :not_found
154       end
155     elsif params[:language]
156       @entries = @entries.visible.where(:language_code => params[:language]).joins(:user).where(:users => { :status => ["active", "confirmed"] })
157       @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
158       @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
159       @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
160     else
161       @entries = @entries.visible.joins(:user).where(:users => { :status => ["active", "confirmed"] })
162       @title = I18n.t('diary_entry.feed.all.title')
163       @description = I18n.t('diary_entry.feed.all.description')
164       @link = "http://#{SERVER_URL}/diary"
165     end
166   end
167
168   def view
169     @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
170     if @entry
171       @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
172     else
173       @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
174       render :action => 'no_such_entry', :status => :not_found
175     end
176   end
177
178   def hide
179     entry = DiaryEntry.find(params[:id])
180     entry.update_attributes({:visible => false}, :without_protection => true)
181     redirect_to :action => "list", :display_name => entry.user.display_name
182   end
183
184   def hidecomment
185     comment = DiaryComment.find(params[:comment])
186     comment.update_attributes({:visible => false}, :without_protection => true)
187     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
188   end
189
190   def comments
191     @comment_pages, @comments = paginate(:diary_comments,
192                                          :conditions => { 
193                                            :user_id => @this_user,
194                                            :visible => true
195                                          },
196                                          :order => 'created_at DESC',
197                                          :per_page => 20)
198     @page = (params[:page] || 1).to_i
199   end  
200 private
201   ##
202   # require that the user is a administrator, or fill out a helpful error message
203   # and return them to the user page.
204   def require_administrator
205     unless @user.administrator?
206       flash[:error] = t('user.filter.not_an_administrator')
207       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]
208     end
209   end
210
211   ##
212   # is this list user specific?
213   def user_specific_list?
214     params[:friends] or params[:nearby]
215   end
216
217   ##
218   # decide on a location for the diary entry map
219   def set_map_location
220     if @diary_entry.latitude and @diary_entry.longitude
221       @lon = @diary_entry.longitude
222       @lat = @diary_entry.latitude
223       @zoom = 12
224     elsif @user.home_lat.nil? or @user.home_lon.nil?
225       @lon = params[:lon] || -0.1
226       @lat = params[:lat] || 51.5
227       @zoom = params[:zoom] || 4
228     else
229       @lon = @user.home_lon
230       @lat = @user.home_lat
231       @zoom = 12
232     end
233   end
234 end