annotate plugins/redmine_bibliography/app/helpers/publications_helper.rb @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents f86e70095184
children
rev   line source
chris@615 1 # -*- coding: utf-8 -*-
luis@407 2 require 'bibtex'
luis@407 3
luis@328 4 module PublicationsHelper
chris@702 5 include AuthorshipsHelper
luis@616 6
luis@616 7 def link_to_publication(publication, options={}, html_options = nil)
luis@616 8 url = {:controller => 'publications', :action => 'show', :id => publication}.merge(options)
luis@616 9 link_to(h(publication.title), url, html_options)
luis@616 10 end
luis@616 11
luis@462 12 def projects_check_box_tags(name, projects)
luis@462 13 s = ''
luis@462 14 projects.sort.each do |project|
luis@1068 15 if User.current.allowed_to?(:edit_publication, project)
luis@713 16 s << "<label>#{ check_box_tag name, project.id, false } #{link_to_project project}</label>\n"
luis@713 17 s << '<br />'
luis@713 18 end
luis@462 19 end
luis@712 20
luis@1324 21 s.html_safe
luis@462 22 end
luis@1068 23
luis@469 24 def link_to_remove_fields(name, f)
chris@573 25 f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", :class => 'icon icon-del')
luis@469 26 end
luis@1068 27
luis@705 28 def link_to_add_author_fields(name, f, association, action)
luis@468 29 new_object = f.object.class.reflect_on_association(association).klass.new
luis@468 30 fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
luis@1388 31 # renders _authorship_fields.html.erb
luis@468 32 render(association.to_s.singularize + "_fields", :f => builder)
luis@1068 33 end
luis@1388 34
luis@1278 35 link_to_function(name, "add_author_fields(this, '#{association}', '#{escape_javascript(fields)}', '#{action}')", { :class => 'icon icon-add', :id => "add_another_author" })
luis@1068 36 end
luis@481 37
luis@481 38 def sanitized_object_name(object_name)
luis@481 39 object_name.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
luis@481 40 end
luis@481 41
luis@481 42 def sanitized_method_name(method_name)
luis@481 43 method_name.sub(/\?$/, "")
luis@481 44 end
luis@1068 45
luis@595 46 def form_tag_name(object_name, method_name)
luis@595 47 str = "#{object_name.to_s}[#{sanitized_method_name(method_name.to_s)}]"
luis@1068 48 str.to_sym
luis@595 49 end
luis@1068 50
luis@1068 51 def form_tag_id(object_name, method_name)
luis@481 52 str = "#{sanitized_object_name(object_name.to_s)}_#{sanitized_method_name(method_name.to_s)}"
luis@481 53 str.to_sym
luis@481 54 end
luis@1068 55
luis@596 56 def form_object_id(object_name)
luis@596 57 str = object_name.split("\[").last().gsub("\]","")
luis@596 58 str.to_sym
luis@596 59 end
luis@693 60
luis@1068 61 def render_authorships_list(publication)
luis@693 62 s = '<p>'
luis@1068 63
luis@693 64 publication.authorships.each do |authorship|
chris@702 65 s << link_to_authorship(authorship)
luis@693 66 s << "<br /><em>#{authorship.institution}</em></p>"
luis@1068 67 end
luis@693 68
luis@1307 69 s.html_safe
luis@693 70 end
luis@1068 71
luis@1068 72 def render_projects_list(publication, show_delete_icon)
luis@691 73 s= ""
luis@1068 74
luis@691 75 publication.projects.visible.each do |proj|
luis@629 76 s << link_to_project(proj, {}, :class => 'publication_project')
luis@1068 77
luis@1068 78 if show_delete_icon
luis@691 79 if User.current.allowed_to?(:edit_publication, @project)
luis@691 80 if @project == proj
luis@945 81 # todo: move this message to yml file
luis@691 82 confirm_msg = 'Are you sure you want to remove the current project from this publication\'s projects list?'
luis@691 83 else
luis@691 84 confirm_msg = false
luis@1068 85 end
luis@1068 86
luis@1317 87 s << link_to(l(:button_delete), { :url => { :controller => 'publications', :action => 'remove_project', :id => publication, :remove_project_id => proj, :project_id => @project }, :method => :post, :confirm => confirm_msg }, :class => 'icon icon-del', :remote => :true)
luis@691 88 end
luis@629 89 end
luis@691 90
luis@1068 91 s << "<br />"
luis@1068 92 end
luis@1068 93
luis@1307 94 s.html_safe
luis@541 95 end
luis@1068 96
luis@1068 97 def print_ieee_format(publication)
luis@1068 98 Rails.cache.fetch("publication-#{publication.id}-ieee") do
Chris@1497 99 entry = publication.print_entry(:ieee)
Chris@1497 100 if entry
Chris@1497 101 entry.html_safe
Chris@1497 102 end
luis@1068 103 end
luis@946 104 end
luis@1068 105
luis@1068 106 def print_bibtex_format(publication)
luis@1068 107 Rails.cache.fetch("publication-#{publication.id}-bibtex") do
luis@1068 108 publication.print_entry(:bibtex)
luis@1068 109 end
luis@945 110 end
luis@1068 111
luis@544 112 def show_bibtex_fields(bibtex_entry)
luis@544 113 s = ""
chris@615 114 bibtex_entry.attributes.keys.sort.each do |key|
chris@615 115 value = bibtex_entry.attributes[key].to_s
chris@615 116 next if key == 'id' or key == 'publication_id' or value == ""
luis@1068 117 s << "<h4>" + l("field_#{key}") + "</h4>"
chris@615 118 s << "<p>"
chris@615 119 if key == "entry_type"
chris@615 120 s << bibtex_entry.entry_type_label
chris@615 121 else
chris@615 122 s << value
luis@544 123 end
chris@615 124 s << "</p>"
luis@544 125 end
luis@544 126 s
luis@1068 127 end
luis@328 128 end
luis@1407 129
luis@1407 130
luis@1407 131 def render_authorship_link(link_class, link_id)
luis@1407 132
luis@1407 133 # Renders a link for an author used when adding authors for a publication
luis@1407 134 # link_class can be either User or Author
luis@1407 135 # link_id will be the id of the Author/User we wish to link
luis@1407 136
luis@1407 137 s= ""
luis@1407 138
luis@1407 139 if link_class == "Author"
luis@1416 140 url = {:controller => 'authors', :action => 'show', :id => link_id}
luis@1416 141 s << link_to(h(Author.find(link_id).name), url)
luis@1407 142 else
luis@1416 143 url = {:controller => 'users', :action => 'show', :id => link_id}
luis@1416 144 s << link_to(h(User.find(link_id).name), url)
luis@1407 145 end
luis@1407 146
luis@1407 147 s.html_safe
luis@1407 148 end
luis@1407 149