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 Notifier::deliver_diary_comment_notification(@diary_comment)
58 redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
60 render :action => 'view'
65 if params[:display_name]
66 @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
69 @title = t 'diary_entry.list.user_title', :user => @this_user.display_name
70 @entry_pages, @entries = paginate(:diary_entries,
71 :conditions => ['user_id = ?', @this_user.id],
72 :order => 'created_at DESC',
75 @title = t'diary_entry.no_such_user.title'
76 @not_found_user = params[:display_name]
78 render :action => 'no_such_user', :status => :not_found
80 elsif params[:language]
81 @title = t 'diary_entry.list.in_language_title', :language => Language.find(params[:language]).english_name
82 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
83 :conditions => ["users.visible = ? AND diary_entries.language_code = ?", true, params[:language]],
84 :order => 'created_at DESC',
87 @title = t 'diary_entry.list.title'
88 @entry_pages, @entries = paginate(:diary_entries, :include => :user,
89 :conditions => ["users.visible = ?", true],
90 :order => 'created_at DESC',
98 if params[:display_name]
99 user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
102 @entries = DiaryEntry.find(:all, :conditions => ['user_id = ?', user.id], :order => 'created_at DESC', :limit => 20)
103 @title = "OpenStreetMap diary entries for #{user.display_name}"
104 @description = "Recent OpenStreetmap diary entries from #{user.display_name}"
105 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
107 render :nothing => true, :status => :not_found
109 elsif params[:language]
110 @entries = DiaryEntry.find(:all, :include => :user,
111 :conditions => ["users.visible = ? AND diary_entries.language_code = ?", true, params[:language]],
112 :order => 'created_at DESC', :limit => 20)
113 @title = "OpenStreetMap diary entries in #{Language.find(params[:language]).english_name}"
114 @description = "Recent diary entries from users of OpenStreetMap in #{Language.find(params[:language]).english_name}"
115 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
117 @entries = DiaryEntry.find(:all, :include => :user,
118 :conditions => ["users.visible = ?", true],
119 :order => 'created_at DESC', :limit => 20)
120 @title = "OpenStreetMap diary entries"
121 @description = "Recent diary entries from users of OpenStreetMap"
122 @link = "http://#{SERVER_URL}/diary"
127 user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
130 @entry = DiaryEntry.find(:first, :conditions => ['user_id = ? AND id = ?', user.id, params[:id]])
132 @title = t 'diary_entry.view.title', :user => params[:display_name]
134 render :action => 'no_such_entry', :status => :not_found
137 @not_found_user = params[:display_name]
139 render :action => 'no_such_user', :status => :not_found