To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / plugins / redmine_bibliography / app / controllers / publications_controller.rb @ 1434:a9cfc6dcfb83
History | View | Annotate | Download (7.59 KB)
| 1 | 613:3b63cea01e7b | luisf | # -*- coding: utf-8 -*-
|
|---|---|---|---|
| 2 | 385:a6f8c0584a92 | luis | # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
|
| 3 | |||
| 4 | 328:aed18b463206 | luis | class PublicationsController < ApplicationController |
| 5 | 425:4ecbc22579e2 | luis | unloadable |
| 6 | 946:a0c9cc95bcf3 | luis | |
| 7 | 560:735388da579a | luis | model_object Publication
|
| 8 | 1317:2805873c0147 | luis | before_filter :find_model_object, :only => [ :show, :add_project ] |
| 9 | 631:324678bbbe77 | luis | before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ] |
| 10 | 946:a0c9cc95bcf3 | luis | |
| 11 | 404:216a61338322 | luis | def new |
| 12 | 539:d4bf55b01092 | luis | find_project_by_project_id |
| 13 | @publication = Publication.new |
||
| 14 | 946:a0c9cc95bcf3 | luis | |
| 15 | 445:77f88379115a | luis | # we'll always want a new publication to have its bibtex entry
|
| 16 | 539:d4bf55b01092 | luis | @publication.build_bibtex_entry
|
| 17 | 946:a0c9cc95bcf3 | luis | |
| 18 | 519:3be6bc3c2a17 | luis | end
|
| 19 | 409:50474139cad4 | luis | |
| 20 | 1071:fe32745aaa3d | luis | def create |
| 21 | 613:3b63cea01e7b | luisf | @project = Project.find(params[:project_id]) |
| 22 | |||
| 23 | 390:5562a95edbf7 | luis | @publication = Publication.new(params[:publication]) |
| 24 | 553:e25c069ab3b8 | luis | @publication.projects << @project unless @project.nil? |
| 25 | 946:a0c9cc95bcf3 | luis | |
| 26 | 1071:fe32745aaa3d | luis | if @publication.save |
| 27 | 666:865d079e5fa0 | luis | @publication.notify_authors_publication_added(@project) |
| 28 | 946:a0c9cc95bcf3 | luis | |
| 29 | 445:77f88379115a | luis | flash[:notice] = "Successfully created publication." |
| 30 | 613:3b63cea01e7b | luisf | redirect_to :action => :show, :id => @publication, :project_id => @project |
| 31 | 445:77f88379115a | luis | else
|
| 32 | 613:3b63cea01e7b | luisf | render :action => 'new', :project_id => @project |
| 33 | 445:77f88379115a | luis | end
|
| 34 | end
|
||
| 35 | |||
| 36 | def index |
||
| 37 | 538:e25248ba597c | luis | if !params[:project_id].nil? |
| 38 | find_project_by_project_id |
||
| 39 | @project = Project.find(params[:project_id]) |
||
| 40 | @publications = Publication.find :all, :joins => :projects, :conditions => ["project_id = ?", @project.id] |
||
| 41 | else
|
||
| 42 | @publications = Publication.find :all |
||
| 43 | end
|
||
| 44 | 445:77f88379115a | luis | end
|
| 45 | |||
| 46 | def new_from_bibfile |
||
| 47 | 391:fecd4b2f4b77 | luis | @publication.current_step = session[:publication_step] |
| 48 | 946:a0c9cc95bcf3 | luis | |
| 49 | 404:216a61338322 | luis | # contents of the paste text area
|
| 50 | bibtex_entry = params[:bibtex_entry]
|
||
| 51 | 384:4be6b16bc6f9 | luis | |
| 52 | 404:216a61338322 | luis | # method for creating "pasted" bibtex entries
|
| 53 | 424:b601a9e472f3 | luis | if bibtex_entry
|
| 54 | 1071:fe32745aaa3d | luis | parse_bibtex_list bibtex_entry |
| 55 | 404:216a61338322 | luis | end
|
| 56 | 329:4575b631f6ce | luis | end
|
| 57 | |||
| 58 | 1274:5ea1a213c7a5 | luis | def show_bibtex_fields |
| 59 | @fields = []
|
||
| 60 | 675:fccacd8505e3 | luis | |
| 61 | 699:2b665b7e67f4 | luis | unless params[:value].empty? |
| 62 | 1274:5ea1a213c7a5 | luis | @fields = BibtexEntryType.fields(params[:value]) |
| 63 | 699:2b665b7e67f4 | luis | end
|
| 64 | 675:fccacd8505e3 | luis | |
| 65 | respond_to do |format|
|
||
| 66 | format.js {
|
||
| 67 | 1274:5ea1a213c7a5 | luis | render :show_bibtex_fields
|
| 68 | 675:fccacd8505e3 | luis | } |
| 69 | end
|
||
| 70 | end
|
||
| 71 | |||
| 72 | 467:c1ecc16cf38e | luis | def add_author |
| 73 | if (request.xhr?)
|
||
| 74 | render :text => User.find(params[:user_id]).name |
||
| 75 | else
|
||
| 76 | # No? Then render an action.
|
||
| 77 | #render :action => 'view_attribute', :attr => @name
|
||
| 78 | 675:fccacd8505e3 | luis | logger.error { "Error while adding Author to publication." }
|
| 79 | 467:c1ecc16cf38e | luis | end
|
| 80 | end
|
||
| 81 | |||
| 82 | 1071:fe32745aaa3d | luis | def edit |
| 83 | 547:56ad0c490f5e | luis | find_project_by_project_id unless params[:project_id].nil? |
| 84 | 946:a0c9cc95bcf3 | luis | |
| 85 | 428:9cfd7a1d848e | luis | @publication = Publication.find(params[:id]) |
| 86 | 626:e2663e0bd5a6 | luis | @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type |
| 87 | 1071:fe32745aaa3d | luis | @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id) |
| 88 | 430:948400933de8 | luis | end
|
| 89 | |||
| 90 | 1071:fe32745aaa3d | luis | def update |
| 91 | @publication = Publication.find(params[:id]) |
||
| 92 | 626:e2663e0bd5a6 | luis | |
| 93 | 430:948400933de8 | luis | if @publication.update_attributes(params[:publication]) |
| 94 | flash[:notice] = "Successfully updated Publication." |
||
| 95 | 538:e25248ba597c | luis | |
| 96 | 1071:fe32745aaa3d | luis | # expires the previosly cached entries
|
| 97 | Rails.cache.delete "publication-#{@publication.id}-ieee" |
||
| 98 | Rails.cache.delete "publication-#{@publication.id}-bibtex" |
||
| 99 | |||
| 100 | 538:e25248ba597c | luis | if !params[:project_id].nil? |
| 101 | redirect_to :action => :show, :id => @publication, :project_id => params[:project_id] |
||
| 102 | else
|
||
| 103 | redirect_to :action => :show, :id => @publication |
||
| 104 | end
|
||
| 105 | 430:948400933de8 | luis | else
|
| 106 | 448:0a5d997578da | luis | render :action => 'edit' |
| 107 | 1071:fe32745aaa3d | luis | end
|
| 108 | 328:aed18b463206 | luis | end
|
| 109 | |||
| 110 | 946:a0c9cc95bcf3 | luis | |
| 111 | 425:4ecbc22579e2 | luis | def show |
| 112 | 535:dd9d9c0ff0f9 | luis | find_project_by_project_id unless params[:project_id].nil? |
| 113 | 946:a0c9cc95bcf3 | luis | |
| 114 | 425:4ecbc22579e2 | luis | if @publication.nil? |
| 115 | 579:2ada25d4b0a8 | luis | @publications = Publication.all |
| 116 | render "index", :alert => 'The publication was not found!' |
||
| 117 | 425:4ecbc22579e2 | luis | else
|
| 118 | @authors = @publication.authors |
||
| 119 | @bibtext_entry = @publication.bibtex_entry |
||
| 120 | end
|
||
| 121 | 329:4575b631f6ce | luis | end
|
| 122 | |||
| 123 | 406:40144aa9dfe7 | luis | # parse string with bibtex authors
|
| 124 | def parse_authors(authors_entry) |
||
| 125 | # in bibtex the authors are always seperated by "and"
|
||
| 126 | 407:96910efbd45e | luis | return authors_entry.split(" and ") |
| 127 | 406:40144aa9dfe7 | luis | end
|
| 128 | |||
| 129 | 1071:fe32745aaa3d | luis | # parses a list of bibtex
|
| 130 | 406:40144aa9dfe7 | luis | def parse_bibtex_list(bibtex_list) |
| 131 | bibliography = BibTeX.parse bibtex_list
|
||
| 132 | |||
| 133 | no_entries = bibliography.data.length |
||
| 134 | |||
| 135 | # parses the bibtex entries
|
||
| 136 | bibliography.data.map do |d|
|
||
| 137 | 407:96910efbd45e | luis | |
| 138 | if d.class == BibTeX::Entry |
||
| 139 | create_bibtex_entry d |
||
| 140 | end
|
||
| 141 | 406:40144aa9dfe7 | luis | end
|
| 142 | 1071:fe32745aaa3d | luis | end
|
| 143 | 407:96910efbd45e | luis | |
| 144 | 1071:fe32745aaa3d | luis | def create_bibtex_entry(d) |
| 145 | 407:96910efbd45e | luis | @publication = Publication.new |
| 146 | 1071:fe32745aaa3d | luis | @bentry = BibtexEntry.new |
| 147 | 407:96910efbd45e | luis | authors = [] |
| 148 | institution = ""
|
||
| 149 | email = ""
|
||
| 150 | 409:50474139cad4 | luis | |
| 151 | 407:96910efbd45e | luis | d.fields.keys.map do |field|
|
| 152 | case field.to_s
|
||
| 153 | when "author" |
||
| 154 | authors = parse_authors d[field] |
||
| 155 | when "title" |
||
| 156 | @publication.title = d[field]
|
||
| 157 | when "institution" |
||
| 158 | institution = d[field] |
||
| 159 | when "email" |
||
| 160 | email = d[field] |
||
| 161 | else
|
||
| 162 | @bentry[field] = d[field]
|
||
| 163 | end
|
||
| 164 | 1071:fe32745aaa3d | luis | end
|
| 165 | 406:40144aa9dfe7 | luis | |
| 166 | @publication.bibtex_entry = @bentry |
||
| 167 | 407:96910efbd45e | luis | @publication.save
|
| 168 | 409:50474139cad4 | luis | |
| 169 | 407:96910efbd45e | luis | # need to save all authors
|
| 170 | 1071:fe32745aaa3d | luis | # and establish the author-publication association
|
| 171 | # via the authorships table
|
||
| 172 | 407:96910efbd45e | luis | authors.each_with_index.map do |authorname, idx|
|
| 173 | author = Author.new(:name => authorname) |
||
| 174 | if author.save!
|
||
| 175 | 946:a0c9cc95bcf3 | luis | # todo: catch the errors...
|
| 176 | 407:96910efbd45e | luis | puts "SAVED"
|
| 177 | else
|
||
| 178 | puts "NOT SAVED"
|
||
| 179 | 406:40144aa9dfe7 | luis | end
|
| 180 | |||
| 181 | 407:96910efbd45e | luis | author.authorships.create!( |
| 182 | 946:a0c9cc95bcf3 | luis | :publication => @publication, |
| 183 | :institution => institution,
|
||
| 184 | :email => email,
|
||
| 185 | :order => idx)
|
||
| 186 | 407:96910efbd45e | luis | end
|
| 187 | end
|
||
| 188 | 409:50474139cad4 | luis | |
| 189 | 461:841b2e40895d | luis | def autocomplete_for_project |
| 190 | @publication = Publication.find(params[:id]) |
||
| 191 | 946:a0c9cc95bcf3 | luis | |
| 192 | 1253:29dd06e01be3 | luis | @projects = Project.active.name_or_homepage_like(params[:q]).find(:all, :limit => 100) - @publication.projects |
| 193 | 461:841b2e40895d | luis | logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
|
| 194 | render :layout => false |
||
| 195 | 409:50474139cad4 | luis | end
|
| 196 | 471:49fb7a9ef79c | luis | |
| 197 | 1071:fe32745aaa3d | luis | def autocomplete_for_author |
| 198 | 519:3be6bc3c2a17 | luis | @results = []
|
| 199 | 946:a0c9cc95bcf3 | luis | |
| 200 | 596:fcff84e1c1ce | luis | object_id = params[:object_id]
|
| 201 | 598:c6cfe1f2eac1 | luis | @object_name = "publications[authorships_attributes][#{object_id}][search_results]" |
| 202 | 946:a0c9cc95bcf3 | luis | |
| 203 | 674:f3e35d639aa4 | Chris | # cc 20110909 -- revert to like instead of like_unique -- see #289
|
| 204 | 1411:6487f22bee9d | luis | authorships_list = Authorship.like(params[:term]).group('author_id').find(:all, :limit => 100) |
| 205 | |||
| 206 | authors_list = authorships_list.collect do |x| x.author end |
||
| 207 | |||
| 208 | 1283:006057cf8f16 | luis | users_list = User.active.like(params[:term]).find(:all, :limit => 100) |
| 209 | 480:19efecf4561a | luis | |
| 210 | 1411:6487f22bee9d | luis | logger.debug "Query for \"#{params[:term]}\" returned \"#{authors_list.size}\" authors and \"#{users_list.size}\" users"
|
| 211 | 946:a0c9cc95bcf3 | luis | |
| 212 | 1411:6487f22bee9d | luis | # will check if any of the members of the users list
|
| 213 | # doesn't belong to the authors list
|
||
| 214 | 601:1608b3cb50cd | luis | |
| 215 | 1411:6487f22bee9d | luis | @results = authors_list
|
| 216 | 946:a0c9cc95bcf3 | luis | |
| 217 | 1411:6487f22bee9d | luis | users_list.each do |user|
|
| 218 | @results << user unless authors_list.include?(user.author) |
||
| 219 | 592:68c6b060385c | luis | end
|
| 220 | |||
| 221 | 1417:1df2db7f0e4d | luis | logger.debug { "Autocomplete_for_author results --> #{@results}" }
|
| 222 | |||
| 223 | 1071:fe32745aaa3d | luis | render :layout => false |
| 224 | 598:c6cfe1f2eac1 | luis | end
|
| 225 | 946:a0c9cc95bcf3 | luis | |
| 226 | 557:2fe129b04d82 | luis | def sort_author_order |
| 227 | params[:authorships].each_with_index do |id, index| |
||
| 228 | Authorship.update_all(['auth_order=?', index+1], ['id=?', id]) |
||
| 229 | 471:49fb7a9ef79c | luis | end
|
| 230 | render :nothing => true |
||
| 231 | end
|
||
| 232 | 574:f463be9d101a | luis | |
| 233 | def add_project |
||
| 234 | 1071:fe32745aaa3d | luis | @projects = Project.find(params[:publication][:project_ids]) |
| 235 | 574:f463be9d101a | luis | @publication.projects << @projects |
| 236 | 1071:fe32745aaa3d | luis | @project = Project.find(params[:project_id]) |
| 237 | 946:a0c9cc95bcf3 | luis | |
| 238 | 1071:fe32745aaa3d | luis | # TODO luisf should also respond to HTML???
|
| 239 | 574:f463be9d101a | luis | respond_to do |format|
|
| 240 | format.html { redirect_to :back }
|
||
| 241 | 1071:fe32745aaa3d | luis | format.js {
|
| 242 | render(:update) {|page|
|
||
| 243 | page[:add_project_form].reset
|
||
| 244 | 574:f463be9d101a | luis | page.replace_html :list_projects, :partial => 'list_projects' |
| 245 | } |
||
| 246 | } |
||
| 247 | end
|
||
| 248 | end
|
||
| 249 | 946:a0c9cc95bcf3 | luis | |
| 250 | 574:f463be9d101a | luis | def remove_project |
| 251 | 579:2ada25d4b0a8 | luis | @project = Project.find(params[:project_id]) |
| 252 | proj = Project.find(params[:remove_project_id]) |
||
| 253 | 554:026c9747fe9b | luis | |
| 254 | 574:f463be9d101a | luis | if @publication.projects.length > 1 |
| 255 | 579:2ada25d4b0a8 | luis | if @publication.projects.exists? proj |
| 256 | @publication.projects.delete proj if request.post? |
||
| 257 | 554:026c9747fe9b | luis | end
|
| 258 | else
|
||
| 259 | 1071:fe32745aaa3d | luis | logger.error { "Cannot remove project from publication list" }
|
| 260 | 554:026c9747fe9b | luis | end
|
| 261 | 946:a0c9cc95bcf3 | luis | |
| 262 | 591:9e866f13c984 | luis | logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
|
| 263 | 946:a0c9cc95bcf3 | luis | |
| 264 | 1071:fe32745aaa3d | luis | render(:update) {|page|
|
| 265 | 579:2ada25d4b0a8 | luis | page.replace_html "list_projects", :partial => 'list_projects', :id => @publication |
| 266 | 1071:fe32745aaa3d | luis | } |
| 267 | 554:026c9747fe9b | luis | end
|
| 268 | 946:a0c9cc95bcf3 | luis | |
| 269 | 560:735388da579a | luis | def destroy |
| 270 | 1406:20235e6c60c0 | luis | find_project_by_project_id unless params[:project_id].nil? |
| 271 | @publication = Publication.find(params[:id]) |
||
| 272 | 1405:43b303a229e1 | luis | |
| 273 | 560:735388da579a | luis | @publication.destroy
|
| 274 | 946:a0c9cc95bcf3 | luis | |
| 275 | 560:735388da579a | luis | flash[:notice] = "Successfully deleted Publication." |
| 276 | redirect_to :controller => :publications, :action => 'index', :project_id => @project |
||
| 277 | end
|
||
| 278 | 471:49fb7a9ef79c | luis | |
| 279 | 538:e25248ba597c | luis | private |
| 280 | 478:7097dc91e58e | luis | |
| 281 | 328:aed18b463206 | luis | end |