2011-12-14 19:56:23 +00:00
# encoding: utf-8
#
2011-08-31 08:40:05 +00:00
# Redmine - project management software
2017-06-25 08:40:31 +00:00
# Copyright (C) 2006-2017 Jean-Philippe Lang
2006-12-03 19:55:45 +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.
2011-08-31 08:40:05 +00:00
#
2006-12-03 19:55:45 +00:00
# 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.
2011-08-31 08:40:05 +00:00
#
2006-12-03 19:55:45 +00:00
# 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 MyHelper
2016-10-29 08:10:19 +00:00
# Renders the blocks
def render_blocks ( blocks , user , options = { } )
s = '' . html_safe
if blocks . present?
blocks . each do | block |
2017-03-13 19:57:49 +00:00
s << render_block ( block , user ) . to_s
2016-10-29 08:10:19 +00:00
end
end
s
end
2017-03-13 19:57:49 +00:00
# Renders a single block
def render_block ( block , user )
content = render_block_content ( block , user )
if content . present?
2017-03-14 18:25:23 +00:00
handle = content_tag ( 'span' , '' , :class = > 'sort-handle' , :title = > l ( :button_move ) )
close = link_to ( l ( :button_delete ) ,
{ :action = > " remove_block " , :block = > block } ,
:remote = > true , :method = > 'post' ,
:class = > " icon-only icon-close " , :title = > l ( :button_delete ) )
2017-03-13 19:57:49 +00:00
content = content_tag ( 'div' , handle + close , :class = > 'contextual' ) + content
content_tag ( 'div' , content , :class = > " mypage-box " , :id = > " block- #{ block } " )
end
end
2016-10-29 08:10:19 +00:00
# Renders a single block content
def render_block_content ( block , user )
2017-03-16 18:02:43 +00:00
unless block_definition = Redmine :: MyPage . find_block ( block )
2016-10-29 08:10:19 +00:00
Rails . logger . warn ( " Unknown block \" #{ block } \" found in #{ user . login } (id= #{ user . id } ) preferences " )
return
end
2016-10-30 08:32:31 +00:00
settings = user . pref . my_page_settings ( block )
2017-03-16 18:02:43 +00:00
if partial = block_definition [ :partial ]
begin
render ( :partial = > partial , :locals = > { :user = > user , :settings = > settings , :block = > block } )
rescue ActionView :: MissingTemplate
Rails . logger . warn ( " Partial \" #{ partial } \" missing for block \" #{ block } \" found in #{ user . login } (id= #{ user . id } ) preferences " )
return nil
end
else
send " render_ #{ block_definition [ :name ] } _block " , block , settings
2016-10-29 08:10:19 +00:00
end
end
2017-03-16 18:26:43 +00:00
# Returns the select tag used to add a block to My page
2016-10-29 10:10:31 +00:00
def block_select_tag ( user )
2017-03-16 18:02:43 +00:00
blocks_in_use = user . pref . my_page_layout . values . flatten
2016-10-29 10:10:31 +00:00
options = content_tag ( 'option' )
2017-03-16 18:02:43 +00:00
Redmine :: MyPage . block_options ( blocks_in_use ) . each do | label , block |
options << content_tag ( 'option' , label , :value = > block , :disabled = > block . blank? )
2016-10-29 10:10:31 +00:00
end
2017-03-13 20:02:34 +00:00
select_tag ( 'block' , options , :id = > " block-select " , :onchange = > " $(' # block-form').submit(); " )
2016-10-29 10:10:31 +00:00
end
2017-03-16 18:26:43 +00:00
def render_calendar_block ( block , settings )
calendar = Redmine :: Helpers :: Calendar . new ( User . current . today , current_language , :week )
calendar . events = Issue . visible .
2017-11-26 14:37:31 +00:00
where ( :project_id = > User . current . projects . pluck ( :id ) ) .
2017-03-16 18:26:43 +00:00
where ( " (start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) " , calendar . startdt , calendar . enddt , calendar . startdt , calendar . enddt ) .
2012-12-02 21:08:03 +00:00
includes ( :project , :tracker , :priority , :assigned_to ) .
2014-10-22 17:37:16 +00:00
references ( :project , :tracker , :priority , :assigned_to ) .
to_a
2012-12-02 21:08:03 +00:00
2017-03-16 18:26:43 +00:00
render :partial = > 'my/blocks/calendar' , :locals = > { :calendar = > calendar , :block = > block }
2012-12-02 21:08:03 +00:00
end
2017-03-16 18:26:43 +00:00
def render_documents_block ( block , settings )
documents = Document . visible . order ( " #{ Document . table_name } .created_on DESC " ) . limit ( 10 ) . to_a
render :partial = > 'my/blocks/documents' , :locals = > { :block = > block , :documents = > documents }
2012-12-02 21:08:03 +00:00
end
2017-03-16 18:02:43 +00:00
def render_issuesassignedtome_block ( block , settings )
2017-03-14 18:18:19 +00:00
query = IssueQuery . new ( :name = > l ( :label_assigned_to_me_issues ) , :user = > User . current )
query . add_filter 'assigned_to_id' , '=' , [ 'me' ]
2018-10-29 06:14:56 +00:00
query . add_filter 'project.status' , '=' , [ '1' ]
2017-03-14 18:18:19 +00:00
query . column_names = settings [ :columns ] . presence || [ 'project' , 'tracker' , 'status' , 'subject' ]
query . sort_criteria = settings [ :sort ] . presence || [ [ 'priority' , 'desc' ] , [ 'updated_on' , 'desc' ] ]
issues = query . issues ( :limit = > 10 )
2017-03-16 18:02:43 +00:00
render :partial = > 'my/blocks/issues' , :locals = > { :query = > query , :issues = > issues , :block = > block }
2012-12-02 21:08:03 +00:00
end
2017-03-16 18:02:43 +00:00
def render_issuesreportedbyme_block ( block , settings )
2017-03-14 18:18:19 +00:00
query = IssueQuery . new ( :name = > l ( :label_reported_issues ) , :user = > User . current )
query . add_filter 'author_id' , '=' , [ 'me' ]
2018-10-29 06:14:56 +00:00
query . add_filter 'project.status' , '=' , [ '1' ]
2017-03-14 18:18:19 +00:00
query . column_names = settings [ :columns ] . presence || [ 'project' , 'tracker' , 'status' , 'subject' ]
query . sort_criteria = settings [ :sort ] . presence || [ [ 'updated_on' , 'desc' ] ]
issues = query . issues ( :limit = > 10 )
2017-03-16 18:02:43 +00:00
render :partial = > 'my/blocks/issues' , :locals = > { :query = > query , :issues = > issues , :block = > block }
2017-03-14 18:18:19 +00:00
end
2017-03-16 18:02:43 +00:00
def render_issueswatched_block ( block , settings )
2017-03-14 18:18:19 +00:00
query = IssueQuery . new ( :name = > l ( :label_watched_issues ) , :user = > User . current )
query . add_filter 'watcher_id' , '=' , [ 'me' ]
2018-10-29 06:14:56 +00:00
query . add_filter 'project.status' , '=' , [ '1' ]
2017-03-14 18:18:19 +00:00
query . column_names = settings [ :columns ] . presence || [ 'project' , 'tracker' , 'status' , 'subject' ]
query . sort_criteria = settings [ :sort ] . presence || [ [ 'updated_on' , 'desc' ] ]
issues = query . issues ( :limit = > 10 )
2017-03-16 18:02:43 +00:00
render :partial = > 'my/blocks/issues' , :locals = > { :query = > query , :issues = > issues , :block = > block }
2012-12-02 21:08:03 +00:00
end
2017-03-16 18:02:43 +00:00
def render_issuequery_block ( block , settings )
2017-03-15 17:59:31 +00:00
query = IssueQuery . visible . find_by_id ( settings [ :query_id ] )
2017-03-16 18:02:43 +00:00
if query
query . column_names = settings [ :columns ] if settings [ :columns ] . present?
query . sort_criteria = settings [ :sort ] if settings [ :sort ] . present?
issues = query . issues ( :limit = > 10 )
render :partial = > 'my/blocks/issues' , :locals = > { :query = > query , :issues = > issues , :block = > block , :settings = > settings }
else
2017-03-18 08:36:52 +00:00
queries = IssueQuery . visible . sorted
render :partial = > 'my/blocks/issue_query_selection' , :locals = > { :queries = > queries , :block = > block , :settings = > settings }
2017-03-16 18:02:43 +00:00
end
2017-03-15 17:59:31 +00:00
end
2017-03-16 18:26:43 +00:00
def render_news_block ( block , settings )
news = News . visible .
2017-11-26 14:37:31 +00:00
where ( :project_id = > User . current . projects . pluck ( :id ) ) .
2012-12-02 21:08:03 +00:00
limit ( 10 ) .
includes ( :project , :author ) .
2014-10-22 17:37:16 +00:00
references ( :project , :author ) .
2012-12-02 21:08:03 +00:00
order ( " #{ News . table_name } .created_on DESC " ) .
2014-10-22 17:37:16 +00:00
to_a
2017-03-16 18:26:43 +00:00
render :partial = > 'my/blocks/news' , :locals = > { :block = > block , :news = > news }
2012-12-02 21:08:03 +00:00
end
2017-03-16 18:26:43 +00:00
def render_timelog_block ( block , settings )
2016-10-30 08:32:31 +00:00
days = settings [ :days ] . to_i
days = 7 if days < 1 || days > 365
entries = TimeEntry .
where ( " #{ TimeEntry . table_name } .user_id = ? AND #{ TimeEntry . table_name } .spent_on BETWEEN ? AND ? " , User . current . id , User . current . today - ( days - 1 ) , User . current . today ) .
2015-03-10 19:42:45 +00:00
joins ( :activity , :project ) .
references ( :issue = > [ :tracker , :status ] ) .
includes ( :issue = > [ :tracker , :status ] ) .
2012-12-02 21:08:03 +00:00
order ( " #{ TimeEntry . table_name } .spent_on DESC, #{ Project . table_name } .name ASC, #{ Tracker . table_name } .position ASC, #{ Issue . table_name } .id ASC " ) .
2014-10-22 17:37:16 +00:00
to_a
2017-03-16 18:26:43 +00:00
entries_by_day = entries . group_by ( & :spent_on )
2016-10-30 08:32:31 +00:00
2017-03-16 18:26:43 +00:00
render :partial = > 'my/blocks/timelog' , :locals = > { :block = > block , :entries = > entries , :entries_by_day = > entries_by_day , :days = > days }
2012-12-02 21:08:03 +00:00
end
2018-07-11 14:51:07 +00:00
def render_activity_block ( block , settings )
2018-10-20 23:43:25 +00:00
events_by_day = Redmine :: Activity :: Fetcher . new ( User . current , :author = > User . current ) . events ( nil , nil , :limit = > 10 ) . group_by { | event | User . current . time_to_date ( event . event_datetime ) }
2018-07-11 14:51:07 +00:00
render :partial = > 'my/blocks/activity' , :locals = > { :events_by_day = > events_by_day }
end
2006-12-03 19:55:45 +00:00
end