annotate app/controllers/timelog_controller.rb @ 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@119 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1295 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
Chris@1295 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@1295 41 helper :queries
Chris@1295 42 include QueriesHelper
Chris@909 43
chris@37 44 def index
Chris@1295 45 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
Chris@1295 46 scope = time_entry_scope
Chris@909 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@909 50
Chris@441 51 respond_to do |format|
Chris@441 52 format.html {
Chris@441 53 # Paginate results
Chris@1115 54 @entry_count = scope.count
Chris@1295 55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
Chris@1115 56 @entries = scope.all(
Chris@1115 57 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1115 58 :order => sort_clause,
Chris@1295 59 :limit => @entry_pages.per_page,
Chris@1295 60 :offset => @entry_pages.offset
Chris@1115 61 )
Chris@1115 62 @total_hours = scope.sum(:hours).to_f
Chris@0 63
Chris@441 64 render :layout => !request.xhr?
Chris@441 65 }
Chris@441 66 format.api {
Chris@1115 67 @entry_count = scope.count
Chris@441 68 @offset, @limit = api_offset_and_limit
Chris@1115 69 @entries = scope.all(
Chris@1115 70 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1115 71 :order => sort_clause,
Chris@1115 72 :limit => @limit,
Chris@1115 73 :offset => @offset
Chris@1115 74 )
Chris@441 75 }
Chris@441 76 format.atom {
Chris@1115 77 entries = scope.all(
Chris@1115 78 :include => [:project, :activity, :user, {:issue => :tracker}],
Chris@1115 79 :order => "#{TimeEntry.table_name}.created_on DESC",
Chris@1115 80 :limit => Setting.feeds_limit.to_i
Chris@1115 81 )
Chris@441 82 render_feed(entries, :title => l(:label_spent_time))
Chris@441 83 }
Chris@441 84 format.csv {
Chris@441 85 # Export all entries
Chris@1115 86 @entries = scope.all(
Chris@1115 87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
Chris@1115 88 :order => sort_clause
Chris@1115 89 )
Chris@1295 90 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
Chris@441 91 }
Chris@0 92 end
Chris@0 93 end
Chris@909 94
Chris@1115 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@1115 100
Chris@1115 101 respond_to do |format|
Chris@1115 102 format.html { render :layout => !request.xhr? }
Chris@1115 103 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
Chris@1115 104 end
Chris@1115 105 end
Chris@1115 106
Chris@119 107 def show
Chris@119 108 respond_to do |format|
Chris@119 109 # TODO: Implement html response
Chris@119 110 format.html { render :nothing => true, :status => 406 }
Chris@119 111 format.api
Chris@119 112 end
Chris@119 113 end
chris@37 114
chris@37 115 def new
chris@37 116 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
Chris@929 117 @time_entry.safe_attributes = params[:time_entry]
chris@37 118 end
chris@37 119
chris@37 120 def create
Chris@0 121 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
Chris@929 122 @time_entry.safe_attributes = params[:time_entry]
Chris@909 123
Chris@0 124 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
Chris@909 125
chris@37 126 if @time_entry.save
Chris@119 127 respond_to do |format|
Chris@119 128 format.html {
Chris@1115 129 flash[:notice] = l(:notice_successful_create)
Chris@1115 130 if params[:continue]
Chris@1115 131 if params[:project_id]
Chris@1295 132 options = {
Chris@1115 133 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
Chris@1115 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@1115 141 else
Chris@1295 142 options = {
Chris@1115 143 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
Chris@1115 144 :back_url => params[:back_url]
Chris@1295 145 }
Chris@1295 146 redirect_to new_time_entry_path(options)
Chris@1115 147 end
Chris@1115 148 else
Chris@1295 149 redirect_back_or_default project_time_entries_path(@time_entry.project)
Chris@1115 150 end
Chris@119 151 }
Chris@119 152 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
Chris@119 153 end
chris@37 154 else
Chris@119 155 respond_to do |format|
Chris@1115 156 format.html { render :action => 'new' }
Chris@119 157 format.api { render_validation_errors(@time_entry) }
Chris@119 158 end
Chris@909 159 end
Chris@0 160 end
Chris@909 161
chris@37 162 def edit
Chris@929 163 @time_entry.safe_attributes = params[:time_entry]
chris@37 164 end
chris@37 165
chris@37 166 def update
Chris@929 167 @time_entry.safe_attributes = params[:time_entry]
Chris@909 168
chris@37 169 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
Chris@909 170
chris@37 171 if @time_entry.save
Chris@119 172 respond_to do |format|
Chris@119 173 format.html {
Chris@119 174 flash[:notice] = l(:notice_successful_update)
Chris@1295 175 redirect_back_or_default project_time_entries_path(@time_entry.project)
Chris@119 176 }
Chris@1115 177 format.api { render_api_ok }
Chris@119 178 end
chris@37 179 else
Chris@119 180 respond_to do |format|
Chris@119 181 format.html { render :action => 'edit' }
Chris@119 182 format.api { render_validation_errors(@time_entry) }
Chris@119 183 end
Chris@909 184 end
chris@37 185 end
chris@37 186
Chris@441 187 def bulk_edit
Chris@441 188 @available_activities = TimeEntryActivity.shared.active
Chris@441 189 @custom_fields = TimeEntry.first.available_custom_fields
Chris@441 190 end
Chris@441 191
Chris@441 192 def bulk_update
Chris@441 193 attributes = parse_params_for_bulk_time_entry_attributes(params)
Chris@441 194
Chris@441 195 unsaved_time_entry_ids = []
Chris@441 196 @time_entries.each do |time_entry|
Chris@441 197 time_entry.reload
Chris@929 198 time_entry.safe_attributes = attributes
Chris@441 199 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
Chris@441 200 unless time_entry.save
Chris@441 201 # Keep unsaved time_entry ids to display them in flash error
Chris@441 202 unsaved_time_entry_ids << time_entry.id
Chris@441 203 end
Chris@441 204 end
Chris@441 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@441 207 end
Chris@441 208
Chris@0 209 def destroy
Chris@1115 210 destroyed = TimeEntry.transaction do
Chris@1115 211 @time_entries.each do |t|
Chris@441 212 unless t.destroy && t.destroyed?
Chris@1115 213 raise ActiveRecord::Rollback
Chris@441 214 end
Chris@119 215 end
Chris@0 216 end
Chris@441 217
Chris@441 218 respond_to do |format|
Chris@441 219 format.html {
Chris@1115 220 if destroyed
Chris@1115 221 flash[:notice] = l(:notice_successful_delete)
Chris@1115 222 else
Chris@1115 223 flash[:error] = l(:notice_unable_delete_time_entry)
Chris@1115 224 end
Chris@1295 225 redirect_back_or_default project_time_entries_path(@projects.first)
Chris@441 226 }
Chris@1115 227 format.api {
Chris@1115 228 if destroyed
Chris@1115 229 render_api_ok
Chris@1115 230 else
Chris@1115 231 render_validation_errors(@time_entries)
Chris@1115 232 end
Chris@1115 233 }
Chris@441 234 end
Chris@0 235 end
Chris@0 236
Chris@0 237 private
chris@37 238 def find_time_entry
chris@37 239 @time_entry = TimeEntry.find(params[:id])
chris@37 240 unless @time_entry.editable_by?(User.current)
chris@37 241 render_403
chris@37 242 return false
chris@37 243 end
chris@37 244 @project = @time_entry.project
chris@37 245 rescue ActiveRecord::RecordNotFound
chris@37 246 render_404
chris@37 247 end
chris@37 248
Chris@441 249 def find_time_entries
Chris@441 250 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
Chris@441 251 raise ActiveRecord::RecordNotFound if @time_entries.empty?
Chris@441 252 @projects = @time_entries.collect(&:project).compact.uniq
Chris@441 253 @project = @projects.first if @projects.size == 1
Chris@441 254 rescue ActiveRecord::RecordNotFound
Chris@441 255 render_404
Chris@441 256 end
Chris@441 257
Chris@441 258 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
Chris@441 259 if unsaved_time_entry_ids.empty?
Chris@441 260 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
Chris@441 261 else
Chris@441 262 flash[:error] = l(:notice_failed_to_save_time_entries,
Chris@441 263 :count => unsaved_time_entry_ids.size,
Chris@441 264 :total => time_entries.size,
Chris@441 265 :ids => '#' + unsaved_time_entry_ids.join(', #'))
Chris@441 266 end
Chris@441 267 end
Chris@441 268
Chris@1115 269 def find_optional_project_for_new_time_entry
Chris@1115 270 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
Chris@1115 271 @project = Project.find(project_id)
Chris@1115 272 end
Chris@119 273 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
Chris@119 274 @issue = Issue.find(issue_id)
Chris@1115 275 @project ||= @issue.project
Chris@0 276 end
Chris@0 277 rescue ActiveRecord::RecordNotFound
Chris@0 278 render_404
Chris@0 279 end
Chris@909 280
Chris@1115 281 def find_project_for_new_time_entry
Chris@1115 282 find_optional_project_for_new_time_entry
Chris@1115 283 if @project.nil?
Chris@1115 284 render_404
Chris@1115 285 end
Chris@1115 286 end
Chris@1115 287
Chris@0 288 def find_optional_project
Chris@0 289 if !params[:issue_id].blank?
Chris@0 290 @issue = Issue.find(params[:issue_id])
Chris@0 291 @project = @issue.project
Chris@0 292 elsif !params[:project_id].blank?
Chris@0 293 @project = Project.find(params[:project_id])
Chris@0 294 end
Chris@0 295 end
Chris@909 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@0 304 end
Chris@1295 305 scope
Chris@0 306 end
Chris@0 307
Chris@441 308 def parse_params_for_bulk_time_entry_attributes(params)
Chris@441 309 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
Chris@441 310 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
Chris@441 311 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
Chris@441 312 attributes
Chris@441 313 end
Chris@0 314 end