You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timelog_controller.rb 9.5KB

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