diff app/controllers/timelog_controller.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
line wrap: on
line diff
--- a/app/controllers/timelog_controller.rb	Fri Jun 14 09:07:32 2013 +0100
+++ b/app/controllers/timelog_controller.rb	Fri Jun 14 09:28:30 2013 +0100
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2012  Jean-Philippe Lang
+# Copyright (C) 2006-2013  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -30,41 +30,34 @@
   accept_rss_auth :index
   accept_api_auth :index, :show, :create, :update, :destroy
 
+  rescue_from Query::StatementInvalid, :with => :query_statement_invalid
+
   helper :sort
   include SortHelper
   helper :issues
   include TimelogHelper
   helper :custom_fields
   include CustomFieldsHelper
+  helper :queries
+  include QueriesHelper
 
   def index
-    sort_init 'spent_on', 'desc'
-    sort_update 'spent_on' => ['spent_on', "#{TimeEntry.table_name}.created_on"],
-                'user' => 'user_id',
-                'activity' => 'activity_id',
-                'project' => "#{Project.table_name}.name",
-                'issue' => 'issue_id',
-                'hours' => 'hours'
+    @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
+    scope = time_entry_scope
 
-    retrieve_date_range
-
-    scope = TimeEntry.visible.spent_between(@from, @to)
-    if @issue
-      scope = scope.on_issue(@issue)
-    elsif @project
-      scope = scope.on_project(@project, Setting.display_subprojects_issues?)
-    end
+    sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
+    sort_update(@query.sortable_columns)
 
     respond_to do |format|
       format.html {
         # Paginate results
         @entry_count = scope.count
-        @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
+        @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
         @entries = scope.all(
           :include => [:project, :activity, :user, {:issue => :tracker}],
           :order => sort_clause,
-          :limit  =>  @entry_pages.items_per_page,
-          :offset =>  @entry_pages.current.offset
+          :limit  =>  @entry_pages.per_page,
+          :offset =>  @entry_pages.offset
         )
         @total_hours = scope.sum(:hours).to_f
 
@@ -94,14 +87,16 @@
           :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
           :order => sort_clause
         )
-        send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv')
+        send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
       }
     end
   end
 
   def report
-    retrieve_date_range
-    @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], @from, @to)
+    @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
+    scope = time_entry_scope
+
+    @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
 
     respond_to do |format|
       format.html { render :layout => !request.xhr? }
@@ -134,16 +129,24 @@
           flash[:notice] = l(:notice_successful_create)
           if params[:continue]
             if params[:project_id]
-              redirect_to :action => 'new', :project_id => @time_entry.project, :issue_id => @time_entry.issue,
+              options = {
                 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
                 :back_url => params[:back_url]
+              }
+              if @time_entry.issue
+                redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
+              else
+                redirect_to new_project_time_entry_path(@time_entry.project, options)
+              end
             else
-              redirect_to :action => 'new',
+              options = {
                 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
                 :back_url => params[:back_url]
+              }
+              redirect_to new_time_entry_path(options)
             end
           else
-            redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+            redirect_back_or_default project_time_entries_path(@time_entry.project)
           end
         }
         format.api  { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
@@ -169,7 +172,7 @@
       respond_to do |format|
         format.html {
           flash[:notice] = l(:notice_successful_update)
-          redirect_back_or_default :action => 'index', :project_id => @time_entry.project
+          redirect_back_or_default project_time_entries_path(@time_entry.project)
         }
         format.api  { render_api_ok }
       end
@@ -200,7 +203,7 @@
       end
     end
     set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
-    redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first})
+    redirect_back_or_default project_time_entries_path(@projects.first)
   end
 
   def destroy
@@ -219,7 +222,7 @@
         else
           flash[:error] = l(:notice_unable_delete_time_entry)
         end
-        redirect_back_or_default(:action => 'index', :project_id => @projects.first)
+        redirect_back_or_default project_time_entries_path(@projects.first)
       }
       format.api  {
         if destroyed
@@ -291,51 +294,15 @@
     end
   end
 
-  # Retrieves the date range based on predefined ranges or specific from/to param dates
-  def retrieve_date_range
-    @free_period = false
-    @from, @to = nil, nil
-
-    if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
-      case params[:period].to_s
-      when 'today'
-        @from = @to = Date.today
-      when 'yesterday'
-        @from = @to = Date.today - 1
-      when 'current_week'
-        @from = Date.today - (Date.today.cwday - 1)%7
-        @to = @from + 6
-      when 'last_week'
-        @from = Date.today - 7 - (Date.today.cwday - 1)%7
-        @to = @from + 6
-      when 'last_2_weeks'
-        @from = Date.today - 14 - (Date.today.cwday - 1)%7
-        @to = @from + 13
-      when '7_days'
-        @from = Date.today - 7
-        @to = Date.today
-      when 'current_month'
-        @from = Date.civil(Date.today.year, Date.today.month, 1)
-        @to = (@from >> 1) - 1
-      when 'last_month'
-        @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
-        @to = (@from >> 1) - 1
-      when '30_days'
-        @from = Date.today - 30
-        @to = Date.today
-      when 'current_year'
-        @from = Date.civil(Date.today.year, 1, 1)
-        @to = Date.civil(Date.today.year, 12, 31)
-      end
-    elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
-      begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
-      begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
-      @free_period = true
-    else
-      # default
+  # Returns the TimeEntry scope for index and report actions
+  def time_entry_scope
+    scope = TimeEntry.visible.where(@query.statement)
+    if @issue
+      scope = scope.on_issue(@issue)
+    elsif @project
+      scope = scope.on_project(@project, Setting.display_subprojects_issues?)
     end
-
-    @from, @to = @to, @from if @from && @to && @from > @to
+    scope
   end
 
   def parse_params_for_bulk_time_entry_attributes(params)