| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2023-01-01 06:19:35 +00:00
										 |  |  | # Copyright (C) 2006-2023  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Redmine | 
					
						
							|  |  |  |   module MyPage | 
					
						
							|  |  |  |     include Redmine::I18n | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 17:17:47 +00:00
										 |  |  |     CORE_GROUPS = ['top', 'left', 'right'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |     CORE_BLOCKS = { | 
					
						
							| 
									
										
										
										
											2020-10-05 15:47:50 +00:00
										 |  |  |       'issuesassignedtome' => {:label => :label_assigned_to_me_issues}, | 
					
						
							|  |  |  |       'issuesreportedbyme' => {:label => :label_reported_issues}, | 
					
						
							|  |  |  |       'issuesupdatedbyme' => {:label => :label_updated_issues}, | 
					
						
							|  |  |  |       'issueswatched' => {:label => :label_watched_issues}, | 
					
						
							|  |  |  |       'issuequery' => {:label => :label_issue_plural, :max_occurs => 3}, | 
					
						
							|  |  |  |       'news' => {:label => :label_news_latest}, | 
					
						
							|  |  |  |       'calendar' => {:label => :label_calendar}, | 
					
						
							|  |  |  |       'documents' => {:label => :label_document_plural}, | 
					
						
							|  |  |  |       'timelog' => {:label => :label_spent_time}, | 
					
						
							|  |  |  |       'activity' => {:label => :label_activity} | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 17:17:47 +00:00
										 |  |  |     def self.groups | 
					
						
							|  |  |  |       CORE_GROUPS.dup.freeze | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |     # Returns the available blocks | 
					
						
							|  |  |  |     def self.blocks | 
					
						
							|  |  |  |       CORE_BLOCKS.merge(additional_blocks).freeze | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-16 18:02:43 +00:00
										 |  |  |     def self.block_options(blocks_in_use=[]) | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |       options = [] | 
					
						
							| 
									
										
										
										
											2017-03-14 18:18:19 +00:00
										 |  |  |       blocks.each do |block, block_options| | 
					
						
							| 
									
										
										
										
											2020-11-07 12:30:23 +00:00
										 |  |  |         indexes = blocks_in_use.map do |n| | 
					
						
							| 
									
										
										
										
											2017-03-16 18:02:43 +00:00
										 |  |  |           if n =~ /\A#{block}(__(\d+))?\z/ | 
					
						
							|  |  |  |             $2.to_i | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2020-11-07 12:30:23 +00:00
										 |  |  |         end.compact | 
					
						
							| 
									
										
										
										
											2017-03-16 18:02:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         occurs = indexes.size | 
					
						
							|  |  |  |         block_id = indexes.any? ? "#{block}__#{indexes.max + 1}" : block | 
					
						
							|  |  |  |         disabled = (occurs >= (Redmine::MyPage.blocks[block][:max_occurs] || 1)) | 
					
						
							|  |  |  |         block_id = nil if disabled | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-14 18:18:19 +00:00
										 |  |  |         label = block_options[:label] | 
					
						
							| 
									
										
										
										
											2017-03-16 18:02:43 +00:00
										 |  |  |         options << [l("my.blocks.#{label}", :default => [label, label.to_s.humanize]), block_id] | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       options | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-16 18:02:43 +00:00
										 |  |  |     def self.valid_block?(block, blocks_in_use=[]) | 
					
						
							|  |  |  |       block.present? && block_options(blocks_in_use).map(&:last).include?(block) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.find_block(block) | 
					
						
							|  |  |  |       block.to_s =~  /\A(.*?)(__\d+)?\z/ | 
					
						
							|  |  |  |       name = $1 | 
					
						
							|  |  |  |       blocks.has_key?(name) ? blocks[name].merge(:name => name) : nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |     # Returns the additional blocks that are defined by plugin partials | 
					
						
							|  |  |  |     def self.additional_blocks | 
					
						
							| 
									
										
										
										
											2020-10-05 15:47:18 +00:00
										 |  |  |       @@additional_blocks ||= | 
					
						
							|  |  |  |         Dir.glob( | 
					
						
							|  |  |  |           "#{Redmine::Plugin.directory}/*/app/views/my/blocks/_*.{rhtml,erb}" | 
					
						
							|  |  |  |         ).inject({}) do |h, file| | 
					
						
							|  |  |  |           name = File.basename(file).split('.').first.gsub(/^_/, '') | 
					
						
							|  |  |  |           h[name] = {:label => name.to_sym, :partial => "my/blocks/#{name}"} | 
					
						
							|  |  |  |           h | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-10-29 10:10:31 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Returns the default layout for My Page | 
					
						
							|  |  |  |     def self.default_layout | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         'left' => ['issuesassignedtome'], | 
					
						
							|  |  |  |         'right' => ['issuesreportedbyme'] | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |