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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. before_action :authorize_logging_time_for_other_users, :only => [:create, :update]
  27. accept_rss_auth :index
  28. accept_api_auth :index, :show, :create, :update, :destroy
  29. rescue_from Query::StatementInvalid, :with => :query_statement_invalid
  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 {
  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. }
  48. format.api {
  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. }
  53. format.atom {
  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. }
  57. format.csv {
  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. }
  62. end
  63. end
  64. def report
  65. retrieve_time_entry_query
  66. scope = time_entry_scope
  67. @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
  68. respond_to do |format|
  69. format.html { render :layout => !request.xhr? }
  70. format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
  71. end
  72. end
  73. def show
  74. respond_to do |format|
  75. # TODO: Implement html response
  76. format.html { head 406 }
  77. format.api
  78. end
  79. end
  80. def new
  81. @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :author => User.current, :spent_on => User.current.today)
  82. @time_entry.safe_attributes = params[:time_entry]
  83. end
  84. def create
  85. @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :author => User.current, :user => User.current, :spent_on => User.current.today)
  86. @time_entry.safe_attributes = params[:time_entry]
  87. if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
  88. render_403
  89. return
  90. end
  91. call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
  92. if @time_entry.save
  93. respond_to do |format|
  94. format.html {
  95. flash[:notice] = l(:notice_successful_create)
  96. if params[:continue]
  97. options = {
  98. :time_entry => {
  99. :project_id => params[:time_entry][:project_id],
  100. :issue_id => @time_entry.issue_id,
  101. :spent_on => @time_entry.spent_on,
  102. :activity_id => @time_entry.activity_id
  103. },
  104. :back_url => params[:back_url]
  105. }
  106. if params[:project_id] && @time_entry.project
  107. redirect_to new_project_time_entry_path(@time_entry.project, options)
  108. elsif params[:issue_id] && @time_entry.issue
  109. redirect_to new_issue_time_entry_path(@time_entry.issue, options)
  110. else
  111. redirect_to new_time_entry_path(options)
  112. end
  113. else
  114. redirect_back_or_default project_time_entries_path(@time_entry.project)
  115. end
  116. }
  117. format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
  118. end
  119. else
  120. respond_to do |format|
  121. format.html { render :action => 'new' }
  122. format.api { render_validation_errors(@time_entry) }
  123. end
  124. end
  125. end
  126. def edit
  127. @time_entry.safe_attributes = params[:time_entry]
  128. end
  129. def update
  130. @time_entry.safe_attributes = params[:time_entry]
  131. call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
  132. if @time_entry.save
  133. respond_to do |format|
  134. format.html {
  135. flash[:notice] = l(:notice_successful_update)
  136. redirect_back_or_default project_time_entries_path(@time_entry.project)
  137. }
  138. format.api { render_api_ok }
  139. end
  140. else
  141. respond_to do |format|
  142. format.html { render :action => 'edit' }
  143. format.api { render_validation_errors(@time_entry) }
  144. end
  145. end
  146. end
  147. def bulk_edit
  148. @target_projects = Project.allowed_to(:log_time).to_a
  149. @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
  150. if params[:time_entry]
  151. @target_project = @target_projects.detect {|p| p.id.to_s == params[:time_entry][:project_id].to_s}
  152. end
  153. if @target_project
  154. @available_activities = @target_project.activities
  155. else
  156. @available_activities = @projects.map(&:activities).reduce(:&)
  157. end
  158. @time_entry_params = params[:time_entry] || {}
  159. @time_entry_params[:custom_field_values] ||= {}
  160. end
  161. def bulk_update
  162. attributes = parse_params_for_bulk_update(params[:time_entry])
  163. unsaved_time_entries = []
  164. saved_time_entries = []
  165. @time_entries.each do |time_entry|
  166. time_entry.reload
  167. time_entry.safe_attributes = attributes
  168. call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
  169. if time_entry.save
  170. saved_time_entries << time_entry
  171. else
  172. unsaved_time_entries << time_entry
  173. end
  174. end
  175. if unsaved_time_entries.empty?
  176. flash[:notice] = l(:notice_successful_update) unless saved_time_entries.empty?
  177. redirect_back_or_default project_time_entries_path(@projects.first)
  178. else
  179. @saved_time_entries = @time_entries
  180. @unsaved_time_entries = unsaved_time_entries
  181. @time_entries = TimeEntry.where(:id => unsaved_time_entries.map(&:id)).
  182. preload(:project => :time_entry_activities).
  183. preload(:user).to_a
  184. bulk_edit
  185. render :action => 'bulk_edit'
  186. end
  187. end
  188. def destroy
  189. destroyed = TimeEntry.transaction do
  190. @time_entries.each do |t|
  191. unless t.destroy && t.destroyed?
  192. raise ActiveRecord::Rollback
  193. end
  194. end
  195. end
  196. respond_to do |format|
  197. format.html {
  198. if destroyed
  199. flash[:notice] = l(:notice_successful_delete)
  200. else
  201. flash[:error] = l(:notice_unable_delete_time_entry)
  202. end
  203. redirect_back_or_default project_time_entries_path(@projects.first), :referer => true
  204. }
  205. format.api {
  206. if destroyed
  207. render_api_ok
  208. else
  209. render_validation_errors(@time_entries)
  210. end
  211. }
  212. end
  213. end
  214. private
  215. def find_time_entry
  216. @time_entry = TimeEntry.find(params[:id])
  217. @project = @time_entry.project
  218. rescue ActiveRecord::RecordNotFound
  219. render_404
  220. end
  221. def check_editability
  222. unless @time_entry.editable_by?(User.current)
  223. render_403
  224. return false
  225. end
  226. end
  227. def authorize_logging_time_for_other_users
  228. if !User.current.allowed_to?(:log_time_for_other_users, @project) && params['time_entry'].present? && params['time_entry']['user_id'].present? && params['time_entry']['user_id'].to_i != User.current.id
  229. render_error :message => l(:error_not_allowed_to_log_time_for_other_users), :status => 403
  230. return false
  231. end
  232. end
  233. def find_time_entries
  234. @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
  235. preload(:project => :time_entry_activities).
  236. preload(:user).to_a
  237. raise ActiveRecord::RecordNotFound if @time_entries.empty?
  238. raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
  239. @projects = @time_entries.collect(&:project).compact.uniq
  240. @project = @projects.first if @projects.size == 1
  241. rescue ActiveRecord::RecordNotFound
  242. render_404
  243. end
  244. def find_optional_issue
  245. if params[:issue_id].present?
  246. @issue = Issue.find(params[:issue_id])
  247. @project = @issue.project
  248. authorize
  249. else
  250. find_optional_project
  251. end
  252. end
  253. # Returns the TimeEntry scope for index and report actions
  254. def time_entry_scope(options={})
  255. @query.results_scope(options)
  256. end
  257. def retrieve_time_entry_query
  258. retrieve_query(TimeEntryQuery, false, :defaults => @default_columns_names)
  259. end
  260. end