]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entry_controller.rb
bit more putway stuff
[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 :require_user, :only => [:new, :edit]
6   before_filter :check_database_availability
7
8   def new
9     @title = 'New diary entry'
10
11     if params[:diary_entry]     
12       @diary_entry = DiaryEntry.new(params[:diary_entry])
13       @diary_entry.user = @user
14
15       if @diary_entry.save 
16         redirect_to :controller => 'diary_entry', :action => 'list', :display_name => @user.display_name 
17       else
18         render :action => 'edit'
19       end
20     else
21       render :action => 'edit'
22     end
23   end
24
25   def edit
26     @title= 'Edit diary entry'
27     @diary_entry = DiaryEntry.find(params[:id])
28
29     if @user != @diary_entry.user
30       redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
31     elsif params[:diary_entry]
32       @diary_entry.title = params[:diary_entry][:title]
33       @diary_entry.body = params[:diary_entry][:body]
34       @diary_entry.latitude = params[:diary_entry][:latitude]
35       @diary_entry.longitude = params[:diary_entry][:longitude]
36
37       if @diary_entry.save
38         redirect_to :controller => 'diary_entry', :action => 'view', :id => params[:id]
39       end
40     end
41   rescue ActiveRecord::RecordNotFound
42     render :action => "no_such_entry", :status => :not_found
43   end
44
45   def comment
46     @entry = DiaryEntry.find(params[:id])
47     @diary_comment = @entry.diary_comments.build(params[:diary_comment])
48     @diary_comment.user = @user
49     if @diary_comment.save
50       Notifier::deliver_diary_comment_notification(@diary_comment)
51       redirect_to :controller => 'diary_entry', :action => 'view', :display_name => @entry.user.display_name, :id => @entry.id
52     else
53       render :action => 'view'
54     end
55   end
56
57   def list
58     if params[:display_name]
59       @this_user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
60
61       if @this_user
62         @title = @this_user.display_name + "'s diary"
63         @entry_pages, @entries = paginate(:diary_entries,
64                                           :conditions => ['user_id = ?', @this_user.id],
65                                           :order => 'created_at DESC',
66                                           :per_page => 20)
67       else
68         @not_found_user = params[:display_name]
69
70         render :action => 'no_such_user', :status => :not_found
71       end
72     else
73       @title = "Users' diaries"
74       @entry_pages, @entries = paginate(:diary_entries, :include => :user,
75                                         :conditions => ["users.visible = ?", true],
76                                         :order => 'created_at DESC',
77                                         :per_page => 20)
78     end
79   end
80
81   def rss
82     if params[:display_name]
83       user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
84
85       if user
86         @entries = DiaryEntry.find(:all, :conditions => ['user_id = ?', user.id], :order => 'created_at DESC', :limit => 20)
87         @title = "OpenStreetMap diary entries for #{user.display_name}"
88         @description = "Recent OpenStreetmap diary entries from #{user.display_name}"
89         @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
90
91         render :content_type => Mime::RSS
92       else
93         render :nothing => true, :status => :not_found
94       end
95     else
96       @entries = DiaryEntry.find(:all, :include => :user,
97                                  :conditions => ["users.visible = ?", true],
98                                  :order => 'created_at DESC', :limit => 20)
99       @title = "OpenStreetMap diary entries"
100       @description = "Recent diary entries from users of OpenStreetMap"
101       @link = "http://#{SERVER_URL}/diary"
102
103       render :content_type => Mime::RSS
104     end
105   end
106
107   def view
108     user = User.find_by_display_name(params[:display_name], :conditions => {:visible => true})
109
110     if user
111       @entry = DiaryEntry.find(:first, :conditions => ['user_id = ? AND id = ?', user.id, params[:id]])
112       @title = "Users' diaries | #{params[:display_name]}"
113     else
114       @not_found_user = params[:display_name]
115
116       render :action => 'no_such_user', :status => :not_found
117     end
118   end
119 end