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]
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] and @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
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 => ["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 => ["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
156 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
157 render :action => 'no_such_entry', :status => :not_found
162 entry = DiaryEntry.find(params[:id])
163 entry.update_attributes(:visible => false)
164 redirect_to :action => "list", :display_name => entry.user.display_name
168 comment = DiaryComment.find(params[:comment])
169 comment.update_attributes(:visible => false)
170 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
174 @comment_pages, @comments = paginate(:diary_comments,
176 :user_id => @this_user,
179 :order => 'created_at DESC',
181 @page = (params[:page] || 1).to_i
185 # return permitted diary entry parameters
187 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
191 # return permitted diary comment parameters
193 params.require(:diary_comment).permit(:body)
197 # require that the user is a administrator, or fill out a helpful error message
198 # and return them to the user page.
199 def require_administrator
200 unless @user.administrator?
201 flash[:error] = t('user.filter.not_an_administrator')
202 redirect_to :controller => 'diary_entry', :action => 'view'
207 # is this list user specific?
208 def user_specific_list?
209 params[:friends] or params[:nearby]
213 # decide on a location for the diary entry map
215 if @diary_entry.latitude and @diary_entry.longitude
216 @lon = @diary_entry.longitude
217 @lat = @diary_entry.latitude
219 elsif @user.home_lat.nil? or @user.home_lon.nil?
220 @lon = params[:lon] || -0.1
221 @lat = params[:lat] || 51.5
222 @zoom = params[:zoom] || 4
224 @lon = @user.home_lon
225 @lat = @user.home_lat