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 @ 1073:3af6f66496a5

History | View | Annotate | Download (9.09 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
          s
54
        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
          s
108
        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
            debugger
120 1067:2ad2f9ab46a6 luis
121 739:b7ac21913927 luis
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 1067:2ad2f9ab46a6 luis
                while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
133 739:b7ac21913927 luis
                  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 1067:2ad2f9ab46a6 luis
                link_to( highlight_tokens(project.name, tokens),
140 739:b7ac21913927 luis
                  {: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 1067:2ad2f9ab46a6 luis
151 739:b7ac21913927 luis
              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"
166
        end
167 1067:2ad2f9ab46a6 luis
168 800:95b78e19e586 luis
        # 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
195
196
        end
197 1067:2ad2f9ab46a6 luis
198
199
200 800:95b78e19e586 luis
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 806:a42dcc01dfee chris
              s << " <span class='public'>" << l(:field_is_public) << "</span>"
216 800:95b78e19e586 luis
            else
217 806:a42dcc01dfee chris
              s << " <span class='private'>" << l(:field_is_private) << "</span>"
218 800:95b78e19e586 luis
            end
219 1067:2ad2f9ab46a6 luis
220 806:a42dcc01dfee chris
            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 800:95b78e19e586 luis
            s << render_project_short_description(project)
228
229
            s << "</div>\n"
230
231
            cs = ''
232
            project.children.each do |child|
233 806:a42dcc01dfee chris
              cs << render_my_project_in_hierarchy_with_tags(child)
234 800:95b78e19e586 luis
            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 1067:2ad2f9ab46a6 luis
247
248 739:b7ac21913927 luis
        private
249 1067:2ad2f9ab46a6 luis
250 739:b7ac21913927 luis
        # 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 1067:2ad2f9ab46a6 luis
          regexp = Regexp.new "(#{re_tokens.join('|')})", Regexp::IGNORECASE
255 739:b7ac21913927 luis
          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 1067:2ad2f9ab46a6 luis
268 739:b7ac21913927 luis
      end
269
    end
270
  end
271
end