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 @ 767:dd33798e514d

History | View | Annotate | Download (7.11 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
          debugger
23
          
24
          custom_fields = ""
25
          s = ""
26
          if projects.any?
27
            tokens = RedmineProjectFiltering.calculate_tokens(question, custom_fields)
28
            
29
            s << "<div class='autoscroll'>"
30
            s << "<table class='list projects'>"
31
            s << "<thead><tr>"
32
        
33
            s << sort_header_tag('name', :caption => l("field_name"))
34
            s << "<th class='managers'>" << l("label_managers") << "</th>"
35
            s << "<th class='tags'>" << l("tags") << "</th>"
36
            s << sort_header_tag('created_on', :default_order => 'desc')
37
            s << sort_header_tag('updated_on', :default_order => 'desc')
38
        
39
            s << "</tr></thead><tbody>"
40
        
41
            original_project = @project
42
        
43
            projects.each do |project|
44
              s << render_project_in_table_with_filtering(project, cycle('odd', 'even'), 0, tokens)
45
            end
46
        
47
            s << "</table>"
48
          else
49
            s << "\n"
50
          end
51
          @project = original_project
52

    
53
          s
54
        end
55

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

    
60
          classes = (level == 0 ? 'root' : 'child')
61

    
62
          s = ""
63

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

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

    
96
          s << "</tr>"
97

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

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

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

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