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, :subscribe, :unsubscribe]
7 before_action :lookup_this_user, :only => [:view, :comments]
8 before_action :check_database_readable
9 before_action :check_database_writable, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
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)
28 # Subscribe user to diary comments
29 @diary_entry.subscribers << @user
31 redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
33 render :action => "edit"
36 default_lang = @user.preferences.where(:k => "diary.default_language").first
37 lang_code = default_lang ? default_lang.v : @user.preferred_language
38 @diary_entry = DiaryEntry.new(:language_code => lang_code)
40 render :action => "edit"
45 @title = t "diary_entry.edit.title"
46 @diary_entry = DiaryEntry.find(params[:id])
48 if @user != @diary_entry.user
49 redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
50 elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
51 redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
55 rescue ActiveRecord::RecordNotFound
56 render :action => "no_such_entry", :status => :not_found
60 @entry = DiaryEntry.find(params[:id])
61 @diary_comment = @entry.comments.build(comment_params)
62 @diary_comment.user = @user
63 if @diary_comment.save
65 # Notify current subscribers of the new comment
66 @entry.subscribers.visible.each do |user|
68 Notifier.diary_comment_notification(@diary_comment, user).deliver_now
72 # Add the commenter to the subscribers if necessary
73 @entry.subscribers << @user unless @entry.subscribers.exists?(@user.id)
75 redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
77 render :action => "view"
79 rescue ActiveRecord::RecordNotFound
80 render :action => "no_such_entry", :status => :not_found
84 @entry = DiaryEntry.find(params[:id])
86 if ! diary_entry.subscribers.exists?(@user.id)
87 diary_entry.subscribers << @user
89 redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
93 @entry = DiaryEntry.find(params[:id])
95 if diary_entry.subscribers.exists?(@user.id)
96 diary_entry.subscribers.delete(@user)
98 redirect_to :controller => "diary_entry", :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
102 if params[:display_name]
103 @this_user = User.active.find_by_display_name(params[:display_name])
106 @title = t "diary_entry.list.user_title", :user => @this_user.display_name
107 @entries = @this_user.diary_entries
109 render_unknown_user params[:display_name]
112 elsif params[:friends]
114 @title = t "diary_entry.list.title_friends"
115 @entries = DiaryEntry.where(:user_id => @user.friend_users)
120 elsif params[:nearby]
122 @title = t "diary_entry.list.title_nearby"
123 @entries = DiaryEntry.where(:user_id => @user.nearby)
129 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
132 @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
133 @entries = @entries.where(:language_code => params[:language])
135 @title = t "diary_entry.list.title"
139 @page = (params[:page] || 1).to_i
142 @entries = @entries.visible
143 @entries = @entries.order("created_at DESC")
144 @entries = @entries.offset((@page - 1) * @page_size)
145 @entries = @entries.limit(@page_size)
146 @entries = @entries.includes(:user, :language)
150 if params[:display_name]
151 user = User.active.find_by_display_name(params[:display_name])
154 @entries = user.diary_entries
155 @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
156 @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
157 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
159 render :text => "", :status => :not_found
163 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
166 @entries = @entries.where(:language_code => params[:language])
167 @title = I18n.t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
168 @description = I18n.t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
169 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
171 @title = I18n.t("diary_entry.feed.all.title")
172 @description = I18n.t("diary_entry.feed.all.description")
173 @link = "http://#{SERVER_URL}/diary"
177 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
181 @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
183 @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
185 @title = t "diary_entry.no_such_entry.title", :id => params[:id]
186 render :action => "no_such_entry", :status => :not_found
191 entry = DiaryEntry.find(params[:id])
192 entry.update_attributes(:visible => false)
193 redirect_to :action => "list", :display_name => entry.user.display_name
197 comment = DiaryComment.find(params[:comment])
198 comment.update_attributes(:visible => false)
199 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
203 @comment_pages, @comments = paginate(:diary_comments,
205 :user_id => @this_user,
208 :order => "created_at DESC",
210 @page = (params[:page] || 1).to_i
216 # return permitted diary entry parameters
218 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
222 # return permitted diary comment parameters
224 params.require(:diary_comment).permit(:body)
228 # require that the user is a administrator, or fill out a helpful error message
229 # and return them to the user page.
230 def require_administrator
231 unless @user.administrator?
232 flash[:error] = t("user.filter.not_an_administrator")
233 redirect_to :controller => "diary_entry", :action => "view"
238 # decide on a location for the diary entry map
240 if @diary_entry.latitude && @diary_entry.longitude
241 @lon = @diary_entry.longitude
242 @lat = @diary_entry.latitude
244 elsif @user.home_lat.nil? || @user.home_lon.nil?
245 @lon = params[:lon] || -0.1
246 @lat = params[:lat] || 51.5
247 @zoom = params[:zoom] || 4
249 @lon = @user.home_lon
250 @lat = @user.home_lat