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 @ 1440:7465cceb8dfc

History | View | Annotate | Download (7.56 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 1317:2805873c0147 luis
  before_filter :find_model_object, :only => [ :show, :add_project ]
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 519:3be6bc3c2a17 luis
  end
19 409:50474139cad4 luis
20 1071:fe32745aaa3d luis
  def create
21 613:3b63cea01e7b luisf
    @project = Project.find(params[:project_id])
22
23 390:5562a95edbf7 luis
    @publication = Publication.new(params[:publication])
24 553:e25c069ab3b8 luis
    @publication.projects << @project unless @project.nil?
25 946:a0c9cc95bcf3 luis
26 1071:fe32745aaa3d luis
    if @publication.save
27 666:865d079e5fa0 luis
      @publication.notify_authors_publication_added(@project)
28 946:a0c9cc95bcf3 luis
29 445:77f88379115a luis
      flash[:notice] = "Successfully created publication."
30 613:3b63cea01e7b luisf
      redirect_to :action => :show, :id => @publication, :project_id => @project
31 445:77f88379115a luis
    else
32 613:3b63cea01e7b luisf
      render :action => 'new', :project_id => @project
33 445:77f88379115a luis
    end
34
  end
35
36
  def index
37 538:e25248ba597c luis
    if !params[:project_id].nil?
38
      find_project_by_project_id
39
      @project = Project.find(params[:project_id])
40
      @publications = Publication.find :all, :joins => :projects, :conditions => ["project_id = ?", @project.id]
41
    else
42
      @publications = Publication.find :all
43
    end
44 445:77f88379115a luis
  end
45
46
  def new_from_bibfile
47 391:fecd4b2f4b77 luis
    @publication.current_step = session[:publication_step]
48 946:a0c9cc95bcf3 luis
49 404:216a61338322 luis
    # contents of the paste text area
50
    bibtex_entry = params[:bibtex_entry]
51 384:4be6b16bc6f9 luis
52 404:216a61338322 luis
    # method for creating "pasted" bibtex entries
53 424:b601a9e472f3 luis
    if bibtex_entry
54 1071:fe32745aaa3d luis
      parse_bibtex_list bibtex_entry
55 404:216a61338322 luis
    end
56 329:4575b631f6ce luis
  end
57
58 1274:5ea1a213c7a5 luis
  def show_bibtex_fields
59
    @fields = []
60 675:fccacd8505e3 luis
61 699:2b665b7e67f4 luis
    unless params[:value].empty?
62 1274:5ea1a213c7a5 luis
      @fields = BibtexEntryType.fields(params[:value])
63 699:2b665b7e67f4 luis
    end
64 675:fccacd8505e3 luis
65
    respond_to do |format|
66
      format.js {
67 1274:5ea1a213c7a5 luis
        render :show_bibtex_fields
68 675:fccacd8505e3 luis
      }
69
    end
70
  end
71
72 467:c1ecc16cf38e luis
  def add_author
73
    if (request.xhr?)
74
      render :text => User.find(params[:user_id]).name
75
    else
76
      # No?  Then render an action.
77
      #render :action => 'view_attribute', :attr => @name
78 675:fccacd8505e3 luis
      logger.error { "Error while adding Author to publication." }
79 467:c1ecc16cf38e luis
    end
80
  end
81
82 1071:fe32745aaa3d luis
  def edit
83 547:56ad0c490f5e luis
    find_project_by_project_id unless params[:project_id].nil?
84 946:a0c9cc95bcf3 luis
85 428:9cfd7a1d848e luis
    @publication = Publication.find(params[:id])
86 626:e2663e0bd5a6 luis
    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
87 1071:fe32745aaa3d luis
    @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)
88 430:948400933de8 luis
  end
89
90 1071:fe32745aaa3d luis
  def update
91
    @publication = Publication.find(params[:id])
92 626:e2663e0bd5a6 luis
93 430:948400933de8 luis
    if @publication.update_attributes(params[:publication])
94
      flash[:notice] = "Successfully updated Publication."
95 538:e25248ba597c luis
96 1071:fe32745aaa3d luis
      # expires the previosly cached entries
97
      Rails.cache.delete "publication-#{@publication.id}-ieee"
98
      Rails.cache.delete "publication-#{@publication.id}-bibtex"
99
100 538:e25248ba597c luis
      if !params[:project_id].nil?
101
        redirect_to :action => :show, :id => @publication, :project_id => params[:project_id]
102
      else
