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