comparison vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb @ 640:47b21bb3fa03 cannam_integration

Merge from branch "feature_36"
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 06 Sep 2011 12:23:51 +0100
parents 324678bbbe77
children 525f48af3f54
comparison
equal deleted inserted replaced
572:031d70972601 640:47b21bb3fa03
1 # -*- coding: utf-8 -*-
1 # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb 2 # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
2 3
3 class PublicationsController < ApplicationController 4 class PublicationsController < ApplicationController
4 unloadable 5 unloadable
5 6
6 model_object Publication 7 model_object Publication
7 before_filter :find_model_object, :except => [:new, :create, :index, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author] 8 before_filter :find_model_object, :except => [:new, :create, :index, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author, :get_user_info ]
8 9 before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ]
9 # before_filter :find_project, :except => [:autocomplete_for_project, :add_author, :sort_authors, :autocomplete_for_author]
10 10
11 def new 11 def new
12 find_project_by_project_id 12 find_project_by_project_id
13 @publication = Publication.new 13 @publication = Publication.new
14 14
15 # we'll always want a new publication to have its bibtex entry 15 # we'll always want a new publication to have its bibtex entry
16 @publication.build_bibtex_entry 16 @publication.build_bibtex_entry
17 17
18 # and at least one author 18 # and at least one author
19 # @publication.authorships.build.build_author 19 # @publication.authorships.build.build_author
20 20 @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
21 @project_id = params[:project_id] 21
22 @current_user = User.current 22
23 end 23 end
24
25 24
26 def create 25 def create
27 find_project_by_project_id 26 @project = Project.find(params[:project_id])
28 27
28 @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
29
29 @publication = Publication.new(params[:publication]) 30 @publication = Publication.new(params[:publication])
30
31 # @project = Project.find(params[:project_id])
32 @publication.projects << @project unless @project.nil? 31 @publication.projects << @project unless @project.nil?
33 32
34 if @publication.save 33 if @publication.save
35 flash[:notice] = "Successfully created publication." 34 flash[:notice] = "Successfully created publication."
36 redirect_to :action => :show, :id => @publication, :project_id => @project.id 35 redirect_to :action => :show, :id => @publication, :project_id => @project
37 else 36 else
38 render :action => 'new' 37 render :action => 'new', :project_id => @project
39 end 38 end
40 end 39 end
41 40
42 def index 41 def index
43 if !params[:project_id].nil? 42 if !params[:project_id].nil?
71 end 70 end
72 end 71 end
73 72
74 def edit 73 def edit
75 find_project_by_project_id unless params[:project_id].nil? 74 find_project_by_project_id unless params[:project_id].nil?
76 75
76 @edit_view = true;
77
78 @options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
77 @publication = Publication.find(params[:id]) 79 @publication = Publication.find(params[:id])
78 @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type 80 @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
81
82 # todo: should be removed?
83 @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
79 end 84 end
80 85
81 def update 86 def update
82 @publication = Publication.find(params[:id]) 87 @publication = Publication.find(params[:id])
88
89 # todo: should be removed?
90 @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
83 91
84 logger.error { "INSIDE THE UPDATE ACTION IN THE PUBLICATION CONTROLLER" } 92 logger.error { "INSIDE THE UPDATE ACTION IN THE PUBLICATION CONTROLLER" }
85 93
86 if @publication.update_attributes(params[:publication]) 94 if @publication.update_attributes(params[:publication])
87 flash[:notice] = "Successfully updated Publication." 95 flash[:notice] = "Successfully updated Publication."
97 end 105 end
98 106
99 def show 107 def show
100 find_project_by_project_id unless params[:project_id].nil? 108 find_project_by_project_id unless params[:project_id].nil?
101 109
102 @publication = Publication.find(params[:id])
103
104 if @publication.nil? 110 if @publication.nil?
105 @publications = Publication.all 111 @publications = Publication.all
106 render "index", :alert => 'The publication was not found!' 112 render "index", :alert => 'The publication was not found!'
107 else 113 else
108 @authors = @publication.authors 114 @authors = @publication.authors
109 @bibtext_entry = @publication.bibtex_entry 115 @bibtext_entry = @publication.bibtex_entry
110 end 116 end
111 end 117 end
187 @publication = Publication.new 193 @publication = Publication.new
188 194
189 195
190 end 196 end
191 197
192 def add_project
193 @projects = Project.find(params[:publication][:project_ids])
194 @publication = Publication.find(params[:id])
195 @publication.projects << @projects
196
197 # TODO luisf should also respond to HTML???
198 respond_to do |format|
199 format.js
200 end
201 end
202
203 def autocomplete_for_project 198 def autocomplete_for_project
204 @publication = Publication.find(params[:id]) 199 @publication = Publication.find(params[:id])
205 200
206 @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects 201 @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects
207 logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results" 202 logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
208 render :layout => false 203 render :layout => false
209 end 204 end
210 205
211 def autocomplete_for_author 206 def autocomplete_for_author
212 @results = [] 207 @results = []
213 208
214 authors_list = Author.like(params[:q]).find(:all, :limit => 100) 209 object_id = params[:object_id]
210 @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
211
212 authorships_list = Authorship.like_unique(params[:q]).find(:all, :limit => 100)
215 users_list = User.active.like(params[:q]).find(:all, :limit => 100) 213 users_list = User.active.like(params[:q]).find(:all, :limit => 100)
216 214
217 logger.debug "Query for \"#{params[:q]}\" returned \"#{authors_list.size}\" authors and \"#{users_list.size}\" users" 215 logger.debug "Query for \"#{params[:q]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
218 216
219 # need to subtract both lists 217 @results = users_list
220 # give priority to the users 218
221 users_list.each do |user| 219 # TODO: can be optimized…
222 @results << user 220 authorships_list.each do |authorship|
223 end 221 flag = true
224 222
225 authors_list.each do |author| 223 users_list.each do |user|
226 @results << author unless users_list.include?(author.user) 224 if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
227 end 225 Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
228 226 flag = false
229 render :layout => false 227 break
228 end
229 end
230
231 @results << authorship if flag
232 end
233
234 render :layout => false
235 end
236
237
238 def get_user_info
239 object_id = params[:object_id]
240 value = params[:value]
241 classname = Kernel.const_get(value.split('_')[0])
242
243 item = classname.find(value.split('_')[1])
244
245 name_field = "publication_authorships_attributes_#{object_id}_name_on_paper".to_sym
246 email_field = "publication_authorships_attributes_#{object_id}_email".to_sym
247 institution_field = "publication_authorships_attributes_#{object_id}_institution".to_sym
248
249 yes_radio = "publication_authorships_attributes_#{object_id}_identify_author_yes".to_sym
250
251 respond_to do |format|
252 format.js {logger.error { "JS" }
253 render(:update) {|page|
254 page[name_field].value = item.name
255 page[email_field].value = item.mail
256 page[institution_field].value = item.institution
257
258 page[yes_radio].checked = true
259 page[name_field].readOnly = true
260 page[email_field].readOnly = true
261 page[institution_field].readOnly = true
262 }
263 }
264 end
230 end 265 end
231 266
232 def sort_author_order 267 def sort_author_order
233 params[:authorships].each_with_index do |id, index| 268 params[:authorships].each_with_index do |id, index|
234 Authorship.update_all(['auth_order=?', index+1], ['id=?', id]) 269 Authorship.update_all(['auth_order=?', index+1], ['id=?', id])
235 end 270 end
236 render :nothing => true 271 render :nothing => true
237 end 272 end
238 273
239 def remove_from_project_list 274 def add_project
240 pub = Publication.find(params[:id]) 275 @projects = Project.find(params[:publication][:project_ids])
241 proj = Project.find(params[:project_id]) 276 @publication.projects << @projects
242 277 @project = Project.find(params[:project_id])
243 if pub.projects.length > 1 278
244 if pub.projects.exists? proj 279 # TODO luisf should also respond to HTML???
245 pub.projects.delete proj if request.post? 280 respond_to do |format|
281 format.html { redirect_to :back }
282 format.js {
283 render(:update) {|page|
284 page[:add_project_form].reset
285 page.replace_html :list_projects, :partial => 'list_projects'
286 }
287 }
288 end
289 end
290
291
292 def remove_project
293 @project = Project.find(params[:project_id])
294 proj = Project.find(params[:remove_project_id])
295
296 if @publication.projects.length > 1
297 if @publication.projects.exists? proj
298 @publication.projects.delete proj if request.post?
246 end 299 end
247 else 300 else
248 logger.error { "Cannot remove project from publication list" } 301 logger.error { "Cannot remove project from publication list" }
249 end 302 end
250 303
251 respond_to do |format| 304 logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
252 format.js { render(:update) {|page| page.replace_html "list_projects", :partial => 'list_projects', :id => pub} } 305
253 end 306 render(:update) {|page|
254 307 page.replace_html "list_projects", :partial => 'list_projects', :id => @publication
255 308 }
256 end 309 end
257 310
258 def destroy 311 def destroy
259 find_project_by_project_id 312 find_project_by_project_id
260 313
261 @publication.destroy 314 @publication.destroy
262 315
263 flash[:notice] = "Successfully deleted Publication." 316 flash[:notice] = "Successfully deleted Publication."
264 redirect_to :controller => :publications, :action => 'index', :project_id => @project 317 redirect_to :controller => :publications, :action => 'index', :project_id => @project
265 end 318 end
266 319
267 def identify_author
268
269 end
270
271 private 320 private
272 321
273 end 322 end