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 / plugins / redmine_bibliography / app / controllers / publications_controller.rb @ 1283:006057cf8f16

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