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_helper_patch.rb @ 1244:a3ed5c4d90f0

History | View | Annotate | Download (9.13 KB)

1
module RedmineTags
2
  module Patches
3
    module ProjectsHelperPatch
4

    
5
      def self.included(base) # :nodoc:
6
        base.send(:include, InstanceMethods)
7
        base.send(:include, TagsHelper)
8

    
9
        base.class_eval do
10
          unloadable
11
        end
12
      end
13

    
14
      module InstanceMethods
15
        # Renders a tree of projects that the current user does not belong
16
        # to, or of all projects if the current user is not logged in.  The
17
        # given collection may be a subset of the whole project tree
18
        # (eg. some intermediate nodes are private and can not be seen).  We
19
        # are potentially interested in various things: the project name,
20
        # description, manager(s), creation date, last activity date,
21
        # general activity level, whether there is anything actually hosted
22
        # here for the project, etc.
23
        def render_project_table_with_filtering(projects, question)
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='tags'>" << l("tags") << "</th>"
35
            s << "<th class='managers'>" << l("label_managers") << "</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.html_safe
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 << highlight_tokens(render_project_short_description(project), tokens)
70
          s << "</td>"
71

    
72
          # taglist
73
          s << "<td class='tags' align=top>" << project.tag_counts.collect{ |t| render_project_tag_link(t) }.join(', ') << "</td>"
74

    
75
          s << "<td class='managers' align=top>"
76

    
77
          u = project.users_by_role
78
          if u
79
            u.keys.each do |r|
80
              if r.allowed_to?(:edit_project)
81
                mgrs = []
82
                u[r].sort.each do |m|
83
                  mgrs << link_to_user(m)
84
                end
85
                if mgrs.size < 3
86
                  s << '<nobr>' << mgrs.join(', ') << '</nobr>'
87
                else
88
                  s << mgrs.join(', ')
89
                end
90
              end
91
            end
92
          end
93

    
94
          s << "</td>"
95

    
96
          s << "<td class='created_on' align=top>" << format_date(project.created_on) << "</td>"
97
          s << "<td class='updated_on' align=top>" << format_date(project.updated_on) << "</td>"
98

    
99
          s << "</tr>"
100

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

    
107
          s
108
        end
109

    
110

    
111

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

    
121

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

    
145
           #  CustomField.usable_for_project_filtering.each do |field|
146
           #    value_model = project.custom_value_for(field.id)
147
           #    value = value_model.present? ? value_model.value : nil
148
           #    s << "<li><b>#{field.name.humanize}:</b> #{highlight_tokens(value, tokens)}</li>" if value.present?
149
           #  end
150

    
151
              s << "</ul>"
152
              s << "<div class='clear'></div>"
153
              unless project.description.blank?
154
                s << "<div class='wiki description'>"
155
                s << "<b>#{ t(:field_description) }:</b>"
156
                s << highlight_tokens(textilizable(project.short_description, :project => project), tokens)
157
                s << "\n</div>"
158
              end
159
              s << "</div>"
160
              ancestors << project
161
            end
162
            ancestors.size.times{ s << "</li></ul>" }
163
            @project = original_project
164
          end
165
          (s.join "\n").html_safe
166
        end
167

    
168
        # Renders a tree of projects where the current user belongs
169
        # as a nested set of unordered lists
170
        # The given collection may be a subset of the whole project tree
171
        # (eg. some intermediate nodes are private and can not be seen)
172
        def render_my_project_hierarchy_with_tags(projects)
173

    
174
          s = ''
175

    
176
          original_project = @project
177

    
178
          projects.each do |project|
179
            if project.root? || !projects.include?(project.parent)
180
              s << render_my_project_in_hierarchy_with_tags(project)
181
            end
182
          end
183

    
184
          @project = original_project
185

    
186
          if s != ''
187
            a = ''
188
            a << "<ul class='projects root'>\n"
189
            a << s
190
            a << "</ul>\n"
191
            s = a
192
          end
193

    
194
          s.html_safe
195

    
196
        end
197

    
198

    
199

    
200

    
201
        def render_my_project_in_hierarchy_with_tags(project)
202

    
203
          s = ''
204

    
205
          if User.current.member_of?(project)
206

    
207
            # set the project environment to please macros.
208
            @project = project
209

    
210
            classes = (project.root? ? 'root' : 'child')
211

    
212
            s << "<li class='#{classes}'><div class='#{classes}'>" +
213
              link_to_project(project, {}, :class => "project my-project")
214
            if project.is_public?
215
              s << " <span class='public'>" << l(:field_is_public) << "</span>"
216
            else
217
              s << " <span class='private'>" << l(:field_is_private) << "</span>"
218
            end
219

    
220
            tc = project.tag_counts
221
            if tc.empty?
222
              s << " <span class='no-tags'>" << l(:field_no_tags) << "</span>"
223
            else
224
              s << " <span class='tags'>" << tc.collect{ |t| render_project_tag_link(t) }.join(', ') << "</span>"
225
            end
226

    
227
            s << render_project_short_description(project)
228

    
229
            s << "</div>\n"
230

    
231
            cs = ''
232
            project.children.each do |child|
233
              cs << render_my_project_in_hierarchy_with_tags(child)
234
            end
235

    
236
            if cs != ''
237
              s << "<ul class='projects'>\n" << cs << "</ul>\n";
238
            end
239

    
240
          end
241

    
242
          s
243

    
244
        end
245

    
246

    
247

    
248
        private
249

    
250
        # copied from search_helper. This one doesn't escape html or limit the text length
251
        def highlight_tokens(text, tokens)
252
          return text unless text && tokens && !tokens.empty?
253
          re_tokens = tokens.collect {|t| Regexp.escape(t)}
254
          regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
255
          result = ''
256
          text.split(regexp).each_with_index do |words, i|
257
            words = words.mb_chars
258
            if i.even?
259
              result << words
260
            else
261
              t = (tokens.index(words.downcase) || 0) % 4
262
              result << content_tag('span', words, :class => "highlight token-#{t}")
263
            end
264
          end
265
          result
266
        end
267

    
268
      end
269
    end
270
  end
271
end
272