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

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