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]
7 before_filter :check_database_readable
8 before_filter :check_database_writable, :only => [:new, :edit]
11 @title = t 'diary_entry.new.title'
13 if params[:diary_entry]
14 @diary_entry = DiaryEntry.new(params[:diary_entry])
15 @diary_entry.user = @user
18 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
20 default_lang.v = @diary_entry.language_code
23 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
25 redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name
27 render :action => 'edit'
30 default_lang = @user.preferences.find(:first, :conditions => {:k => "diary.default_language"})
31 lang_code = default_lang ? default_lang.v : @user.preferred_language
32 @diary_entry = DiaryEntry.new(:language_code => lang_code)
33 render :action => 'edit'
38 @title= t 'diary_entry.edit.title'
39 @diary_entry = DiaryEntry.find(params[:id])
41 if @user != @diary_entry.user
42 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
43 elsif params[:diary_entry]
44 if @diary_entry.update_attributes(params[:diary_entry])
45 redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
48 rescue ActiveRecord::RecordNotFound
49 render :action => "no_such_entry", :status => :not_found
53 @entry = DiaryEntry.find(params[:id])
54 @diary_comment = @entry.diary_comments.build(params[:diary_comment])
55 @diary_comment.user = @user
56 if @diary_comment.save
57 if @diary_comment.user != @entry.user
58 Notifier::deliver_diary_comment_notification(@diary_comment)
61 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
63 render :action => 'view'
68 if params[:display_name]
69 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
72 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
73 @entry_pages, @entries = paginate(:diary_entries,
74 :conditions => ['user_id = ?', @this_user.id],
75 :order => 'created_at DESC',
78 @title = t'diary_entry.no_such_user.title'
79 @not_found_user = params[:display_name]
81 render :action => 'no_such_user', :status => :not_found
83 elsif params[:language]
84 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
85 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
86 :conditions => ["users.visible = ? AND diary_entries.language_code = ?", true, params[:language]],
87 :order => 'created_at DESC',
90 @title = t 'diary_entry.list.title'
91 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
92 :conditions => ["users.visible = ?", true],
93 :order => 'created_at DESC',
101 if params[:display_name]
102 user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
105 @entries = DiaryEntry.find(:all, :conditions => ['user_id = ?', user.id], :order => 'created_at DESC', :limit => 20)
106 @title = "OpenStreetMap diary entries for #{user.display_name}"
107 @description = "Recent OpenStreetmap diary entries from #{user.display_name}"
108 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
110 render :nothing => true, :status => :not_found
112 elsif params[:language]
113 @entries = DiaryEntry.find(:all, :include => :user,
114 :conditions => ["users.visible = ? AND diary_entries.language_code = ?", true, params[:language]],
115 :order => 'created_at DESC', :limit => 20)
116 @title = "OpenStreetMap diary entries in #{Language.find(params[:language]).english_name}"
117 @description = "Recent diary entries from users of OpenStreetMap in #{Language.find(params[:language]).english_name}"
118 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
120 @entries = DiaryEntry.find(:all, :include => :user,
121 :conditions => ["users.visible = ?", true],
122 :order => 'created_at DESC', :limit => 20)
123 @title = "OpenStreetMap diary entries"
124 @description = "Recent diary entries from users of OpenStreetMap"
125 @link = "http://#{SERVER_URL}/diary"
130 user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
133 @entry = DiaryEntry.find(:first, :conditions => ['user_id = ? AND id = ?', user.id, params[:id]])
135 @title = t 'diary_entry.view.title', :user => params[:display_name]
137 @title = t 'diary_entry.no_such_entry.title', :id => params[:id]
138 render :action => 'no_such_entry', :status => :not_found
141 @not_found_user = params[:display_name]
143 render :action => 'no_such_user', :status => :not_found