1 class DiaryEntryController < ApplicationController
2 layout "site", :except => :rss
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment]
7 before_action :lookup_this_user, :only => [:view, :comments]
8 before_action :check_database_readable
9 before_action :check_database_writable, :only => [:new, :edit]
10 before_action :require_administrator, :only => [:hide, :hidecomment]
13 @title = t "diary_entry.new.title"
15 if params[:diary_entry]
16 @diary_entry = DiaryEntry.new(entry_params)
17 @diary_entry.user = @user
20 default_lang = @user.preferences.where(:k => "diary.default_language").first
22 default_lang.v = @diary_entry.language_code
25 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
27 redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
29 render :action => "edit"
32 default_lang = @user.preferences.where(:k => "diary.default_language").first
33 lang_code = default_lang ? default_lang.v : @user.preferred_language
34 @diary_entry = DiaryEntry.new(:language_code => lang_code)
36 render :action => "edit"
41 @title = t "diary_entry.edit.title"
42 @diary_entry = DiaryEntry.find(params[:id])
44 if @user != @diary_entry.user
45 redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
46 elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
47 redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
51 rescue ActiveRecord::RecordNotFound
52 render :action => "no_such_entry", :status => :not_found
56 @entry = DiaryEntry.find(params[:id])
57 @diary_comment = @entry.comments.build(comment_params)
58 @diary_comment.user = @user
59 if @diary_comment.save
60 if @diary_comment.user != @entry.user
61 Notifier.diary_comment_notification(@diary_comment).deliver_now
64 redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
66 render :action => "view"
68 rescue ActiveRecord::RecordNotFound
69 render :action => "no_such_entry", :status => :not_found
73 if params[:display_name]
74 @this_user = User.active.find_by_display_name(params[:display_name])
77 @title = t "diary_entry.list.user_title", :user => @this_user.display_name
78 @entries = @this_user.diary_entries
80 render_unknown_user params[:display_name]
83 elsif params[:friends]
85 @title = t "diary_entry.list.title_friends"
86 @entries = DiaryEntry.where(:user_id => @user.friend_users)
93 @title = t "diary_entry.list.title_nearby"
94 @entries = DiaryEntry.where(:user_id => @user.nearby)
100 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
103 @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
104 @entries = @entries.where(:language_code => params[:language])
106 @title = t "diary_entry.list.title"
110 @page = (params[:page] || 1).to_i
113 @entries = @entries.visible
114 @entries = @entries.order("created_at DESC")
115 @entries = @entries.offset((@page - 1) * @page_size)
116 @entries = @entries.limit(@page_size)
117 @entries = @entries.includes(:user, :language)
121 if params[:display_name]
122 user = User.active.find_by_display_name(params[:display_name])
125 @entries = user.diary_entries
126 @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
127 @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
128 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
130 render :text => "", :status => :not_found
134 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
137 @entries = @entries.where(:language_code => params[:language])
138 @title = I18n.t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
139 @description = I18n.t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
140 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
142 @title = I18n.t("diary_entry.feed.all.title")
143 @description = I18n.t("diary_entry.feed.all.description")
144 @link = "http://#{SERVER_URL}/diary"
148 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
152 @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
154 @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
155 if params[:comment_id]
156 @reported_comment = DiaryComment.where(:id => params[:comment_id])
159 @title = t "diary_entry.no_such_entry.title", :id => params[:id]
160 render :action => "no_such_entry", :status => :not_found
165 entry = DiaryEntry.find(params[:id])
166 entry.update_attributes(:visible => false)
167 redirect_to :action => "list", :display_name => entry.user.display_name
171 comment = DiaryComment.find(params[:comment])
172 comment.update_attributes(:visible => false)
173 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
177 @comment_pages, @comments = paginate(:diary_comments,
179 :user_id => @this_user,
182 :order => "created_at DESC",
184 @page = (params[:page] || 1).to_i
190 # return permitted diary entry parameters
192 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
196 # return permitted diary comment parameters
198 params.require(:diary_comment).permit(:body)
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"
212 # decide on a location for the diary entry map
214 if @diary_entry.latitude && @diary_entry.longitude
215 @lon = @diary_entry.longitude
216 @lat = @diary_entry.latitude
218 elsif @user.home_lat.nil? || @user.home_lon.nil?
219 @lon = params[:lon] || -0.1
220 @lat = params[:lat] || 51.5
221 @zoom = params[:zoom] || 4
223 @lon = @user.home_lon
224 @lat = @user.home_lat