]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
Made rubocop happy by formatting and minor syntax tweaks.
[rails.git] / app / controllers / diary_entry_controller.rb
1 class DiaryEntryController < ApplicationController
2   layout "site", :except => :rss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment]
7   before_action :lookup_this_user, :only => [:view, :comments]
8   before_action :check_database_readable
9   before_action :check_database_writable, :only => [:new, :edit]
10   before_action :require_administrator, :only => [:hide, :hidecomment]
11
12   def new
13     @title = t "diary_entry.new.title"
14
15     if params[:diary_entry]
16       @diary_entry = DiaryEntry.new(entry_params)
17       @diary_entry.user = @user
18
19       if @diary_entry.save
20         default_lang = @user.preferences.where(:k => "diary.default_language").first
21         if default_lang
22           default_lang.v = @diary_entry.language_code
23           default_lang.save!
24         else
25           @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
26         end
27         redirect_to :controller => "diary_entry", :action => "list", :display_name => @user.display_name
28       else
29         render :action => "edit"
30       end
31     else
32       default_lang = @user.preferences.where(:k => "diary.default_language").first
33       lang_code = default_lang ? default_lang.v : @user.preferred_language
34       @diary_entry = DiaryEntry.new(:language_code => lang_code)
35       set_map_location
36       render :action => "edit"
37     end
38   end
39
40   def edit
41     @title = t "diary_entry.edit.title"
42     @diary_entry = DiaryEntry.find(params[:id])
43
44     if @user != @diary_entry.user
45       redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
46     elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
47       redirect_to :controller => "diary_entry", :action => "view", :id => params[:id]
48     end
49
50     set_map_location
51   rescue ActiveRecord::RecordNotFound
52     render :action => "no_such_entry", :status => :not_found
53   end
54
55   def comment
56     @entry = DiaryEntry.find(params[:id])
57     @diary_comment = @entry.comments.build(comment_params)
58     @diary_comment.user = @user
59     if @diary_comment.save
60       if @diary_comment.user != @entry.user
61         Notifier.diary_comment_notification(@diary_comment).deliver_now
62       end
63
64       redirect_to :controller => "diary_entry", :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
65     else
66       render :action => "view"
67     end
68   rescue ActiveRecord::RecordNotFound
69     render :action => "no_such_entry", :status => :not_found
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         @entries = @this_user.diary_entries
79       else
80         render_unknown_user params[:display_name]
81         return
82       end
83     elsif params[:friends]
84       if @user
85         @title = t "diary_entry.list.title_friends"
86         @entries = DiaryEntry.where(:user_id => @user.friend_users)
87       else
88         require_user
89         return
90       end
91     elsif params[:nearby]
92       if @user
93         @title = t "diary_entry.list.title_nearby"
94         @entries = DiaryEntry.where(:user_id => @user.nearby)
95       else
96         require_user
97         return
98       end
99     else
100       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
101
102       if params[:language]
103         @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
104         @entries = @entries.where(:language_code => params[:language])
105       else
106         @title = t "diary_entry.list.title"
107       end
108     end
109
110     @page = (params[:page] || 1).to_i
111     @page_size = 20
112
113     @entries = @entries.visible
114     @entries = @entries.order("created_at DESC")
115     @entries = @entries.offset((@page - 1) * @page_size)
116     @entries = @entries.limit(@page_size)
117     @entries = @entries.includes(:user, :language)
118   end
119
120   def rss
121     if params[:display_name]
122       user = User.active.find_by_display_name(params[:display_name])
123
124       if user
125         @entries = user.diary_entries
126         @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
127         @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
128         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
129       else
130         render :text => "", :status => :not_found
131         return
132       end
133     else
134       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
135
136       if params[:language]
137         @entries = @entries.where(:language_code => params[:language])
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]}"
141       else
142         @title = I18n.t("diary_entry.feed.all.title")
143         @description = I18n.t("diary_entry.feed.all.description")
144         @link = "http://#{SERVER_URL}/diary"
145       end
146     end
147
148     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
149   end
150
151   def view
152     @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
153     if @entry
154       @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
155       if params[:comment_id]
156         @reported_comment = DiaryComment.where(:id => params[:comment_id])
157       end
158     else
159       @title = t "diary_entry.no_such_entry.title", :id => params[:id]
160       render :action => "no_such_entry", :status => :not_found
161     end
162   end
163
164   def hide
165     entry = DiaryEntry.find(params[:id])
166     entry.update_attributes(:visible => false)
167     redirect_to :action => "list", :display_name => entry.user.display_name
168   end
169
170   def hidecomment
171     comment = DiaryComment.find(params[:comment])
172     comment.update_attributes(:visible => false)
173     redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
174   end
175
176   def comments
177     @comment_pages, @comments = paginate(:diary_comments,
178                                          :conditions => {
179                                            :user_id => @this_user,
180                                            :visible => true
181                                          },
182                                          :order => "created_at DESC",
183                                          :per_page => 20)
184     @page = (params[:page] || 1).to_i
185   end
186
187   private
188
189   ##
190   # return permitted diary entry parameters
191   def entry_params
192     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
193   end
194
195   ##
196   # return permitted diary comment parameters
197   def comment_params
198     params.require(:diary_comment).permit(:body)
199   end
200
201   ##
202   # require that the user is a administrator, or fill out a helpful error message
203   # and return them to the user page.
204   def require_administrator
205     unless @user.administrator?
206       flash[:error] = t("user.filter.not_an_administrator")
207       redirect_to :controller => "diary_entry", :action => "view"
208     end
209   end
210
211   ##
212   # decide on a location for the diary entry map
213   def set_map_location
214     if @diary_entry.latitude && @diary_entry.longitude
215       @lon = @diary_entry.longitude
216       @lat = @diary_entry.latitude
217       @zoom = 12
218     elsif @user.home_lat.nil? || @user.home_lon.nil?
219       @lon = params[:lon] || -0.1
220       @lat = params[:lat] || 51.5
221       @zoom = params[:zoom] || 4
222     else
223       @lon = @user.home_lon
224       @lat = @user.home_lat
225       @zoom = 12
226     end
227   end
228 end