To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / plugins / redmine_tags / lib / redmine_tags / patches / projects_controller_patch.rb @ 1244:a3ed5c4d90f0

History | View | Annotate | Download (4.56 KB)

1
# -*- coding: utf-8 -*-
2
require_dependency 'projects_controller'
3

    
4
module RedmineTags
5
  module Patches
6
    module ProjectsControllerPatch
7
      def self.included(base)
8
        base.send(:include, InstanceMethods)
9
        base.class_eval do
10
          unloadable
11
          skip_before_filter :authorize, :only => [:set_fieldset_status]
12
          skip_before_filter :find_project, :only => [:set_fieldset_status]
13
          before_filter :add_tags_to_project, :only => [:save, :update]
14

    
15
          alias :index filtered_index
16
        end
17
      end
18

    
19
      module InstanceMethods
20

    
21
        def add_tags_to_project
22

    
23
          if params && params[:project] && !params[:project][:tag_list].nil?
24
            old_tags = @project.tag_list.to_s.downcase
25
            new_tags = params[:project][:tag_list].to_s.downcase
26

    
27
            unless (old_tags == new_tags)
28
              @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
29
            end
30
          end
31
        end
32

    
33
        def paginate_projects
34
          sort_init 'name'
35
          sort_update %w(name lft created_on updated_on)
36
          @limit = per_page_option
37

    
38
          # Only top-level visible projects are counted --lf.10Jan2013
39
          top_level_visible_projects = @projects.select{ |p| p.parent_id.nil? and p.visible? }
40
          @project_count = top_level_visible_projects.count
41

    
42
          # Project.visible_roots.find(@projects).count
43

    
44
          @project_pages = ActionController::Pagination::Paginator.new self, @project_count, @limit, params['page']
45
          @offset ||= @project_pages.current.offset
46
        end
47

    
48
        def set_fieldset_status
49

    
50
          # luisf. test for missing parameters………
51
          field = params[:field_id]
52
          status = params[:status]
53

    
54
          session[(field + "_status").to_sym] = status
55
          render :nothing => true
56
        end
57

    
58
        # gets the status of the collabsible fieldsets
59
        def get_fieldset_statuses
60
          if session[:my_projects_fieldset_status].nil?
61
            @myproj_status = "true"
62
          else
63
            @myproj_status = session[:my_projects_fieldset_status]
64
          end
65

    
66
          if session[:filters_fieldset_status].nil?
67
            @filter_status = "false"
68
          else
69
            @filter_status = session[:filters_fieldset_status]
70
          end
71

    
72
          if params && params[:project] && !params[:project][:tag_list].nil?
73
            @filter_status = "true"
74
          end
75

    
76
        end
77

    
78
        # Lists visible projects. Paginator is for top-level projects only
79
        # (subprojects belong to them)
80
        def filtered_index
81
          @project = Project.new
82
          filter_projects
83
          get_fieldset_statuses
84

    
85
          sort_clause = "name"
86

    
87
          respond_to do |format|
88
            format.html {
89
              paginate_projects
90

    
91
              # todo: check ordering ~luisf.14/Jan/2013
92
              @projects = @projects[@offset, @limit]
93

    
94
              render :template => 'projects/index.html.erb', :layout => !request.xhr?
95
            }
96
            format.api {
97
              @offset, @limit = api_offset_and_limit
98
              @project_count = Project.visible.count
99
              @projects = Project.visible.find(@projects, :offset => @offset, :limit => @limit, :order => 'lft')
100
            }
101
            format.atom {
102
              projects = Project.visible.find(:all, :order => 'created_on DESC', :limit => Setting.feeds_limit.to_i)
103
              render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
104
            }
105
            format.js {
106
              paginate_projects
107
              @projects = Project.visible_roots.find(@projects, :offset => @offset, :limit => @limit, :order => sort_clause)
108
              render :update do |page|
109
                page.replace_html 'projects', :partial => 'filtered_projects'
110
              end
111
            }
112
          end
113
        end
114

    
115
        private
116

    
117
        def filter_projects
118
          @question = (params[:q] || "").strip
119

    
120
          if params.has_key?(:project)
121
            @tag_list = (params[:project][:tag_list] || "").strip.split(",")
122
          else
123
            @tag_list = []
124
          end
125

    
126
          if  @question == ""
127
            @projects = Project.visible_roots
128
          else
129
            @projects = Project.visible_roots.find(Project.visible.search_by_question(@question))
130
          end
131

    
132
          unless @tag_list.empty?
133
            @tagged_projects_ids = Project.visible.tagged_with(@tag_list).collect{ |project| Project.find(project.id).root }
134

    
135
            @projects = @projects & @tagged_projects_ids
136
            @projects = @projects.uniq
137
          end
138
        end
139
      end
140
    end
141
  end
142
end