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, :view, :layout => false
12 caches_action :rss, :layout => true
13 cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment], :unless => STATUS == :database_offline
16 @title = t 'diary_entry.new.title'
18 if params[:diary_entry]
19 @diary_entry = DiaryEntry.new(params[:diary_entry])
20 @diary_entry.user = @user
23 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
25 default_lang.v = @diary_entry.language_code
28 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
30 redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name
32 render :action => 'edit'
35 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
36 lang_code = default_lang ? default_lang.v : @user.preferred_language
37 @diary_entry = DiaryEntry.new(:language_code => lang_code)
38 render :action => 'edit'
43 @title= t 'diary_entry.edit.title'
44 @diary_entry = DiaryEntry.find(params[:id])
46 if @user != @diary_entry.user
47 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
48 elsif params[:diary_entry]
49 if @diary_entry.update_attributes(params[:diary_entry])
50 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
53 rescue ActiveRecord::RecordNotFound
54 render :action => "no_such_entry", :status => :not_found
58 @entry = DiaryEntry.find(params[:id])
59 @diary_comment = @entry.comments.build(params[:diary_comment])
60 @diary_comment.user = @user
61 if @diary_comment.save
62 if @diary_comment.user != @entry.user
63 Notifier::deliver_diary_comment_notification(@diary_comment)
66 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
68 render :action => 'view'
73 if params[:display_name]
74 @this_user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
77 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
78 @entry_pages, @entries = paginate(:diary_entries,
80 :user_id => @this_user.id,
83 :order => 'created_at DESC',
86 @title = t'diary_entry.no_such_user.title'
87 @not_found_user = params[:display_name]
89 render :action => 'no_such_user', :status => :not_found
91 elsif params[:language]
92 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
93 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
95 :users => { :status => ["active", "confirmed"] },
97 :language_code => params[:language]
99 :order => 'created_at DESC',
102 @title = t 'diary_entry.list.title'
103 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
105 :users => { :status => ["active", "confirmed"] },
108 :order => 'created_at DESC',
114 request.format = :rss
116 if params[:display_name]
117 user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
120 @entries = DiaryEntry.find(:all,
125 :order => 'created_at DESC',
127 @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
128 @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
129 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
131 render :nothing => true, :status => :not_found
133 elsif params[:language]
134 @entries = DiaryEntry.find(:all, :include => :user,
136 :users => { :status => ["active", "confirmed"] },
138 :language_code => params[:language]
140 :order => 'created_at DESC',
142 @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
143 @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
144 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
146 @entries = DiaryEntry.find(:all, :include => :user,
148 :users => { :status => ["active", "confirmed"] },
151 :order => 'created_at DESC',
153 @title = I18n.t('diary_entry.feed.all.title')
154 @description = I18n.t('diary_entry.feed.all.description')
155 @link = "http://#{SERVER_URL}/diary"
160 user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
163 @entry = DiaryEntry.find(:first, :conditions => {
169 @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
171 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
172 render :action => 'no_such_entry', :status => :not_found
175 @not_found_user = params[:display_name]
177 render :action => 'no_such_user', :status => :not_found
182 entry = DiaryEntry.find(params[:id])
183 entry.update_attributes(:visible => false)
184 redirect_to :action => "list", :display_name => entry.user.display_name
188 comment = DiaryComment.find(params[:comment])
189 comment.update_attributes(:visible => false)
190 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
194 # require that the user is a administrator, or fill out a helpful error message
195 # and return them to the user page.
196 def require_administrator
197 unless @user.administrator?
198 flash[:error] = t('user.filter.not_an_administrator')
199 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]