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

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