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 @ 1024:503ed68a4e0f

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