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 @ 1406:20235e6c60c0

History | View | Annotate | Download (8.05 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 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 1274:5ea1a213c7a5 luis
  def show_bibtex_fields
64
    @fields = []
65 675:fccacd8505e3 luis
66 699:2b665b7e67f4 luis
    unless params[:value].empty?
67 1274:5ea1a213c7a5 luis
      @fields = BibtexEntryType.fields(params[:value])
68 699:2b665b7e67f4 luis
    end
69 675:fccacd8505e3 luis
70
    respond_to do |format|
71
      format.js {
72 1274:5ea1a213c7a5 luis
        render :show_bibtex_fields
73 675:fccacd8505e3 luis
      }
74
    end
75
  end
76
77 467:c1ecc16cf38e luis
  def add_author
78
    if (request.xhr?)
79
      render :text => User.find(params[:user_id]).name
80
    else
81
      # No?  Then render an action.
82
      #render :action => 'view_attribute', :attr => @name
83 675:fccacd8505e3 luis
      logger.error { "Error while adding Author to publication." }
84 467:c1ecc16cf38e luis
    end
85
  end
86
87 1071:fe32745aaa3d luis
  def edit
88 547:56ad0c490f5e luis
    find_project_by_project_id unless params[:project_id].nil?
89 946:a0c9cc95bcf3 luis
90 609:3221b2ab7804 luis
    @edit_view = true;
91 428:9cfd7a1d848e luis
    @publication = Publication.find(params[:id])
92 626:e2663e0bd5a6 luis
    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
93
94 1071:fe32745aaa3d luis
    @author_options = []
95 946:a0c9cc95bcf3 luis
96 1071:fe32745aaa3d luis
    @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)
97 430:948400933de8 luis
  end
98
99 1071:fe32745aaa3d luis
  def update
100
    @publication = Publication.find(params[:id])
101 661:6246ad0f9e98 chris
    @author_options = []
102 626:e2663e0bd5a6 luis
103 430:948400933de8 luis
    if @publication.update_attributes(params[:publication])
104
      flash[:notice] = "Successfully updated Publication."
105 538:e25248ba597c luis
106 1071:fe32745aaa3d luis
      # expires the previosly cached entries
107
      Rails.cache.delete "publication-#{@publication.id}-ieee"
108
      Rails.cache.delete "publication-#{@publication.id}-bibtex"
109
110 538:e25248ba597c luis
      if !params[:project_id].nil?
111
        redirect_to :action => :show, :id => @publication, :project_id => params[:project_id]
112
      else
113
        redirect_to :action => :show, :id => @publication
114
      end
115 430:948400933de8 luis
    else
116 448:0a5d997578da luis
      render :action => 'edit'
117 1071:fe32745aaa3d luis
    end
118 328:aed18b463206 luis
  end
119
120 946:a0c9cc95bcf3 luis
121 425:4ecbc22579e2 luis
  def show
122 535:dd9d9c0ff0f9 luis
    find_project_by_project_id unless params[:project_id].nil?
123 946:a0c9cc95bcf3 luis
124 425:4ecbc22579e2 luis
    if @publication.nil?
125 579:2ada25d4b0a8 luis
      @publications = Publication.all
126
      render "index", :alert => 'The publication was not found!'
127 425:4ecbc22579e2 luis
    else
128
      @authors = @publication.authors
129
      @bibtext_entry = @publication.bibtex_entry
130
    end
131 329:4575b631f6ce luis
  end
132
133 406:40144aa9dfe7 luis
  # parse string with bibtex authors
134
  def parse_authors(authors_entry)
135
    # in bibtex the authors are always seperated by "and"
136 407:96910efbd45e luis
    return authors_entry.split(" and ")
137 406:40144aa9dfe7 luis
  end
138
139 1071:fe32745aaa3d luis
  # parses a list of bibtex
140 406:40144aa9dfe7 luis
  def parse_bibtex_list(bibtex_list)
141
    bibliography = BibTeX.parse bibtex_list
142
143
    no_entries = bibliography.data.length
144
145
    # parses the bibtex entries
146
    bibliography.data.map do |d|
147 407:96910efbd45e luis
148
      if d.class == BibTeX::Entry
149
        create_bibtex_entry d
150
      end
151 406:40144aa9dfe7 luis
    end
152 1071:fe32745aaa3d luis
  end
153 407:96910efbd45e luis
154 1071:fe32745aaa3d luis
  def create_bibtex_entry(d)
155 407:96910efbd45e luis
    @publication = Publication.new
156 1071:fe32745aaa3d luis
    @bentry = BibtexEntry.new
157 407:96910efbd45e luis
    authors = []
158
    institution = ""
159
    email = ""
160 409:50474139cad4 luis
161 407:96910efbd45e luis
    d.fields.keys.map do |field|
162
      case field.to_s
163
      when "author"
164
        authors = parse_authors d[field]
165
      when "title"
166
        @publication.title = d[field]
167
      when "institution"
168
        institution = d[field]
169
      when "email"
170
        email = d[field]
171
      else
172
        @bentry[field] = d[field]
173
      end
174 1071:fe32745aaa3d luis
    end
175 406:40144aa9dfe7 luis
176
    @publication.bibtex_entry = @bentry
177 407:96910efbd45e luis
    @publication.save
178 409:50474139cad4 luis
179 1071:fe32745aaa3d luis
    # what is this for???
180 424:b601a9e472f3 luis
    # @created_publications << @publication.id
181 409:50474139cad4 luis
182 407:96910efbd45e luis
    # need to save all authors
183 1071:fe32745aaa3d luis
    #   and establish the author-publication association
184
    #   via the authorships table
