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)
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]
51 if @diary_entry.update_attributes(params[:diary_entry])
52 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(params[:diary_comment])
62 @diary_comment.user = @user
63 if @diary_comment.save
64 if @diary_comment.user != @entry.user
65 Notifier.diary_comment_notification(@diary_comment).deliver
68 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
70 render :action => 'view'
75 if params[:display_name]
76 @this_user = User.active.find_by_display_name(params[:display_name])
79 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
80 @entry_pages, @entries = paginate(:diary_entries,
82 :user_id => @this_user.id,
85 :order => 'created_at DESC',
88 render_unknown_user params[:display_name]
90 elsif params[:language]
91 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
92 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
94 :users => { :status => ["active", "confirmed"] },
96 :language_code => params[:language]
98 :order => 'created_at DESC',
100 elsif params[:friends]
102 @title = t 'diary_entry.list.title_friends'
103 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
105 :user_id => @user.friend_users,
108 :order => 'created_at DESC',
114 elsif params[:nearby]
116 @title = t 'diary_entry.list.title_nearby'
117 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
119 :user_id => @user.nearby,
122 :order => 'created_at DESC',
129 @title = t 'diary_entry.list.title'
130 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
132 :users => { :status => ["active", "confirmed"] },
135 :order => 'created_at DESC',
141 @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
143 if params[:display_name]
144 user = User.active.find_by_display_name(params[:display_name])
147 @entries = user.diary_entries.visible
148 @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
149 @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
150 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
152 render :nothing => true, :status => :not_found
154 elsif params[:language]
155 @entries = @entries.visible.where(:language_code => params[:language]).joins(:user).where(:users => { :status => ["active", "confirmed"] })
156 @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
157 @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
158 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
160 @entries = @entries.visible.joins(:user).where(:users => { :status => ["active", "confirmed"] })
161 @title = I18n.t('diary_entry.feed.all.title')
162 @description = I18n.t('diary_entry.feed.all.description')
163 @link = "http://#{SERVER_URL}/diary"
168 @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
170 @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
172 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
173 render :action => 'no_such_entry', :status => :not_found
178 entry = DiaryEntry.find(params[:id])
179 entry.update_attributes({:visible => false}, :without_protection => true)
180 redirect_to :action => "list", :display_name => entry.user.display_name
184 comment = DiaryComment.find(params[:comment])
185 comment.update_attributes({:visible => false}, :without_protection => true)
186 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
190 @comment_pages, @comments = paginate(:diary_comments,
192 :user_id => @this_user,
195 :order => 'created_at DESC',
197 @page = (params[:page] || 1).to_i
201 # require that the user is a administrator, or fill out a helpful error message
202 # and return them to the user page.
203 def require_administrator
204 unless @user.administrator?
205 flash[:error] = t('user.filter.not_an_administrator')
206 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]
211 # is this list user specific?
212 def user_specific_list?
213 params[:friends] or params[:nearby]