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 @ 1051:ef882e222003

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