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 @ 1432:ebda59ca84db

History | View | Annotate | Download (9.13 KB)

1 739:b7ac21913927 luis
module RedmineTags
2
  module Patches
3
    module ProjectsHelperPatch
4
5
      def self.included(base) # :nodoc:
6
        base.send(:include, InstanceMethods)
7 1073:3af6f66496a5 luis
        base.send(:include, TagsHelper)
8
9 739:b7ac21913927 luis
        base.class_eval do
10
          unloadable
11
        end
12
      end
13
14 1067:2ad2f9ab46a6 luis
      module InstanceMethods
15 753:7877f25a19d6 luis
        # 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 1067:2ad2f9ab46a6 luis
        def render_project_table_with_filtering(projects, question)
24 753:7877f25a19d6 luis
          custom_fields = ""
25
          s = ""
26
          if projects.any?
27
            tokens = RedmineProjectFiltering.calculate_tokens(question, custom_fields)
28 1067:2ad2f9ab46a6 luis
29 753:7877f25a19d6 luis
            s << "<div class='autoscroll'>"
30
            s << "<table class='list projects'>"
31
            s << "<thead><tr>"
32 1067:2ad2f9ab46a6 luis
33 753:7877f25a19d6 luis
            s << sort_header_tag('name', :caption => l("field_name"))
34 806:a42dcc01dfee chris
            s << "<th class='tags'>" << l("tags") << "</th>"
35 753:7877f25a19d6 luis
            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 1067:2ad2f9ab46a6 luis
39 753:7877f25a19d6 luis
            s << "</tr></thead><tbody>"
40 1067:2ad2f9ab46a6 luis
41 753:7877f25a19d6 luis
            original_project = @project
42 1067:2ad2f9ab46a6 luis
43 753:7877f25a19d6 luis
            projects.each do |project|
44
              s << render_project_in_table_with_filtering(project, cycle('odd', 'even'), 0, tokens)
45
            end
46 1067:2ad2f9ab46a6 luis
47 753:7877f25a19d6 luis
            s << "</table>"
48
          else
49
            s << "\n"
50
          end
51
          @project = original_project
52
53 1150:367c016157da luis
          s.html_safe
54 753:7877f25a19d6 luis
        end
55
56 1067:2ad2f9ab46a6 luis
        def render_project_in_table_with_filtering(project, oddeven, level, tokens)
57 753:7877f25a19d6 luis
          # 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 796:6d3ad4b3a500 luis
          s << highlight_tokens(render_project_short_description(project), tokens)
70 806:a42dcc01dfee chris
          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 753:7877f25a19d6 luis
          s << "<td class='managers' align=top>"
76 1067:2ad2f9ab46a6 luis
77 753:7877f25a19d6 luis
          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 1067:2ad2f9ab46a6 luis
96 753:7877f25a19d6 luis
          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 1254:a54ce736229d luis
          s.html_safe
108 753:7877f25a19d6 luis
        end
109 1067:2ad2f9ab46a6 luis
110
111
112 739:b7ac21913927 luis
        # 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
120
            ancestors = []
121
            original_project = @project
122
            projects.each do |project|
123
              # set the project environment to please macros.
124
              @project = project
125
              if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
126
                s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>"
127
              else
128
                ancestors.pop
129
                s << "</li>"
130 1067:2ad2f9ab46a6 luis
                while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
131 739:b7ac21913927 luis
                  ancestors.pop
132
                  s << "</ul></li>"
133
                end
134
              end
135
              classes = (ancestors.empty? ? 'root' : 'child')
136
              s << "<li class='#{classes}'><div class='#{classes}'>" +
137 1067:2ad2f9ab46a6 luis
                link_to( highlight_tokens(project.name, tokens),
138 739:b7ac21913927 luis
                  {:controller => 'projects', :action => 'show', :id => project},
139
                  :class => "project #{User.current.member_of?(project) ? 'my-project' : nil}"
140
                )
141
              s << "<ul class='filter_fields'>"
142
143
           #  CustomField.usable_for_project_filtering.each do |field|
144
           #    value_model = project.custom_value_for(field.id)
145
           #    value = value_model.present? ? value_model.value : nil
146
           #    s << "<li><b>#{field.name.humanize}:</b> #{highlight_tokens(value, tokens)}</li>" if value.present?
147
           #  end
148 1067:2ad2f9ab46a6 luis
149 739:b7ac21913927 luis
              s << "</ul>"
