Chris@119: # Redmine - project management software Chris@507: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class TimelogController < ApplicationController Chris@0: menu_item :issues chris@37: before_filter :find_project, :only => [:new, :create] Chris@441: before_filter :find_time_entry, :only => [:show, :edit, :update] Chris@441: before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy] chris@37: before_filter :authorize, :except => [:index] chris@37: before_filter :find_optional_project, :only => [:index] Chris@507: accept_rss_auth :index Chris@507: accept_api_auth :index, :show, :create, :update, :destroy Chris@909: Chris@0: helper :sort Chris@0: include SortHelper Chris@0: helper :issues Chris@0: include TimelogHelper Chris@0: helper :custom_fields Chris@0: include CustomFieldsHelper Chris@909: chris@37: def index Chris@0: sort_init 'spent_on', 'desc' Chris@0: sort_update 'spent_on' => 'spent_on', Chris@0: 'user' => 'user_id', Chris@0: 'activity' => 'activity_id', Chris@0: 'project' => "#{Project.table_name}.name", Chris@0: 'issue' => 'issue_id', Chris@0: 'hours' => 'hours' Chris@909: Chris@0: cond = ARCondition.new Chris@441: if @issue Chris@441: cond << "#{Issue.table_name}.root_id = #{@issue.root_id} AND #{Issue.table_name}.lft >= #{@issue.lft} AND #{Issue.table_name}.rgt <= #{@issue.rgt}" Chris@441: elsif @project Chris@0: cond << @project.project_condition(Setting.display_subprojects_issues?) Chris@0: end Chris@909: Chris@0: retrieve_date_range Chris@0: cond << ['spent_on BETWEEN ? AND ?', @from, @to] Chris@0: Chris@441: respond_to do |format| Chris@441: format.html { Chris@441: # Paginate results Chris@441: @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions) Chris@441: @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] Chris@909: @entries = TimeEntry.visible.find(:all, Chris@441: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@441: :conditions => cond.conditions, Chris@441: :order => sort_clause, Chris@441: :limit => @entry_pages.items_per_page, Chris@441: :offset => @entry_pages.current.offset) Chris@441: @total_hours = TimeEntry.visible.sum(:hours, :include => [:project, :issue], :conditions => cond.conditions).to_f Chris@0: Chris@441: render :layout => !request.xhr? Chris@441: } Chris@441: format.api { Chris@441: @entry_count = TimeEntry.visible.count(:include => [:project, :issue], :conditions => cond.conditions) Chris@441: @offset, @limit = api_offset_and_limit Chris@909: @entries = TimeEntry.visible.find(:all, Chris@441: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@441: :conditions => cond.conditions, Chris@441: :order => sort_clause, Chris@441: :limit => @limit, Chris@441: :offset => @offset) Chris@441: } Chris@441: format.atom { Chris@441: entries = TimeEntry.visible.find(:all, Chris@441: :include => [:project, :activity, :user, {:issue => :tracker}], Chris@441: :conditions => cond.conditions, Chris@441: :order => "#{TimeEntry.table_name}.created_on DESC", Chris@441: :limit => Setting.feeds_limit.to_i) Chris@441: render_feed(entries, :title => l(:label_spent_time)) Chris@441: } Chris@441: format.csv { Chris@441: # Export all entries Chris@909: @entries = TimeEntry.visible.find(:all, Chris@441: :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], Chris@441: :conditions => cond.conditions, Chris@441: :order => sort_clause) Chris@441: send_data(entries_to_csv(@entries), :type => 'text/csv; header=present', :filename => 'timelog.csv') Chris@441: } Chris@0: end Chris@0: end Chris@909: Chris@119: def show Chris@119: respond_to do |format| Chris@119: # TODO: Implement html response Chris@119: format.html { render :nothing => true, :status => 406 } Chris@119: format.api Chris@119: end Chris@119: end chris@37: chris@37: def new chris@37: @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) Chris@929: @time_entry.safe_attributes = params[:time_entry] Chris@909: chris@37: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) chris@37: render :action => 'edit' chris@37: end chris@37: chris@37: verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } chris@37: def create Chris@0: @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today) Chris@929: @time_entry.safe_attributes = params[:time_entry] Chris@909: Chris@0: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) Chris@909: chris@37: if @time_entry.save Chris@119: respond_to do |format| Chris@119: format.html { Chris@119: flash[:notice] = l(:notice_successful_update) Chris@119: redirect_back_or_default :action => 'index', :project_id => @time_entry.project Chris@119: } Chris@119: format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) } Chris@119: end chris@37: else Chris@119: respond_to do |format| Chris@119: format.html { render :action => 'edit' } Chris@119: format.api { render_validation_errors(@time_entry) } Chris@119: end Chris@909: end Chris@0: end Chris@909: chris@37: def edit Chris@929: @time_entry.safe_attributes = params[:time_entry] Chris@909: chris@37: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) chris@37: end chris@37: chris@37: verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } chris@37: def update Chris@929: @time_entry.safe_attributes = params[:time_entry] Chris@909: chris@37: call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry }) Chris@909: chris@37: if @time_entry.save Chris@119: respond_to do |format| Chris@119: format.html { Chris@119: flash[:notice] = l(:notice_successful_update) Chris@119: redirect_back_or_default :action => 'index', :project_id => @time_entry.project Chris@119: } Chris@119: format.api { head :ok } Chris@119: end chris@37: else Chris@119: respond_to do |format| Chris@119: format.html { render :action => 'edit' } Chris@119: format.api { render_validation_errors(@time_entry) } Chris@119: end Chris@909: end chris@37: end chris@37: Chris@441: def bulk_edit Chris@441: @available_activities = TimeEntryActivity.shared.active Chris@441: @custom_fields = TimeEntry.first.available_custom_fields Chris@441: end Chris@441: Chris@441: def bulk_update Chris@441: attributes = parse_params_for_bulk_time_entry_attributes(params) Chris@441: Chris@441: unsaved_time_entry_ids = [] Chris@441: @time_entries.each do |time_entry| Chris@441: time_entry.reload Chris@929: time_entry.safe_attributes = attributes Chris@441: call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry }) Chris@441: unless time_entry.save Chris@441: # Keep unsaved time_entry ids to display them in flash error Chris@441: unsaved_time_entry_ids << time_entry.id Chris@441: end Chris@441: end Chris@441: set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids) Chris@441: redirect_back_or_default({:controller => 'timelog', :action => 'index', :project_id => @projects.first}) Chris@441: end Chris@441: chris@37: verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed } Chris@0: def destroy Chris@909: @time_entries.each do |t| Chris@441: begin Chris@441: unless t.destroy && t.destroyed? Chris@441: respond_to do |format| Chris@441: format.html { Chris@441: flash[:error] = l(:notice_unable_delete_time_entry) Chris@441: redirect_to :back Chris@441: } Chris@441: format.api { render_validation_errors(t) } Chris@441: end Chris@441: return Chris@441: end Chris@441: rescue ::ActionController::RedirectBackError Chris@441: redirect_to :action => 'index', :project_id => @projects.first Chris@441: return Chris@119: end Chris@0: end Chris@441: Chris@441: respond_to do |format| Chris@441: format.html { Chris@441: flash[:notice] = l(:notice_successful_delete) Chris@441: redirect_back_or_default(:action => 'index', :project_id => @projects.first) Chris@441: } Chris@441: format.api { head :ok } Chris@441: end Chris@0: end Chris@0: Chris@0: private chris@37: def find_time_entry chris@37: @time_entry = TimeEntry.find(params[:id]) chris@37: unless @time_entry.editable_by?(User.current) chris@37: render_403 chris@37: return false chris@37: end chris@37: @project = @time_entry.project chris@37: rescue ActiveRecord::RecordNotFound chris@37: render_404 chris@37: end chris@37: Chris@441: def find_time_entries Chris@441: @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids]) Chris@441: raise ActiveRecord::RecordNotFound if @time_entries.empty? Chris@441: @projects = @time_entries.collect(&:project).compact.uniq Chris@441: @project = @projects.first if @projects.size == 1 Chris@441: rescue ActiveRecord::RecordNotFound Chris@441: render_404 Chris@441: end Chris@441: Chris@441: def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids) Chris@441: if unsaved_time_entry_ids.empty? Chris@441: flash[:notice] = l(:notice_successful_update) unless time_entries.empty? Chris@441: else Chris@441: flash[:error] = l(:notice_failed_to_save_time_entries, Chris@441: :count => unsaved_time_entry_ids.size, Chris@441: :total => time_entries.size, Chris@441: :ids => '#' + unsaved_time_entry_ids.join(', #')) Chris@441: end Chris@441: end Chris@441: Chris@0: def find_project Chris@119: if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present? Chris@119: @issue = Issue.find(issue_id) Chris@0: @project = @issue.project Chris@119: elsif (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present? Chris@119: @project = Project.find(project_id) Chris@0: else Chris@0: render_404 Chris@0: return false Chris@0: end Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@909: Chris@0: def find_optional_project Chris@0: if !params[:issue_id].blank? Chris@0: @issue = Issue.find(params[:issue_id]) Chris@0: @project = @issue.project Chris@0: elsif !params[:project_id].blank? Chris@0: @project = Project.find(params[:project_id]) Chris@0: end Chris@0: deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true) Chris@0: end Chris@909: Chris@0: # Retrieves the date range based on predefined ranges or specific from/to param dates Chris@0: def retrieve_date_range Chris@0: @free_period = false Chris@0: @from, @to = nil, nil Chris@0: Chris@0: if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) Chris@0: case params[:period].to_s Chris@0: when 'today' Chris@0: @from = @to = Date.today Chris@0: when 'yesterday' Chris@0: @from = @to = Date.today - 1 Chris@0: when 'current_week' Chris@0: @from = Date.today - (Date.today.cwday - 1)%7 Chris@0: @to = @from + 6 Chris@0: when 'last_week' Chris@0: @from = Date.today - 7 - (Date.today.cwday - 1)%7 Chris@0: @to = @from + 6 Chris@0: when '7_days' Chris@0: @from = Date.today - 7 Chris@0: @to = Date.today Chris@0: when 'current_month' Chris@0: @from = Date.civil(Date.today.year, Date.today.month, 1) Chris@0: @to = (@from >> 1) - 1 Chris@0: when 'last_month' Chris@0: @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 Chris@0: @to = (@from >> 1) - 1 Chris@0: when '30_days' Chris@0: @from = Date.today - 30 Chris@0: @to = Date.today Chris@0: when 'current_year' Chris@0: @from = Date.civil(Date.today.year, 1, 1) Chris@0: @to = Date.civil(Date.today.year, 12, 31) Chris@0: end Chris@0: elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) Chris@0: begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end Chris@0: begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end Chris@0: @free_period = true Chris@0: else Chris@0: # default Chris@0: end Chris@909: Chris@0: @from, @to = @to, @from if @from && @to && @from > @to chris@22: @from ||= (TimeEntry.earilest_date_for_project(@project) || Date.today) chris@22: @to ||= (TimeEntry.latest_date_for_project(@project) || Date.today) Chris@0: end Chris@0: Chris@441: def parse_params_for_bulk_time_entry_attributes(params) Chris@441: attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?} Chris@441: attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'} Chris@441: attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values] Chris@441: attributes Chris@441: end Chris@0: end