annotate .svn/pristine/e5/e5c904dde1892f8ff602788f810c1a9ce2872e82.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class TimelogController < ApplicationController
Chris@1295 19 menu_item :issues
Chris@1295 20
Chris@1295 21 before_filter :find_project_for_new_time_entry, :only => [:create]
Chris@1295 22 before_filter :find_time_entry, :only => [:show, :edit, :update]
Chris@1295 23 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
Chris@1295 24 before_filter :authorize, :except => [:new, :index, :report]
Chris@1295 25
Chris@1295 26 before_filter :find_optional_project, :only => [:index, :report]
Chris@1295 27 before_filter :find_optional_project_for_new_time_entry, :only => [:new]
Chris@1295 28 before_filter :authorize_global, :only => [:new, :index, :report]
Chris@1295 29
Chris@1295 30 accept_rss_auth :index
Chris@1295 31 accept_api_auth :index, :show, :create, :update, :destroy
Chris@1295 32
Chris@1295 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
Chris@1295 34
Chris@1295 35 helper :sort
Chris@1295 36 include SortHelper
Chris@1295 37 helper :issues
Chris@1295 38 include TimelogHelper
Chris@1295 39 helper :custom_fields
Chris@1295 40 include CustomFieldsHelper
Chris@1295 41 helper :queries
Chris@1295 42 include QueriesHelper
Chris@1295 43
Chris@1295 44 def index
Chris@1295 45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
Chris@1295 46 scope = time_entry_scope
Chris@1295 47
Chris@1295 48 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
Chris@1295 49 sort_update(@query.sortable_columns)
Chris@1295 50
Chris@1295 51 respond_to do |format|
Chris@1295 52 format.html {
Chris@1295 53 # Paginate results
Chris@1295 54 @entry_count = scope.count
Chris@1295 55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
Chris@1295 56 @entries = scope.all(
Chris@1295 57 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1295 58 :order => sort_clause,
Chris@1295 59 :limit => @entry_pages.per_page,
Chris@1295 60 :offset => @entry_pages.offset
Chris@1295 61 )
Chris@1295 62 @total_hours = scope.sum(:hours).to_f
Chris@1295 63
Chris@1295 64 render :layout => !request.xhr?
Chris@1295 65 }
Chris@1295 66 format.api {
Chris@1295 67 @entry_count = scope.count
Chris@1295 68 @offset, @limit = api_offset_and_limit
Chris@1295 69 @entries = scope.all(
Chris@1295 70 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1295 71 :order => sort_clause,
Chris@1295 72 :limit => @limit,
Chris@1295 73 :offset => @offset
Chris@1295 74 )
Chris@1295 75 }
Chris@1295 76 format.atom {
Chris@1295 77 entries = scope.all(
Chris@1295 78 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1295 79 :order => "#{TimeEntry.table_name}.created_on DESC",
Chris@1295 80 :limit => Setting.feeds_limit.to_i
Chris@1295 81 )
Chris@1295 82 render_feed(entries, :title => l(:label_spent_time))
Chris@1295 83 }
Chris@1295 84 format.csv {
Chris@1295 85 # Export all entries
Chris@1295 86 @entries = scope.all(
Chris@1295 87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
Chris@1295 88 :order => sort_clause
Chris@1295 89 )
Chris@1295 90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
Chris@1295 91 }
Chris@1295 92 end
Chris@1295 93 end
Chris@1295 94
Chris@1295 95 def report
Chris@1295 96 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
Chris@1295 97 scope = time_entry_scope
Chris@1295 98
Chris@1295 99 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
Chris@1295 100
Chris@1295 101 respond_to do |format|
Chris@1295 102 format.html { render :layout => !request.xhr? }
Chris@1295 103 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
Chris@1295 104 end
Chris@1295 105 end
Chris@1295 106
Chris@1295 107 def show
Chris@1295 108 respond_to do |format|
Chris@1295 109 # TODO: Implement html response
Chris@1295 110 format.html { render :nothing => true, :status => 406 }
Chris@1295 111 format.api
Chris@1295 112 end
Chris@1295 113 end
Chris@1295 114
Chris@1295 115 def new
Chris@1295 116 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
Chris@1295 117 @time_entry.safe_attributes = params[:time_entry]
Chris@1295 118 end
Chris@1295 119
Chris@1295 120 def create
Chris@1295 121 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
Chris@1295 122 @time_entry.safe_attributes = params[:time_entry]
Chris@1295 123
Chris@1295 124 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
Chris@1295 125
Chris@1295 126 if @time_entry.save
Chris@1295 127 respond_to do |format|
Chris@1295 128 format.html {
Chris@1295 129 flash[:notice] = l(:notice_successful_create)
Chris@1295 130 if params[:continue]
Chris@1295 131 if params[:project_id]
Chris@1295 132 options = {
Chris@1295 133 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
Chris@1295 134 :back_url => params[:back_url]
Chris@1295 135 }
Chris@1295 136 if @time_entry.issue
Chris@1295 137 redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
Chris@1295 138 else
Chris@1295 139 redirect_to new_project_time_entry_path(@time_entry.project, options)
Chris@1295 140 end
Chris@1295 141 else
Chris@1295 142 options = {
Chris@1295 143 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
Chris@1295 144 :back_url => params[:back_url]
Chris@1295 145 }
Chris@1295 146 redirect_to new_time_entry_path(options)
Chris@1295 147 end
Chris@1295 148 else
Chris@1295 149 redirect_back_or_default project_time_entries_path(@time_entry.project)
Chris@1295 150 end
Chris@1295 151 }
Chris@1295 152 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
Chris@1295 153 end
Chris@1295 154 else
Chris@1295 155 respond_to do |format|
Chris@1295 156 format.html { render :action => 'new' }
Chris@1295 157 format.api { render_validation_errors(@time_entry) }
Chris@1295 158 end
Chris@1295 159 end
Chris@1295 160 end
Chris@1295 161
Chris@1295 162 def edit
Chris@1295 163 @time_entry.safe_attributes = params[:time_entry]
Chris@1295 164 end
Chris@1295 165
Chris@1295 166 def update
Chris@1295 167 @time_entry.safe_attributes = params[:time_entry]
Chris@1295 168
Chris@1295 169 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
Chris@1295 170
Chris@1295 171 if @time_entry.save
Chris@1295 172 respond_to do |format|
Chris@1295 173 format.html {
Chris@1295 174 flash[:notice] = l(:notice_successful_update)
Chris@1295 175 redirect_back_or_default project_time_entries_path(@time_entry.project)
Chris@1295 176 }
Chris@1295 177 format.api { render_api_ok }
Chris@1295 178 end
Chris@1295 179 else
Chris@1295 180 respond_to do |format|
Chris@1295 181 format.html { render :action => 'edit' }
Chris@1295 182 format.api { render_validation_errors(@time_entry) }
Chris@1295 183 end
Chris@1295 184 end
Chris@1295 185 end
Chris@1295 186
Chris@1295 187 def bulk_edit
Chris@1295 188 @available_activities = TimeEntryActivity.shared.active
Chris@1295 189 @custom_fields = TimeEntry.first.available_custom_fields
Chris@1295 190 end
Chris@1295 191
Chris@1295 192 def bulk_update
Chris@1295 193 attributes = parse_params_for_bulk_time_entry_attributes(params)
Chris@1295 194
Chris@1295 195 unsaved_time_entry_ids = []
Chris@1295 196 @time_entries.each do |time_entry|
Chris@1295 197 time_entry.reload
Chris@1295 198 time_entry.safe_attributes = attributes
Chris@1295 199 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
Chris@1295 200 unless time_entry.save
Chris@1295 201 # Keep unsaved time_entry ids to display them in flash error
Chris@1295 202 unsaved_time_entry_ids << time_entry.id
Chris@1295 203 end
Chris@1295 204 end
Chris@1295 205 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
Chris@1295 206 redirect_back_or_default project_time_entries_path(@projects.first)
Chris@1295 207 end
Chris@1295 208
Chris@1295 209 def destroy
Chris@1295 210 destroyed = TimeEntry.transaction do
Chris@1295 211 @time_entries.each do |t|
Chris@1295 212 unless t.destroy && t.destroyed?
Chris@1295 213 raise ActiveRecord::Rollback
Chris@1295 214 end
Chris@1295 215 end
Chris@1295 216 end
Chris@1295 217
Chris@1295 218 respond_to do |format|
Chris@1295 219 format.html {
Chris@1295 220 if destroyed
Chris@1295 221 flash[:notice] = l(:notice_successful_delete)
Chris@1295 222 else
Chris@1295 223 flash[:error] = l(:notice_unable_delete_time_entry)
Chris@1295 224 end
Chris@1295 225 redirect_back_or_default project_time_entries_path(@projects.first)
Chris@1295 226 }
Chris@1295 227 format.api {
Chris@1295 228 if destroyed
Chris@1295 229 render_api_ok
Chris@1295 230 else
Chris@1295 231 render_validation_errors(@time_entries)
Chris@1295 232 end
Chris@1295 233 }
Chris@1295 234 end
Chris@1295 235 end
Chris@1295 236
Chris@1295 237 private
Chris@1295 238 def find_time_entry
Chris@1295 239 @time_entry = TimeEntry.find(params[:id])
Chris@1295 240 unless @time_entry.editable_by?(User.current)
Chris@1295 241 render_403
Chris@1295 242 return false
Chris@1295 243 end
Chris@1295 244 @project = @time_entry.project
Chris@1295 245 rescue ActiveRecord::RecordNotFound
Chris@1295 246 render_404
Chris@1295 247 end
Chris@1295 248
Chris@1295 249 def find_time_entries
Chris@1295 250 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
Chris@1295 251 raise ActiveRecord::RecordNotFound if @time_entries.empty?
Chris@1295 252 @projects = @time_entries.collect(&:project).compact.uniq
Chris@1295 253 @project = @projects.first if @projects.size == 1
Chris@1295 254 rescue ActiveRecord::RecordNotFound
Chris@1295 255 render_404
Chris@1295 256 end
Chris@1295 257
Chris@1295 258 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
Chris@1295 259 if unsaved_time_entry_ids.empty?
Chris@1295 260 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
Chris@1295 261 else
Chris@1295 262 flash[:error] = l(:notice_failed_to_save_time_entries,
Chris@1295 263 :count => unsaved_time_entry_ids.size,
Chris@1295 264 :total => time_entries.size,
Chris@1295 265 :ids => '#' + unsaved_time_entry_ids.join(', #'))
Chris@1295 266 end
Chris@1295 267 end
Chris@1295 268
Chris@1295 269 def find_optional_project_for_new_time_entry
Chris@1295 270 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
Chris@1295 271 @project = Project.find(project_id)
Chris@1295 272 end
Chris@1295 273 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
Chris@1295 274 @issue = Issue.find(issue_id)
Chris@1295 275 @project ||= @issue.project
Chris@1295 276 end
Chris@1295 277 rescue ActiveRecord::RecordNotFound
Chris@1295 278 render_404
Chris@1295 279 end
Chris@1295 280
Chris@1295 281 def find_project_for_new_time_entry
Chris@1295 282 find_optional_project_for_new_time_entry
Chris@1295 283 if @project.nil?
Chris@1295 284 render_404
Chris@1295 285 end
Chris@1295 286 end
Chris@1295 287
Chris@1295 288 def find_optional_project
Chris@1295 289 if !params[:issue_id].blank?
Chris@1295 290 @issue = Issue.find(params[:issue_id])
Chris@1295 291 @project = @issue.project
Chris@1295 292 elsif !params[:project_id].blank?
Chris@1295 293 @project = Project.find(params[:project_id])
Chris@1295 294 end
Chris@1295 295 end
Chris@1295 296
Chris@1295 297 # Returns the TimeEntry scope for index and report actions
Chris@1295 298 def time_entry_scope
Chris@1295 299 scope = TimeEntry.visible.where(@query.statement)
Chris@1295 300 if @issue
Chris@1295 301 scope = scope.on_issue(@issue)
Chris@1295 302 elsif @project
Chris@1295 303 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
Chris@1295 304 end
Chris@1295 305 scope
Chris@1295 306 end
Chris@1295 307
Chris@1295 308 def parse_params_for_bulk_time_entry_attributes(params)
Chris@1295 309 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
Chris@1295 310 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
Chris@1295 311 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
Chris@1295 312 attributes
Chris@1295 313 end
Chris@1295 314 end