2010-12-04 11:13:15 +01:00
|
|
|
# Redmine - project management software
|
2016-03-13 11:30:10 +01:00
|
|
|
# Copyright (C) 2006-2016 Jean-Philippe Lang
|
2007-06-24 18:07:06 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-21 11:26:32 +02:00
|
|
|
#
|
2007-06-24 18:07:06 +02:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-21 11:26:32 +02:00
|
|
|
#
|
2007-06-24 18:07:06 +02:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2007-03-23 13:22:31 +01:00
|
|
|
class TimelogController < ApplicationController
|
2016-08-20 14:18:29 +02:00
|
|
|
menu_item :time_entries
|
2012-01-21 15:26:51 +01:00
|
|
|
|
2016-07-14 09:27:31 +02:00
|
|
|
before_action :find_time_entry, :only => [:show, :edit, :update]
|
2016-11-18 09:37:07 +01:00
|
|
|
before_action :check_editability, :only => [:edit, :update]
|
2016-07-14 09:27:31 +02:00
|
|
|
before_action :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
|
|
|
|
before_action :authorize, :only => [:show, :edit, :update, :bulk_edit, :bulk_update, :destroy]
|
2012-01-21 15:26:51 +01:00
|
|
|
|
2016-07-14 09:27:31 +02:00
|
|
|
before_action :find_optional_issue, :only => [:new, :create]
|
|
|
|
before_action :find_optional_project, :only => [:index, :report]
|
|
|
|
before_action :authorize_global, :only => [:new, :create, :index, :report]
|
2012-01-21 15:26:51 +01:00
|
|
|
|
2011-07-09 10:56:07 +02:00
|
|
|
accept_rss_auth :index
|
|
|
|
accept_api_auth :index, :show, :create, :update, :destroy
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2012-12-09 22:13:26 +01:00
|
|
|
rescue_from Query::StatementInvalid, :with => :query_statement_invalid
|
|
|
|
|
2007-03-23 13:22:31 +01:00
|
|
|
helper :sort
|
|
|
|
include SortHelper
|
2007-10-03 19:20:04 +02:00
|
|
|
helper :issues
|
2008-02-26 19:15:58 +01:00
|
|
|
include TimelogHelper
|
2008-04-09 19:45:39 +02:00
|
|
|
helper :custom_fields
|
|
|
|
include CustomFieldsHelper
|
2012-12-09 18:57:18 +01:00
|
|
|
helper :queries
|
2013-02-19 21:41:54 +01:00
|
|
|
include QueriesHelper
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2010-10-06 20:23:45 +02:00
|
|
|
def index
|
2016-07-12 19:40:19 +02:00
|
|
|
retrieve_time_entry_query
|
2012-12-09 18:57:18 +01:00
|
|
|
sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
|
|
|
|
sort_update(@query.sortable_columns)
|
2013-07-28 12:13:48 +02:00
|
|
|
scope = time_entry_scope(:order => sort_clause).
|
2016-10-29 16:19:25 +02:00
|
|
|
preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
|
|
|
|
preload(:project, :user)
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2011-03-16 18:29:30 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2011-12-04 23:49:46 +01:00
|
|
|
@entry_count = scope.count
|
2013-01-08 21:18:12 +01:00
|
|
|
@entry_pages = Paginator.new @entry_count, per_page_option, params['page']
|
2014-10-22 19:37:16 +02:00
|
|
|
@entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
|
2008-04-05 18:40:26 +02:00
|
|
|
|
2011-03-16 18:29:30 +01:00
|
|
|
render :layout => !request.xhr?
|
|
|
|
}
|
|
|
|
format.api {
|
2011-12-04 23:49:46 +01:00
|
|
|
@entry_count = scope.count
|
2011-05-22 12:48:59 +02:00
|
|
|
@offset, @limit = api_offset_and_limit
|
2014-10-22 19:37:16 +02:00
|
|
|
@entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
|
2011-03-16 18:29:30 +01:00
|
|
|
}
|
|
|
|
format.atom {
|
2014-10-22 19:37:16 +02:00
|
|
|
entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
|
2011-03-16 18:29:30 +01:00
|
|
|
render_feed(entries, :title => l(:label_spent_time))
|
|
|
|
}
|
|
|
|
format.csv {
|
|
|
|
# Export all entries
|
2014-10-22 19:37:16 +02:00
|
|
|
@entries = scope.to_a
|
2013-02-23 12:07:43 +01:00
|
|
|
send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
|
2011-03-16 18:29:30 +01:00
|
|
|
}
|
2008-02-26 19:15:58 +01:00
|
|
|
end
|
2007-03-23 13:22:31 +01:00
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2011-12-02 20:33:05 +01:00
|
|
|
def report
|
2016-07-12 19:40:19 +02:00
|
|
|
retrieve_time_entry_query
|
2012-12-09 18:57:18 +01:00
|
|
|
scope = time_entry_scope
|
|
|
|
|
|
|
|
@report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
|
2011-12-02 20:33:05 +01:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :layout => !request.xhr? }
|
|
|
|
format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-04 11:13:15 +01:00
|
|
|
def show
|
|
|
|
respond_to do |format|
|
|
|
|
# TODO: Implement html response
|
2016-07-17 08:35:28 +02:00
|
|
|
format.html { head 406 }
|
2010-12-04 18:43:39 +01:00
|
|
|
format.api
|
2010-12-04 11:13:15 +01:00
|
|
|
end
|
|
|
|
end
|
2010-10-07 17:51:09 +02:00
|
|
|
|
|
|
|
def new
|
|
|
|
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
|
2012-03-06 21:23:00 +01:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2010-10-07 17:51:09 +02:00
|
|
|
end
|
2010-10-08 17:39:39 +02:00
|
|
|
|
|
|
|
def create
|
|
|
|
@time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
|
2012-03-06 21:23:00 +01:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2014-10-04 09:23:14 +02:00
|
|
|
if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
|
|
|
|
render_403
|
|
|
|
return
|
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2010-10-08 17:39:39 +02:00
|
|
|
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2010-10-08 17:39:39 +02:00
|
|
|
if @time_entry.save
|
2010-12-04 11:13:15 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2012-01-21 11:37:19 +01:00
|
|
|
flash[:notice] = l(:notice_successful_create)
|
|
|
|
if params[:continue]
|
2014-10-04 09:23:14 +02:00
|
|
|
options = {
|
|
|
|
:time_entry => {
|
|
|
|
:project_id => params[:time_entry][:project_id],
|
|
|
|
:issue_id => @time_entry.issue_id,
|
|
|
|
:activity_id => @time_entry.activity_id
|
|
|
|
},
|
|
|
|
:back_url => params[:back_url]
|
|
|
|
}
|
|
|
|
if params[:project_id] && @time_entry.project
|
|
|
|
redirect_to new_project_time_entry_path(@time_entry.project, options)
|
|
|
|
elsif params[:issue_id] && @time_entry.issue
|
|
|
|
redirect_to new_issue_time_entry_path(@time_entry.issue, options)
|
2012-01-21 15:26:51 +01:00
|
|
|
else
|
2012-12-11 20:39:47 +01:00
|
|
|
redirect_to new_time_entry_path(options)
|
2012-01-21 15:26:51 +01:00
|
|
|
end
|
2012-01-21 11:37:19 +01:00
|
|
|
else
|
2012-12-11 20:39:47 +01:00
|
|
|
redirect_back_or_default project_time_entries_path(@time_entry.project)
|
2012-01-21 11:37:19 +01:00
|
|
|
end
|
2010-12-04 11:13:15 +01:00
|
|
|
}
|
2010-12-04 18:43:39 +01:00
|
|
|
format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
|
2010-12-04 11:13:15 +01:00
|
|
|
end
|
2010-10-08 17:39:39 +02:00
|
|
|
else
|
2010-12-04 11:13:15 +01:00
|
|
|
respond_to do |format|
|
2012-01-21 15:26:51 +01:00
|
|
|
format.html { render :action => 'new' }
|
2010-12-04 11:13:15 +01:00
|
|
|
format.api { render_validation_errors(@time_entry) }
|
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
end
|
2010-10-08 17:39:39 +02:00
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2007-03-23 13:22:31 +01:00
|
|
|
def edit
|
2012-03-06 21:23:00 +01:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2010-10-11 17:31:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-03-06 21:23:00 +01:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2009-05-31 00:49:39 +02:00
|
|
|
call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2010-10-11 17:31:42 +02:00
|
|
|
if @time_entry.save
|
2010-12-04 11:13:15 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:notice] = l(:notice_successful_update)
|
2012-12-11 20:39:47 +01:00
|
|
|
redirect_back_or_default project_time_entries_path(@time_entry.project)
|
2010-12-04 11:13:15 +01:00
|
|
|
}
|
2012-07-14 10:13:55 +02:00
|
|
|
format.api { render_api_ok }
|
2010-12-04 11:13:15 +01:00
|
|
|
end
|
2010-10-11 17:31:42 +02:00
|
|
|
else
|
2010-12-04 11:13:15 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => 'edit' }
|
|
|
|
format.api { render_validation_errors(@time_entry) }
|
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
end
|
2007-03-23 13:22:31 +01:00
|
|
|
end
|
2010-10-11 17:31:42 +02:00
|
|
|
|
2011-04-04 13:53:29 +02:00
|
|
|
def bulk_edit
|
2016-11-18 09:21:11 +01:00
|
|
|
@available_activities = @projects.map(&:activities).reduce(:&)
|
2016-10-23 13:51:26 +02:00
|
|
|
@custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
|
2011-04-04 13:53:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def bulk_update
|
2016-06-15 20:24:02 +02:00
|
|
|
attributes = parse_params_for_bulk_update(params[:time_entry])
|
2011-04-04 13:53:29 +02:00
|
|
|
|
|
|
|
unsaved_time_entry_ids = []
|
|
|
|
@time_entries.each do |time_entry|
|
|
|
|
time_entry.reload
|
2012-03-06 21:23:00 +01:00
|
|
|
time_entry.safe_attributes = attributes
|
2011-04-04 13:53:29 +02:00
|
|
|
call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
|
|
|
|
unless time_entry.save
|
2014-12-14 22:53:38 +01:00
|
|
|
logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info?
|
2011-04-04 13:53:29 +02:00
|
|
|
# Keep unsaved time_entry ids to display them in flash error
|
|
|
|
unsaved_time_entry_ids << time_entry.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
|
2012-12-11 20:39:47 +01:00
|
|
|
redirect_back_or_default project_time_entries_path(@projects.first)
|
2011-04-04 13:53:29 +02:00
|
|
|
end
|
|
|
|
|
2008-03-14 22:17:09 +01:00
|
|
|
def destroy
|
2012-03-04 16:24:14 +01:00
|
|
|
destroyed = TimeEntry.transaction do
|
|
|
|
@time_entries.each do |t|
|
2011-04-04 13:54:47 +02:00
|
|
|
unless t.destroy && t.destroyed?
|
2012-03-04 16:24:14 +01:00
|
|
|
raise ActiveRecord::Rollback
|
2011-04-04 13:54:47 +02:00
|
|
|
end
|
2010-12-04 11:13:15 +01:00
|
|
|
end
|
2010-06-20 21:30:51 +02:00
|
|
|
end
|
2011-04-04 13:54:47 +02:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2012-03-04 16:24:14 +01:00
|
|
|
if destroyed
|
|
|
|
flash[:notice] = l(:notice_successful_delete)
|
|
|
|
else
|
|
|
|
flash[:error] = l(:notice_unable_delete_time_entry)
|
|
|
|
end
|
2016-12-11 10:25:41 +01:00
|
|
|
redirect_back_or_default project_time_entries_path(@projects.first), :referer => true
|
2011-04-04 13:54:47 +02:00
|
|
|
}
|
2012-03-04 16:24:14 +01:00
|
|
|
format.api {
|
|
|
|
if destroyed
|
2012-07-14 10:13:55 +02:00
|
|
|
render_api_ok
|
2012-03-04 16:24:14 +01:00
|
|
|
else
|
|
|
|
render_validation_errors(@time_entries)
|
|
|
|
end
|
|
|
|
}
|
2011-04-04 13:54:47 +02:00
|
|
|
end
|
2008-03-14 22:17:09 +01:00
|
|
|
end
|
2007-03-23 13:22:31 +01:00
|
|
|
|
|
|
|
private
|
2010-11-16 21:27:45 +01:00
|
|
|
def find_time_entry
|
|
|
|
@time_entry = TimeEntry.find(params[:id])
|
2016-11-18 09:37:07 +01:00
|
|
|
@project = @time_entry.project
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_editability
|
2010-11-16 21:27:45 +01:00
|
|
|
unless @time_entry.editable_by?(User.current)
|
|
|
|
render_403
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-04 13:53:29 +02:00
|
|
|
def find_time_entries
|
2016-11-18 09:21:11 +01:00
|
|
|
@time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
|
|
|
|
preload(:project => :time_entry_activities).
|
|
|
|
preload(:user).to_a
|
|
|
|
|
2011-04-04 13:53:29 +02:00
|
|
|
raise ActiveRecord::RecordNotFound if @time_entries.empty?
|
2015-05-09 12:10:28 +02:00
|
|
|
raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
|
2011-04-04 13:53:29 +02:00
|
|
|
@projects = @time_entries.collect(&:project).compact.uniq
|
|
|
|
@project = @projects.first if @projects.size == 1
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
|
|
|
|
if unsaved_time_entry_ids.empty?
|
|
|
|
flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
|
|
|
|
else
|
|
|
|
flash[:error] = l(:notice_failed_to_save_time_entries,
|
|
|
|
:count => unsaved_time_entry_ids.size,
|
|
|
|
:total => time_entries.size,
|
|
|
|
:ids => '#' + unsaved_time_entry_ids.join(', #'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-12 21:28:49 +02:00
|
|
|
def find_optional_issue
|
2014-10-04 09:23:14 +02:00
|
|
|
if params[:issue_id].present?
|
2008-08-31 18:34:54 +02:00
|
|
|
@issue = Issue.find(params[:issue_id])
|
|
|
|
@project = @issue.project
|
2016-07-12 21:28:49 +02:00
|
|
|
else
|
|
|
|
find_optional_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_optional_project
|
|
|
|
if params[:project_id].present?
|
2008-08-31 18:34:54 +02:00
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
end
|
2014-10-04 09:23:14 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
2008-08-31 18:34:54 +02:00
|
|
|
end
|
2011-08-21 11:26:32 +02:00
|
|
|
|
2012-12-09 18:57:18 +01:00
|
|
|
# Returns the TimeEntry scope for index and report actions
|
2013-07-28 11:59:34 +02:00
|
|
|
def time_entry_scope(options={})
|
2016-07-12 21:28:49 +02:00
|
|
|
@query.results_scope(options)
|
2012-12-09 18:57:18 +01:00
|
|
|
end
|
2016-07-12 19:40:19 +02:00
|
|
|
|
|
|
|
def retrieve_time_entry_query
|
|
|
|
retrieve_query(TimeEntryQuery, false)
|
|
|
|
end
|
2007-03-23 13:22:31 +01:00
|
|
|
end
|