103
        redirect_to :action => :show, :id => @publication
104
      end
105 430:948400933de8 luis
    else
106 448:0a5d997578da luis
      render :action => 'edit'
107 1071:fe32745aaa3d luis
    end
108 328:aed18b463206 luis
  end
109
110 946:a0c9cc95bcf3 luis
111 425:4ecbc22579e2 luis
  def show
112 535:dd9d9c0ff0f9 luis
    find_project_by_project_id unless params[:project_id].nil?
113 946:a0c9cc95bcf3 luis
114 425:4ecbc22579e2 luis
    if @publication.nil?
115 579:2ada25d4b0a8 luis
      @publications = Publication.all
116
      render "index", :alert => 'The publication was not found!'
117 425:4ecbc22579e2 luis
    else
118
      @authors = @publication.authors
119
      @bibtext_entry = @publication.bibtex_entry
120
    end
121 329:4575b631f6ce luis
  end
122
123 406:40144aa9dfe7 luis
  # parse string with bibtex authors
124
  def parse_authors(authors_entry)
125
    # in bibtex the authors are always seperated by "and"
126 407:96910efbd45e luis
    return authors_entry.split(" and ")
127 406:40144aa9dfe7 luis
  end
128
129 1071:fe32745aaa3d luis
  # parses a list of bibtex
130 406:40144aa9dfe7 luis
  def parse_bibtex_list(bibtex_list)
131
    bibliography = BibTeX.parse bibtex_list
132
133
    no_entries = bibliography.data.length
134
135
    # parses the bibtex entries
136
    bibliography.data.map do |d|
137 407:96910efbd45e luis
138
      if d.class == BibTeX::Entry
139
        create_bibtex_entry d
140
      end
141 406:40144aa9dfe7 luis
    end
142 1071:fe32745aaa3d luis
  end
143 407:96910efbd45e luis
144 1071:fe32745aaa3d luis
  def create_bibtex_entry(d)
145 407:96910efbd45e luis
    @publication = Publication.new
146 1071:fe32745aaa3d luis
    @bentry = BibtexEntry.new
147 407:96910efbd45e luis
    authors = []
148
    institution = ""
149
    email = ""
150 409:50474139cad4 luis
151 407:96910efbd45e luis
    d.fields.keys.map do |field|
152
      case field.to_s
153
      when "author"
154
        authors = parse_authors d[field]
155
      when "title"
156
        @publication.title = d[field]
157
      when "institution"
158
        institution = d[field]
159
      when "email"
160
        email = d[field]
161
      else
162
        @bentry[field] = d[field]
163
      end
164 1071:fe32745aaa3d luis
    end
165 406:40144aa9dfe7 luis
166
    @publication.bibtex_entry = @bentry
167 407:96910efbd45e luis
    @publication.save
168 409:50474139cad4 luis
169 407:96910efbd45e luis
    # need to save all authors
170 1071:fe32745aaa3d luis
    #   and establish the author-publication association
171
    #   via the authorships table
172 407:96910efbd45e luis
    authors.each_with_index.map do |authorname, idx|
173
      author = Author.new(:name => authorname)
174
      if author.save!
175 946:a0c9cc95bcf3 luis
        # todo: catch the errors...
176 407:96910efbd45e luis
        puts "SAVED"
177
      else
178
        puts "NOT SAVED"
179 406:40144aa9dfe7 luis
      end
180
181 407:96910efbd45e luis
      author.authorships.create!(
182 946:a0c9cc95bcf3 luis
      :publication => @publication,
183
      :institution => institution,
184
      :email => email,
185
      :order => idx)
186 407:96910efbd45e luis
    end
187
  end
188 409:50474139cad4 luis
189 461:841b2e40895d luis
  def autocomplete_for_project
190
    @publication = Publication.find(params[:id])
191 946:a0c9cc95bcf3 luis
192 1253:29dd06e01be3 luis
    @projects = Project.active.name_or_homepage_like(params[:q]).find(:all, :limit => 100) - @publication.projects
193 461:841b2e40895d luis
    logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
194
    render :layout => false
195 409:50474139cad4 luis
  end
196 471:49fb7a9ef79c luis
197 1071:fe32745aaa3d luis
  def autocomplete_for_author
198 519:3be6bc3c2a17 luis
    @results = []
199 946:a0c9cc95bcf3 luis
200 596:fcff84e1c1ce luis
    object_id = params[:object_id]
201 598:c6cfe1f2eac1 luis
    @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
