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