150
              s << "<div class='clear'></div>"
151
              unless project.description.blank?
152
                s << "<div class='wiki description'>"
153
                s << "<b>#{ t(:field_description) }:</b>"
154
                s << highlight_tokens(textilizable(project.short_description, :project => project), tokens)
155
                s << "\n</div>"
156
              end
157
              s << "</div>"
158
              ancestors << project
159
            end
160
            ancestors.size.times{ s << "</li></ul>" }
161
            @project = original_project
162
          end
163 1157:13ab8b6e8132 chris
          (s.join "\n").html_safe
164 739:b7ac21913927 luis
        end
165 1067:2ad2f9ab46a6 luis
166 800:95b78e19e586 luis
        # Renders a tree of projects where the current user belongs
167
        # as a nested set of unordered lists
168
        # The given collection may be a subset of the whole project tree
169
        # (eg. some intermediate nodes are private and can not be seen)
170
        def render_my_project_hierarchy_with_tags(projects)
171
172
          s = ''
173
174
          original_project = @project
175
176
          projects.each do |project|
177
            if project.root? || !projects.include?(project.parent)
178
              s << render_my_project_in_hierarchy_with_tags(project)
179
            end
180
          end
181
182
          @project = original_project
183
184
          if s != ''
185
            a = ''
186
            a << "<ul class='projects root'>\n"
187
            a << s
188
            a << "</ul>\n"
189
            s = a
190
          end
191
192 1157:13ab8b6e8132 chris
          s.html_safe
193 800:95b78e19e586 luis
194
        end
195 1067:2ad2f9ab46a6 luis
196
197
198 800:95b78e19e586 luis
199
        def render_my_project_in_hierarchy_with_tags(project)
200
201
          s = ''
202
203
          if User.current.member_of?(project)
204
205
            # set the project environment to please macros.
206
            @project = project
207
208
            classes = (project.root? ? 'root' : 'child')
209
210
            s << "<li class='#{classes}'><div class='#{classes}'>" +
211
              link_to_project(project, {}, :class => "project my-project")
212
            if project.is_public?
213 806:a42dcc01dfee chris
              s << " <span class='public'>" << l(:field_is_public) << "</span>"
214 800:95b78e19e586 luis
            else
215 806:a42dcc01dfee chris
              s << " <span class='private'>" << l(:field_is_private) << "</span>"
216 800:95b78e19e586 luis
            end
217 1067:2ad2f9ab46a6 luis
218 806:a42dcc01dfee chris
            tc = project.tag_counts
219
            if tc.empty?
220
              s << " <span class='no-tags'>" << l(:field_no_tags) << "</span>"
221
            else
222
              s << " <span class='tags'>" << tc.collect{ |t| render_project_tag_link(t) }.join(', ') << "</span>"
223
            end
224
225 800:95b78e19e586 luis
            s << render_project_short_description(project)
226
227
            s << "</div>\n"
228
229
            cs = ''
230
            project.children.each do |child|
231 806:a42dcc01dfee chris
              cs << render_my_project_in_hierarchy_with_tags(child)
232 800:95b78e19e586 luis
            end
233
234
            if cs != ''
235
              s << "<ul class='projects'>\n" << cs << "</ul>\n";
236
            end
237
238
          end
239
240 1254:a54ce736229d luis
          s.html_safe
241 800:95b78e19e586 luis
242
        end
243
244 739:b7ac21913927 luis
        private
245 1067:2ad2f9ab46a6 luis
246 739:b7ac21913927 luis
        # copied from search_helper. This one doesn't escape html or limit the text length
247
        def highlight_tokens(text, tokens)
248
          return text unless text && tokens && !tokens.empty?
249
          re_tokens = tokens.collect {|t| Regexp.escape(t)}
250 1067:2ad2f9ab46a6 luis
          regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
251 739:b7ac21913927 luis
          result = ''
252
          text.split(regexp).each_with_index do |words, i|
253
            words = words.mb_chars
254
            if i.even?
255
              result << words
256
            else
257
              t = (tokens.index(words.downcase) || 0) % 4
258
              result << content_tag('span', words, :class => "highlight token-#{t}")
259
            end
260
          end
261 1254:a54ce736229d luis
          result.html_safe
262 739:b7ac21913927 luis
        end
263 1067:2ad2f9ab46a6 luis
264 739:b7ac21913927 luis
      end
265
    end
266
  end
267
end