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 @ 1081:b56a4c5afa35

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