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]
12 @title = t 'diary_entry.new.title'
14 if params[:diary_entry]
15 @diary_entry = DiaryEntry.new(params[:diary_entry])
16 @diary_entry.user = @user
19 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
21 default_lang.v = @diary_entry.language_code
24 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
26 redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name
28 render :action => 'edit'
31 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
32 lang_code = default_lang ? default_lang.v : @user.preferred_language
33 @diary_entry = DiaryEntry.new(:language_code => lang_code)
34 render :action => 'edit'
39 @title= t 'diary_entry.edit.title'
40 @diary_entry = DiaryEntry.find(params[:id])
42 if @user != @diary_entry.user
43 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
44 elsif params[:diary_entry]
45 if @diary_entry.update_attributes(params[:diary_entry])
46 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
49 rescue ActiveRecord::RecordNotFound
50 render :action => "no_such_entry", :status => :not_found
54 @entry = DiaryEntry.find(params[:id])
55 @diary_comment = @entry.comments.build(params[:diary_comment])
56 @diary_comment.user = @user
57 if @diary_comment.save
58 if @diary_comment.user != @entry.user
59 Notifier::deliver_diary_comment_notification(@diary_comment)
62 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
64 render :action => 'view'
69 if params[:display_name]
70 @this_user = User.find_by_display_name(params[:display_name], :conditions => { :visible => true })
73 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
74 @entry_pages, @entries = paginate(:diary_entries,
76 :user_id => @this_user.id,
79 :order => 'created_at DESC',
82 @title = t'diary_entry.no_such_user.title'
83 @not_found_user = params[:display_name]
85 render :action => 'no_such_user', :status => :not_found
87 elsif params[:language]
88 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
89 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
91 :users => { :visible => true },
93 :language_code => params[:language]
95 :order => 'created_at DESC',
98 @title = t 'diary_entry.list.title'
99 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
101 :users => { :visible => true },
104 :order => 'created_at DESC',
110 request.format = :rss
112 if params[:display_name]
113 user = User.find_by_display_name(params[:display_name], :conditions => { :visible => true })
116 @entries = DiaryEntry.find(:all,
121 :order => 'created_at DESC',
123 @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
124 @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
125 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
127 render :nothing => true, :status => :not_found
129 elsif params[:language]
130 @entries = DiaryEntry.find(:all, :include => :user,
132 :users => { :visible => true },
134 :language_code => params[:language]
136 :order => 'created_at DESC',
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 @entries = DiaryEntry.find(:all, :include => :user,
144 :users => { :visible => true },
147 :order => 'created_at DESC',
149 @title = I18n.t('diary_entry.feed.all.title')
150 @description = I18n.t('diary_entry.feed.all.description')
151 @link = "http://#{SERVER_URL}/diary"
156 user = User.find_by_display_name(params[:display_name], :conditions => { :visible => true })
159 @entry = DiaryEntry.find(:first, :conditions => {
165 @title = t 'diary_entry.view.title', :user => params[:display_name]
167 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
168 render :action => 'no_such_entry', :status => :not_found
171 @not_found_user = params[:display_name]
173 render :action => 'no_such_user', :status => :not_found
178 entry = DiaryEntry.find(params[:id])
179 entry.update_attributes(:visible => false)
180 redirect_to :action => "list", :display_name => entry.user.display_name
184 comment = DiaryComment.find(params[:comment])
185 comment.update_attributes(:visible => false)
186 redirect_to :action => "view", :id => comment.diary_entry.id
190 # require that the user is a administrator, or fill out a helpful error message
191 # and return them to the user page.
192 def require_administrator
193 unless @user.administrator?
194 flash[:error] = t('user.filter.not_an_administrator')
195 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]