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

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