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 @ 1052:6674e52e20bf

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