185 407:96910efbd45e luis
    authors.each_with_index.map do |authorname, idx|
186
      author = Author.new(:name => authorname)
187
      if author.save!
188 946:a0c9cc95bcf3 luis
        # todo: catch the errors...
189 407:96910efbd45e luis
        puts "SAVED"
190
      else
191
        puts "NOT SAVED"
192 406:40144aa9dfe7 luis
      end
193
194 407:96910efbd45e luis
      author.authorships.create!(
195 946:a0c9cc95bcf3 luis
      :publication => @publication,
196
      :institution => institution,
197
      :email => email,
198
      :order => idx)
199 407:96910efbd45e luis
    end
200
  end
201 409:50474139cad4 luis
202 461:841b2e40895d luis
  def autocomplete_for_project
203
    @publication = Publication.find(params[:id])
204 946:a0c9cc95bcf3 luis
205 1253:29dd06e01be3 luis
    @projects = Project.active.name_or_homepage_like(params[:q]).find(:all, :limit => 100) - @publication.projects
206 461:841b2e40895d luis
    logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
207
    render :layout => false
208 409:50474139cad4 luis
  end
209 471:49fb7a9ef79c luis
210 1071:fe32745aaa3d luis
  def autocomplete_for_author
211 519:3be6bc3c2a17 luis
    @results = []
212 946:a0c9cc95bcf3 luis
213 596:fcff84e1c1ce luis
    object_id = params[:object_id]
214 598:c6cfe1f2eac1 luis
    @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
215 946:a0c9cc95bcf3 luis
216 674:f3e35d639aa4 Chris
    # cc 20110909 -- revert to like instead of like_unique -- see #289
217 1283:006057cf8f16 luis
    authorships_list = Authorship.like(params[:term]).find(:all, :limit => 100)
218
    users_list = User.active.like(params[:term]).find(:all, :limit => 100)
219 480:19efecf4561a luis
220 1283:006057cf8f16 luis
    logger.debug "Query for \"#{params[:term]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
221 946:a0c9cc95bcf3 luis
222 601:1608b3cb50cd luis
    @results = users_list
223
224 1071:fe32745aaa3d luis
    # TODO: can be optimized…
225
    authorships_list.each do |authorship|
226 601:1608b3cb50cd luis
      flag = true
227 946:a0c9cc95bcf3 luis
228 601:1608b3cb50cd luis
      users_list.each do |user|
229
        if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
230
          Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
231
          flag = false
232
          break
233
        end
234
      end
235
236
      @results << authorship if flag
237 592:68c6b060385c luis
    end
238
239 1071:fe32745aaa3d luis
    render :layout => false
240 598:c6cfe1f2eac1 luis
  end
241 946:a0c9cc95bcf3 luis
242 557:2fe129b04d82 luis
  def sort_author_order
243
    params[:authorships].each_with_index do |id, index|
244
      Authorship.update_all(['auth_order=?', index+1], ['id=?', id])
245 471:49fb7a9ef79c luis
    end
246
    render :nothing => true
247
  end
248 574:f463be9d101a luis
249
  def add_project
250 1071:fe32745aaa3d luis
    @projects = Project.find(params[:publication][:project_ids])
251 574:f463be9d101a luis
    @publication.projects << @projects
252 1071:fe32745aaa3d luis
    @project = Project.find(params[:project_id])
253 946:a0c9cc95bcf3 luis
254 1071:fe32745aaa3d luis
    # TODO luisf should also respond to HTML???
255 574:f463be9d101a luis
    respond_to do |format|
256
      format.html { redirect_to :back }
257 1071:fe32745aaa3d luis
      format.js {
258
        render(:update) {|page|
259
          page[:add_project_form].reset
260 574:f463be9d101a luis
          page.replace_html :list_projects, :partial => 'list_projects'
261
        }
262
      }
263
    end
264
  end
265 946:a0c9cc95bcf3 luis
266 574:f463be9d101a luis
  def remove_project
267 579:2ada25d4b0a8 luis
    @project = Project.find(params[:project_id])
268
    proj = Project.find(params[:remove_project_id])
269 554:026c9747fe9b luis
270 574:f463be9d101a luis
    if @publication.projects.length > 1
271 579:2ada25d4b0a8 luis
      if @publication.projects.exists? proj
272
        @publication.projects.delete proj if request.post?
273 554:026c9747fe9b luis
      end
274
    else
275 1071:fe32745aaa3d luis
      logger.error { "Cannot remove project from publication list" }
276 554:026c9747fe9b luis
    end
277 946:a0c9cc95bcf3 luis
278 591:9e866f13c984 luis
    logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
279 946:a0c9cc95bcf3 luis
280 1071:fe32745aaa3d luis
    render(:update) {|page|
281 579:2ada25d4b0a8 luis
      page.replace_html "list_projects", :partial => 'list_projects', :id  => @publication
282 1071:fe32745aaa3d luis
    }
283 554:026c9747fe9b luis
  end
284 946:a0c9cc95bcf3 luis
285 560:735388da579a luis
  def destroy
286 1406:20235e6c60c0 luis
    find_project_by_project_id unless params[:project_id].nil?
287
    @publication = Publication.find(params[:id])
288 1405:43b303a229e1 luis
289 560:735388da579a luis
    @publication.destroy
290 946:a0c9cc95bcf3 luis
291 560:735388da579a luis
    flash[:notice] = "Successfully deleted Publication."
292
    redirect_to :controller => :publications, :action => 'index', :project_id => @project
293
  end
294 471:49fb7a9ef79c luis
295 538:e25248ba597c luis
  private
296 478:7097dc91e58e luis
297 328:aed18b463206 luis
end