To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / vendor / plugins / redmine_bibliography / app / controllers / publications_controller.rb @ 1053:80cf86be1611

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