1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class WatchersController < ApplicationController
19 before_filter :find_project
20 before_filter :require_login, :check_project_privacy, :only => [:watch, :unwatch]
21 before_filter :authorize, :only => [:new, :destroy]
22
23 def watch
24 if @watched.respond_to?(:visible?) && !@watched.visible?(User.current)
25 render_403
26 else
27 set_watcher(User.current, true)
28 end
29 end
30
31 def unwatch
32 set_watcher(User.current, false)
33 end
34
35 def new
36 respond_to do |format|
37 format.js do
38 render :update do |page|
39 page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched}
40 page << "showModal('ajax-modal', '400px');"
41 page << "$('ajax-modal').addClassName('new-watcher');"
42 end
43 end
44 end
45 end
46
47 def create
48 if params[:watcher].is_a?(Hash) && request.post?
49 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
50 user_ids.each do |user_id|
51 Watcher.create(:watchable => @watched, :user_id => user_id)
52 end
53 end
54 respond_to do |format|
55 format.html { redirect_to :back }
56 format.js do
57 render :update do |page|
58 page.replace_html 'ajax-modal', :partial => 'watchers/new', :locals => {:watched => @watched}
59 page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
60 end
61 end
62 end
63 rescue ::ActionController::RedirectBackError
64 render :text => 'Watcher added.', :layout => true
65 end
66
67 def append
68 if params[:watcher].is_a?(Hash)
69 user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
70 users = User.active.find_all_by_id(user_ids)
71 respond_to do |format|
72 format.js do
73 render :update do |page|
74 users.each do |user|
75 page.select("#issue_watcher_user_ids_#{user.id}").each do |item|
76 page.remove item
77 end
78 end
79 page.insert_html :bottom, 'watchers_inputs', :text => watchers_checkboxes(nil, users, true)
80 end
81 end
82 end
83 end
84 end
85
86 def destroy
87 @watched.set_watcher(User.find(params[:user_id]), false) if request.post?
88 respond_to do |format|
89 format.html { redirect_to :back }
90 format.js do
91 render :update do |page|
92 page.replace_html 'watchers', :partial => 'watchers/watchers', :locals => {:watched => @watched}
93 end
94 end
95 end
96 end
97
98 def autocomplete_for_user
99 @users = User.active.like(params[:q]).find(:all, :limit => 100)
100 if @watched
101 @users -= @watched.watcher_users
102 end
103 render :layout => false
104 end
105
106 private
107 def find_project
108 if params[:object_type] && params[:object_id]
109 klass = Object.const_get(params[:object_type].camelcase)
110 return false unless klass.respond_to?('watched_by')
111 @watched = klass.find(params[:object_id])
112 @project = @watched.project
113 elsif params[:project_id]
114 @project = Project.visible.find(params[:project_id])
115 end
116 rescue
117 render_404
118 end
119
120 def set_watcher(user, watching)
121 @watched.set_watcher(user, watching)
122 respond_to do |format|
123 format.html { redirect_to :back }
124 format.js do
125 render(:update) do |page|
126 c = watcher_css(@watched)
127 page.select(".#{c}").each do |item|
128 page.replace_html item, watcher_link(@watched, user)
129 end
130 end
131 end
132 end
133 rescue ::ActionController::RedirectBackError
134 render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true
135 end
136 end