1 class DiaryEntryController < ApplicationController
2 layout 'site', :except => :rss
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]
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]
18 @title = t 'diary_entry.new.title'
20 if params[:diary_entry]
21 @diary_entry = DiaryEntry.new(params[:diary_entry])
22 @diary_entry.user = @user
25 default_lang = @user.preferences.where(:k => "diary.default_language").first
27 default_lang.v = @diary_entry.language_code
30 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
32 redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name
34 render :action => 'edit'
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)
41 render :action => 'edit'
46 @title= t 'diary_entry.edit.title'
47 @diary_entry = DiaryEntry.find(params[:id])
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]
56 rescue ActiveRecord::RecordNotFound
57 render :action => "no_such_entry", :status => :not_found
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
69 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
71 render :action => 'view'
73 rescue ActiveRecord::RecordNotFound
74 render :action => "no_such_entry", :status => :not_found
78 if params[:display_name]
79 @this_user = User.active.find_by_display_name(params[:display_name])
82 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
83 @entry_pages, @entries = paginate(:diary_entries,
85 :user_id => @this_user.id,
88 :order => 'created_at DESC',
91 render_unknown_user params[:display_name]
93 elsif params[:language]
94 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
95 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
97 :users => { :status => ["active", "confirmed"] },
99 :language_code => params[:language]
101 :order => 'created_at DESC',
103 elsif params[:friends]
105 @title = t 'diary_entry.list.title_friends'
106 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
108 :user_id => @user.friend_users,
111 :order => 'created_at DESC',
117 elsif params[:nearby]
119 @title = t 'diary_entry.list.title_nearby'
120 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
122 :user_id => @user.nearby,
125 :order => 'created_at DESC',
132 @title = t 'diary_entry.list.title'
133 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
135 :users => { :status => ["active", "confirmed"] },
138 :order => 'created_at DESC',
144 @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
146 if params[:display_name]
147 user = User.active.find_by_display_name(params[:display_name])
150 @entries = user.diary_entries.visible
151 @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
152 @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
153 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
155 render :nothing => true, :status => :not_found
157 elsif params[:language]
158 @entries = @entries.visible.where(:language_code => params[:language]).joins(:user).where(:users => { :status => ["active", "confirmed"] })
159 @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
160 @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
161 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
163 @entries = @entries.visible.joins(:user).where(:users => { :status => ["active", "confirmed"] })
164 @title = I18n.t('diary_entry.feed.all.title')
165 @description = I18n.t('diary_entry.feed.all.description')
166 @link = "http://#{SERVER_URL}/diary"
171 @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
173 @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
175 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
176 render :action => 'no_such_entry', :status => :not_found
181 entry = DiaryEntry.find(params[:id])
182 entry.update_attributes({:visible => false}, :without_protection => true)
183 redirect_to :action => "list", :display_name => entry.user.display_name
187 comment = DiaryComment.find(params[:comment])
188 comment.update_attributes({:visible => false}, :without_protection => true)
189 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
193 @comment_pages, @comments = paginate(:diary_comments,
195 :user_id => @this_user,
198 :order => 'created_at DESC',
200 @page = (params[:page] || 1).to_i
204 # require that the user is a administrator, or fill out a helpful error message
205 # and return them to the user page.
206 def require_administrator
207 unless @user.administrator?
208 flash[:error] = t('user.filter.not_an_administrator')
209 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]
214 # is this list user specific?
215 def user_specific_list?
216 params[:friends] or params[:nearby]
220 # decide on a location for the diary entry map
222 if @diary_entry.latitude and @diary_entry.longitude
223 @lon = @diary_entry.longitude
224 @lat = @diary_entry.latitude
226 elsif @user.home_lat.nil? or @user.home_lon.nil?
227 @lon = params[:lon] || -0.1
228 @lat = params[:lat] || 51.5
229 @zoom = params[:zoom] || 4
231 @lon = @user.home_lon
232 @lat = @user.home_lat