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

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