Add some more social features
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout 'site', :except => :rss
3
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]
10
11   caches_action :list, :view, :layout => false
12   caches_action :rss, :layout => true
13   cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment]
14
15   def new
16     @title = t 'diary_entry.new.title'
17
18     if params[:diary_entry]
19       @diary_entry = DiaryEntry.new(params[:diary_entry])
20       @diary_entry.user = @user
21
22       if @diary_entry.save
23         default_lang = @user.preferences.where(:k => "diary.default_language").first
24         if default_lang
25           default_lang.v = @diary_entry.language_code
26           default_lang.save!
27         else
28           @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
29         end
30         redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name 
31       else
32         render :action => 'edit'
33       end
34     else
35       default_lang = @user.preferences.where(:k => "diary.default_language").first
36       lang_code = default_lang ? default_lang.v : @user.preferred_language
37       @diary_entry = DiaryEntry.new(:language_code => lang_code)
38       render :action => 'edit'
39     end
40   end
41
42   def edit
43     @title= t 'diary_entry.edit.title'
44     @diary_entry = DiaryEntry.find(params[:id])
45
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]
51       end
52     end
53   rescue ActiveRecord::RecordNotFound
54     render :action => "no_such_entry", :status => :not_found
55   end
56
57   def comment
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.diary_comment_notification(@diary_comment).deliver
64       end
65
66       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
67     else
68       render :action => 'view'
69     end
70   end
71
72   def list
73     if params[:display_name]
74       @this_user = User.active.find_by_display_name(params[:display_name])
75
76       if @this_user
77         @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
78         @entry_pages, @entries = paginate(:diary_entries,
79                                           :conditions => { 
80                                             :user_id => @this_user.id,
81                                             :visible => true 
82                                           },
83                                           :order => 'created_at DESC',
84                                           :per_page => 20)
85       else
86         @title = t'diary_entry.no_such_user.title'
87         @not_found_user = params[:display_name]
88
89         render :action => 'no_such_user', :status => :not_found
90       end
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,
94                                         :conditions => {
95                                           :users => { :status => ["active", "confirmed"] },
96                                           :visible => true,
97                                           :language_code => params[:language]
98                                         },
99                                         :order => 'created_at DESC',
100                                         :per_page => 20)
101     elsif params[:friends]
102       if @user
103         @title = t 'diary_entry.list.title_friends'
104         @entry_pages, @entries = paginate(:diary_entries, :include => :user,
105                                           :conditions => {
106                                             :user_id => @user.friend_users.public,
107                                             :visible => true
108                                           },
109                                           :order => 'created_at DESC',
110                                           :per_page => 20)
111       else
112           require_user
113           return     
114       end
115     elsif params[:nearby]
116       if @user
117         @title = t 'diary_entry.list.title_nearby'
118         @entry_pages, @entries = paginate(:diary_entries, :include => :user,
119                                           :conditions => {
120                                             :user_id => @user.nearby,
121                                             :visible => true
122                                           },
123                                           :order => 'created_at DESC',
124                                           :per_page => 20)                                        
125       else
126           require_user
127           return     
128       end                                  
129     else
130       @title = t 'diary_entry.list.title'
131       @entry_pages, @entries = paginate(:diary_entries, :include => :user,
132                                         :conditions => {
133                                           :users => { :status => ["active", "confirmed"] },
134                                           :visible => true
135                                         },
136                                         :order => 'created_at DESC',
137                                         :per_page => 20)
138     end
139   end
140
141   def rss
142     @entries = DiaryEntry.includes(:user).order("created_at DESC").limit(20)
143
144     if params[:display_name]
145       user = User.active.find_by_display_name(params[:display_name])
146
147       if user
148         @entries = user.diary_entries.visible
149         @title = I18n.t('diary_entry.feed.user.title', :user => user.display_name)
150         @description = I18n.t('diary_entry.feed.user.description', :user => user.display_name)
151         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
152       else
153         render :nothing => true, :status => :not_found
154       end
155     elsif params[:language]
156       @entries = @entries.visible.where(:language_code => params[:language]).joins(:user).where(:users => { :status => ["active", "confirmed"] })
157       @title = I18n.t('diary_entry.feed.language.title', :language_name => Language.find(params[:language]).english_name)
158       @description = I18n.t('diary_entry.feed.language.description', :language_name => Language.find(params[:language]).english_name)
159       @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
160     else
161       @entries = @entries.visible.joins(:user).where(:users => { :status => ["active", "confirmed"] })
162       @title = I18n.t('diary_entry.feed.all.title')
163       @description = I18n.t('diary_entry.feed.all.description')
164       @link = "http://#{SERVER_URL}/diary"
165     end
166   end
167
168   def view
169     user = User.active.find_by_display_name(params[:display_name])
170
171     if user
172       @entry = user.diary_entries.visible.where(:id => params[:id]).first
173       if @entry
174         @title = t 'diary_entry.view.title', :user => params[:display_name], :title => @entry.title
175       else
176         @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
177         render :action => 'no_such_entry', :status => :not_found
178       end
179     else
180       @not_found_user = params[:display_name]
181
182       render :action => 'no_such_user', :status => :not_found
183     end
184   end
185
186   def hide
187     entry = DiaryEntry.find(params[:id])
188     entry.update_attributes(:visible => false)
189     redirect_to :action => "list", :display_name => entry.user.display_name
190   end
191
192   def hidecomment
193     comment = DiaryComment.find(params[:comment])
194     comment.update_attributes(:visible => false)
195     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
196   end
197 private
198   ##
199   # require that the user is a administrator, or fill out a helpful error message
200   # and return them to the user page.
201   def require_administrator
202     unless @user.administrator?
203       flash[:error] = t('user.filter.not_an_administrator')
204       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => params[:id]
205     end
206   end
207 end