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

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