Revision 407:96910efbd45e vendor/plugins/redmine_bibliography/app/controllers
| vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb | ||
|---|---|---|
| 2 | 2 |
|
| 3 | 3 |
class PublicationsController < ApplicationController |
| 4 | 4 |
|
| 5 |
|
|
| 6 | 5 |
def new |
| 7 | 6 |
# we always try to create at least one publication |
| 8 | 7 |
@publication = Publication.new |
| 9 |
|
|
| 8 |
|
|
| 10 | 9 |
# the step we're at in the form |
| 11 | 10 |
@publication.current_step = session[:publication_step] |
| 12 | 11 |
end |
| ... | ... | |
| 18 | 17 |
# contents of the paste text area |
| 19 | 18 |
bibtex_entry = params[:bibtex_entry] |
| 20 | 19 |
|
| 21 |
# debug message |
|
| 22 |
logger.error bibtex_entry |
|
| 23 |
|
|
| 24 | 20 |
# method for creating "pasted" bibtex entries |
| 25 | 21 |
if bibtex_entry |
| 26 |
parse_bibtex_text bibtex_entry
|
|
| 22 |
parse_bibtex_list bibtex_entry
|
|
| 27 | 23 |
end |
| 28 | 24 |
|
| 29 | 25 |
# form's flow control |
| ... | ... | |
| 52 | 48 |
|
| 53 | 49 |
logger.error "AAAA update" |
| 54 | 50 |
|
| 55 |
|
|
| 56 | 51 |
end |
| 57 | 52 |
|
| 58 | 53 |
def show |
| ... | ... | |
| 60 | 55 |
@authors = @publication.authors |
| 61 | 56 |
end |
| 62 | 57 |
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 | 58 |
# parse string with bibtex authors |
| 75 | 59 |
def parse_authors(authors_entry) |
| 76 | 60 |
# in bibtex the authors are always seperated by "and" |
| 77 |
authors = authors_entry.split(" and ")
|
|
| 78 |
|
|
| 79 |
# need to save all authors |
|
| 80 |
|
|
| 81 |
|
|
| 82 |
return authors |
|
| 83 |
end |
|
| 84 |
|
|
| 85 |
# parses the bibtex file |
|
| 86 |
def parse_bibtex_file |
|
| 87 |
|
|
| 61 |
return authors_entry.split(" and ")
|
|
| 88 | 62 |
end |
| 89 | 63 |
|
| 90 | 64 |
# parses a list of bibtex |
| ... | ... | |
| 93 | 67 |
|
| 94 | 68 |
no_entries = bibliography.data.length |
| 95 | 69 |
|
| 96 |
puts "Gonna parse " + no_entries.to_s + " Bibtex entries" |
|
| 97 |
|
|
| 98 | 70 |
# parses the bibtex entries |
| 99 | 71 |
bibliography.data.map do |d| |
| 100 |
create_bibtex_entry d |
|
| 72 |
|
|
| 73 |
if d.class == BibTeX::Entry |
|
| 74 |
create_bibtex_entry d |
|
| 75 |
end |
|
| 101 | 76 |
end |
| 77 |
end |
|
| 78 |
|
|
| 79 |
def create_bibtex_entry(d) |
|
| 80 |
@publication = Publication.new |
|
| 81 |
@bentry = BibtexEntry.new |
|
| 82 |
authors = [] |
|
| 83 |
institution = "" |
|
| 84 |
email = "" |
|
| 85 |
|
|
| 86 |
d.fields.keys.map do |field| |
|
| 87 |
case field.to_s |
|
| 88 |
when "author" |
|
| 89 |
authors = parse_authors d[field] |
|
| 90 |
when "title" |
|
| 91 |
@publication.title = d[field] |
|
| 92 |
when "institution" |
|
| 93 |
institution = d[field] |
|
| 94 |
when "email" |
|
| 95 |
email = d[field] |
|
| 96 |
else |
|
| 97 |
@bentry[field] = d[field] |
|
| 98 |
end |
|
| 99 |
end |
|
| 102 | 100 |
|
| 103 | 101 |
@publication.bibtex_entry = @bentry |
| 104 |
|
|
| 105 |
if @publication.save |
|
| 106 |
puts "SAVED" |
|
| 107 |
else |
|
| 108 |
puts "NOT SAVED" |
|
| 109 |
end |
|
| 110 |
|
|
| 111 |
Rails.logger.error @publication.bibtex_entry |
|
| 112 |
end |
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
def create_bibtex_entry(d) |
|
| 117 |
|
|
| 118 |
if d.class == BibTeX::Entry |
|
| 119 |
# creates a new BibTex instance |
|
| 120 |
@bentry = BibtexEntry.new |
|
| 121 |
|
|
| 122 |
d.fields.keys.map do |field| |
|
| 123 |
|
|
| 124 |
case field.to_s |
|
| 125 |
when "author" |
|
| 126 |
authors = parse_authors d[field] |
|
| 127 |
puts "Number of authors: " + authors.length.to_s |
|
| 128 |
when "title" |
|
| 129 |
puts "The title " + d[field] |
|
| 130 |
@publication.title = d[field] |
|
| 131 |
when "The institution" |
|
| 132 |
puts "institution " + d[field] |
|
| 133 |
when "email" |
|
| 134 |
puts "The email " + d[field] |
|
| 135 |
else |
|
| 136 |
@bentry[field] = d[field] |
|
| 137 |
puts field.to_s + " " + d[field] |
|
| 138 |
end |
|
| 102 |
@publication.save |
|
| 103 |
|
|
| 104 |
# need to save all authors |
|
| 105 |
# and establish the author-publication association |
|
| 106 |
# via the authorships table |
|
| 107 |
authors.each_with_index.map do |authorname, idx| |
|
| 108 |
author = Author.new(:name => authorname) |
|
| 109 |
if author.save! |
|
| 110 |
puts "SAVED" |
|
| 111 |
else |
|
| 112 |
puts "NOT SAVED" |
|
| 139 | 113 |
end |
| 140 | 114 |
|
| 141 |
@bentry.save! |
|
| 142 |
end |
|
| 115 |
author.authorships.create!( |
|
| 116 |
:publication => @publication, |
|
| 117 |
:institution => institution, |
|
| 118 |
:email => email, |
|
| 119 |
:order => idx) |
|
| 120 |
|
|
| 121 |
end |
|
| 122 |
end |
|
| 123 |
|
|
| 124 |
|
|
| 125 |
# parses the bibtex file |
|
| 126 |
def parse_bibtex_file |
|
| 127 |
|
|
| 143 | 128 |
end |
| 144 | 129 |
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 130 |
|
|
| 149 | 131 |
end |
Also available in: Unified diff