2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2010-12-04 10:13:15 +00:00
|
|
|
# Redmine - project management software
|
2022-01-02 05:29:10 +00:00
|
|
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
2007-06-24 16:07:06 +00: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 09:26:32 +00:00
|
|
|
#
|
2007-06-24 16:07:06 +00: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 09:26:32 +00:00
|
|
|
#
|
2007-06-24 16:07:06 +00: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 12:22:31 +00:00
|
|
|
class TimelogController < ApplicationController
|
2016-08-20 12:18:29 +00:00
|
|
|
menu_item :time_entries
|
2012-01-21 14:26:51 +00:00
|
|
|
|
2016-07-14 07:27:31 +00:00
|
|
|
before_action :find_time_entry, :only => [:show, :edit, :update]
|
2016-11-18 08:37:07 +00:00
|
|
|
before_action :check_editability, :only => [:edit, :update]
|
2016-07-14 07:27:31 +00: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 14:26:51 +00:00
|
|
|
|
2016-07-14 07:27:31 +00:00
|
|
|
before_action :find_optional_issue, :only => [:new, :create]
|
|
|
|
|
before_action :find_optional_project, :only => [:index, :report]
|
2012-01-21 14:26:51 +00:00
|
|
|
|
2011-07-09 08:56:07 +00:00
|
|
|
accept_rss_auth :index
|
|
|
|
|
accept_api_auth :index, :show, :create, :update, :destroy
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2012-12-09 21:13:26 +00:00
|
|
|
rescue_from Query::StatementInvalid, :with => :query_statement_invalid
|
2021-05-28 03:58:01 +00:00
|
|
|
rescue_from Query::QueryError, :with => :query_error
|
2012-12-09 21:13:26 +00:00
|
|
|
|
2007-10-03 17:20:04 +00:00
|
|
|
helper :issues
|
2008-02-26 18:15:58 +00:00
|
|
|
include TimelogHelper
|
2008-04-09 17:45:39 +00:00
|
|
|
helper :custom_fields
|
|
|
|
|
include CustomFieldsHelper
|
2012-12-09 17:57:18 +00:00
|
|
|
helper :queries
|
2013-02-19 20:41:54 +00:00
|
|
|
include QueriesHelper
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2010-10-06 18:23:45 +00:00
|
|
|
def index
|
2016-07-12 17:40:19 +00:00
|
|
|
retrieve_time_entry_query
|
2017-03-13 19:17:59 +00:00
|
|
|
scope = time_entry_scope.
|
2016-10-29 14:19:25 +00:00
|
|
|
preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
|
|
|
|
|
preload(:project, :user)
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2011-03-16 17:29:30 +00:00
|
|
|
respond_to do |format|
|
2020-10-05 15:48:39 +00:00
|
|
|
format.html do
|
2011-12-04 22:49:46 +00:00
|
|
|
@entry_count = scope.count
|
2013-01-08 20:18:12 +00:00
|
|
|
@entry_pages = Paginator.new @entry_count, per_page_option, params['page']
|
2014-10-22 17:37:16 +00:00
|
|
|
@entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
|
2008-04-05 16:40:26 +00:00
|
|
|
|
2011-03-16 17:29:30 +00:00
|
|
|
render :layout => !request.xhr?
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
|
|
|
|
format.api do
|
2011-12-04 22:49:46 +00:00
|
|
|
@entry_count = scope.count
|
2011-05-22 10:48:59 +00:00
|
|
|
@offset, @limit = api_offset_and_limit
|
2014-10-22 17:37:16 +00:00
|
|
|
@entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
|
|
|
|
format.atom do
|
2014-10-22 17:37:16 +00:00
|
|
|
entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
|
2011-03-16 17:29:30 +00:00
|
|
|
render_feed(entries, :title => l(:label_spent_time))
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
|
|
|
|
format.csv do
|
2011-03-16 17:29:30 +00:00
|
|
|
# Export all entries
|
2014-10-22 17:37:16 +00:00
|
|
|
@entries = scope.to_a
|
2013-02-23 11:07:43 +00:00
|
|
|
send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
2008-02-26 18:15:58 +00:00
|
|
|
end
|
2007-03-23 12:22:31 +00:00
|
|
|
end
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2011-12-02 19:33:05 +00:00
|
|
|
def report
|
2016-07-12 17:40:19 +00:00
|
|
|
retrieve_time_entry_query
|
2012-12-09 17:57:18 +00:00
|
|
|
scope = time_entry_scope
|
|
|
|
|
|
2021-08-19 13:13:26 +00:00
|
|
|
@report = Redmine::Helpers::TimeReport.new(@project, params[:criteria], params[:columns], scope)
|
2011-12-02 19:33:05 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
2020-10-11 12:57:23 +00:00
|
|
|
format.html {render :layout => !request.xhr?}
|
2020-10-03 15:36:59 +00:00
|
|
|
format.csv do
|
|
|
|
|
send_data(report_to_csv(@report), :type => 'text/csv; header=present',
|
|
|
|
|
:filename => 'timelog.csv')
|
|
|
|
|
end
|
2011-12-02 19:33:05 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2010-12-04 10:13:15 +00:00
|
|
|
def show
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
# TODO: Implement html response
|
2020-10-11 12:57:23 +00:00
|
|
|
format.html {head 406}
|
2010-12-04 17:43:39 +00:00
|
|
|
format.api
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
|
|
|
|
end
|
2010-10-07 15:51:09 +00:00
|
|
|
|
|
|
|
|
def new
|
2020-10-03 15:36:59 +00:00
|
|
|
@time_entry ||=
|
|
|
|
|
TimeEntry.new(:project => @project, :issue => @issue,
|
|
|
|
|
:author => User.current, :spent_on => User.current.today)
|
2012-03-06 20:23:00 +00:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2010-10-07 15:51:09 +00:00
|
|
|
end
|
2010-10-08 15:39:39 +00:00
|
|
|
|
|
|
|
|
def create
|
2020-10-03 15:36:59 +00:00
|
|
|
@time_entry ||=
|
|
|
|
|
TimeEntry.new(:project => @project, :issue => @issue,
|
|
|
|
|
:author => User.current, :user => User.current,
|
|
|
|
|
:spent_on => User.current.today)
|
2012-03-06 20:23:00 +00:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2014-10-04 07:23:14 +00:00
|
|
|
if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
|
|
|
|
|
render_403
|
|
|
|
|
return
|
|
|
|
|
end
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2020-10-03 15:36:59 +00:00
|
|
|
call_hook(:controller_timelog_edit_before_save,
|
|
|
|
|
{:params => params, :time_entry => @time_entry})
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2010-10-08 15:39:39 +00:00
|
|
|
if @time_entry.save
|
2010-12-04 10:13:15 +00:00
|
|
|
respond_to do |format|
|
2020-10-05 15:48:39 +00:00
|
|
|
format.html do
|
2012-01-21 10:37:19 +00:00
|
|
|
flash[:notice] = l(:notice_successful_create)
|
|
|
|
|
if params[:continue]
|
2014-10-04 07:23:14 +00:00
|
|
|
options = {
|
|
|
|
|
:time_entry => {
|
|
|
|
|
:project_id => params[:time_entry][:project_id],
|
|
|
|
|
:issue_id => @time_entry.issue_id,
|
2018-08-02 06:00:10 +00:00
|
|
|
:spent_on => @time_entry.spent_on,
|
2014-10-04 07:23:14 +00:00
|
|
|
: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 14:26:51 +00:00
|
|
|
else
|
2012-12-11 19:39:47 +00:00
|
|
|
redirect_to new_time_entry_path(options)
|
2012-01-21 14:26:51 +00:00
|
|
|
end
|
2012-01-21 10:37:19 +00:00
|
|
|
else
|
2012-12-11 19:39:47 +00:00
|
|
|
redirect_back_or_default project_time_entries_path(@time_entry.project)
|
2012-01-21 10:37:19 +00:00
|
|
|
end
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
2020-10-03 15:36:59 +00:00
|
|
|
format.api do
|
|
|
|
|
render :action => 'show', :status => :created, :location => time_entry_url(@time_entry)
|
|
|
|
|
end
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
2010-10-08 15:39:39 +00:00
|
|
|
else
|
2010-12-04 10:13:15 +00:00
|
|
|
respond_to do |format|
|
2020-10-11 12:57:23 +00:00
|
|
|
format.html {render :action => 'new'}
|
|
|
|
|
format.api {render_validation_errors(@time_entry)}
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
2011-08-21 09:26:32 +00:00
|
|
|
end
|
2010-10-08 15:39:39 +00:00
|
|
|
end
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2007-03-23 12:22:31 +00:00
|
|
|
def edit
|
2012-03-06 20:23:00 +00:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2010-10-11 15:31:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2012-03-06 20:23:00 +00:00
|
|
|
@time_entry.safe_attributes = params[:time_entry]
|
2020-10-03 15:36:59 +00:00
|
|
|
call_hook(:controller_timelog_edit_before_save,
|
|
|
|
|
{:params => params, :time_entry => @time_entry})
|
2011-08-21 09:26:32 +00:00
|
|
|
|
2010-10-11 15:31:42 +00:00
|
|
|
if @time_entry.save
|
2010-12-04 10:13:15 +00:00
|
|
|
respond_to do |format|
|
2020-10-05 15:48:39 +00:00
|
|
|
format.html do
|
2010-12-04 10:13:15 +00:00
|
|
|
flash[:notice] = l(:notice_successful_update)
|
2012-12-11 19:39:47 +00:00
|
|
|
redirect_back_or_default project_time_entries_path(@time_entry.project)
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
2020-10-11 12:57:23 +00:00
|
|
|
format.api {render_api_ok}
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
2010-10-11 15:31:42 +00:00
|
|
|
else
|
2010-12-04 10:13:15 +00:00
|
|
|
respond_to do |format|
|
2020-10-11 12:57:23 +00:00
|
|
|
format.html {render :action => 'edit'}
|
|
|
|
|
format.api {render_validation_errors(@time_entry)}
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
2011-08-21 09:26:32 +00:00
|
|
|
end
|
2007-03-23 12:22:31 +00:00
|
|
|
end
|
2010-10-11 15:31:42 +00:00
|
|
|
|
2011-04-04 11:53:29 +00:00
|
|
|
def bulk_edit
|
2018-09-14 03:40:37 +00:00
|
|
|
@target_projects = Project.allowed_to(:log_time).to_a
|
2016-10-23 11:51:26 +00:00
|
|
|
@custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
|
2018-09-14 03:40:37 +00:00
|
|
|
if params[:time_entry]
|
|
|
|
|
@target_project = @target_projects.detect {|p| p.id.to_s == params[:time_entry][:project_id].to_s}
|
|
|
|
|
end
|
|
|
|
|
if @target_project
|
|
|
|
|
@available_activities = @target_project.activities
|
|
|
|
|
else
|
|
|
|
|
@available_activities = @projects.map(&:activities).reduce(:&)
|
|
|
|
|
end
|
|
|
|
|
@time_entry_params = params[:time_entry] || {}
|
|
|
|
|
@time_entry_params[:custom_field_values] ||= {}
|
2011-04-04 11:53:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def bulk_update
|
2016-06-15 18:24:02 +00:00
|
|
|
attributes = parse_params_for_bulk_update(params[:time_entry])
|
2011-04-04 11:53:29 +00:00
|
|
|
|
2017-04-04 17:22:08 +00:00
|
|
|
unsaved_time_entries = []
|
|
|
|
|
saved_time_entries = []
|
|
|
|
|
|
2011-04-04 11:53:29 +00:00
|
|
|
@time_entries.each do |time_entry|
|
|
|
|
|
time_entry.reload
|
2012-03-06 20:23:00 +00:00
|
|
|
time_entry.safe_attributes = attributes
|
2020-10-03 15:36:59 +00:00
|
|
|
call_hook(
|
|
|
|
|
:controller_time_entries_bulk_edit_before_save,
|
|
|
|
|
{:params => params, :time_entry => time_entry}
|
|
|
|
|
)
|
2017-04-04 17:22:08 +00:00
|
|
|
if time_entry.save
|
|
|
|
|
saved_time_entries << time_entry
|
|
|
|
|
else
|
|
|
|
|
unsaved_time_entries << time_entry
|
2011-04-04 11:53:29 +00:00
|
|
|
end
|
|
|
|
|
end
|
2017-04-04 17:22:08 +00:00
|
|
|
|
|
|
|
|
if unsaved_time_entries.empty?
|
|
|
|
|
flash[:notice] = l(:notice_successful_update) unless saved_time_entries.empty?
|
|
|
|
|
redirect_back_or_default project_time_entries_path(@projects.first)
|
|
|
|
|
else
|
|
|
|
|
@saved_time_entries = @time_entries
|
|
|
|
|
@unsaved_time_entries = unsaved_time_entries
|
|
|
|
|
@time_entries = TimeEntry.where(:id => unsaved_time_entries.map(&:id)).
|
|
|
|
|
preload(:project => :time_entry_activities).
|
|
|
|
|
preload(:user).to_a
|
|
|
|
|
|
|
|
|
|
bulk_edit
|
|
|
|
|
render :action => 'bulk_edit'
|
|
|
|
|
end
|
2011-04-04 11:53:29 +00:00
|
|
|
end
|
|
|
|
|
|
2008-03-14 21:17:09 +00:00
|
|
|
def destroy
|
2012-03-04 15:24:14 +00:00
|
|
|
destroyed = TimeEntry.transaction do
|
|
|
|
|
@time_entries.each do |t|
|
2011-04-04 11:54:47 +00:00
|
|
|
unless t.destroy && t.destroyed?
|
2012-03-04 15:24:14 +00:00
|
|
|
raise ActiveRecord::Rollback
|
2011-04-04 11:54:47 +00:00
|
|
|
end
|
2010-12-04 10:13:15 +00:00
|
|
|
end
|
2010-06-20 19:30:51 +00:00
|
|
|
end
|
2011-04-04 11:54:47 +00:00
|
|
|
|
|
|
|
|
respond_to do |format|
|
2020-10-05 15:48:39 +00:00
|
|
|
format.html do
|
2012-03-04 15:24:14 +00:00
|
|
|
if destroyed
|
|
|
|
|
flash[:notice] = l(:notice_successful_delete)
|
|
|
|
|
else
|
|
|
|
|
flash[:error] = l(:notice_unable_delete_time_entry)
|
|
|
|
|
end
|
2016-12-11 09:25:41 +00:00
|
|
|
redirect_back_or_default project_time_entries_path(@projects.first), :referer => true
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
|
|
|
|
format.api do
|
2012-03-04 15:24:14 +00:00
|
|
|
if destroyed
|
2012-07-14 08:13:55 +00:00
|
|
|
render_api_ok
|
2012-03-04 15:24:14 +00:00
|
|
|
else
|
|
|
|
|
render_validation_errors(@time_entries)
|
|
|
|
|
end
|
2020-10-05 15:48:39 +00:00
|
|
|
end
|
2011-04-04 11:54:47 +00:00
|
|
|
end
|
2008-03-14 21:17:09 +00:00
|
|
|
end
|
2007-03-23 12:22:31 +00:00
|
|
|
|
2019-10-22 13:44:15 +00:00
|
|
|
private
|
|
|
|
|
|
2010-11-16 20:27:45 +00:00
|
|
|
def find_time_entry
|
|
|
|
|
@time_entry = TimeEntry.find(params[:id])
|
2016-11-18 08:37:07 +00:00
|
|
|
@project = @time_entry.project
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check_editability
|
2010-11-16 20:27:45 +00:00
|
|
|
unless @time_entry.editable_by?(User.current)
|
|
|
|
|
render_403
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2011-04-04 11:53:29 +00:00
|
|
|
def find_time_entries
|
2016-11-18 08:21:11 +00:00
|
|
|
@time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
|
|
|
|
|
preload(:project => :time_entry_activities).
|
|
|
|
|
preload(:user).to_a
|
|
|
|
|
|
2011-04-04 11:53:29 +00:00
|
|
|
raise ActiveRecord::RecordNotFound if @time_entries.empty?
|
2015-05-09 10:10:28 +00:00
|
|
|
raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
|
2020-10-03 15:37:30 +00:00
|
|
|
|
2011-04-04 11:53:29 +00:00
|
|
|
@projects = @time_entries.collect(&:project).compact.uniq
|
|
|
|
|
@project = @projects.first if @projects.size == 1
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-12 19:28:49 +00:00
|
|
|
def find_optional_issue
|
2020-04-05 17:29:53 +00:00
|
|
|
if params[:issue_id].present?
|
|
|
|
|
@issue = Issue.find(params[:issue_id])
|
2008-08-31 16:34:54 +00:00
|
|
|
@project = @issue.project
|
2017-06-26 19:53:09 +00:00
|
|
|
authorize
|
2016-07-12 19:28:49 +00:00
|
|
|
else
|
|
|
|
|
find_optional_project
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-09 17:57:18 +00:00
|
|
|
# Returns the TimeEntry scope for index and report actions
|
2013-07-28 09:59:34 +00:00
|
|
|
def time_entry_scope(options={})
|
2016-07-12 19:28:49 +00:00
|
|
|
@query.results_scope(options)
|
2012-12-09 17:57:18 +00:00
|
|
|
end
|
2016-07-12 17:40:19 +00:00
|
|
|
|
|
|
|
|
def retrieve_time_entry_query
|
2018-09-23 11:59:52 +00:00
|
|
|
retrieve_query(TimeEntryQuery, false, :defaults => @default_columns_names)
|
2016-07-12 17:40:19 +00:00
|
|
|
end
|
2021-05-28 03:58:01 +00:00
|
|
|
|
|
|
|
|
def query_error(exception)
|
|
|
|
|
session.delete(:time_entry_query)
|
|
|
|
|
super
|
|
|
|
|
end
|
2007-03-23 12:22:31 +00:00
|
|
|
end
|