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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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 :issues
  19. before_filter :find_project_for_new_time_entry, :only => [:create]
  20. before_filter :find_time_entry, :only => [:show, :edit, :update]
  21. before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
  22. before_filter :authorize, :except => [:new, :index, :report]
  23. before_filter :find_optional_project, :only => [:index, :report]
  24. before_filter :find_optional_project_for_new_time_entry, :only => [:new]
  25. before_filter :authorize_global, :only => [:new, :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. @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
  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. includes(:project, :user, :issue).
  43. preload(:issue => [:project, :tracker, :status, :assigned_to, :priority])
  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).all
  49. @total_hours = scope.sum(:hours).to_f
  50. render :layout => !request.xhr?
  51. }
  52. format.api {
  53. @entry_count = scope.count
  54. @offset, @limit = api_offset_and_limit
  55. @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).all
  56. }
  57. format.atom {
  58. entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").all
  59. render_feed(entries, :title => l(:label_spent_time))
  60. }
  61. format.csv {
  62. # Export all entries
  63. @entries = scope.all
  64. send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
  65. }
  66. end
  67. end
  68. def report
  69. @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
  70. scope = time_entry_scope
  71. @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
  72. respond_to do |format|
  73. format.html { render :layout => !request.xhr? }
  74. format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
  75. end
  76. end
  77. def show
  78. respond_to do |format|
  79. # TODO: Implement html response
  80. format.html { render :nothing => true, :status => 406 }
  81. format.api
  82. end
  83. end
  84. def new
  85. @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
  86. @time_entry.safe_attributes = params[:time_entry]
  87. end
  88. def create
  89. @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
  90. @time_entry.safe_attributes = params[:time_entry]
  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. if params[:project_id]
  98. options = {
  99. :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
  100. :back_url => params[:back_url]
  101. }
  102. if @time_entry.issue
  103. redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
  104. else
  105. redirect_to new_project_time_entry_path(@time_entry.project, options)
  106. end
  107. else
  108. options = {
  109. :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
  110. :back_url => params[:back_url]
  111. }
  112. redirect_to new_time_entry_path(options)
  113. end
  114. else
  115. redirect_back_or_default project_time_entries_path(@time_entry.project)
  116. end
  117. }
  118. format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
  119. end
  120. else
  121. respond_to do |format|
  122. format.html { render :action => 'new' }
  123. format.api { render_validation_errors(@time_entry) }
  124. end
  125. end
  126. end
  127. def edit
  128. @time_entry.safe_attributes = params[:time_entry]
  129. end
  130. def update
  131. @time_entry.safe_attributes = params[:time_entry]
  132. call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
  133. if @time_entry.save
  134. respond_to do |format|
  135. format.html {
  136. flash[:notice] = l(:notice_successful_update)
  137. redirect_back_or_default project_time_entries_path(@time_entry.project)
  138. }
  139. format.api { render_api_ok }
  140. end
  141. else
  142. respond_to do |format|
  143. format.html { render :action => 'edit' }
  144. format.api { render_validation_errors(@time_entry) }
  145. end
  146. end
  147. end
  148. def bulk_edit
  149. @available_activities = TimeEntryActivity.shared.active
  150. @custom_fields = TimeEntry.first.available_custom_fields
  151. end
  152. def bulk_update
  153. attributes = parse_params_for_bulk_time_entry_attributes(params)
  154. unsaved_time_entry_ids = []
  155. @time_entries.each do |time_entry|
  156. time_entry.reload
  157. time_entry.safe_attributes = attributes
  158. call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
  159. unless time_entry.save
  160. logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info
  161. # Keep unsaved time_entry ids to display them in flash error
  162. unsaved_time_entry_ids << time_entry.id
  163. end
  164. end
  165. set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
  166. redirect_back_or_default project_time_entries_path(@projects.first)
  167. end
  168. def destroy
  169. destroyed = TimeEntry.transaction do
  170. @time_entries.each do |t|
  171. unless t.destroy && t.destroyed?
  172. raise ActiveRecord::Rollback
  173. end
  174. end
  175. end
  176. respond_to do |format|
  177. format.html {
  178. if destroyed
  179. flash[:notice] = l(:notice_successful_delete)
  180. else
  181. flash[:error] = l(:notice_unable_delete_time_entry)
  182. end
  183. redirect_back_or_default project_time_entries_path(@projects.first)
  184. }
  185. format.api {
  186. if destroyed
  187. render_api_ok
  188. else
  189. render_validation_errors(@time_entries)
  190. end
  191. }
  192. end
  193. end
  194. private
  195. def find_time_entry
  196. @time_entry = TimeEntry.find(params[:id])
  197. unless @time_entry.editable_by?(User.current)
  198. render_403
  199. return false
  200. end
  201. @project = @time_entry.project
  202. rescue ActiveRecord::RecordNotFound
  203. render_404
  204. end
  205. def find_time_entries
  206. @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
  207. raise ActiveRecord::RecordNotFound if @time_entries.empty?
  208. @projects = @time_entries.collect(&:project).compact.uniq
  209. @project = @projects.first if @projects.size == 1
  210. rescue ActiveRecord::RecordNotFound
  211. render_404
  212. end
  213. def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
  214. if unsaved_time_entry_ids.empty?
  215. flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
  216. else
  217. flash[:error] = l(:notice_failed_to_save_time_entries,
  218. :count => unsaved_time_entry_ids.size,
  219. :total => time_entries.size,
  220. :ids => '#' + unsaved_time_entry_ids.join(', #'))
  221. end
  222. end
  223. def find_optional_project_for_new_time_entry
  224. if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
  225. @project = Project.find(project_id)
  226. end
  227. if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
  228. @issue = Issue.find(issue_id)
  229. @project ||= @issue.project
  230. end
  231. rescue ActiveRecord::RecordNotFound
  232. render_404
  233. end
  234. def find_project_for_new_time_entry
  235. find_optional_project_for_new_time_entry
  236. if @project.nil?
  237. render_404
  238. end
  239. end
  240. def find_optional_project
  241. if !params[:issue_id].blank?
  242. @issue = Issue.find(params[:issue_id])
  243. @project = @issue.project
  244. elsif !params[:project_id].blank?
  245. @project = Project.find(params[:project_id])
  246. end
  247. end
  248. # Returns the TimeEntry scope for index and report actions
  249. def time_entry_scope(options={})
  250. scope = @query.results_scope(options)
  251. if @issue
  252. scope = scope.on_issue(@issue)
  253. end
  254. scope
  255. end
  256. def parse_params_for_bulk_time_entry_attributes(params)
  257. attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
  258. attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
  259. attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
  260. attributes
  261. end
  262. end