@@ -1,254 +1,254 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class TimelogController < ApplicationController |
|
19 | 19 | layout 'base' |
|
20 | 20 | menu_item :issues |
|
21 | 21 | before_filter :find_project, :authorize |
|
22 | 22 | |
|
23 | 23 | verify :method => :post, :only => :destroy, :redirect_to => { :action => :details } |
|
24 | 24 | |
|
25 | 25 | helper :sort |
|
26 | 26 | include SortHelper |
|
27 | 27 | helper :issues |
|
28 | 28 | include TimelogHelper |
|
29 | 29 | helper :custom_fields |
|
30 | 30 | include CustomFieldsHelper |
|
31 | 31 | |
|
32 | 32 | def report |
|
33 | 33 | @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id", |
|
34 | 34 | :klass => Project, |
|
35 | 35 | :label => :label_project}, |
|
36 | 36 | 'version' => {:sql => "#{Issue.table_name}.fixed_version_id", |
|
37 | 37 | :klass => Version, |
|
38 | 38 | :label => :label_version}, |
|
39 | 39 | 'category' => {:sql => "#{Issue.table_name}.category_id", |
|
40 | 40 | :klass => IssueCategory, |
|
41 | 41 | :label => :field_category}, |
|
42 | 42 | 'member' => {:sql => "#{TimeEntry.table_name}.user_id", |
|
43 | 43 | :klass => User, |
|
44 | 44 | :label => :label_member}, |
|
45 | 45 | 'tracker' => {:sql => "#{Issue.table_name}.tracker_id", |
|
46 | 46 | :klass => Tracker, |
|
47 | 47 | :label => :label_tracker}, |
|
48 | 48 | 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id", |
|
49 | 49 | :klass => Enumeration, |
|
50 | 50 | :label => :label_activity}, |
|
51 | 51 | 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id", |
|
52 | 52 | :klass => Issue, |
|
53 | 53 | :label => :label_issue} |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | # Add list and boolean custom fields as available criterias |
|
57 | 57 | @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf| |
|
58 | 58 | @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)", |
|
59 | 59 | :format => cf.field_format, |
|
60 | 60 | :label => cf.name} |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | @criterias = params[:criterias] || [] |
|
64 | 64 | @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria} |
|
65 | 65 | @criterias.uniq! |
|
66 | 66 | @criterias = @criterias[0,3] |
|
67 | 67 | |
|
68 | 68 | @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month' |
|
69 | 69 | |
|
70 | 70 | retrieve_date_range |
|
71 | 71 | |
|
72 | 72 | unless @criterias.empty? |
|
73 | 73 | sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ') |
|
74 | 74 | sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ') |
|
75 | 75 | |
|
76 | 76 | sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours" |
|
77 | 77 | sql << " FROM #{TimeEntry.table_name}" |
|
78 | 78 | sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id" |
|
79 | 79 | sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id" |
|
80 | 80 | sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?) |
|
81 | 81 | sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries) |
|
82 | 82 | sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)] |
|
83 | 83 | sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on" |
|
84 | 84 | |
|
85 | 85 | @hours = ActiveRecord::Base.connection.select_all(sql) |
|
86 | 86 | |
|
87 | 87 | @hours.each do |row| |
|
88 | 88 | case @columns |
|
89 | 89 | when 'year' |
|
90 | 90 | row['year'] = row['tyear'] |
|
91 | 91 | when 'month' |
|
92 | 92 | row['month'] = "#{row['tyear']}-#{row['tmonth']}" |
|
93 | 93 | when 'week' |
|
94 | 94 | row['week'] = "#{row['tyear']}-#{row['tweek']}" |
|
95 | 95 | when 'day' |
|
96 | 96 | row['day'] = "#{row['spent_on']}" |
|
97 | 97 | end |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f} |
|
101 | 101 | |
|
102 | 102 | @periods = [] |
|
103 | 103 | # Date#at_beginning_of_ not supported in Rails 1.2.x |
|
104 | 104 | date_from = @from.to_time |
|
105 | 105 | # 100 columns max |
|
106 | 106 | while date_from <= @to.to_time && @periods.length < 100 |
|
107 | 107 | case @columns |
|
108 | 108 | when 'year' |
|
109 | 109 | @periods << "#{date_from.year}" |
|
110 | 110 | date_from = (date_from + 1.year).at_beginning_of_year |
|
111 | 111 | when 'month' |
|
112 | 112 | @periods << "#{date_from.year}-#{date_from.month}" |
|
113 | 113 | date_from = (date_from + 1.month).at_beginning_of_month |
|
114 | 114 | when 'week' |
|
115 | 115 | @periods << "#{date_from.year}-#{date_from.to_date.cweek}" |
|
116 | 116 | date_from = (date_from + 7.day).at_beginning_of_week |
|
117 | 117 | when 'day' |
|
118 | 118 | @periods << "#{date_from.to_date}" |
|
119 | 119 | date_from = date_from + 1.day |
|
120 | 120 | end |
|
121 | 121 | end |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | respond_to do |format| |
|
125 | 125 | format.html { render :layout => !request.xhr? } |
|
126 | 126 | format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') } |
|
127 | 127 | end |
|
128 | 128 | end |
|
129 | 129 | |
|
130 | 130 | def details |
|
131 | 131 | sort_init 'spent_on', 'desc' |
|
132 | 132 | sort_update |
|
133 | 133 | |
|
134 | 134 | cond = ARCondition.new |
|
135 | 135 | cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) : |
|
136 | 136 | ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]) |
|
137 | 137 | |
|
138 | 138 | retrieve_date_range |
|
139 | 139 | cond << ['spent_on BETWEEN ? AND ?', @from, @to] |
|
140 | 140 | |
|
141 | 141 | TimeEntry.visible_by(User.current) do |
|
142 | 142 | respond_to do |format| |
|
143 | 143 | format.html { |
|
144 | 144 | # Paginate results |
|
145 | 145 | @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions) |
|
146 | 146 | @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page'] |
|
147 | 147 | @entries = TimeEntry.find(:all, |
|
148 | 148 | :include => [:project, :activity, :user, {:issue => :tracker}], |
|
149 | 149 | :conditions => cond.conditions, |
|
150 | 150 | :order => sort_clause, |
|
151 | 151 | :limit => @entry_pages.items_per_page, |
|
152 | 152 | :offset => @entry_pages.current.offset) |
|
153 | 153 | @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f |
|
154 | 154 | |
|
155 | 155 | render :layout => !request.xhr? |
|
156 | 156 | } |
|
157 | 157 | format.csv { |
|
158 | 158 | # Export all entries |
|
159 | 159 | @entries = TimeEntry.find(:all, |
|
160 | 160 | :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}], |
|
161 | 161 | :conditions => cond.conditions, |
|
162 | 162 | :order => sort_clause) |
|
163 | 163 | send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') |
|
164 | 164 | } |
|
165 | 165 | end |
|
166 | 166 | end |
|
167 | 167 | end |
|
168 | 168 | |
|
169 | 169 | def edit |
|
170 | 170 | render_403 and return if @time_entry && !@time_entry.editable_by?(User.current) |
|
171 | 171 | @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today) |
|
172 | 172 | @time_entry.attributes = params[:time_entry] |
|
173 | 173 | if request.post? and @time_entry.save |
|
174 | 174 | flash[:notice] = l(:notice_successful_update) |
|
175 | redirect_to :action => 'details', :project_id => @time_entry.project | |
|
175 | redirect_to(params[:back_url] || {:action => 'details', :project_id => @time_entry.project}) | |
|
176 | 176 | return |
|
177 | 177 | end |
|
178 | 178 | @activities = Enumeration::get_values('ACTI') |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | 181 | def destroy |
|
182 | 182 | render_404 and return unless @time_entry |
|
183 | 183 | render_403 and return unless @time_entry.editable_by?(User.current) |
|
184 | 184 | @time_entry.destroy |
|
185 | 185 | flash[:notice] = l(:notice_successful_delete) |
|
186 | 186 | redirect_to :back |
|
187 | 187 | rescue RedirectBackError |
|
188 | 188 | redirect_to :action => 'details', :project_id => @time_entry.project |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | private |
|
192 | 192 | def find_project |
|
193 | 193 | if params[:id] |
|
194 | 194 | @time_entry = TimeEntry.find(params[:id]) |
|
195 | 195 | @project = @time_entry.project |
|
196 | 196 | elsif params[:issue_id] |
|
197 | 197 | @issue = Issue.find(params[:issue_id]) |
|
198 | 198 | @project = @issue.project |
|
199 | 199 | elsif params[:project_id] |
|
200 | 200 | @project = Project.find(params[:project_id]) |
|
201 | 201 | else |
|
202 | 202 | render_404 |
|
203 | 203 | return false |
|
204 | 204 | end |
|
205 | 205 | rescue ActiveRecord::RecordNotFound |
|
206 | 206 | render_404 |
|
207 | 207 | end |
|
208 | 208 | |
|
209 | 209 | # Retrieves the date range based on predefined ranges or specific from/to param dates |
|
210 | 210 | def retrieve_date_range |
|
211 | 211 | @free_period = false |
|
212 | 212 | @from, @to = nil, nil |
|
213 | 213 | |
|
214 | 214 | if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?) |
|
215 | 215 | case params[:period].to_s |
|
216 | 216 | when 'today' |
|
217 | 217 | @from = @to = Date.today |
|
218 | 218 | when 'yesterday' |
|
219 | 219 | @from = @to = Date.today - 1 |
|
220 | 220 | when 'current_week' |
|
221 | 221 | @from = Date.today - (Date.today.cwday - 1)%7 |
|
222 | 222 | @to = @from + 6 |
|
223 | 223 | when 'last_week' |
|
224 | 224 | @from = Date.today - 7 - (Date.today.cwday - 1)%7 |
|
225 | 225 | @to = @from + 6 |
|
226 | 226 | when '7_days' |
|
227 | 227 | @from = Date.today - 7 |
|
228 | 228 | @to = Date.today |
|
229 | 229 | when 'current_month' |
|
230 | 230 | @from = Date.civil(Date.today.year, Date.today.month, 1) |
|
231 | 231 | @to = (@from >> 1) - 1 |
|
232 | 232 | when 'last_month' |
|
233 | 233 | @from = Date.civil(Date.today.year, Date.today.month, 1) << 1 |
|
234 | 234 | @to = (@from >> 1) - 1 |
|
235 | 235 | when '30_days' |
|
236 | 236 | @from = Date.today - 30 |
|
237 | 237 | @to = Date.today |
|
238 | 238 | when 'current_year' |
|
239 | 239 | @from = Date.civil(Date.today.year, 1, 1) |
|
240 | 240 | @to = Date.civil(Date.today.year, 12, 31) |
|
241 | 241 | end |
|
242 | 242 | elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?)) |
|
243 | 243 | begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end |
|
244 | 244 | begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end |
|
245 | 245 | @free_period = true |
|
246 | 246 | else |
|
247 | 247 | # default |
|
248 | 248 | end |
|
249 | 249 | |
|
250 | 250 | @from, @to = @to, @from if @from && @to && @from > @to |
|
251 | 251 | @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1 |
|
252 | 252 | @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) |
|
253 | 253 | end |
|
254 | 254 | end |
@@ -1,506 +1,510 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module ApplicationHelper |
|
19 | 19 | include Redmine::WikiFormatting::Macros::Definitions |
|
20 | 20 | |
|
21 | 21 | def current_role |
|
22 | 22 | @current_role ||= User.current.role_for_project(@project) |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | # Return true if user is authorized for controller/action, otherwise false |
|
26 | 26 | def authorize_for(controller, action) |
|
27 | 27 | User.current.allowed_to?({:controller => controller, :action => action}, @project) |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | 30 | # Display a link if user is authorized |
|
31 | 31 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
32 | 32 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | # Display a link to user's account page |
|
36 | 36 | def link_to_user(user) |
|
37 | 37 | user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous' |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def link_to_issue(issue, options={}) |
|
41 | 41 | link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def toggle_link(name, id, options={}) |
|
45 | 45 | onclick = "Element.toggle('#{id}'); " |
|
46 | 46 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
47 | 47 | onclick << "return false;" |
|
48 | 48 | link_to(name, "#", :onclick => onclick) |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | def show_and_goto_link(name, id, options={}) |
|
52 | 52 | onclick = "Element.show('#{id}'); " |
|
53 | 53 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
54 | 54 | onclick << "Element.scrollTo('#{id}'); " |
|
55 | 55 | onclick << "return false;" |
|
56 | 56 | link_to(name, "#", options.merge(:onclick => onclick)) |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def image_to_function(name, function, html_options = {}) |
|
60 | 60 | html_options.symbolize_keys! |
|
61 | 61 | tag(:input, html_options.merge({ |
|
62 | 62 | :type => "image", :src => image_path(name), |
|
63 | 63 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
64 | 64 | })) |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def prompt_to_remote(name, text, param, url, html_options = {}) |
|
68 | 68 | html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" |
|
69 | 69 | link_to name, {}, html_options |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def format_date(date) |
|
73 | 73 | return nil unless date |
|
74 | 74 | # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed) |
|
75 | 75 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) |
|
76 | 76 | date.strftime(@date_format) |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def format_time(time, include_date = true) |
|
80 | 80 | return nil unless time |
|
81 | 81 | time = time.to_time if time.is_a?(String) |
|
82 | 82 | zone = User.current.time_zone |
|
83 | 83 | if time.utc? |
|
84 | 84 | local = zone ? zone.adjust(time) : time.getlocal |
|
85 | 85 | else |
|
86 | 86 | local = zone ? zone.adjust(time.getutc) : time |
|
87 | 87 | end |
|
88 | 88 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) |
|
89 | 89 | @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format) |
|
90 | 90 | include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format) |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | def html_hours(text) |
|
94 | 94 | text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>') |
|
95 | 95 | end |
|
96 | 96 | |
|
97 | 97 | def authoring(created, author) |
|
98 | 98 | time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created)) |
|
99 | 99 | l(:label_added_time_by, author || 'Anonymous', time_tag) |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def l_or_humanize(s) |
|
103 | 103 | l_has_string?("label_#{s}".to_sym) ? l("label_#{s}".to_sym) : s.to_s.humanize |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | def day_name(day) |
|
107 | 107 | l(:general_day_names).split(',')[day-1] |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def month_name(month) |
|
111 | 111 | l(:actionview_datehelper_select_month_names).split(',')[month-1] |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def pagination_links_full(paginator, count=nil, options={}) |
|
115 | 115 | page_param = options.delete(:page_param) || :page |
|
116 | 116 | url_param = params.dup |
|
117 | 117 | # don't reuse params if filters are present |
|
118 | 118 | url_param.clear if url_param.has_key?(:set_filter) |
|
119 | 119 | |
|
120 | 120 | html = '' |
|
121 | 121 | html << link_to_remote(('« ' + l(:label_previous)), |
|
122 | 122 | {:update => 'content', |
|
123 | 123 | :url => url_param.merge(page_param => paginator.current.previous), |
|
124 | 124 | :complete => 'window.scrollTo(0,0)'}, |
|
125 | 125 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous |
|
126 | 126 | |
|
127 | 127 | html << (pagination_links_each(paginator, options) do |n| |
|
128 | 128 | link_to_remote(n.to_s, |
|
129 | 129 | {:url => {:params => url_param.merge(page_param => n)}, |
|
130 | 130 | :update => 'content', |
|
131 | 131 | :complete => 'window.scrollTo(0,0)'}, |
|
132 | 132 | {:href => url_for(:params => url_param.merge(page_param => n))}) |
|
133 | 133 | end || '') |
|
134 | 134 | |
|
135 | 135 | html << ' ' + link_to_remote((l(:label_next) + ' »'), |
|
136 | 136 | {:update => 'content', |
|
137 | 137 | :url => url_param.merge(page_param => paginator.current.next), |
|
138 | 138 | :complete => 'window.scrollTo(0,0)'}, |
|
139 | 139 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next |
|
140 | 140 | |
|
141 | 141 | unless count.nil? |
|
142 | 142 | html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ') |
|
143 | 143 | end |
|
144 | 144 | |
|
145 | 145 | html |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | def per_page_links(selected=nil) |
|
149 | 149 | url_param = params.dup |
|
150 | 150 | url_param.clear if url_param.has_key?(:set_filter) |
|
151 | 151 | |
|
152 | 152 | links = Setting.per_page_options_array.collect do |n| |
|
153 | 153 | n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)}, |
|
154 | 154 | {:href => url_for(url_param.merge(:per_page => n))}) |
|
155 | 155 | end |
|
156 | 156 | links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | def breadcrumb(*args) |
|
160 | 160 | content_tag('p', args.join(' » ') + ' » ', :class => 'breadcrumb') |
|
161 | 161 | end |
|
162 | 162 | |
|
163 | 163 | def html_title(*args) |
|
164 | 164 | if args.empty? |
|
165 | 165 | title = [] |
|
166 | 166 | title << @project.name if @project |
|
167 | 167 | title += @html_title if @html_title |
|
168 | 168 | title << Setting.app_title |
|
169 | 169 | title.compact.join(' - ') |
|
170 | 170 | else |
|
171 | 171 | @html_title ||= [] |
|
172 | 172 | @html_title += args |
|
173 | 173 | end |
|
174 | 174 | end |
|
175 | 175 | |
|
176 | 176 | def accesskey(s) |
|
177 | 177 | Redmine::AccessKeys.key_for s |
|
178 | 178 | end |
|
179 | 179 | |
|
180 | 180 | # Formats text according to system settings. |
|
181 | 181 | # 2 ways to call this method: |
|
182 | 182 | # * with a String: textilizable(text, options) |
|
183 | 183 | # * with an object and one of its attribute: textilizable(issue, :description, options) |
|
184 | 184 | def textilizable(*args) |
|
185 | 185 | options = args.last.is_a?(Hash) ? args.pop : {} |
|
186 | 186 | case args.size |
|
187 | 187 | when 1 |
|
188 | 188 | obj = nil |
|
189 | 189 | text = args.shift |
|
190 | 190 | when 2 |
|
191 | 191 | obj = args.shift |
|
192 | 192 | text = obj.send(args.shift).to_s |
|
193 | 193 | else |
|
194 | 194 | raise ArgumentError, 'invalid arguments to textilizable' |
|
195 | 195 | end |
|
196 | 196 | return '' if text.blank? |
|
197 | 197 | |
|
198 | 198 | only_path = options.delete(:only_path) == false ? false : true |
|
199 | 199 | |
|
200 | 200 | # when using an image link, try to use an attachment, if possible |
|
201 | 201 | attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) |
|
202 | 202 | |
|
203 | 203 | if attachments |
|
204 | 204 | text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m| |
|
205 | 205 | style = $1 |
|
206 | 206 | filename = $6 |
|
207 | 207 | rf = Regexp.new(filename, Regexp::IGNORECASE) |
|
208 | 208 | # search for the picture in attachments |
|
209 | 209 | if found = attachments.detect { |att| att.filename =~ rf } |
|
210 | 210 | image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found |
|
211 | 211 | desc = found.description.to_s.gsub(/^([^\(\)]*).*$/, "\\1") |
|
212 | 212 | alt = desc.blank? ? nil : "(#{desc})" |
|
213 | 213 | "!#{style}#{image_url}#{alt}!" |
|
214 | 214 | else |
|
215 | 215 | "!#{style}#{filename}!" |
|
216 | 216 | end |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | text = (Setting.text_formatting == 'textile') ? |
|
221 | 221 | Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } : |
|
222 | 222 | simple_format(auto_link(h(text))) |
|
223 | 223 | |
|
224 | 224 | # different methods for formatting wiki links |
|
225 | 225 | case options[:wiki_links] |
|
226 | 226 | when :local |
|
227 | 227 | # used for local links to html files |
|
228 | 228 | format_wiki_link = Proc.new {|project, title| "#{title}.html" } |
|
229 | 229 | when :anchor |
|
230 | 230 | # used for single-file wiki export |
|
231 | 231 | format_wiki_link = Proc.new {|project, title| "##{title}" } |
|
232 | 232 | else |
|
233 | 233 | format_wiki_link = Proc.new {|project, title| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title) } |
|
234 | 234 | end |
|
235 | 235 | |
|
236 | 236 | project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) |
|
237 | 237 | |
|
238 | 238 | # Wiki links |
|
239 | 239 | # |
|
240 | 240 | # Examples: |
|
241 | 241 | # [[mypage]] |
|
242 | 242 | # [[mypage|mytext]] |
|
243 | 243 | # wiki links can refer other project wikis, using project name or identifier: |
|
244 | 244 | # [[project:]] -> wiki starting page |
|
245 | 245 | # [[project:|mytext]] |
|
246 | 246 | # [[project:mypage]] |
|
247 | 247 | # [[project:mypage|mytext]] |
|
248 | 248 | text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m| |
|
249 | 249 | link_project = project |
|
250 | 250 | esc, all, page, title = $1, $2, $3, $5 |
|
251 | 251 | if esc.nil? |
|
252 | 252 | if page =~ /^([^\:]+)\:(.*)$/ |
|
253 | 253 | link_project = Project.find_by_name($1) || Project.find_by_identifier($1) |
|
254 | 254 | page = $2 |
|
255 | 255 | title ||= $1 if page.blank? |
|
256 | 256 | end |
|
257 | 257 | |
|
258 | 258 | if link_project && link_project.wiki |
|
259 | 259 | # check if page exists |
|
260 | 260 | wiki_page = link_project.wiki.find_page(page) |
|
261 | 261 | link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)), |
|
262 | 262 | :class => ('wiki-page' + (wiki_page ? '' : ' new'))) |
|
263 | 263 | else |
|
264 | 264 | # project or wiki doesn't exist |
|
265 | 265 | title || page |
|
266 | 266 | end |
|
267 | 267 | else |
|
268 | 268 | all |
|
269 | 269 | end |
|
270 | 270 | end |
|
271 | 271 | |
|
272 | 272 | # Redmine links |
|
273 | 273 | # |
|
274 | 274 | # Examples: |
|
275 | 275 | # Issues: |
|
276 | 276 | # #52 -> Link to issue #52 |
|
277 | 277 | # Changesets: |
|
278 | 278 | # r52 -> Link to revision 52 |
|
279 | 279 | # commit:a85130f -> Link to scmid starting with a85130f |
|
280 | 280 | # Documents: |
|
281 | 281 | # document#17 -> Link to document with id 17 |
|
282 | 282 | # document:Greetings -> Link to the document with title "Greetings" |
|
283 | 283 | # document:"Some document" -> Link to the document with title "Some document" |
|
284 | 284 | # Versions: |
|
285 | 285 | # version#3 -> Link to version with id 3 |
|
286 | 286 | # version:1.0.0 -> Link to version named "1.0.0" |
|
287 | 287 | # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" |
|
288 | 288 | # Attachments: |
|
289 | 289 | # attachment:file.zip -> Link to the attachment of the current object named file.zip |
|
290 | 290 | # Source files: |
|
291 | 291 | # source:some/file -> Link to the file located at /some/file in the project's repository |
|
292 | 292 | # source:some/file@52 -> Link to the file's revision 52 |
|
293 | 293 | # source:some/file#L120 -> Link to line 120 of the file |
|
294 | 294 | # source:some/file@52#L120 -> Link to line 120 of the file's revision 52 |
|
295 | 295 | # export:some/file -> Force the download of the file |
|
296 | 296 | text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version|commit|source|export)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m| |
|
297 | 297 | leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8 |
|
298 | 298 | link = nil |
|
299 | 299 | if esc.nil? |
|
300 | 300 | if prefix.nil? && sep == 'r' |
|
301 | 301 | if project && (changeset = project.changesets.find_by_revision(oid)) |
|
302 | 302 | link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid}, |
|
303 | 303 | :class => 'changeset', |
|
304 | 304 | :title => truncate(changeset.comments, 100)) |
|
305 | 305 | end |
|
306 | 306 | elsif sep == '#' |
|
307 | 307 | oid = oid.to_i |
|
308 | 308 | case prefix |
|
309 | 309 | when nil |
|
310 | 310 | if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current)) |
|
311 | 311 | link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid}, |
|
312 | 312 | :class => (issue.closed? ? 'issue closed' : 'issue'), |
|
313 | 313 | :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})") |
|
314 | 314 | link = content_tag('del', link) if issue.closed? |
|
315 | 315 | end |
|
316 | 316 | when 'document' |
|
317 | 317 | if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
318 | 318 | link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, |
|
319 | 319 | :class => 'document' |
|
320 | 320 | end |
|
321 | 321 | when 'version' |
|
322 | 322 | if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
323 | 323 | link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, |
|
324 | 324 | :class => 'version' |
|
325 | 325 | end |
|
326 | 326 | end |
|
327 | 327 | elsif sep == ':' |
|
328 | 328 | # removes the double quotes if any |
|
329 | 329 | name = oid.gsub(%r{^"(.*)"$}, "\\1") |
|
330 | 330 | case prefix |
|
331 | 331 | when 'document' |
|
332 | 332 | if project && document = project.documents.find_by_title(name) |
|
333 | 333 | link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, |
|
334 | 334 | :class => 'document' |
|
335 | 335 | end |
|
336 | 336 | when 'version' |
|
337 | 337 | if project && version = project.versions.find_by_name(name) |
|
338 | 338 | link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, |
|
339 | 339 | :class => 'version' |
|
340 | 340 | end |
|
341 | 341 | when 'commit' |
|
342 | 342 | if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"])) |
|
343 | 343 | link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, :class => 'changeset', :title => truncate(changeset.comments, 100) |
|
344 | 344 | end |
|
345 | 345 | when 'source', 'export' |
|
346 | 346 | if project && project.repository |
|
347 | 347 | name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$} |
|
348 | 348 | path, rev, anchor = $1, $3, $5 |
|
349 | 349 | link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, :path => path, |
|
350 | 350 | :rev => rev, |
|
351 | 351 | :anchor => anchor, |
|
352 | 352 | :format => (prefix == 'export' ? 'raw' : nil)}, |
|
353 | 353 | :class => (prefix == 'export' ? 'source download' : 'source') |
|
354 | 354 | end |
|
355 | 355 | when 'attachment' |
|
356 | 356 | if attachments && attachment = attachments.detect {|a| a.filename == name } |
|
357 | 357 | link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment}, |
|
358 | 358 | :class => 'attachment' |
|
359 | 359 | end |
|
360 | 360 | end |
|
361 | 361 | end |
|
362 | 362 | end |
|
363 | 363 | leading + (link || "#{prefix}#{sep}#{oid}") |
|
364 | 364 | end |
|
365 | 365 | |
|
366 | 366 | text |
|
367 | 367 | end |
|
368 | 368 | |
|
369 | 369 | # Same as Rails' simple_format helper without using paragraphs |
|
370 | 370 | def simple_format_without_paragraph(text) |
|
371 | 371 | text.to_s. |
|
372 | 372 | gsub(/\r\n?/, "\n"). # \r\n and \r -> \n |
|
373 | 373 | gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br |
|
374 | 374 | gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br |
|
375 | 375 | end |
|
376 | 376 | |
|
377 | 377 | def error_messages_for(object_name, options = {}) |
|
378 | 378 | options = options.symbolize_keys |
|
379 | 379 | object = instance_variable_get("@#{object_name}") |
|
380 | 380 | if object && !object.errors.empty? |
|
381 | 381 | # build full_messages here with controller current language |
|
382 | 382 | full_messages = [] |
|
383 | 383 | object.errors.each do |attr, msg| |
|
384 | 384 | next if msg.nil? |
|
385 | 385 | msg = msg.first if msg.is_a? Array |
|
386 | 386 | if attr == "base" |
|
387 | 387 | full_messages << l(msg) |
|
388 | 388 | else |
|
389 | 389 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" |
|
390 | 390 | end |
|
391 | 391 | end |
|
392 | 392 | # retrieve custom values error messages |
|
393 | 393 | if object.errors[:custom_values] |
|
394 | 394 | object.custom_values.each do |v| |
|
395 | 395 | v.errors.each do |attr, msg| |
|
396 | 396 | next if msg.nil? |
|
397 | 397 | msg = msg.first if msg.is_a? Array |
|
398 | 398 | full_messages << "« " + v.custom_field.name + " » " + l(msg) |
|
399 | 399 | end |
|
400 | 400 | end |
|
401 | 401 | end |
|
402 | 402 | content_tag("div", |
|
403 | 403 | content_tag( |
|
404 | 404 | options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":" |
|
405 | 405 | ) + |
|
406 | 406 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), |
|
407 | 407 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" |
|
408 | 408 | ) |
|
409 | 409 | else |
|
410 | 410 | "" |
|
411 | 411 | end |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | def lang_options_for_select(blank=true) |
|
415 | 415 | (blank ? [["(auto)", ""]] : []) + |
|
416 | 416 | GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last } |
|
417 | 417 | end |
|
418 | 418 | |
|
419 | 419 | def label_tag_for(name, option_tags = nil, options = {}) |
|
420 | 420 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
421 | 421 | content_tag("label", label_text) |
|
422 | 422 | end |
|
423 | 423 | |
|
424 | 424 | def labelled_tabular_form_for(name, object, options, &proc) |
|
425 | 425 | options[:html] ||= {} |
|
426 | 426 | options[:html][:class] = 'tabular' unless options[:html].has_key?(:class) |
|
427 | 427 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
428 | 428 | end |
|
429 | 429 | |
|
430 | def back_url_hidden_field_tag | |
|
431 | hidden_field_tag 'back_url', (params[:back_url] || request.env['HTTP_REFERER']) | |
|
432 | end | |
|
433 | ||
|
430 | 434 | def check_all_links(form_name) |
|
431 | 435 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
432 | 436 | " | " + |
|
433 | 437 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
434 | 438 | end |
|
435 | 439 | |
|
436 | 440 | def progress_bar(pcts, options={}) |
|
437 | 441 | pcts = [pcts, pcts] unless pcts.is_a?(Array) |
|
438 | 442 | pcts[1] = pcts[1] - pcts[0] |
|
439 | 443 | pcts << (100 - pcts[1] - pcts[0]) |
|
440 | 444 | width = options[:width] || '100px;' |
|
441 | 445 | legend = options[:legend] || '' |
|
442 | 446 | content_tag('table', |
|
443 | 447 | content_tag('tr', |
|
444 | 448 | (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') + |
|
445 | 449 | (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') + |
|
446 | 450 | (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '') |
|
447 | 451 | ), :class => 'progress', :style => "width: #{width};") + |
|
448 | 452 | content_tag('p', legend, :class => 'pourcent') |
|
449 | 453 | end |
|
450 | 454 | |
|
451 | 455 | def context_menu_link(name, url, options={}) |
|
452 | 456 | options[:class] ||= '' |
|
453 | 457 | if options.delete(:selected) |
|
454 | 458 | options[:class] << ' icon-checked disabled' |
|
455 | 459 | options[:disabled] = true |
|
456 | 460 | end |
|
457 | 461 | if options.delete(:disabled) |
|
458 | 462 | options.delete(:method) |
|
459 | 463 | options.delete(:confirm) |
|
460 | 464 | options.delete(:onclick) |
|
461 | 465 | options[:class] << ' disabled' |
|
462 | 466 | url = '#' |
|
463 | 467 | end |
|
464 | 468 | link_to name, url, options |
|
465 | 469 | end |
|
466 | 470 | |
|
467 | 471 | def calendar_for(field_id) |
|
468 | 472 | include_calendar_headers_tags |
|
469 | 473 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
470 | 474 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
471 | 475 | end |
|
472 | 476 | |
|
473 | 477 | def include_calendar_headers_tags |
|
474 | 478 | unless @calendar_headers_tags_included |
|
475 | 479 | @calendar_headers_tags_included = true |
|
476 | 480 | content_for :header_tags do |
|
477 | 481 | javascript_include_tag('calendar/calendar') + |
|
478 | 482 | javascript_include_tag("calendar/lang/calendar-#{current_language}.js") + |
|
479 | 483 | javascript_include_tag('calendar/calendar-setup') + |
|
480 | 484 | stylesheet_link_tag('calendar') |
|
481 | 485 | end |
|
482 | 486 | end |
|
483 | 487 | end |
|
484 | 488 | |
|
485 | 489 | def wikitoolbar_for(field_id) |
|
486 | 490 | return '' unless Setting.text_formatting == 'textile' |
|
487 | 491 | |
|
488 | 492 | help_link = l(:setting_text_formatting) + ': ' + |
|
489 | 493 | link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'), |
|
490 | 494 | :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;") |
|
491 | 495 | |
|
492 | 496 | javascript_include_tag('jstoolbar/jstoolbar') + |
|
493 | 497 | javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") + |
|
494 | 498 | javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();") |
|
495 | 499 | end |
|
496 | 500 | |
|
497 | 501 | def content_for(name, content = nil, &block) |
|
498 | 502 | @has_content ||= {} |
|
499 | 503 | @has_content[name] = true |
|
500 | 504 | super(name, content, &block) |
|
501 | 505 | end |
|
502 | 506 | |
|
503 | 507 | def has_content?(name) |
|
504 | 508 | (@has_content && @has_content[name]) || false |
|
505 | 509 | end |
|
506 | 510 | end |
@@ -1,16 +1,17 | |||
|
1 | 1 | <h2><%= l(:label_spent_time) %></h2> |
|
2 | 2 | |
|
3 | 3 | <% labelled_tabular_form_for :time_entry, @time_entry, :url => {:action => 'edit', :project_id => @time_entry.project} do |f| %> |
|
4 | 4 | <%= error_messages_for 'time_entry' %> |
|
5 | <%= back_url_hidden_field_tag %> | |
|
5 | 6 | |
|
6 | 7 | <div class="box"> |
|
7 | 8 | <p><%= f.text_field :issue_id, :size => 6 %> <em><%= h("#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}") if @time_entry.issue %></em></p> |
|
8 | 9 | <p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p> |
|
9 | 10 | <p><%= f.text_field :hours, :size => 6, :required => true %></p> |
|
10 | 11 | <p><%= f.text_field :comments, :size => 100 %></p> |
|
11 | 12 | <p><%= f.select :activity_id, (@activities.collect {|p| [p.name, p.id]}), :required => true %></p> |
|
12 | 13 | </div> |
|
13 | 14 | |
|
14 | 15 | <%= submit_tag l(:button_save) %> |
|
15 | 16 | |
|
16 | 17 | <% end %> |
General Comments 0
You need to be logged in to leave comments.
Login now