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 / vendor / plugins / redmine_tags / lib / redmine_tags / patches / projects_helper_patch.rb @ 778:c118d3389088

History | View | Annotate | Download (7.09 KB)

1
module RedmineTags
2
  module Patches
3
    module ProjectsHelperPatch
4

    
5
      def self.included(base) # :nodoc:
6
        base.send(:include, InstanceMethods)
7
        base.class_eval do
8
          unloadable
9
        end
10
      end
11

    
12
      module InstanceMethods        
13
        # Renders a tree of projects that the current user does not belong
14
        # to, or of all projects if the current user is not logged in.  The
15
        # given collection may be a subset of the whole project tree
16
        # (eg. some intermediate nodes are private and can not be seen).  We
17
        # are potentially interested in various things: the project name,
18
        # description, manager(s), creation date, last activity date,
19
        # general activity level, whether there is anything actually hosted
20
        # here for the project, etc.
21
        def render_project_table_with_filtering(projects, question)          
22
          custom_fields = ""
23
          s = ""
24
          if projects.any?
25
            tokens = RedmineProjectFiltering.calculate_tokens(question, custom_fields)
26
            
27
            s << "<div class='autoscroll'>"
28
            s << "<table class='list projects'>"
29
            s << "<thead><tr>"
30
        
31
            s << sort_header_tag('name', :caption => l("field_name"))
32
            s << "<th class='managers'>" << l("label_managers") << "</th>"
33
            s << "<th class='tags'>" << l("tags") << "</th>"
34
            s << sort_header_tag('created_on', :default_order => 'desc')
35
            s << sort_header_tag('updated_on', :default_order => 'desc')
36
        
37
            s << "</tr></thead><tbody>"
38
        
39
            original_project = @project
40
        
41
            projects.each do |project|
42
              s << render_project_in_table_with_filtering(project, cycle('odd', 'even'), 0, tokens)
43
            end
44
        
45
            s << "</table>"
46
          else
47
            s << "\n"
48
          end
49
          @project = original_project
50

    
51
          s
52
        end
53

    
54
        def render_project_in_table_with_filtering(project, oddeven, level, tokens)          
55
          # set the project environment to please macros.
56
          @project = project
57

    
58
          classes = (level == 0 ? 'root' : 'child')
59

    
60
          s = ""
61

    
62
          s << "<tr class='#{oddeven} #{classes} level#{level}'>"
63
          s << "<td class='firstcol' align=top><div class='name hosted_here"
64
          s << " no_description" if project.description.blank?
65
          s << "'>" << link_to( highlight_tokens(project.name, tokens), {:controller => 'projects', :action => 'show', :id => project}, :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}")
66
          s << "</div>"
67
          s << render_project_short_description(project)
68
          s << "<td class='managers' align=top>"
69
           
70
          u = project.users_by_role
71
          if u
72
            u.keys.each do |r|
73
              if r.allowed_to?(:edit_project)
74
                mgrs = []
75
                u[r].sort.each do |m|
76
                  mgrs << link_to_user(m)
77
                end
78
                if mgrs.size < 3
79
                  s << '<nobr>' << mgrs.join(', ') << '</nobr>'
80
                else
81
                  s << mgrs.join(', ')
82
                end
83
              end
84
            end
85
          end
86

    
87
          s << "</td>"
88
          
89
          # taglist
90
          s << "<td class='tags' align=top>" << project.tag_counts.collect{ |t| render_project_tag_link(t) }.join(', ') << "</td>"
91
          s << "<td class='created_on' align=top>" << format_date(project.created_on) << "</td>"
92
          s << "<td class='updated_on' align=top>" << format_date(project.updated_on) << "</td>"
93

    
94
          s << "</tr>"
95

    
96
          project.children.each do |child|
97
            if child.is_public? or User.current.member_of?(child)
98
              s << render_project_in_table_with_filtering(child, oddeven, level + 1, tokens)
99
            end
100
          end
101

    
102
          s
103
        end
104
        
105
        
106
        
107
        # Renders a tree of projects as a nested set of unordered lists
108
        # The given collection may be a subset of the whole project tree
109
        # (eg. some intermediate nodes are private and can not be seen)
110
        def render_project_hierarchy_with_filtering(projects,custom_fields,question)
111
          s = []
112
          if projects.any?
113
            tokens = RedmineProjectFiltering.calculate_tokens(question, custom_fields)
114
            debugger
115
            
116

    
117
            ancestors = []
118
            original_project = @project
119
            projects.each do |project|
120
              # set the project environment to please macros.
121
              @project = project
122
              if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
123
                s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>"
124
              else
125
                ancestors.pop
126
                s << "</li>"
127
                while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) 
128
                  ancestors.pop
129
                  s << "</ul></li>"
130
                end
131
              end
132
              classes = (ancestors.empty? ? 'root' : 'child')
133
              s << "<li class='#{classes}'><div class='#{classes}'>" +
134
                link_to( highlight_tokens(project.name, tokens), 
135
                  {:controller => 'projects', :action => 'show', :id => project},
136
                  :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}"
137
                )
138
              s << "<ul class='filter_fields'>"
139

    
140
           #  CustomField.usable_for_project_filtering.each do |field|
141
           #    value_model = project.custom_value_for(field.id)
142
           #    value = value_model.present? ? value_model.value : nil
143
           #    s << "<li><b>#{field.name.humanize}:</b> #{highlight_tokens(value, tokens)}</li>" if value.present?
144
           #  end
145
              
146
              s << "</ul>"
147
              s << "<div class='clear'></div>"
148
              unless project.description.blank?
149
                s << "<div class='wiki description'>"
150
                s << "<b>#{ t(:field_description) }:</b>"
151
                s << highlight_tokens(textilizable(project.short_description, :project => project), tokens)
152
                s << "\n</div>"
153
              end
154
              s << "</div>"
155
              ancestors << project
156
            end
157
            ancestors.size.times{ s << "</li></ul>" }
158
            @project = original_project
159
          end
160
          s.join "\n"
161
        end
162
        
163
        private
164
        
165
        # copied from search_helper. This one doesn't escape html or limit the text length
166
        def highlight_tokens(text, tokens)
167
          return text unless text && tokens && !tokens.empty?
168
          re_tokens = tokens.collect {|t| Regexp.escape(t)}
169
          regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE    
170
          result = ''
171
          text.split(regexp).each_with_index do |words, i|
172
            words = words.mb_chars
173
            if i.even?
174
              result << words
175
            else
176
              t = (tokens.index(words.downcase) || 0) % 4
177
              result << content_tag('span', words, :class => "highlight token-#{t}")
178
            end
179
          end
180
          result
181
        end
182
      
183
      end
184
    end
185
  end
186
end
187