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 @ 1421:69d32b831d2b

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