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 :check_database_readable
8 before_filter :check_database_writable, :only => [:new, :edit]
9 before_filter :require_administrator, :only => [:hide, :hidecomment]
11 caches_action :list, :layout => false, :unless => :user_specific_list?
12 caches_action :rss, :layout => true
13 caches_action :view, :layout => false
14 cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment]
17 @title = t 'diary_entry.new.title'
19 if params[:diary_entry]
20 @diary_entry = DiaryEntry.new(params[:diary_entry])
21 @diary_entry.user = @user
24 default_lang = @user.preferences.where(:k => "diary.default_language").first
26 default_lang.v = @diary_entry.language_code
29 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
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)
39 render :action => 'edit'
44 @title= t 'diary_entry.edit.title'
45 @diary_entry = DiaryEntry.find(params[:id])
47 if @user != @diary_entry.user
48 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
49 elsif params[:diary_entry]
50 if @diary_entry.update_attributes(params[:diary_entry])
51 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
54 rescue ActiveRecord::RecordNotFound
55 render :action => "no_such_entry", :status => :not_found
59 @entry = DiaryEntry.find(params[:id])
60 @diary_comment = @entry.comments.build(params[:diary_comment])
61 @diary_comment.user = @user
62 if @diary_comment.save
63 if @diary_comment.user != @entry.user
64 Notifier.diary_comment_notification(@diary_comment).deliver
67 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
69 render :action => 'view'
74 if params[:display_name]
75 @this_user = User.active.find_by_display_name(params[:display_name])
78 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
79 @entry_pages, @entries = paginate(:diary_entries,
81 :user_id => @this_user.id,
84 :order => 'created_at DESC',
87 @title = t'diary_entry.no_such_user.title'
88 @not_found_user = params[:display_name]
90 render :action => 'no_such_user', :status => :not_found
92 elsif params[:language]
93 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
94 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
96 :users => { :status => ["active", "confirmed"] },
98 :language_code => params[:language]
100 :order => 'created_at DESC',
102 elsif params[:friends]
104 @title = t 'diary_entry.list.title_friends'
105 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
107 :user_id => @user.friend_users,
110 :order => 'created_at DESC',
116 elsif params[:nearby]
118 @title = t 'diary_entry.list.title_nearby'
119 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
121 :user_id => @user.nearby,
124 :order => 'created_at DESC',
131 @title = t 'diary_entry.list.title'
132 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
134 :users => { :status => ["active", "confirmed"] },
137 :order => 'created_at DESC',
143 @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
145 if params[:display_name]
146 user = User.active.find_by_display_name(params[:display_name])
149 @entries = user.diary_entries.visible
150 @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
151 @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
152 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
154 render :nothing => true, :status => :not_found
156 elsif params[:language]
157 @entries = @entries.visible.where(:language_code => params[:language]).joins(:user).where(:users => { :status => ["active", "confirmed"] })
158 @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
159 @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
160 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
162 @entries = @entries.visible.joins(:user).where(:users => { :status => ["active", "confirmed"] })
163 @title = I18n.t('diary_entry.feed.all.title')
164 @description = I18n.t('diary_entry.feed.all.description')
165 @link = "http://#{SERVER_URL}/diary"
170 user = User.active.find_by_display_name(params[:display_name])
173 @entry = user.diary_entries.visible.where(:id => params[:id]).first
175 @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
177 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
178 render :action => 'no_such_entry', :status => :not_found
181 @not_found_user = params[:display_name]
183 render :action => 'no_such_user', :status => :not_found
188 entry = DiaryEntry.find(params[:id])
189 entry.update_attributes(:visible => false)
190 redirect_to :action => "list", :display_name => entry.user.display_name
194 comment = DiaryComment.find(params[:comment])
195 comment.update_attributes(:visible => false)
196 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
200 @this_user = User.active.find_by_display_name(params[:display_name])
203 @comment_pages, @comments = paginate(:diary_comments,
204 :conditions => { :user_id => @this_user },
205 :order => 'created_at DESC',
207 @page = (params[:page] || 1).to_i
209 @title = t'diary_entry.no_such_user.title'
210 @not_found_user = params[:display_name]
211 render :action => 'no_such_user', :status => :not_found
216 # require that the user is a administrator, or fill out a helpful error message
217 # and return them to the user page.
218 def require_administrator
219 unless @user.administrator?
220 flash[:error] = t('user.filter.not_an_administrator')
221 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]
226 # is this list user specific?
227 def user_specific_list?
228 params[:friends] or params[:nearby]