To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / redmine_bibliography / app / controllers / publications_controller.rb @ 951:010291c90b0b
History | View | Annotate | Download (10.8 KB)
| 1 | 613:3b63cea01e7b | luisf | # -*- coding: utf-8 -*-
|
|---|---|---|---|
| 2 | 385:a6f8c0584a92 | luis | # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
|
| 3 | |||
| 4 | 947:be4106d14a35 | luis | class BibtexParsingError < Exception; end |
| 5 | |||
| 6 | 328:aed18b463206 | luis | class PublicationsController < ApplicationController |
| 7 | 425:4ecbc22579e2 | luis | unloadable |
| 8 | 946:a0c9cc95bcf3 | luis | |
| 9 | 560:735388da579a | luis | model_object Publication
|
| 10 | 947:be4106d14a35 | luis | before_filter :find_model_object, :except => [:parse_bibtex, :new, :create, :index, :get_bibtex_required_fields, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author, :get_user_info ] |
| 11 | 631:324678bbbe77 | luis | before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ] |
| 12 | 946:a0c9cc95bcf3 | luis | |
| 13 | 404:216a61338322 | luis | def new |
| 14 | 539:d4bf55b01092 | luis | find_project_by_project_id |
| 15 | @publication = Publication.new |
||
| 16 | 946:a0c9cc95bcf3 | luis | |
| 17 | 445:77f88379115a | luis | # we'll always want a new publication to have its bibtex entry
|
| 18 | 539:d4bf55b01092 | luis | @publication.build_bibtex_entry
|
| 19 | 946:a0c9cc95bcf3 | luis | |
| 20 | 446:995d4c99843d | luis | # and at least one author
|
| 21 | 608:c7559c3a8d03 | luis | # @publication.authorships.build.build_author
|
| 22 | 647:525f48af3f54 | chris | @author_options = [["#{User.current.name} (@#{User.current.mail.partition('@')[2]})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]] |
| 23 | 947:be4106d14a35 | luis | end
|
| 24 | 614:712324fee0c0 | luis | |
| 25 | |||
| 26 | 947:be4106d14a35 | luis | def parse_bibtex |
| 27 | find_project_by_project_id |
||
| 28 | |||
| 29 | @bibtex_paste = params[:bibtex_paste] |
||
| 30 | |||
| 31 | begin
|
||
| 32 | bib = BibTeX.parse(@bibtex_paste) |
||
| 33 | |||
| 34 | if bib.errors.present? or bib[0].class == NilClass |
||
| 35 | raise BibtexParsingError, "Bibtex Parsing Error" |
||
| 36 | end
|
||
| 37 | |||
| 38 | respond_to do |format|
|
||
| 39 | format.js {
|
||
| 40 | render(:update) {|page|
|
||
| 41 | flash[:notice] = "Correctly parsed BibTeX entry" |
||
| 42 | |||
| 43 | bibtex_entry_no = BibtexEntryType.find_by_name(bib[0].type.to_s).id |
||
| 44 | page["publication_title"].value = bib[0][:title] |
||
| 45 | page["publication_bibtex_entry_attributes_entry_type"].value = bibtex_entry_no
|
||
| 46 | 951:010291c90b0b | luis | |
| 47 | 947:be4106d14a35 | luis | BibtexEntryType.fields(bibtex_entry_no).each do |field| |
| 48 | page["publication_bibtex_entry_attributes_#{field}"].value = bib[0][field] |
||
| 49 | end
|
||
| 50 | 951:010291c90b0b | luis | |
| 51 | # for each author simulates a click and fills the author info
|
||
| 52 | bib[0].authors.each do |author| |
||
| 53 | page["add_another_author"].click
|
||
| 54 | # page.alert(bib[0].authors.length)
|
||
| 55 | # page.alert(page["authors"].first.id)
|
||
| 56 | |||
| 57 | end
|
||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | 947:be4106d14a35 | luis | } |
| 62 | } |
||
| 63 | end
|
||
| 64 | |||
| 65 | rescue BibtexParsingError => e |
||
| 66 | logger.error { "Bibtex Parsing Error #{bib.errors}" }
|
||
| 67 | |||
| 68 | # todo: not showing... should be inside render?
|
||
| 69 | flash[:error] = e.message
|
||
| 70 | |||
| 71 | respond_to do |format|
|
||
| 72 | format.js{
|
||
| 73 | render(:update) {|page|
|
||
| 74 | } |
||
| 75 | } |
||
| 76 | end
|
||
| 77 | |||
| 78 | end
|
||
| 79 | |||
| 80 | 519:3be6bc3c2a17 | luis | end
|
| 81 | 409:50474139cad4 | luis | |
| 82 | 947:be4106d14a35 | luis | |
| 83 | def create |
||
| 84 | 613:3b63cea01e7b | luisf | @project = Project.find(params[:project_id]) |
| 85 | |||
| 86 | 661:6246ad0f9e98 | chris | @author_options = []
|
| 87 | 614:712324fee0c0 | luis | |
| 88 | 390:5562a95edbf7 | luis | @publication = Publication.new(params[:publication]) |
| 89 | 553:e25c069ab3b8 | luis | @publication.projects << @project unless @project.nil? |
| 90 | 946:a0c9cc95bcf3 | luis | |
| 91 | 448:0a5d997578da | luis | if @publication.save |
| 92 | 666:865d079e5fa0 | luis | @publication.notify_authors_publication_added(@project) |
| 93 | 946:a0c9cc95bcf3 | luis | |
| 94 | 445:77f88379115a | luis | flash[:notice] = "Successfully created publication." |
| 95 | 613:3b63cea01e7b | luisf | redirect_to :action => :show, :id => @publication, :project_id => @project |
| 96 | 445:77f88379115a | luis | else
|
| 97 | 613:3b63cea01e7b | luisf | render :action => 'new', :project_id => @project |
| 98 | 445:77f88379115a | luis | end
|
| 99 | end
|
||
| 100 | |||
| 101 | def index |
||
| 102 | 538:e25248ba597c | luis | if !params[:project_id].nil? |
| 103 | find_project_by_project_id |
||
| 104 | @project = Project.find(params[:project_id]) |
||
| 105 | @publications = Publication.find :all, :joins => :projects, :conditions => ["project_id = ?", @project.id] |
||
| 106 | else
|
||
| 107 | @publications = Publication.find :all |
||
| 108 | end
|
||
| 109 | 445:77f88379115a | luis | end
|
| 110 | |||
| 111 | def new_from_bibfile |
||
| 112 | 391:fecd4b2f4b77 | luis | @publication.current_step = session[:publication_step] |
| 113 | 946:a0c9cc95bcf3 | luis | |
| 114 | 404:216a61338322 | luis | # contents of the paste text area
|
| 115 | bibtex_entry = params[:bibtex_entry]
|
||
| 116 | 384:4be6b16bc6f9 | luis | |
| 117 | 404:216a61338322 | luis | # method for creating "pasted" bibtex entries
|
| 118 | 424:b601a9e472f3 | luis | if bibtex_entry
|
| 119 | 409:50474139cad4 | luis | parse_bibtex_list bibtex_entry |
| 120 | 404:216a61338322 | luis | end
|
| 121 | 329:4575b631f6ce | luis | end
|
| 122 | |||
| 123 | 675:fccacd8505e3 | luis | def get_bibtex_required_fields |
| 124 | |||
| 125 | 947:be4106d14a35 | luis | fields = BibtexEntryType.fields(params[:q]) |
| 126 | 675:fccacd8505e3 | luis | |
| 127 | respond_to do |format|
|
||
| 128 | format.js {
|
||
| 129 | 699:2b665b7e67f4 | luis | render(:update) {|page|
|
| 130 | 947:be4106d14a35 | luis | if params[:q].empty? |
| 131 | 699:2b665b7e67f4 | luis | page << "hideOnLoad();"
|
| 132 | else
|
||
| 133 | page << "show_required_bibtex_fields(#{fields.to_json()});"
|
||
| 134 | 675:fccacd8505e3 | luis | end
|
| 135 | } |
||
| 136 | } |
||
| 137 | 946:a0c9cc95bcf3 | luis | |
| 138 | 675:fccacd8505e3 | luis | end
|
| 139 | end
|
||
| 140 | |||
| 141 | 467:c1ecc16cf38e | luis | def add_author |
| 142 | if (request.xhr?)
|
||
| 143 | render :text => User.find(params[:user_id]).name |
||
| 144 | else
|
||
| 145 | # No? Then render an action.
|
||
| 146 | #render :action => 'view_attribute', :attr => @name
|
||
| 147 | 675:fccacd8505e3 | luis | logger.error { "Error while adding Author to publication." }
|
| 148 | 467:c1ecc16cf38e | luis | end
|
| 149 | end
|
||
| 150 | |||
| 151 | 547:56ad0c490f5e | luis | def edit |
| 152 | find_project_by_project_id unless params[:project_id].nil? |
||
| 153 | 946:a0c9cc95bcf3 | luis | |
| 154 | 609:3221b2ab7804 | luis | @edit_view = true; |
| 155 | 428:9cfd7a1d848e | luis | @publication = Publication.find(params[:id]) |
| 156 | 626:e2663e0bd5a6 | luis | @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type |
| 157 | |||
| 158 | 647:525f48af3f54 | chris | @author_options = []
|
| 159 | 946:a0c9cc95bcf3 | luis | |
| 160 | 685:4481db876cdb | luis | @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id) |
| 161 | 430:948400933de8 | luis | end
|
| 162 | |||
| 163 | 445:77f88379115a | luis | def update |
| 164 | 448:0a5d997578da | luis | @publication = Publication.find(params[:id]) |
| 165 | 538:e25248ba597c | luis | |
| 166 | 661:6246ad0f9e98 | chris | @author_options = []
|
| 167 | 626:e2663e0bd5a6 | luis | |
| 168 | 430:948400933de8 | luis | if @publication.update_attributes(params[:publication]) |
| 169 | 947:be4106d14a35 | luis | flash[:notice] = "Successfully Updated Publication." |
| 170 | 538:e25248ba597c | luis | |
| 171 | if !params[:project_id].nil? |
||
| 172 | redirect_to :action => :show, :id => @publication, :project_id => params[:project_id] |
||
| 173 | else
|
||
| 174 | redirect_to :action => :show, :id => @publication |
||
| 175 | end
|
||
| 176 | 430:948400933de8 | luis | else
|
| 177 | 448:0a5d997578da | luis | render :action => 'edit' |
| 178 | end
|
||
| 179 | 328:aed18b463206 | luis | end
|
| 180 | |||
| 181 | 946:a0c9cc95bcf3 | luis | |
| 182 | 425:4ecbc22579e2 | luis | def show |
| 183 | 535:dd9d9c0ff0f9 | luis | find_project_by_project_id unless params[:project_id].nil? |
| 184 | 946:a0c9cc95bcf3 | luis | |
| 185 | 425:4ecbc22579e2 | luis | if @publication.nil? |
| 186 | 579:2ada25d4b0a8 | luis | @publications = Publication.all |
| 187 | render "index", :alert => 'The publication was not found!' |
||
| 188 | 425:4ecbc22579e2 | luis | else
|
| 189 | @authors = @publication.authors |
||
| 190 | @bibtext_entry = @publication.bibtex_entry |
||
| 191 | end
|
||
| 192 | 329:4575b631f6ce | luis | end
|
| 193 | |||
| 194 | 406:40144aa9dfe7 | luis | # parse string with bibtex authors
|
| 195 | def parse_authors(authors_entry) |
||
| 196 | # in bibtex the authors are always seperated by "and"
|
||
| 197 | 407:96910efbd45e | luis | return authors_entry.split(" and ") |
| 198 | 406:40144aa9dfe7 | luis | end
|
| 199 | |||
| 200 | # parses a list of bibtex
|
||
| 201 | def parse_bibtex_list(bibtex_list) |
||
| 202 | bibliography = BibTeX.parse bibtex_list
|
||
| 203 | |||
| 204 | no_entries = bibliography.data.length |
||
| 205 | |||
| 206 | # parses the bibtex entries
|
||
| 207 | bibliography.data.map do |d|
|
||
| 208 | 407:96910efbd45e | luis | |
| 209 | if d.class == BibTeX::Entry |
||
| 210 | create_bibtex_entry d |
||
| 211 | end
|
||
| 212 | 406:40144aa9dfe7 | luis | end
|
| 213 | 407:96910efbd45e | luis | end
|
| 214 | |||
| 215 | 409:50474139cad4 | luis | def create_bibtex_entry(d) |
| 216 | 407:96910efbd45e | luis | @publication = Publication.new |
| 217 | @bentry = BibtexEntry.new |
||
| 218 | authors = [] |
||
| 219 | institution = ""
|
||
| 220 | email = ""
|
||
| 221 | 409:50474139cad4 | luis | |
| 222 | 407:96910efbd45e | luis | d.fields.keys.map do |field|
|
| 223 | case field.to_s
|
||
| 224 | when "author" |
||
| 225 | authors = parse_authors d[field] |
||
| 226 | when "title" |
||
| 227 | @publication.title = d[field]
|
||
| 228 | when "institution" |
||
| 229 | institution = d[field] |
||
| 230 | when "email" |
||
| 231 | email = d[field] |
||
| 232 | else
|
||
| 233 | @bentry[field] = d[field]
|
||
| 234 | end
|
||
| 235 | end
|
||
| 236 | 406:40144aa9dfe7 | luis | |
| 237 | @publication.bibtex_entry = @bentry |
||
| 238 | 407:96910efbd45e | luis | @publication.save
|
| 239 | 409:50474139cad4 | luis | |
| 240 | 424:b601a9e472f3 | luis | # what is this for???
|
| 241 | # @created_publications << @publication.id
|
||
| 242 | 409:50474139cad4 | luis | |
| 243 | 407:96910efbd45e | luis | # need to save all authors
|
| 244 | # and establish the author-publication association
|
||
| 245 | # via the authorships table
|
||
| 246 | authors.each_with_index.map do |authorname, idx|
|
||
| 247 | author = Author.new(:name => authorname) |
||
| 248 | if author.save!
|
||
| 249 | 946:a0c9cc95bcf3 | luis | # todo: catch the errors...
|
| 250 | 407:96910efbd45e | luis | puts "SAVED"
|
| 251 | else
|
||
| 252 | puts "NOT SAVED"
|
||
| 253 | 406:40144aa9dfe7 | luis | end
|
| 254 | |||
| 255 | 407:96910efbd45e | luis | author.authorships.create!( |
| 256 | 946:a0c9cc95bcf3 | luis | :publication => @publication, |
| 257 | :institution => institution,
|
||
| 258 | :email => email,
|
||
| 259 | :order => idx)
|
||
| 260 | 407:96910efbd45e | luis | end
|
| 261 | end
|
||
| 262 | 409:50474139cad4 | luis | |
| 263 | 407:96910efbd45e | luis | # parses the bibtex file
|
| 264 | def parse_bibtex_file |
||
| 265 | |||
| 266 | 406:40144aa9dfe7 | luis | end
|
| 267 | |||
| 268 | 444:b461f84ed41a | luis | def import |
| 269 | @publication = Publication.new |
||
| 270 | 946:a0c9cc95bcf3 | luis | |
| 271 | |||
| 272 | 444:b461f84ed41a | luis | end
|
| 273 | 946:a0c9cc95bcf3 | luis | |
| 274 | 461:841b2e40895d | luis | def autocomplete_for_project |
| 275 | @publication = Publication.find(params[:id]) |
||
| 276 | 946:a0c9cc95bcf3 | luis | |
| 277 | 464:fbdfec975bfa | luis | @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects |
| 278 | 461:841b2e40895d | luis | logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
|
| 279 | render :layout => false |
||
| 280 | 409:50474139cad4 | luis | end
|
| 281 | 947:be4106d14a35 | luis | |
| 282 | 471:49fb7a9ef79c | luis | |
| 283 | 601:1608b3cb50cd | luis | def autocomplete_for_author |
| 284 | 519:3be6bc3c2a17 | luis | @results = []
|
| 285 | 946:a0c9cc95bcf3 | luis | |
| 286 | 596:fcff84e1c1ce | luis | object_id = params[:object_id]
|
| 287 | 598:c6cfe1f2eac1 | luis | @object_name = "publications[authorships_attributes][#{object_id}][search_results]" |
| 288 | 946:a0c9cc95bcf3 | luis | |
| 289 | 674:f3e35d639aa4 | Chris | # cc 20110909 -- revert to like instead of like_unique -- see #289
|
| 290 | authorships_list = Authorship.like(params[:q]).find(:all, :limit => 100) |
||
| 291 | 480:19efecf4561a | luis | users_list = User.active.like(params[:q]).find(:all, :limit => 100) |
| 292 | |||
| 293 | 591:9e866f13c984 | luis | logger.debug "Query for \"#{params[:q]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
|
| 294 | 946:a0c9cc95bcf3 | luis | |
| 295 | 601:1608b3cb50cd | luis | @results = users_list
|
| 296 | |||
| 297 | # TODO: can be optimized…
|
||
| 298 | authorships_list.each do |authorship|
|
||
| 299 | flag = true
|
||
| 300 | 946:a0c9cc95bcf3 | luis | |
| 301 | 601:1608b3cb50cd | luis | users_list.each do |user|
|
| 302 | if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
|
||
| 303 | Rails.logger.debug { "Rejecting Authorship #{authorship.id}" } |
||
| 304 | flag = false
|
||
| 305 | break
|
||
| 306 | end
|
||
| 307 | end
|
||
| 308 | |||
| 309 | @results << authorship if flag |
||
| 310 | 592:68c6b060385c | luis | end
|
| 311 | |||
| 312 | 598:c6cfe1f2eac1 | luis | render :layout => false |
| 313 | end
|
||
| 314 | 946:a0c9cc95bcf3 | luis | |
| 315 | 598:c6cfe1f2eac1 | luis | def get_user_info |
| 316 | object_id = params[:object_id]
|
||
| 317 | value = params[:value]
|
||
| 318 | classname = Kernel.const_get(value.split('_')[0]) |
||
| 319 | 592:68c6b060385c | luis | |
| 320 | 598:c6cfe1f2eac1 | luis | item = classname.find(value.split('_')[1]) |
| 321 | |||
| 322 | name_field = "publication_authorships_attributes_#{object_id}_name_on_paper".to_sym
|
||
| 323 | email_field = "publication_authorships_attributes_#{object_id}_email".to_sym
|
||
| 324 | 600:c3c1091639ad | luis | institution_field = "publication_authorships_attributes_#{object_id}_institution".to_sym
|
| 325 | 946:a0c9cc95bcf3 | luis | |
| 326 | 600:c3c1091639ad | luis | yes_radio = "publication_authorships_attributes_#{object_id}_identify_author_yes".to_sym
|
| 327 | 946:a0c9cc95bcf3 | luis | |
| 328 | 598:c6cfe1f2eac1 | luis | respond_to do |format|
|
| 329 | 675:fccacd8505e3 | luis | format.js {
|
| 330 | 598:c6cfe1f2eac1 | luis | render(:update) {|page|
|
| 331 | page[name_field].value = item.name |
||
| 332 | page[email_field].value = item.mail |
||
| 333 | 600:c3c1091639ad | luis | page[institution_field].value = item.institution |
| 334 | |||
| 335 | page[yes_radio].checked = true
|
||
| 336 | 601:1608b3cb50cd | luis | page[name_field].readOnly = true
|
| 337 | page[email_field].readOnly = true
|
||
| 338 | page[institution_field].readOnly = true
|
||
| 339 | 598:c6cfe1f2eac1 | luis | } |
| 340 | } |
||
| 341 | end
|
||
| 342 | 477:aeedcec4df5f | luis | end
|
| 343 | 471:49fb7a9ef79c | luis | |
| 344 | 557:2fe129b04d82 | luis | def sort_author_order |
| 345 | params[:authorships].each_with_index do |id, index| |
||
| 346 | Authorship.update_all(['auth_order=?', index+1], ['id=?', id]) |
||
| 347 | 471:49fb7a9ef79c | luis | end
|
| 348 | render :nothing => true |
||
| 349 | end
|
||
| 350 | 574:f463be9d101a | luis | |
| 351 | def add_project |
||
| 352 | @projects = Project.find(params[:publication][:project_ids]) |
||
| 353 | @publication.projects << @projects |
||
| 354 | 579:2ada25d4b0a8 | luis | @project = Project.find(params[:project_id]) |
| 355 | 946:a0c9cc95bcf3 | luis | |
| 356 | 574:f463be9d101a | luis | # TODO luisf should also respond to HTML???
|
| 357 | respond_to do |format|
|
||
| 358 | format.html { redirect_to :back }
|
||
| 359 | format.js {
|
||
| 360 | render(:update) {|page|
|
||
| 361 | page[:add_project_form].reset
|
||
| 362 | page.replace_html :list_projects, :partial => 'list_projects' |
||
| 363 | } |
||
| 364 | } |
||
| 365 | end
|
||
| 366 | end
|
||
| 367 | 946:a0c9cc95bcf3 | luis | |
| 368 | |||
| 369 | 574:f463be9d101a | luis | def remove_project |
| 370 | 579:2ada25d4b0a8 | luis | @project = Project.find(params[:project_id]) |
| 371 | proj = Project.find(params[:remove_project_id]) |
||
| 372 | 554:026c9747fe9b | luis | |
| 373 | 574:f463be9d101a | luis | if @publication.projects.length > 1 |
| 374 | 579:2ada25d4b0a8 | luis | if @publication.projects.exists? proj |
| 375 | @publication.projects.delete proj if request.post? |
||
| 376 | 554:026c9747fe9b | luis | end
|
| 377 | else
|
||
| 378 | 579:2ada25d4b0a8 | luis | logger.error { "Cannot remove project from publication list" }
|
| 379 | 554:026c9747fe9b | luis | end
|
| 380 | 946:a0c9cc95bcf3 | luis | |
| 381 | 591:9e866f13c984 | luis | logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
|
| 382 | 946:a0c9cc95bcf3 | luis | |
| 383 | 579:2ada25d4b0a8 | luis | render(:update) {|page|
|
| 384 | page.replace_html "list_projects", :partial => 'list_projects', :id => @publication |
||
| 385 | } |
||
| 386 | 554:026c9747fe9b | luis | end
|
| 387 | 946:a0c9cc95bcf3 | luis | |
| 388 | 560:735388da579a | luis | def destroy |
| 389 | find_project_by_project_id |
||
| 390 | 946:a0c9cc95bcf3 | luis | |
| 391 | 560:735388da579a | luis | @publication.destroy
|
| 392 | 946:a0c9cc95bcf3 | luis | |
| 393 | 560:735388da579a | luis | flash[:notice] = "Successfully deleted Publication." |
| 394 | redirect_to :controller => :publications, :action => 'index', :project_id => @project |
||
| 395 | end
|
||
| 396 | 471:49fb7a9ef79c | luis | |
| 397 | 538:e25248ba597c | luis | private |
| 398 | 478:7097dc91e58e | luis | |
| 399 | 328:aed18b463206 | luis | end |