@@ -1,332 +1,341 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require 'uri' |
|
18 | require 'uri' | |
19 | require 'cgi' |
|
19 | require 'cgi' | |
20 |
|
20 | |||
21 | class ApplicationController < ActionController::Base |
|
21 | class ApplicationController < ActionController::Base | |
22 | include Redmine::I18n |
|
22 | include Redmine::I18n | |
23 |
|
23 | |||
24 | layout 'base' |
|
24 | layout 'base' | |
25 | exempt_from_layout 'builder' |
|
25 | exempt_from_layout 'builder' | |
26 |
|
26 | |||
27 | # Remove broken cookie after upgrade from 0.8.x (#4292) |
|
27 | # Remove broken cookie after upgrade from 0.8.x (#4292) | |
28 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 |
|
28 | # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 | |
29 | # TODO: remove it when Rails is fixed |
|
29 | # TODO: remove it when Rails is fixed | |
30 | before_filter :delete_broken_cookies |
|
30 | before_filter :delete_broken_cookies | |
31 | def delete_broken_cookies |
|
31 | def delete_broken_cookies | |
32 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ |
|
32 | if cookies['_redmine_session'] && cookies['_redmine_session'] !~ /--/ | |
33 | cookies.delete '_redmine_session' |
|
33 | cookies.delete '_redmine_session' | |
34 | redirect_to home_path |
|
34 | redirect_to home_path | |
35 | return false |
|
35 | return false | |
36 | end |
|
36 | end | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | before_filter :user_setup, :check_if_login_required, :set_localization |
|
39 | before_filter :user_setup, :check_if_login_required, :set_localization | |
40 | filter_parameter_logging :password |
|
40 | filter_parameter_logging :password | |
41 | protect_from_forgery |
|
41 | protect_from_forgery | |
42 |
|
42 | |||
43 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token |
|
43 | rescue_from ActionController::InvalidAuthenticityToken, :with => :invalid_authenticity_token | |
44 |
|
44 | |||
45 | include Redmine::Search::Controller |
|
45 | include Redmine::Search::Controller | |
46 | include Redmine::MenuManager::MenuController |
|
46 | include Redmine::MenuManager::MenuController | |
47 | helper Redmine::MenuManager::MenuHelper |
|
47 | helper Redmine::MenuManager::MenuHelper | |
48 |
|
48 | |||
49 | Redmine::Scm::Base.all.each do |scm| |
|
49 | Redmine::Scm::Base.all.each do |scm| | |
50 | require_dependency "repository/#{scm.underscore}" |
|
50 | require_dependency "repository/#{scm.underscore}" | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def user_setup |
|
53 | def user_setup | |
54 | # Check the settings cache for each request |
|
54 | # Check the settings cache for each request | |
55 | Setting.check_cache |
|
55 | Setting.check_cache | |
56 | # Find the current user |
|
56 | # Find the current user | |
57 | User.current = find_current_user |
|
57 | User.current = find_current_user | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | # Returns the current user or nil if no user is logged in |
|
60 | # Returns the current user or nil if no user is logged in | |
61 | # and starts a session if needed |
|
61 | # and starts a session if needed | |
62 | def find_current_user |
|
62 | def find_current_user | |
63 | if session[:user_id] |
|
63 | if session[:user_id] | |
64 | # existing session |
|
64 | # existing session | |
65 | (User.active.find(session[:user_id]) rescue nil) |
|
65 | (User.active.find(session[:user_id]) rescue nil) | |
66 | elsif cookies[:autologin] && Setting.autologin? |
|
66 | elsif cookies[:autologin] && Setting.autologin? | |
67 | # auto-login feature starts a new session |
|
67 | # auto-login feature starts a new session | |
68 | user = User.try_to_autologin(cookies[:autologin]) |
|
68 | user = User.try_to_autologin(cookies[:autologin]) | |
69 | session[:user_id] = user.id if user |
|
69 | session[:user_id] = user.id if user | |
70 | user |
|
70 | user | |
71 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) |
|
71 | elsif params[:format] == 'atom' && params[:key] && accept_key_auth_actions.include?(params[:action]) | |
72 | # RSS key authentication does not start a session |
|
72 | # RSS key authentication does not start a session | |
73 | User.find_by_rss_key(params[:key]) |
|
73 | User.find_by_rss_key(params[:key]) | |
74 | elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) |
|
74 | elsif Setting.rest_api_enabled? && ['xml', 'json'].include?(params[:format]) | |
75 | if params[:key].present? && accept_key_auth_actions.include?(params[:action]) |
|
75 | if params[:key].present? && accept_key_auth_actions.include?(params[:action]) | |
76 | # Use API key |
|
76 | # Use API key | |
77 | User.find_by_api_key(params[:key]) |
|
77 | User.find_by_api_key(params[:key]) | |
78 | else |
|
78 | else | |
79 | # HTTP Basic, either username/password or API key/random |
|
79 | # HTTP Basic, either username/password or API key/random | |
80 | authenticate_with_http_basic do |username, password| |
|
80 | authenticate_with_http_basic do |username, password| | |
81 | User.try_to_login(username, password) || User.find_by_api_key(username) |
|
81 | User.try_to_login(username, password) || User.find_by_api_key(username) | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 | end |
|
84 | end | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | # Sets the logged in user |
|
87 | # Sets the logged in user | |
88 | def logged_user=(user) |
|
88 | def logged_user=(user) | |
89 | reset_session |
|
89 | reset_session | |
90 | if user && user.is_a?(User) |
|
90 | if user && user.is_a?(User) | |
91 | User.current = user |
|
91 | User.current = user | |
92 | session[:user_id] = user.id |
|
92 | session[:user_id] = user.id | |
93 | else |
|
93 | else | |
94 | User.current = User.anonymous |
|
94 | User.current = User.anonymous | |
95 | end |
|
95 | end | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | # check if login is globally required to access the application |
|
98 | # check if login is globally required to access the application | |
99 | def check_if_login_required |
|
99 | def check_if_login_required | |
100 | # no check needed if user is already logged in |
|
100 | # no check needed if user is already logged in | |
101 | return true if User.current.logged? |
|
101 | return true if User.current.logged? | |
102 | require_login if Setting.login_required? |
|
102 | require_login if Setting.login_required? | |
103 | end |
|
103 | end | |
104 |
|
104 | |||
105 | def set_localization |
|
105 | def set_localization | |
106 | lang = nil |
|
106 | lang = nil | |
107 | if User.current.logged? |
|
107 | if User.current.logged? | |
108 | lang = find_language(User.current.language) |
|
108 | lang = find_language(User.current.language) | |
109 | end |
|
109 | end | |
110 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] |
|
110 | if lang.nil? && request.env['HTTP_ACCEPT_LANGUAGE'] | |
111 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase |
|
111 | accept_lang = parse_qvalues(request.env['HTTP_ACCEPT_LANGUAGE']).first.downcase | |
112 | if !accept_lang.blank? |
|
112 | if !accept_lang.blank? | |
113 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) |
|
113 | lang = find_language(accept_lang) || find_language(accept_lang.split('-').first) | |
114 | end |
|
114 | end | |
115 | end |
|
115 | end | |
116 | lang ||= Setting.default_language |
|
116 | lang ||= Setting.default_language | |
117 | set_language_if_valid(lang) |
|
117 | set_language_if_valid(lang) | |
118 | end |
|
118 | end | |
119 |
|
119 | |||
120 | def require_login |
|
120 | def require_login | |
121 | if !User.current.logged? |
|
121 | if !User.current.logged? | |
122 | # Extract only the basic url parameters on non-GET requests |
|
122 | # Extract only the basic url parameters on non-GET requests | |
123 | if request.get? |
|
123 | if request.get? | |
124 | url = url_for(params) |
|
124 | url = url_for(params) | |
125 | else |
|
125 | else | |
126 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) |
|
126 | url = url_for(:controller => params[:controller], :action => params[:action], :id => params[:id], :project_id => params[:project_id]) | |
127 | end |
|
127 | end | |
128 | respond_to do |format| |
|
128 | respond_to do |format| | |
129 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
129 | format.html { redirect_to :controller => "account", :action => "login", :back_url => url } | |
130 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } |
|
130 | format.atom { redirect_to :controller => "account", :action => "login", :back_url => url } | |
131 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
131 | format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
132 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } |
|
132 | format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } | |
133 | end |
|
133 | end | |
134 | return false |
|
134 | return false | |
135 | end |
|
135 | end | |
136 | true |
|
136 | true | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | def require_admin |
|
139 | def require_admin | |
140 | return unless require_login |
|
140 | return unless require_login | |
141 | if !User.current.admin? |
|
141 | if !User.current.admin? | |
142 | render_403 |
|
142 | render_403 | |
143 | return false |
|
143 | return false | |
144 | end |
|
144 | end | |
145 | true |
|
145 | true | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | def deny_access |
|
148 | def deny_access | |
149 | User.current.logged? ? render_403 : require_login |
|
149 | User.current.logged? ? render_403 : require_login | |
150 | end |
|
150 | end | |
151 |
|
151 | |||
152 | # Authorize the user for the requested action |
|
152 | # Authorize the user for the requested action | |
153 | def authorize(ctrl = params[:controller], action = params[:action], global = false) |
|
153 | def authorize(ctrl = params[:controller], action = params[:action], global = false) | |
154 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) |
|
154 | allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project, :global => global) | |
155 | allowed ? true : deny_access |
|
155 | allowed ? true : deny_access | |
156 | end |
|
156 | end | |
157 |
|
157 | |||
158 | # Authorize the user for the requested action outside a project |
|
158 | # Authorize the user for the requested action outside a project | |
159 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) |
|
159 | def authorize_global(ctrl = params[:controller], action = params[:action], global = true) | |
160 | authorize(ctrl, action, global) |
|
160 | authorize(ctrl, action, global) | |
161 | end |
|
161 | end | |
162 |
|
162 | |||
163 | # Find project of id params[:id] |
|
163 | # Find project of id params[:id] | |
164 | def find_project |
|
164 | def find_project | |
165 | @project = Project.find(params[:id]) |
|
165 | @project = Project.find(params[:id]) | |
166 | rescue ActiveRecord::RecordNotFound |
|
166 | rescue ActiveRecord::RecordNotFound | |
167 | render_404 |
|
167 | render_404 | |
168 | end |
|
168 | end | |
169 |
|
169 | |||
170 | # Finds and sets @project based on @object.project |
|
170 | # Finds and sets @project based on @object.project | |
171 | def find_project_from_association |
|
171 | def find_project_from_association | |
172 | render_404 unless @object.present? |
|
172 | render_404 unless @object.present? | |
173 |
|
173 | |||
174 | @project = @object.project |
|
174 | @project = @object.project | |
175 | rescue ActiveRecord::RecordNotFound |
|
175 | rescue ActiveRecord::RecordNotFound | |
176 | render_404 |
|
176 | render_404 | |
177 | end |
|
177 | end | |
178 |
|
178 | |||
179 | def find_model_object |
|
179 | def find_model_object | |
180 | model = self.class.read_inheritable_attribute('model_object') |
|
180 | model = self.class.read_inheritable_attribute('model_object') | |
181 | if model |
|
181 | if model | |
182 | @object = model.find(params[:id]) |
|
182 | @object = model.find(params[:id]) | |
183 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object |
|
183 | self.instance_variable_set('@' + controller_name.singularize, @object) if @object | |
184 | end |
|
184 | end | |
185 | rescue ActiveRecord::RecordNotFound |
|
185 | rescue ActiveRecord::RecordNotFound | |
186 | render_404 |
|
186 | render_404 | |
187 | end |
|
187 | end | |
188 |
|
188 | |||
189 | def self.model_object(model) |
|
189 | def self.model_object(model) | |
190 | write_inheritable_attribute('model_object', model) |
|
190 | write_inheritable_attribute('model_object', model) | |
191 | end |
|
191 | end | |
192 |
|
192 | |||
193 | # make sure that the user is a member of the project (or admin) if project is private |
|
193 | # make sure that the user is a member of the project (or admin) if project is private | |
194 | # used as a before_filter for actions that do not require any particular permission on the project |
|
194 | # used as a before_filter for actions that do not require any particular permission on the project | |
195 | def check_project_privacy |
|
195 | def check_project_privacy | |
196 | if @project && @project.active? |
|
196 | if @project && @project.active? | |
197 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? |
|
197 | if @project.is_public? || User.current.member_of?(@project) || User.current.admin? | |
198 | true |
|
198 | true | |
199 | else |
|
199 | else | |
200 | User.current.logged? ? render_403 : require_login |
|
200 | User.current.logged? ? render_403 : require_login | |
201 | end |
|
201 | end | |
202 | else |
|
202 | else | |
203 | @project = nil |
|
203 | @project = nil | |
204 | render_404 |
|
204 | render_404 | |
205 | false |
|
205 | false | |
206 | end |
|
206 | end | |
207 | end |
|
207 | end | |
208 |
|
208 | |||
209 | def redirect_back_or_default(default) |
|
209 | def redirect_back_or_default(default) | |
210 | back_url = CGI.unescape(params[:back_url].to_s) |
|
210 | back_url = CGI.unescape(params[:back_url].to_s) | |
211 | if !back_url.blank? |
|
211 | if !back_url.blank? | |
212 | begin |
|
212 | begin | |
213 | uri = URI.parse(back_url) |
|
213 | uri = URI.parse(back_url) | |
214 | # do not redirect user to another host or to the login or register page |
|
214 | # do not redirect user to another host or to the login or register page | |
215 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) |
|
215 | if (uri.relative? || (uri.host == request.host)) && !uri.path.match(%r{/(login|account/register)}) | |
216 | redirect_to(back_url) |
|
216 | redirect_to(back_url) | |
217 | return |
|
217 | return | |
218 | end |
|
218 | end | |
219 | rescue URI::InvalidURIError |
|
219 | rescue URI::InvalidURIError | |
220 | # redirect to default |
|
220 | # redirect to default | |
221 | end |
|
221 | end | |
222 | end |
|
222 | end | |
223 | redirect_to default |
|
223 | redirect_to default | |
224 | end |
|
224 | end | |
225 |
|
225 | |||
226 | def render_403 |
|
226 | def render_403 | |
227 | @project = nil |
|
227 | @project = nil | |
228 | respond_to do |format| |
|
228 | respond_to do |format| | |
229 | format.html { render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403 } |
|
229 | format.html { render :template => "common/403", :layout => (request.xhr? ? false : 'base'), :status => 403 } | |
230 | format.atom { head 403 } |
|
230 | format.atom { head 403 } | |
231 | format.xml { head 403 } |
|
231 | format.xml { head 403 } | |
232 | format.json { head 403 } |
|
232 | format.json { head 403 } | |
233 | end |
|
233 | end | |
234 | return false |
|
234 | return false | |
235 | end |
|
235 | end | |
236 |
|
236 | |||
237 | def render_404 |
|
237 | def render_404 | |
238 | respond_to do |format| |
|
238 | respond_to do |format| | |
239 | format.html { render :template => "common/404", :layout => !request.xhr?, :status => 404 } |
|
239 | format.html { render :template => "common/404", :layout => !request.xhr?, :status => 404 } | |
240 | format.atom { head 404 } |
|
240 | format.atom { head 404 } | |
241 | format.xml { head 404 } |
|
241 | format.xml { head 404 } | |
242 | format.json { head 404 } |
|
242 | format.json { head 404 } | |
243 | end |
|
243 | end | |
244 | return false |
|
244 | return false | |
245 | end |
|
245 | end | |
246 |
|
246 | |||
247 | def render_error(msg) |
|
247 | def render_error(msg) | |
248 | respond_to do |format| |
|
248 | respond_to do |format| | |
249 | format.html { |
|
249 | format.html { | |
250 | flash.now[:error] = msg |
|
250 | flash.now[:error] = msg | |
251 | render :text => '', :layout => !request.xhr?, :status => 500 |
|
251 | render :text => '', :layout => !request.xhr?, :status => 500 | |
252 | } |
|
252 | } | |
253 | format.atom { head 500 } |
|
253 | format.atom { head 500 } | |
254 | format.xml { head 500 } |
|
254 | format.xml { head 500 } | |
255 | format.json { head 500 } |
|
255 | format.json { head 500 } | |
256 | end |
|
256 | end | |
257 | end |
|
257 | end | |
258 |
|
258 | |||
259 | def invalid_authenticity_token |
|
259 | def invalid_authenticity_token | |
260 | if api_request? |
|
260 | if api_request? | |
261 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." |
|
261 | logger.error "Form authenticity token is missing or is invalid. API calls must include a proper Content-type header (text/xml or text/json)." | |
262 | end |
|
262 | end | |
263 | render_error "Invalid form authenticity token." |
|
263 | render_error "Invalid form authenticity token." | |
264 | end |
|
264 | end | |
265 |
|
265 | |||
266 | def render_feed(items, options={}) |
|
266 | def render_feed(items, options={}) | |
267 | @items = items || [] |
|
267 | @items = items || [] | |
268 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } |
|
268 | @items.sort! {|x,y| y.event_datetime <=> x.event_datetime } | |
269 | @items = @items.slice(0, Setting.feeds_limit.to_i) |
|
269 | @items = @items.slice(0, Setting.feeds_limit.to_i) | |
270 | @title = options[:title] || Setting.app_title |
|
270 | @title = options[:title] || Setting.app_title | |
271 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' |
|
271 | render :template => "common/feed.atom.rxml", :layout => false, :content_type => 'application/atom+xml' | |
272 | end |
|
272 | end | |
273 |
|
273 | |||
274 | def self.accept_key_auth(*actions) |
|
274 | def self.accept_key_auth(*actions) | |
275 | actions = actions.flatten.map(&:to_s) |
|
275 | actions = actions.flatten.map(&:to_s) | |
276 | write_inheritable_attribute('accept_key_auth_actions', actions) |
|
276 | write_inheritable_attribute('accept_key_auth_actions', actions) | |
277 | end |
|
277 | end | |
278 |
|
278 | |||
279 | def accept_key_auth_actions |
|
279 | def accept_key_auth_actions | |
280 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] |
|
280 | self.class.read_inheritable_attribute('accept_key_auth_actions') || [] | |
281 | end |
|
281 | end | |
282 |
|
282 | |||
283 | # Returns the number of objects that should be displayed |
|
283 | # Returns the number of objects that should be displayed | |
284 | # on the paginated list |
|
284 | # on the paginated list | |
285 | def per_page_option |
|
285 | def per_page_option | |
286 | per_page = nil |
|
286 | per_page = nil | |
287 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) |
|
287 | if params[:per_page] && Setting.per_page_options_array.include?(params[:per_page].to_s.to_i) | |
288 | per_page = params[:per_page].to_s.to_i |
|
288 | per_page = params[:per_page].to_s.to_i | |
289 | session[:per_page] = per_page |
|
289 | session[:per_page] = per_page | |
290 | elsif session[:per_page] |
|
290 | elsif session[:per_page] | |
291 | per_page = session[:per_page] |
|
291 | per_page = session[:per_page] | |
292 | else |
|
292 | else | |
293 | per_page = Setting.per_page_options_array.first || 25 |
|
293 | per_page = Setting.per_page_options_array.first || 25 | |
294 | end |
|
294 | end | |
295 | per_page |
|
295 | per_page | |
296 | end |
|
296 | end | |
297 |
|
297 | |||
298 | # qvalues http header parser |
|
298 | # qvalues http header parser | |
299 | # code taken from webrick |
|
299 | # code taken from webrick | |
300 | def parse_qvalues(value) |
|
300 | def parse_qvalues(value) | |
301 | tmp = [] |
|
301 | tmp = [] | |
302 | if value |
|
302 | if value | |
303 | parts = value.split(/,\s*/) |
|
303 | parts = value.split(/,\s*/) | |
304 | parts.each {|part| |
|
304 | parts.each {|part| | |
305 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) |
|
305 | if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part) | |
306 | val = m[1] |
|
306 | val = m[1] | |
307 | q = (m[2] or 1).to_f |
|
307 | q = (m[2] or 1).to_f | |
308 | tmp.push([val, q]) |
|
308 | tmp.push([val, q]) | |
309 | end |
|
309 | end | |
310 | } |
|
310 | } | |
311 | tmp = tmp.sort_by{|val, q| -q} |
|
311 | tmp = tmp.sort_by{|val, q| -q} | |
312 | tmp.collect!{|val, q| val} |
|
312 | tmp.collect!{|val, q| val} | |
313 | end |
|
313 | end | |
314 | return tmp |
|
314 | return tmp | |
315 | rescue |
|
315 | rescue | |
316 | nil |
|
316 | nil | |
317 | end |
|
317 | end | |
318 |
|
318 | |||
319 | # Returns a string that can be used as filename value in Content-Disposition header |
|
319 | # Returns a string that can be used as filename value in Content-Disposition header | |
320 | def filename_for_content_disposition(name) |
|
320 | def filename_for_content_disposition(name) | |
321 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name |
|
321 | request.env['HTTP_USER_AGENT'] =~ %r{MSIE} ? ERB::Util.url_encode(name) : name | |
322 | end |
|
322 | end | |
323 |
|
323 | |||
324 | def api_request? |
|
324 | def api_request? | |
325 | %w(xml json).include? params[:format] |
|
325 | %w(xml json).include? params[:format] | |
326 | end |
|
326 | end | |
327 |
|
327 | |||
328 | # Renders a warning flash if obj has unsaved attachments |
|
328 | # Renders a warning flash if obj has unsaved attachments | |
329 | def render_attachment_warning_if_needed(obj) |
|
329 | def render_attachment_warning_if_needed(obj) | |
330 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? |
|
330 | flash[:warning] = l(:warning_attachments_not_saved, obj.unsaved_attachments.size) if obj.unsaved_attachments.present? | |
331 | end |
|
331 | end | |
|
332 | ||||
|
333 | # Rescues an invalid query statement. Just in case... | |||
|
334 | def query_statement_invalid(exception) | |||
|
335 | logger.error "Query::StatementInvalid: #{exception.message}" if logger | |||
|
336 | session.delete(:query) | |||
|
337 | sort_clear if respond_to?(:sort_clear) | |||
|
338 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." | |||
|
339 | end | |||
|
340 | ||||
332 | end |
|
341 | end |
@@ -1,63 +1,56 | |||||
1 | class GanttsController < ApplicationController |
|
1 | class GanttsController < ApplicationController | |
2 | before_filter :find_optional_project |
|
2 | before_filter :find_optional_project | |
3 |
|
3 | |||
4 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
4 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid | |
5 |
|
5 | |||
6 | helper :issues |
|
6 | helper :issues | |
7 | helper :projects |
|
7 | helper :projects | |
8 | helper :queries |
|
8 | helper :queries | |
9 | include QueriesHelper |
|
9 | include QueriesHelper | |
|
10 | helper :sort | |||
|
11 | include SortHelper | |||
10 | include Redmine::Export::PDF |
|
12 | include Redmine::Export::PDF | |
11 |
|
13 | |||
12 | def show |
|
14 | def show | |
13 | @gantt = Redmine::Helpers::Gantt.new(params) |
|
15 | @gantt = Redmine::Helpers::Gantt.new(params) | |
14 | retrieve_query |
|
16 | retrieve_query | |
15 | @query.group_by = nil |
|
17 | @query.group_by = nil | |
16 | if @query.valid? |
|
18 | if @query.valid? | |
17 | events = [] |
|
19 | events = [] | |
18 | # Issues that have start and due dates |
|
20 | # Issues that have start and due dates | |
19 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], |
|
21 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], | |
20 | :order => "start_date, due_date", |
|
22 | :order => "start_date, due_date", | |
21 | :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] |
|
23 | :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] | |
22 | ) |
|
24 | ) | |
23 | # Issues that don't have a due date but that are assigned to a version with a date |
|
25 | # Issues that don't have a due date but that are assigned to a version with a date | |
24 | events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], |
|
26 | events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version], | |
25 | :order => "start_date, effective_date", |
|
27 | :order => "start_date, effective_date", | |
26 | :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] |
|
28 | :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to] | |
27 | ) |
|
29 | ) | |
28 | # Versions |
|
30 | # Versions | |
29 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) |
|
31 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to]) | |
30 |
|
32 | |||
31 | @gantt.events = events |
|
33 | @gantt.events = events | |
32 | end |
|
34 | end | |
33 |
|
35 | |||
34 | basename = (@project ? "#{@project.identifier}-" : '') + 'gantt' |
|
36 | basename = (@project ? "#{@project.identifier}-" : '') + 'gantt' | |
35 |
|
37 | |||
36 | respond_to do |format| |
|
38 | respond_to do |format| | |
37 | format.html { render :action => "show", :layout => !request.xhr? } |
|
39 | format.html { render :action => "show", :layout => !request.xhr? } | |
38 | format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image') |
|
40 | format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image') | |
39 | format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") } |
|
41 | format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") } | |
40 | end |
|
42 | end | |
41 | end |
|
43 | end | |
42 |
|
44 | |||
43 | private |
|
45 | private | |
44 |
|
46 | |||
45 | # Rescues an invalid query statement. Just in case... |
|
|||
46 | # TODO: Refactor, move to ApplicationController with IssuesController |
|
|||
47 | def query_statement_invalid(exception) |
|
|||
48 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
|||
49 | session.delete(:query) |
|
|||
50 | sort_clear |
|
|||
51 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
|||
52 | end |
|
|||
53 |
|
||||
54 | # TODO: Refactor, duplicates IssuesController |
|
47 | # TODO: Refactor, duplicates IssuesController | |
55 | def find_optional_project |
|
48 | def find_optional_project | |
56 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
49 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
57 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
50 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) | |
58 | allowed ? true : deny_access |
|
51 | allowed ? true : deny_access | |
59 | rescue ActiveRecord::RecordNotFound |
|
52 | rescue ActiveRecord::RecordNotFound | |
60 | render_404 |
|
53 | render_404 | |
61 | end |
|
54 | end | |
62 |
|
55 | |||
63 | end |
|
56 | end |
@@ -1,523 +1,515 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class IssuesController < ApplicationController |
|
18 | class IssuesController < ApplicationController | |
19 | menu_item :new_issue, :only => :new |
|
19 | menu_item :new_issue, :only => :new | |
20 | default_search_scope :issues |
|
20 | default_search_scope :issues | |
21 |
|
21 | |||
22 | before_filter :find_issue, :only => [:show, :edit, :update, :reply] |
|
22 | before_filter :find_issue, :only => [:show, :edit, :update, :reply] | |
23 | before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] |
|
23 | before_filter :find_issues, :only => [:bulk_edit, :move, :destroy] | |
24 | before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete] |
|
24 | before_filter :find_project, :only => [:new, :create, :update_form, :preview, :auto_complete] | |
25 | before_filter :authorize, :except => [:index, :changes, :calendar, :preview, :context_menu] |
|
25 | before_filter :authorize, :except => [:index, :changes, :calendar, :preview, :context_menu] | |
26 | before_filter :find_optional_project, :only => [:index, :changes, :calendar] |
|
26 | before_filter :find_optional_project, :only => [:index, :changes, :calendar] | |
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] |
|
27 | before_filter :check_for_default_issue_status, :only => [:new, :create] | |
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] |
|
28 | before_filter :build_new_issue_from_params, :only => [:new, :create] | |
29 | accept_key_auth :index, :show, :changes |
|
29 | accept_key_auth :index, :show, :changes | |
30 |
|
30 | |||
31 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
31 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid | |
32 |
|
32 | |||
33 | helper :journals |
|
33 | helper :journals | |
34 | helper :projects |
|
34 | helper :projects | |
35 | include ProjectsHelper |
|
35 | include ProjectsHelper | |
36 | helper :custom_fields |
|
36 | helper :custom_fields | |
37 | include CustomFieldsHelper |
|
37 | include CustomFieldsHelper | |
38 | helper :issue_relations |
|
38 | helper :issue_relations | |
39 | include IssueRelationsHelper |
|
39 | include IssueRelationsHelper | |
40 | helper :watchers |
|
40 | helper :watchers | |
41 | include WatchersHelper |
|
41 | include WatchersHelper | |
42 | helper :attachments |
|
42 | helper :attachments | |
43 | include AttachmentsHelper |
|
43 | include AttachmentsHelper | |
44 | helper :queries |
|
44 | helper :queries | |
45 | include QueriesHelper |
|
45 | include QueriesHelper | |
46 | helper :sort |
|
46 | helper :sort | |
47 | include SortHelper |
|
47 | include SortHelper | |
48 | include IssuesHelper |
|
48 | include IssuesHelper | |
49 | helper :timelog |
|
49 | helper :timelog | |
50 | include Redmine::Export::PDF |
|
50 | include Redmine::Export::PDF | |
51 |
|
51 | |||
52 | verify :method => [:post, :delete], |
|
52 | verify :method => [:post, :delete], | |
53 | :only => :destroy, |
|
53 | :only => :destroy, | |
54 | :render => { :nothing => true, :status => :method_not_allowed } |
|
54 | :render => { :nothing => true, :status => :method_not_allowed } | |
55 |
|
55 | |||
56 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
|
56 | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } | |
57 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
57 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } | |
58 |
|
58 | |||
59 | def index |
|
59 | def index | |
60 | retrieve_query |
|
60 | retrieve_query | |
61 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
61 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) | |
62 | sort_update(@query.sortable_columns) |
|
62 | sort_update(@query.sortable_columns) | |
63 |
|
63 | |||
64 | if @query.valid? |
|
64 | if @query.valid? | |
65 | limit = case params[:format] |
|
65 | limit = case params[:format] | |
66 | when 'csv', 'pdf' |
|
66 | when 'csv', 'pdf' | |
67 | Setting.issues_export_limit.to_i |
|
67 | Setting.issues_export_limit.to_i | |
68 | when 'atom' |
|
68 | when 'atom' | |
69 | Setting.feeds_limit.to_i |
|
69 | Setting.feeds_limit.to_i | |
70 | else |
|
70 | else | |
71 | per_page_option |
|
71 | per_page_option | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | @issue_count = @query.issue_count |
|
74 | @issue_count = @query.issue_count | |
75 | @issue_pages = Paginator.new self, @issue_count, limit, params['page'] |
|
75 | @issue_pages = Paginator.new self, @issue_count, limit, params['page'] | |
76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], |
|
76 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], | |
77 | :order => sort_clause, |
|
77 | :order => sort_clause, | |
78 | :offset => @issue_pages.current.offset, |
|
78 | :offset => @issue_pages.current.offset, | |
79 | :limit => limit) |
|
79 | :limit => limit) | |
80 | @issue_count_by_group = @query.issue_count_by_group |
|
80 | @issue_count_by_group = @query.issue_count_by_group | |
81 |
|
81 | |||
82 | respond_to do |format| |
|
82 | respond_to do |format| | |
83 | format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } |
|
83 | format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? } | |
84 | format.xml { render :layout => false } |
|
84 | format.xml { render :layout => false } | |
85 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
85 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } | |
86 | format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } |
|
86 | format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') } | |
87 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } |
|
87 | format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') } | |
88 | end |
|
88 | end | |
89 | else |
|
89 | else | |
90 | # Send html if the query is not valid |
|
90 | # Send html if the query is not valid | |
91 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) |
|
91 | render(:template => 'issues/index.rhtml', :layout => !request.xhr?) | |
92 | end |
|
92 | end | |
93 | rescue ActiveRecord::RecordNotFound |
|
93 | rescue ActiveRecord::RecordNotFound | |
94 | render_404 |
|
94 | render_404 | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def changes |
|
97 | def changes | |
98 | retrieve_query |
|
98 | retrieve_query | |
99 | sort_init 'id', 'desc' |
|
99 | sort_init 'id', 'desc' | |
100 | sort_update(@query.sortable_columns) |
|
100 | sort_update(@query.sortable_columns) | |
101 |
|
101 | |||
102 | if @query.valid? |
|
102 | if @query.valid? | |
103 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", |
|
103 | @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC", | |
104 | :limit => 25) |
|
104 | :limit => 25) | |
105 | end |
|
105 | end | |
106 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) |
|
106 | @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name) | |
107 | render :layout => false, :content_type => 'application/atom+xml' |
|
107 | render :layout => false, :content_type => 'application/atom+xml' | |
108 | rescue ActiveRecord::RecordNotFound |
|
108 | rescue ActiveRecord::RecordNotFound | |
109 | render_404 |
|
109 | render_404 | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | def show |
|
112 | def show | |
113 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") |
|
113 | @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") | |
114 | @journals.each_with_index {|j,i| j.indice = i+1} |
|
114 | @journals.each_with_index {|j,i| j.indice = i+1} | |
115 | @journals.reverse! if User.current.wants_comments_in_reverse_order? |
|
115 | @journals.reverse! if User.current.wants_comments_in_reverse_order? | |
116 | @changesets = @issue.changesets.visible.all |
|
116 | @changesets = @issue.changesets.visible.all | |
117 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? |
|
117 | @changesets.reverse! if User.current.wants_comments_in_reverse_order? | |
118 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
118 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
119 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
119 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
120 | @priorities = IssuePriority.all |
|
120 | @priorities = IssuePriority.all | |
121 | @time_entry = TimeEntry.new |
|
121 | @time_entry = TimeEntry.new | |
122 | respond_to do |format| |
|
122 | respond_to do |format| | |
123 | format.html { render :template => 'issues/show.rhtml' } |
|
123 | format.html { render :template => 'issues/show.rhtml' } | |
124 | format.xml { render :layout => false } |
|
124 | format.xml { render :layout => false } | |
125 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } |
|
125 | format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' } | |
126 | format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } |
|
126 | format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") } | |
127 | end |
|
127 | end | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | # Add a new issue |
|
130 | # Add a new issue | |
131 | # The new issue will be created from an existing one if copy_from parameter is given |
|
131 | # The new issue will be created from an existing one if copy_from parameter is given | |
132 | def new |
|
132 | def new | |
133 | render :action => 'new', :layout => !request.xhr? |
|
133 | render :action => 'new', :layout => !request.xhr? | |
134 | end |
|
134 | end | |
135 |
|
135 | |||
136 | def create |
|
136 | def create | |
137 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
137 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) | |
138 | if @issue.save |
|
138 | if @issue.save | |
139 | attachments = Attachment.attach_files(@issue, params[:attachments]) |
|
139 | attachments = Attachment.attach_files(@issue, params[:attachments]) | |
140 | render_attachment_warning_if_needed(@issue) |
|
140 | render_attachment_warning_if_needed(@issue) | |
141 | flash[:notice] = l(:notice_successful_create) |
|
141 | flash[:notice] = l(:notice_successful_create) | |
142 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
142 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) | |
143 | respond_to do |format| |
|
143 | respond_to do |format| | |
144 | format.html { |
|
144 | format.html { | |
145 | redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : |
|
145 | redirect_to(params[:continue] ? { :action => 'new', :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } : | |
146 | { :action => 'show', :id => @issue }) |
|
146 | { :action => 'show', :id => @issue }) | |
147 | } |
|
147 | } | |
148 | format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } |
|
148 | format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) } | |
149 | end |
|
149 | end | |
150 | return |
|
150 | return | |
151 | else |
|
151 | else | |
152 | respond_to do |format| |
|
152 | respond_to do |format| | |
153 | format.html { render :action => 'new' } |
|
153 | format.html { render :action => 'new' } | |
154 | format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } |
|
154 | format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return } | |
155 | end |
|
155 | end | |
156 | end |
|
156 | end | |
157 | end |
|
157 | end | |
158 |
|
158 | |||
159 | # Attributes that can be updated on workflow transition (without :edit permission) |
|
159 | # Attributes that can be updated on workflow transition (without :edit permission) | |
160 | # TODO: make it configurable (at least per role) |
|
160 | # TODO: make it configurable (at least per role) | |
161 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) |
|
161 | UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION) | |
162 |
|
162 | |||
163 | def edit |
|
163 | def edit | |
164 | update_issue_from_params |
|
164 | update_issue_from_params | |
165 |
|
165 | |||
166 | @journal = @issue.current_journal |
|
166 | @journal = @issue.current_journal | |
167 |
|
167 | |||
168 | respond_to do |format| |
|
168 | respond_to do |format| | |
169 | format.html { } |
|
169 | format.html { } | |
170 | format.xml { } |
|
170 | format.xml { } | |
171 | end |
|
171 | end | |
172 | end |
|
172 | end | |
173 |
|
173 | |||
174 | def update |
|
174 | def update | |
175 | update_issue_from_params |
|
175 | update_issue_from_params | |
176 |
|
176 | |||
177 | if @issue.save_issue_with_child_records(params, @time_entry) |
|
177 | if @issue.save_issue_with_child_records(params, @time_entry) | |
178 | render_attachment_warning_if_needed(@issue) |
|
178 | render_attachment_warning_if_needed(@issue) | |
179 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
179 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? | |
180 |
|
180 | |||
181 | respond_to do |format| |
|
181 | respond_to do |format| | |
182 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } |
|
182 | format.html { redirect_back_or_default({:action => 'show', :id => @issue}) } | |
183 | format.xml { head :ok } |
|
183 | format.xml { head :ok } | |
184 | end |
|
184 | end | |
185 | else |
|
185 | else | |
186 | render_attachment_warning_if_needed(@issue) |
|
186 | render_attachment_warning_if_needed(@issue) | |
187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
187 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? | |
188 | @journal = @issue.current_journal |
|
188 | @journal = @issue.current_journal | |
189 |
|
189 | |||
190 | respond_to do |format| |
|
190 | respond_to do |format| | |
191 | format.html { render :action => 'edit' } |
|
191 | format.html { render :action => 'edit' } | |
192 | format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } |
|
192 | format.xml { render :xml => @issue.errors, :status => :unprocessable_entity } | |
193 | end |
|
193 | end | |
194 | end |
|
194 | end | |
195 | end |
|
195 | end | |
196 |
|
196 | |||
197 | def reply |
|
197 | def reply | |
198 | journal = Journal.find(params[:journal_id]) if params[:journal_id] |
|
198 | journal = Journal.find(params[:journal_id]) if params[:journal_id] | |
199 | if journal |
|
199 | if journal | |
200 | user = journal.user |
|
200 | user = journal.user | |
201 | text = journal.notes |
|
201 | text = journal.notes | |
202 | else |
|
202 | else | |
203 | user = @issue.author |
|
203 | user = @issue.author | |
204 | text = @issue.description |
|
204 | text = @issue.description | |
205 | end |
|
205 | end | |
206 | # Replaces pre blocks with [...] |
|
206 | # Replaces pre blocks with [...] | |
207 | text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]') |
|
207 | text = text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]') | |
208 | content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " |
|
208 | content = "#{ll(Setting.default_language, :text_user_wrote, user)}\n> " | |
209 | content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" |
|
209 | content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" | |
210 |
|
210 | |||
211 | render(:update) { |page| |
|
211 | render(:update) { |page| | |
212 | page.<< "$('notes').value = \"#{escape_javascript content}\";" |
|
212 | page.<< "$('notes').value = \"#{escape_javascript content}\";" | |
213 | page.show 'update' |
|
213 | page.show 'update' | |
214 | page << "Form.Element.focus('notes');" |
|
214 | page << "Form.Element.focus('notes');" | |
215 | page << "Element.scrollTo('update');" |
|
215 | page << "Element.scrollTo('update');" | |
216 | page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" |
|
216 | page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;" | |
217 | } |
|
217 | } | |
218 | end |
|
218 | end | |
219 |
|
219 | |||
220 | # Bulk edit a set of issues |
|
220 | # Bulk edit a set of issues | |
221 | def bulk_edit |
|
221 | def bulk_edit | |
222 | @issues.sort! |
|
222 | @issues.sort! | |
223 | if request.post? |
|
223 | if request.post? | |
224 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} |
|
224 | attributes = (params[:issue] || {}).reject {|k,v| v.blank?} | |
225 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} |
|
225 | attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} | |
226 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] |
|
226 | attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] | |
227 |
|
227 | |||
228 | unsaved_issue_ids = [] |
|
228 | unsaved_issue_ids = [] | |
229 | @issues.each do |issue| |
|
229 | @issues.each do |issue| | |
230 | issue.reload |
|
230 | issue.reload | |
231 | journal = issue.init_journal(User.current, params[:notes]) |
|
231 | journal = issue.init_journal(User.current, params[:notes]) | |
232 | issue.safe_attributes = attributes |
|
232 | issue.safe_attributes = attributes | |
233 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
233 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) | |
234 | unless issue.save |
|
234 | unless issue.save | |
235 | # Keep unsaved issue ids to display them in flash error |
|
235 | # Keep unsaved issue ids to display them in flash error | |
236 | unsaved_issue_ids << issue.id |
|
236 | unsaved_issue_ids << issue.id | |
237 | end |
|
237 | end | |
238 | end |
|
238 | end | |
239 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
239 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) | |
240 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) |
|
240 | redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project}) | |
241 | return |
|
241 | return | |
242 | end |
|
242 | end | |
243 | @available_statuses = Workflow.available_statuses(@project) |
|
243 | @available_statuses = Workflow.available_statuses(@project) | |
244 | @custom_fields = @project.all_issue_custom_fields |
|
244 | @custom_fields = @project.all_issue_custom_fields | |
245 | end |
|
245 | end | |
246 |
|
246 | |||
247 | def move |
|
247 | def move | |
248 | @issues.sort! |
|
248 | @issues.sort! | |
249 | @copy = params[:copy_options] && params[:copy_options][:copy] |
|
249 | @copy = params[:copy_options] && params[:copy_options][:copy] | |
250 | @allowed_projects = Issue.allowed_target_projects_on_move |
|
250 | @allowed_projects = Issue.allowed_target_projects_on_move | |
251 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] |
|
251 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id] | |
252 | @target_project ||= @project |
|
252 | @target_project ||= @project | |
253 | @trackers = @target_project.trackers |
|
253 | @trackers = @target_project.trackers | |
254 | @available_statuses = Workflow.available_statuses(@project) |
|
254 | @available_statuses = Workflow.available_statuses(@project) | |
255 | if request.post? |
|
255 | if request.post? | |
256 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) |
|
256 | new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id]) | |
257 | unsaved_issue_ids = [] |
|
257 | unsaved_issue_ids = [] | |
258 | moved_issues = [] |
|
258 | moved_issues = [] | |
259 | @issues.each do |issue| |
|
259 | @issues.each do |issue| | |
260 | issue.reload |
|
260 | issue.reload | |
261 | changed_attributes = {} |
|
261 | changed_attributes = {} | |
262 | [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute| |
|
262 | [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute| | |
263 | unless params[valid_attribute].blank? |
|
263 | unless params[valid_attribute].blank? | |
264 | changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute]) |
|
264 | changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute]) | |
265 | end |
|
265 | end | |
266 | end |
|
266 | end | |
267 | issue.init_journal(User.current) |
|
267 | issue.init_journal(User.current) | |
268 | call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) |
|
268 | call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy }) | |
269 | if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes}) |
|
269 | if r = issue.move_to_project(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes}) | |
270 | moved_issues << r |
|
270 | moved_issues << r | |
271 | else |
|
271 | else | |
272 | unsaved_issue_ids << issue.id |
|
272 | unsaved_issue_ids << issue.id | |
273 | end |
|
273 | end | |
274 | end |
|
274 | end | |
275 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) |
|
275 | set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids) | |
276 |
|
276 | |||
277 | if params[:follow] |
|
277 | if params[:follow] | |
278 | if @issues.size == 1 && moved_issues.size == 1 |
|
278 | if @issues.size == 1 && moved_issues.size == 1 | |
279 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first |
|
279 | redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first | |
280 | else |
|
280 | else | |
281 | redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) |
|
281 | redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project) | |
282 | end |
|
282 | end | |
283 | else |
|
283 | else | |
284 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project |
|
284 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project | |
285 | end |
|
285 | end | |
286 | return |
|
286 | return | |
287 | end |
|
287 | end | |
288 | render :layout => false if request.xhr? |
|
288 | render :layout => false if request.xhr? | |
289 | end |
|
289 | end | |
290 |
|
290 | |||
291 | def destroy |
|
291 | def destroy | |
292 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f |
|
292 | @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f | |
293 | if @hours > 0 |
|
293 | if @hours > 0 | |
294 | case params[:todo] |
|
294 | case params[:todo] | |
295 | when 'destroy' |
|
295 | when 'destroy' | |
296 | # nothing to do |
|
296 | # nothing to do | |
297 | when 'nullify' |
|
297 | when 'nullify' | |
298 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) |
|
298 | TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues]) | |
299 | when 'reassign' |
|
299 | when 'reassign' | |
300 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) |
|
300 | reassign_to = @project.issues.find_by_id(params[:reassign_to_id]) | |
301 | if reassign_to.nil? |
|
301 | if reassign_to.nil? | |
302 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
302 | flash.now[:error] = l(:error_issue_not_found_in_project) | |
303 | return |
|
303 | return | |
304 | else |
|
304 | else | |
305 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) |
|
305 | TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues]) | |
306 | end |
|
306 | end | |
307 | else |
|
307 | else | |
308 | unless params[:format] == 'xml' |
|
308 | unless params[:format] == 'xml' | |
309 | # display the destroy form if it's a user request |
|
309 | # display the destroy form if it's a user request | |
310 | return |
|
310 | return | |
311 | end |
|
311 | end | |
312 | end |
|
312 | end | |
313 | end |
|
313 | end | |
314 | @issues.each(&:destroy) |
|
314 | @issues.each(&:destroy) | |
315 | respond_to do |format| |
|
315 | respond_to do |format| | |
316 | format.html { redirect_to :action => 'index', :project_id => @project } |
|
316 | format.html { redirect_to :action => 'index', :project_id => @project } | |
317 | format.xml { head :ok } |
|
317 | format.xml { head :ok } | |
318 | end |
|
318 | end | |
319 | end |
|
319 | end | |
320 |
|
320 | |||
321 | def calendar |
|
321 | def calendar | |
322 | if params[:year] and params[:year].to_i > 1900 |
|
322 | if params[:year] and params[:year].to_i > 1900 | |
323 | @year = params[:year].to_i |
|
323 | @year = params[:year].to_i | |
324 | if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13 |
|
324 | if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13 | |
325 | @month = params[:month].to_i |
|
325 | @month = params[:month].to_i | |
326 | end |
|
326 | end | |
327 | end |
|
327 | end | |
328 | @year ||= Date.today.year |
|
328 | @year ||= Date.today.year | |
329 | @month ||= Date.today.month |
|
329 | @month ||= Date.today.month | |
330 |
|
330 | |||
331 | @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month) |
|
331 | @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month) | |
332 | retrieve_query |
|
332 | retrieve_query | |
333 | @query.group_by = nil |
|
333 | @query.group_by = nil | |
334 | if @query.valid? |
|
334 | if @query.valid? | |
335 | events = [] |
|
335 | events = [] | |
336 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], |
|
336 | events += @query.issues(:include => [:tracker, :assigned_to, :priority], | |
337 | :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt] |
|
337 | :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt] | |
338 | ) |
|
338 | ) | |
339 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt]) |
|
339 | events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt]) | |
340 |
|
340 | |||
341 | @calendar.events = events |
|
341 | @calendar.events = events | |
342 | end |
|
342 | end | |
343 |
|
343 | |||
344 | render :layout => false if request.xhr? |
|
344 | render :layout => false if request.xhr? | |
345 | end |
|
345 | end | |
346 |
|
346 | |||
347 | def context_menu |
|
347 | def context_menu | |
348 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) |
|
348 | @issues = Issue.find_all_by_id(params[:ids], :include => :project) | |
349 | if (@issues.size == 1) |
|
349 | if (@issues.size == 1) | |
350 | @issue = @issues.first |
|
350 | @issue = @issues.first | |
351 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
351 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
352 | end |
|
352 | end | |
353 | projects = @issues.collect(&:project).compact.uniq |
|
353 | projects = @issues.collect(&:project).compact.uniq | |
354 | @project = projects.first if projects.size == 1 |
|
354 | @project = projects.first if projects.size == 1 | |
355 |
|
355 | |||
356 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), |
|
356 | @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)), | |
357 | :log_time => (@project && User.current.allowed_to?(:log_time, @project)), |
|
357 | :log_time => (@project && User.current.allowed_to?(:log_time, @project)), | |
358 | :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))), |
|
358 | :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))), | |
359 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), |
|
359 | :move => (@project && User.current.allowed_to?(:move_issues, @project)), | |
360 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), |
|
360 | :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)), | |
361 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) |
|
361 | :delete => (@project && User.current.allowed_to?(:delete_issues, @project)) | |
362 | } |
|
362 | } | |
363 | if @project |
|
363 | if @project | |
364 | @assignables = @project.assignable_users |
|
364 | @assignables = @project.assignable_users | |
365 | @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to) |
|
365 | @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to) | |
366 | @trackers = @project.trackers |
|
366 | @trackers = @project.trackers | |
367 | end |
|
367 | end | |
368 |
|
368 | |||
369 | @priorities = IssuePriority.all.reverse |
|
369 | @priorities = IssuePriority.all.reverse | |
370 | @statuses = IssueStatus.find(:all, :order => 'position') |
|
370 | @statuses = IssueStatus.find(:all, :order => 'position') | |
371 | @back = params[:back_url] || request.env['HTTP_REFERER'] |
|
371 | @back = params[:back_url] || request.env['HTTP_REFERER'] | |
372 |
|
372 | |||
373 | render :layout => false |
|
373 | render :layout => false | |
374 | end |
|
374 | end | |
375 |
|
375 | |||
376 | def update_form |
|
376 | def update_form | |
377 | if params[:id].blank? |
|
377 | if params[:id].blank? | |
378 | @issue = Issue.new |
|
378 | @issue = Issue.new | |
379 | @issue.project = @project |
|
379 | @issue.project = @project | |
380 | else |
|
380 | else | |
381 | @issue = @project.issues.visible.find(params[:id]) |
|
381 | @issue = @project.issues.visible.find(params[:id]) | |
382 | end |
|
382 | end | |
383 | @issue.attributes = params[:issue] |
|
383 | @issue.attributes = params[:issue] | |
384 | @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq |
|
384 | @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq | |
385 | @priorities = IssuePriority.all |
|
385 | @priorities = IssuePriority.all | |
386 |
|
386 | |||
387 | render :partial => 'attributes' |
|
387 | render :partial => 'attributes' | |
388 | end |
|
388 | end | |
389 |
|
389 | |||
390 | def preview |
|
390 | def preview | |
391 | @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? |
|
391 | @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank? | |
392 | if @issue |
|
392 | if @issue | |
393 | @attachements = @issue.attachments |
|
393 | @attachements = @issue.attachments | |
394 | @description = params[:issue] && params[:issue][:description] |
|
394 | @description = params[:issue] && params[:issue][:description] | |
395 | if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n") |
|
395 | if @description && @description.gsub(/(\r?\n|\n\r?)/, "\n") == @issue.description.to_s.gsub(/(\r?\n|\n\r?)/, "\n") | |
396 | @description = nil |
|
396 | @description = nil | |
397 | end |
|
397 | end | |
398 | @notes = params[:notes] |
|
398 | @notes = params[:notes] | |
399 | else |
|
399 | else | |
400 | @description = (params[:issue] ? params[:issue][:description] : nil) |
|
400 | @description = (params[:issue] ? params[:issue][:description] : nil) | |
401 | end |
|
401 | end | |
402 | render :layout => false |
|
402 | render :layout => false | |
403 | end |
|
403 | end | |
404 |
|
404 | |||
405 | def auto_complete |
|
405 | def auto_complete | |
406 | @issues = [] |
|
406 | @issues = [] | |
407 | q = params[:q].to_s |
|
407 | q = params[:q].to_s | |
408 | if q.match(/^\d+$/) |
|
408 | if q.match(/^\d+$/) | |
409 | @issues << @project.issues.visible.find_by_id(q.to_i) |
|
409 | @issues << @project.issues.visible.find_by_id(q.to_i) | |
410 | end |
|
410 | end | |
411 | unless q.blank? |
|
411 | unless q.blank? | |
412 | @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10) |
|
412 | @issues += @project.issues.visible.find(:all, :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE ?", "%#{q.downcase}%"], :limit => 10) | |
413 | end |
|
413 | end | |
414 | render :layout => false |
|
414 | render :layout => false | |
415 | end |
|
415 | end | |
416 |
|
416 | |||
417 | private |
|
417 | private | |
418 | def find_issue |
|
418 | def find_issue | |
419 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) |
|
419 | @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category]) | |
420 | @project = @issue.project |
|
420 | @project = @issue.project | |
421 | rescue ActiveRecord::RecordNotFound |
|
421 | rescue ActiveRecord::RecordNotFound | |
422 | render_404 |
|
422 | render_404 | |
423 | end |
|
423 | end | |
424 |
|
424 | |||
425 | # Filter for bulk operations |
|
425 | # Filter for bulk operations | |
426 | def find_issues |
|
426 | def find_issues | |
427 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) |
|
427 | @issues = Issue.find_all_by_id(params[:id] || params[:ids]) | |
428 | raise ActiveRecord::RecordNotFound if @issues.empty? |
|
428 | raise ActiveRecord::RecordNotFound if @issues.empty? | |
429 | projects = @issues.collect(&:project).compact.uniq |
|
429 | projects = @issues.collect(&:project).compact.uniq | |
430 | if projects.size == 1 |
|
430 | if projects.size == 1 | |
431 | @project = projects.first |
|
431 | @project = projects.first | |
432 | else |
|
432 | else | |
433 | # TODO: let users bulk edit/move/destroy issues from different projects |
|
433 | # TODO: let users bulk edit/move/destroy issues from different projects | |
434 | render_error 'Can not bulk edit/move/destroy issues from different projects' |
|
434 | render_error 'Can not bulk edit/move/destroy issues from different projects' | |
435 | return false |
|
435 | return false | |
436 | end |
|
436 | end | |
437 | rescue ActiveRecord::RecordNotFound |
|
437 | rescue ActiveRecord::RecordNotFound | |
438 | render_404 |
|
438 | render_404 | |
439 | end |
|
439 | end | |
440 |
|
440 | |||
441 | def find_project |
|
441 | def find_project | |
442 | project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] |
|
442 | project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id] | |
443 | @project = Project.find(project_id) |
|
443 | @project = Project.find(project_id) | |
444 | rescue ActiveRecord::RecordNotFound |
|
444 | rescue ActiveRecord::RecordNotFound | |
445 | render_404 |
|
445 | render_404 | |
446 | end |
|
446 | end | |
447 |
|
447 | |||
448 | def find_optional_project |
|
448 | def find_optional_project | |
449 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? |
|
449 | @project = Project.find(params[:project_id]) unless params[:project_id].blank? | |
450 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) |
|
450 | allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true) | |
451 | allowed ? true : deny_access |
|
451 | allowed ? true : deny_access | |
452 | rescue ActiveRecord::RecordNotFound |
|
452 | rescue ActiveRecord::RecordNotFound | |
453 | render_404 |
|
453 | render_404 | |
454 | end |
|
454 | end | |
455 |
|
455 | |||
456 | # Rescues an invalid query statement. Just in case... |
|
|||
457 | def query_statement_invalid(exception) |
|
|||
458 | logger.error "Query::StatementInvalid: #{exception.message}" if logger |
|
|||
459 | session.delete(:query) |
|
|||
460 | sort_clear |
|
|||
461 | render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator." |
|
|||
462 | end |
|
|||
463 |
|
||||
464 | # Used by #edit and #update to set some common instance variables |
|
456 | # Used by #edit and #update to set some common instance variables | |
465 | # from the params |
|
457 | # from the params | |
466 | # TODO: Refactor, not everything in here is needed by #edit |
|
458 | # TODO: Refactor, not everything in here is needed by #edit | |
467 | def update_issue_from_params |
|
459 | def update_issue_from_params | |
468 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
460 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) | |
469 | @priorities = IssuePriority.all |
|
461 | @priorities = IssuePriority.all | |
470 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) |
|
462 | @edit_allowed = User.current.allowed_to?(:edit_issues, @project) | |
471 | @time_entry = TimeEntry.new |
|
463 | @time_entry = TimeEntry.new | |
472 |
|
464 | |||
473 | @notes = params[:notes] |
|
465 | @notes = params[:notes] | |
474 | @issue.init_journal(User.current, @notes) |
|
466 | @issue.init_journal(User.current, @notes) | |
475 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed |
|
467 | # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed | |
476 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] |
|
468 | if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue] | |
477 | attrs = params[:issue].dup |
|
469 | attrs = params[:issue].dup | |
478 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed |
|
470 | attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed | |
479 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} |
|
471 | attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s} | |
480 | @issue.safe_attributes = attrs |
|
472 | @issue.safe_attributes = attrs | |
481 | end |
|
473 | end | |
482 |
|
474 | |||
483 | end |
|
475 | end | |
484 |
|
476 | |||
485 | # TODO: Refactor, lots of extra code in here |
|
477 | # TODO: Refactor, lots of extra code in here | |
486 | def build_new_issue_from_params |
|
478 | def build_new_issue_from_params | |
487 | @issue = Issue.new |
|
479 | @issue = Issue.new | |
488 | @issue.copy_from(params[:copy_from]) if params[:copy_from] |
|
480 | @issue.copy_from(params[:copy_from]) if params[:copy_from] | |
489 | @issue.project = @project |
|
481 | @issue.project = @project | |
490 | # Tracker must be set before custom field values |
|
482 | # Tracker must be set before custom field values | |
491 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) |
|
483 | @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first) | |
492 | if @issue.tracker.nil? |
|
484 | if @issue.tracker.nil? | |
493 | render_error l(:error_no_tracker_in_project) |
|
485 | render_error l(:error_no_tracker_in_project) | |
494 | return false |
|
486 | return false | |
495 | end |
|
487 | end | |
496 | if params[:issue].is_a?(Hash) |
|
488 | if params[:issue].is_a?(Hash) | |
497 | @issue.safe_attributes = params[:issue] |
|
489 | @issue.safe_attributes = params[:issue] | |
498 | @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project) |
|
490 | @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project) | |
499 | end |
|
491 | end | |
500 | @issue.author = User.current |
|
492 | @issue.author = User.current | |
501 | @issue.start_date ||= Date.today |
|
493 | @issue.start_date ||= Date.today | |
502 | @priorities = IssuePriority.all |
|
494 | @priorities = IssuePriority.all | |
503 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) |
|
495 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true) | |
504 | end |
|
496 | end | |
505 |
|
497 | |||
506 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) |
|
498 | def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids) | |
507 | if unsaved_issue_ids.empty? |
|
499 | if unsaved_issue_ids.empty? | |
508 | flash[:notice] = l(:notice_successful_update) unless issues.empty? |
|
500 | flash[:notice] = l(:notice_successful_update) unless issues.empty? | |
509 | else |
|
501 | else | |
510 | flash[:error] = l(:notice_failed_to_save_issues, |
|
502 | flash[:error] = l(:notice_failed_to_save_issues, | |
511 | :count => unsaved_issue_ids.size, |
|
503 | :count => unsaved_issue_ids.size, | |
512 | :total => issues.size, |
|
504 | :total => issues.size, | |
513 | :ids => '#' + unsaved_issue_ids.join(', #')) |
|
505 | :ids => '#' + unsaved_issue_ids.join(', #')) | |
514 | end |
|
506 | end | |
515 | end |
|
507 | end | |
516 |
|
508 | |||
517 | def check_for_default_issue_status |
|
509 | def check_for_default_issue_status | |
518 | if IssueStatus.default.nil? |
|
510 | if IssueStatus.default.nil? | |
519 | render_error l(:error_no_default_issue_status) |
|
511 | render_error l(:error_no_default_issue_status) | |
520 | return false |
|
512 | return false | |
521 | end |
|
513 | end | |
522 | end |
|
514 | end | |
523 | end |
|
515 | end |
General Comments 0
You need to be logged in to leave comments.
Login now