comparison plugins/redmine_bibliography/app/controllers/publications_controller.rb @ 1484:51364c0cd58f redmine-2.4-integration

Merge from live branch. Still need to merge manually in files overridden by plugins.
author Chris Cannam
date Wed, 15 Jan 2014 09:59:14 +0000
parents 8d721cac2925
children
comparison
equal deleted inserted replaced
1464:261b3d9a4903 1484:51364c0cd58f
1 # -*- coding: utf-8 -*-
2 # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
3
4 class PublicationsController < ApplicationController
5 unloadable
6
7 model_object Publication
8 before_filter :find_model_object, :only => [ :show, :add_project ]
9 before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ]
10
11 def new
12 find_project_by_project_id
13 @publication = Publication.new
14
15 # we'll always want a new publication to have its bibtex entry
16 @publication.build_bibtex_entry
17
18 end
19
20 def create
21 @project = Project.find(params[:project_id])
22
23 @publication = Publication.new(params[:publication])
24 @publication.projects << @project unless @project.nil?
25
26 if @publication.save
27 @publication.notify_authors_publication_added(@project)
28
29 flash[:notice] = "Successfully created publication."
30 redirect_to :action => :show, :id => @publication, :project_id => @project
31 else
32 render :action => 'new', :project_id => @project
33 end
34 end
35
36 def index
37 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 end
45
46 def new_from_bibfile
47 @publication.current_step = session[:publication_step]
48
49 # contents of the paste text area
50 bibtex_entry = params[:bibtex_entry]
51
52 # method for creating "pasted" bibtex entries
53 if bibtex_entry
54 parse_bibtex_list bibtex_entry
55 end
56 end
57
58 def show_bibtex_fields
59 @fields = []
60
61 unless params[:value].empty?
62 @fields = BibtexEntryType.fields(params[:value])
63 end
64
65 respond_to do |format|
66 format.js {
67 render :show_bibtex_fields
68 }
69 end
70 end
71
72 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 logger.error { "Error while adding Author to publication." }
79 end
80 end
81
82 def edit
83 find_project_by_project_id unless params[:project_id].nil?
84
85 @publication = Publication.find(params[:id])
86 @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
87 @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)
88 end
89
90 def update
91 @publication = Publication.find(params[:id])
92
93 if @publication.update_attributes(params[:publication])
94 flash[:notice] = "Successfully updated Publication."
95
96 # expires the previosly cached entries
97 Rails.cache.delete "publication-#{@publication.id}-ieee"
98 Rails.cache.delete "publication-#{@publication.id}-bibtex"
99
100 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 else
106 render :action => 'edit'
107 end
108 end
109
110
111 def show
112 find_project_by_project_id unless params[:project_id].nil?
113
114 if @publication.nil?
115 @publications = Publication.all
116 render "index", :alert => 'The publication was not found!'
117 else
118 @authors = @publication.authors
119 @bibtext_entry = @publication.bibtex_entry
120 end
121 end
122
123 # parse string with bibtex authors
124 def parse_authors(authors_entry)
125 # in bibtex the authors are always seperated by "and"
126 return authors_entry.split(" and ")
127 end
128
129 # parses a list of bibtex
130 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
138 if d.class == BibTeX::Entry
139 create_bibtex_entry d
140 end
141 end
142 end
143
144 def create_bibtex_entry(d)
145 @publication = Publication.new
146 @bentry = BibtexEntry.new
147 authors = []
148 institution = ""
149 email = ""
150
151 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 end
165
166 @publication.bibtex_entry = @bentry
167 @publication.save
168
169 # need to save all authors
170 # and establish the author-publication association
171 # via the authorships table
172 authors.each_with_index.map do |authorname, idx|
173 author = Author.new(:name => authorname)
174 if author.save!
175 # todo: catch the errors...
176 puts "SAVED"
177 else
178 puts "NOT SAVED"
179 end
180
181 author.authorships.create!(
182 :publication => @publication,
183 :institution => institution,
184 :email => email,
185 :order => idx)
186 end
187 end
188
189 def autocomplete_for_project
190 @publication = Publication.find(params[:id])
191
192 @projects = Project.active.name_or_homepage_like(params[:q]).find(:all, :limit => 100) - @publication.projects
193 logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
194 render :layout => false
195 end
196
197 def autocomplete_for_author
198 @results = []
199
200 object_id = params[:object_id]
201 @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
202
203 # 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
206 # name_like scope, defined in lib/user_author patch
207 users_list = User.active.name_like(params[:term]).find(:all, :limit => 100)
208
209 logger.debug "Query for \"#{params[:term]}\" returned \"#{authors_list.size}\" authors and \"#{users_list.size}\" users"
210
211 # will check if any of the members of the users list
212 # doesn't belong to the authors list
213
214 @results = authors_list
215
216 users_list.each do |user|
217 @results << user unless authors_list.include?(user.author)
218 end
219
220 logger.debug { "Autocomplete_for_author results --> #{@results}" }
221
222 render :layout => false
223 end
224
225 def sort_author_order
226 params[:authorships].each_with_index do |id, index|
227 Authorship.update_all(['auth_order=?', index+1], ['id=?', id])
228 end
229 render :nothing => true
230 end
231
232 def add_project
233 @projects = Project.find(params[:publication][:project_ids])
234 @publication.projects << @projects
235 @project = Project.find(params[:project_id])
236
237 # TODO luisf should also respond to HTML???
238 respond_to do |format|
239 format.html { redirect_to :back }
240 format.js {
241 render(:update) {|page|
242 page[:add_project_form].reset
243 page.replace_html :list_projects, :partial => 'list_projects'
244 }
245 }
246 end
247 end
248
249 def remove_project
250 @project = Project.find(params[:project_id])
251 proj = Project.find(params[:remove_project_id])
252
253 if @publication.projects.length > 1
254 if @publication.projects.exists? proj
255 @publication.projects.delete proj if request.post?
256 end
257 else
258 logger.error { "Cannot remove project from publication list" }
259 end
260
261 logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
262
263 render(:update) {|page|
264 page.replace_html "list_projects", :partial => 'list_projects', :id => @publication
265 }
266 end
267
268 def destroy
269 find_project_by_project_id unless params[:project_id].nil?
270 @publication = Publication.find(params[:id])
271
272 @publication.destroy
273
274 flash[:notice] = "Successfully deleted Publication."
275 redirect_to :controller => :publications, :action => 'index', :project_id => @project
276 end
277
278 private
279
280 end