annotate .svn/pristine/41/414344b2f540a15376eaaead993ae04a45a0a168.svn-base @ 1517:dffacf8a6908 redmine-2.5

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