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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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. rescue_from Query::QueryError, :with => :query_error
  30. helper :issues
  31. include TimelogHelper
  32. helper :custom_fields
  33. include CustomFieldsHelper
  34. helper :queries
  35. include QueriesHelper
  36. def index
  37. retrieve_time_entry_query
  38. scope = time_entry_scope.
  39. preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
  40. preload(:project, :user)
  41. respond_to do |format|
  42. format.html do
  43. @entry_count = scope.count
  44. @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
  45. @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
  46. render :layout => !request.xhr?
  47. end
  48. format.api do
  49. @entry_count = scope.count
  50. @offset, @limit = api_offset_and_limit
  51. @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
  52. end
  53. format.atom do
  54. entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
  55. render_feed(entries, :title => l(:label_spent_time))
  56. end
  57. format.csv do
  58. # Export all entries
  59. @entries = scope.to_a
  60. send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
  61. end
  62. end
  63. end
  64. def report
  65. retrieve_time_entry_query
  66. scope = time_entry_scope
  67. @report = Redmine::Helpers::TimeReport.new(@project, params[:criteria], params[:columns], scope)
  68. respond_to do |format|
  69. format.html {render :layout => !request.xhr?}
  70. format.csv do
  71. send_data(report_to_csv(@report), :type => 'text/csv; header=present',
  72. :filename => 'timelog.csv')
  73. end
  74. end
  75. end
  76. def show
  77. respond_to do |format|
  78. # TODO: Implement html response
  79. format.html {head 406}
  80. format.api
  81. end
  82. end
  83. def new
  84. @time_entry ||=
  85. TimeEntry.new(:project => @project, :issue => @issue,
  86. :author => User.current, :spent_on => User.current.today)
  87. @time_entry.safe_attributes = params[:time_entry]
  88. end
  89. def create
  90. @time_entry ||=
  91. TimeEntry.new(:project => @project, :issue => @issue,
  92. :author => User.current, :user => User.current,
  93. :spent_on => User.current.today)
  94. @time_entry.safe_attributes = params[:time_entry]
  95. if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
  96. render_403
  97. return
  98. end
  99. call_hook(:controller_timelog_edit_before_save,
  100. {:params => params, :time_entry => @time_entry})
  101. if @time_entry.save
  102. respond_to do |format|
  103. format.html do
  104. flash[:notice] = l(:notice_successful_create)
  105. if params[:continue]
  106. options = {
  107. :time_entry => {
  108. :project_id => params[:time_entry][:project_id],
  109. :issue_id => @time_entry.issue_id,
  110. :spent_on => @time_entry.spent_on,
  111. :activity_id => @time_entry.activity_id
  112. },
  113. :back_url => params[:back_url]
  114. }
  115. if params[:project_id] && @time_entry.project
  116. redirect_to new_project_time_entry_path(@time_entry.project, options)
  117. elsif params[:issue_id] && @time_entry.issue
  118. redirect_to new_issue_time_entry_path(@time_entry.issue, options)
  119. else
  120. redirect_to new_time_entry_path(options)
  121. end
  122. else
  123. redirect_back_or_default project_time_entries_path(@time_entry.project)
  124. end
  125. end
  126. format.api do
  127. render :action => 'show', :status => :created, :location => time_entry_url(@time_entry)
  128. end
  129. end
  130. else
  131. respond_to do |format|
  132. format.html {render :action => 'new'}
  133. format.api {render_validation_errors(@time_entry)}
  134. end
  135. end
  136. end
  137. def edit
  138. @time_entry.safe_attributes = params[:time_entry]
  139. end
  140. def update
  141. @time_entry.safe_attributes = params[:time_entry]
  142. call_hook(:controller_timelog_edit_before_save,
  143. {:params => params, :time_entry => @time_entry})
  144. if @time_entry.save
  145. respond_to do |format|
  146. format.html do
  147. flash[:notice] = l(:notice_successful_update)
  148. redirect_back_or_default project_time_entries_path(@time_entry.project)
  149. end
  150. format.api {render_api_ok}
  151. end
  152. else
  153. respond_to do |format|
  154. format.html {render :action => 'edit'}
  155. format.api {render_validation_errors(@time_entry)}
  156. end
  157. end
  158. end
  159. def bulk_edit
  160. @target_projects = Project.allowed_to(:log_time).to_a
  161. @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
  162. if params[:time_entry]
  163. @target_project = @target_projects.detect {|p| p.id.to_s == params[:time_entry][:project_id].to_s}
  164. end
  165. if @target_project
  166. @available_activities = @target_project.activities
  167. else
  168. @available_activities = @projects.map(&:activities).reduce(:&)
  169. end
  170. @time_entry_params = params[:time_entry] || {}
  171. @time_entry_params[:custom_field_values] ||= {}
  172. end
  173. def bulk_update
  174. attributes = parse_params_for_bulk_update(params[:time_entry])
  175. unsaved_time_entries = []
  176. saved_time_entries = []
  177. @time_entries.each do |time_entry|
  178. time_entry.reload
  179. time_entry.safe_attributes = attributes
  180. call_hook(
  181. :controller_time_entries_bulk_edit_before_save,
  182. {:params => params, :time_entry => time_entry}
  183. )
  184. if time_entry.save
  185. saved_time_entries << time_entry
  186. else
  187. unsaved_time_entries << time_entry
  188. end
  189. end
  190. if unsaved_time_entries.empty?
  191. flash[:notice] = l(:notice_successful_update) unless saved_time_entries.empty?
  192. redirect_back_or_default project_time_entries_path(@projects.first)
  193. else
  194. @saved_time_entries = @time_entries
  195. @unsaved_time_entries = unsaved_time_entries
  196. @time_entries = TimeEntry.where(:id => unsaved_time_entries.map(&:id)).
  197. preload(:project => :time_entry_activities).
  198. preload(:user).to_a
  199. bulk_edit
  200. render :action => 'bulk_edit'
  201. end
  202. end
  203. def destroy
  204. destroyed = TimeEntry.transaction do
  205. @time_entries.each do |t|
  206. unless t.destroy && t.destroyed?
  207. raise ActiveRecord::Rollback
  208. end
  209. end
  210. end
  211. respond_to do |format|
  212. format.html do
  213. if destroyed
  214. flash[:notice] = l(:notice_successful_delete)
  215. else
  216. flash[:error] = l(:notice_unable_delete_time_entry)
  217. end
  218. redirect_back_or_default project_time_entries_path(@projects.first), :referer => true
  219. end
  220. format.api do
  221. if destroyed
  222. render_api_ok
  223. else
  224. render_validation_errors(@time_entries)
  225. end
  226. end
  227. end
  228. end
  229. private
  230. def find_time_entry
  231. @time_entry = TimeEntry.find(params[:id])
  232. @project = @time_entry.project
  233. rescue ActiveRecord::RecordNotFound
  234. render_404
  235. end
  236. def check_editability
  237. unless @time_entry.editable_by?(User.current)
  238. render_403
  239. return false
  240. end
  241. end
  242. def find_time_entries
  243. @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
  244. preload(:project => :time_entry_activities).
  245. preload(:user).to_a
  246. raise ActiveRecord::RecordNotFound if @time_entries.empty?
  247. raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
  248. @projects = @time_entries.collect(&:project).compact.uniq
  249. @project = @projects.first if @projects.size == 1
  250. rescue ActiveRecord::RecordNotFound
  251. render_404
  252. end
  253. def find_optional_issue
  254. if params[:issue_id].present?
  255. @issue = Issue.find(params[:issue_id])
  256. @project = @issue.project
  257. authorize
  258. else
  259. find_optional_project
  260. end
  261. end
  262. # Returns the TimeEntry scope for index and report actions
  263. def time_entry_scope(options={})
  264. @query.results_scope(options)
  265. end
  266. def retrieve_time_entry_query
  267. retrieve_query(TimeEntryQuery, false, :defaults => @default_columns_names)
  268. end
  269. def query_error(exception)
  270. session.delete(:time_entry_query)
  271. super
  272. end
  273. end