202 946:a0c9cc95bcf3 luis
203 1436:ee598c21c239 luis
    # todo: make sure query works with both pgres and mysql ~lf.20131010
204
    authors_list = Author.joins(:authorships).where("LOWER(authorships.name_on_paper) LIKE LOWER(?)", "%#{params[:term]}%").uniq
205 1411:6487f22bee9d luis
206 1283:006057cf8f16 luis
    users_list = User.active.like(params[:term]).find(:all, :limit => 100)
207 480:19efecf4561a luis
208 1411:6487f22bee9d luis
    logger.debug "Query for \"#{params[:term]}\" returned \"#{authors_list.size}\" authors and \"#{users_list.size}\" users"
209 946:a0c9cc95bcf3 luis
210 1411:6487f22bee9d luis
    # will check if any of the members of the users list
211
    #  doesn't belong to the authors list
212 601:1608b3cb50cd luis
213 1411:6487f22bee9d luis
    @results = authors_list
214 946:a0c9cc95bcf3 luis
215 1411:6487f22bee9d luis
    users_list.each do |user|
216
      @results << user unless authors_list.include?(user.author)
217 592:68c6b060385c luis
    end
218
219 1417:1df2db7f0e4d luis
    logger.debug { "Autocomplete_for_author results --> #{@results}" }
220
221 1071:fe32745aaa3d luis
    render :layout => false
222 598:c6cfe1f2eac1 luis
  end
223 946:a0c9cc95bcf3 luis
224 557:2fe129b04d82 luis
  def sort_author_order
225
    params[:authorships].each_with_index do |id, index|
226
      Authorship.update_all(['auth_order=?', index+1], ['id=?', id])
227 471:49fb7a9ef79c luis
    end
228
    render :nothing => true
229
  end
230 574:f463be9d101a luis
231
  def add_project
232 1071:fe32745aaa3d luis
    @projects = Project.find(params[:publication][:project_ids])
233 574:f463be9d101a luis
    @publication.projects << @projects
234 1071:fe32745aaa3d luis
    @project = Project.find(params[:project_id])
235 946:a0c9cc95bcf3 luis
236 1071:fe32745aaa3d luis
    # TODO luisf should also respond to HTML???
237 574:f463be9d101a luis
    respond_to do |format|
238
      format.html { redirect_to :back }
239 1071:fe32745aaa3d luis
      format.js {
240
        render(:update) {|page|
241
          page[:add_project_form].reset
242 574:f463be9d101a luis
          page.replace_html :list_projects, :partial => 'list_projects'
243
        }
244
      }
245
    end
246
  end
247 946:a0c9cc95bcf3 luis
248 574:f463be9d101a luis
  def remove_project
249 579:2ada25d4b0a8 luis
    @project = Project.find(params[:project_id])
250
    proj = Project.find(params[:remove_project_id])
251 554:026c9747fe9b luis
252 574:f463be9d101a luis
    if @publication.projects.length > 1
253 579:2ada25d4b0a8 luis
      if @publication.projects.exists? proj
254
        @publication.projects.delete proj if request.post?
255 554:026c9747fe9b luis
      end
256
    else
257 1071:fe32745aaa3d luis
      logger.error { "Cannot remove project from publication list" }
258 554:026c9747fe9b luis
    end
259 946:a0c9cc95bcf3 luis
260 591:9e866f13c984 luis
    logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
261 946:a0c9cc95bcf3 luis
262 1071:fe32745aaa3d luis
    render(:update) {|page|
263 579:2ada25d4b0a8 luis
      page.replace_html "list_projects", :partial => 'list_projects', :id  => @publication
264 1071:fe32745aaa3d luis
    }
265 554:026c9747fe9b luis
  end
266 946:a0c9cc95bcf3 luis
267 560:735388da579a luis
  def destroy
268 1406:20235e6c60c0 luis
    find_project_by_project_id unless params[:project_id].nil?
269
    @publication = Publication.find(params[:id])
270 1405:43b303a229e1 luis
271 560:735388da579a luis
    @publication.destroy
272 946:a0c9cc95bcf3 luis
273 560:735388da579a luis
    flash[:notice] = "Successfully deleted Publication."
274
    redirect_to :controller => :publications, :action => 'index', :project_id => @project
275
  end
276 471:49fb7a9ef79c luis
277 538:e25248ba597c luis
  private
278 478:7097dc91e58e luis
279 328:aed18b463206 luis
end