annotate .svn/pristine/f8/f846004b3958b5cdd097fb7a9fa2ca8d32fef589.svn-base @ 1494:e248c7af89ec redmine-2.4

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