diff 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
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Aug 09 16:02:08 2011 +0100
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Sep 06 12:23:51 2011 +0100
@@ -1,12 +1,12 @@
+# -*- coding: utf-8 -*-
 # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
 
 class PublicationsController < ApplicationController
   unloadable
   
   model_object Publication
-  before_filter :find_model_object, :except => [:new, :create, :index, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author]
-  
-  # before_filter :find_project, :except => [:autocomplete_for_project, :add_author, :sort_authors, :autocomplete_for_author]
+  before_filter :find_model_object, :except => [:new, :create, :index, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author, :get_user_info ]  
+  before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ]
     
   def new
     find_project_by_project_id
@@ -16,26 +16,25 @@
     @publication.build_bibtex_entry
     
     # and at least one author
-    # @publication.authorships.build.build_author
-    
-    @project_id = params[:project_id]
-    @current_user = User.current    
+    # @publication.authorships.build.build_author        
+    @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
+
+
   end
 
+  def create    
+    @project = Project.find(params[:project_id])
 
-  def create    
-    find_project_by_project_id
-    
+    @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
+
     @publication = Publication.new(params[:publication])
-        
-    # @project = Project.find(params[:project_id])
     @publication.projects << @project unless @project.nil?
         
     if @publication.save 
       flash[:notice] = "Successfully created publication."
-      redirect_to :action => :show, :id => @publication, :project_id => @project.id
+      redirect_to :action => :show, :id => @publication, :project_id => @project
     else
-      render :action => 'new'
+      render :action => 'new', :project_id => @project
     end
   end
 
@@ -73,14 +72,23 @@
 
   def edit   
     find_project_by_project_id unless params[:project_id].nil?
-     
+    
+    @edit_view = true;
+    
+    @options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
     @publication = Publication.find(params[:id])
-    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type  
+    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
+
+    # todo: should be removed? 
+    @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]  
   end
 
   def update    
     @publication = Publication.find(params[:id])        
 
+    # todo: should be removed? 
+    @author_options = [["#{User.current.name} (#{User.current.mail})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
+
     logger.error { "INSIDE THE UPDATE ACTION IN THE PUBLICATION CONTROLLER" }
 
     if @publication.update_attributes(params[:publication])
@@ -99,11 +107,9 @@
   def show
     find_project_by_project_id unless params[:project_id].nil?
     
-    @publication = Publication.find(params[:id])
-    
     if @publication.nil?
-        @publications = Publication.all
-        render "index", :alert => 'The publication was not found!'
+      @publications = Publication.all
+      render "index", :alert => 'The publication was not found!'
     else
       @authors = @publication.authors
       @bibtext_entry = @publication.bibtex_entry
@@ -189,17 +195,6 @@
     
   end
   
-  def add_project
-    @projects = Project.find(params[:publication][:project_ids])    
-    @publication = Publication.find(params[:id])        
-    @publication.projects << @projects
-    
-    # TODO luisf should also respond to HTML??? 
-    respond_to do |format|
-      format.js      
-    end
-  end
-
   def autocomplete_for_project
     @publication = Publication.find(params[:id])
         
@@ -208,25 +203,65 @@
     render :layout => false
   end
 
-  def autocomplete_for_author
+  def autocomplete_for_author    
     @results = []
     
-    authors_list = Author.like(params[:q]).find(:all, :limit => 100)    
+    object_id = params[:object_id]
+    @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
+        
+    authorships_list = Authorship.like_unique(params[:q]).find(:all, :limit => 100)
     users_list = User.active.like(params[:q]).find(:all, :limit => 100)
 
-    logger.debug "Query for \"#{params[:q]}\" returned \"#{authors_list.size}\" authors and \"#{users_list.size}\" users"
+    logger.debug "Query for \"#{params[:q]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
     
-    # need to subtract both lists
-    # give priority to the users    
-    users_list.each do |user|      
-      @results << user
+    @results = users_list
+
+    # TODO: can be optimized…    
+    authorships_list.each do |authorship|      
+      flag = true
+      
+      users_list.each do |user|
+        if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
+          Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
+          flag = false
+          break
+        end
+      end
+
+      @results << authorship if flag
     end
+
+    render :layout => false    
+  end
+  
+  
+  def get_user_info
+    object_id = params[:object_id]
+    value = params[:value]
+    classname = Kernel.const_get(value.split('_')[0])
+
+    item = classname.find(value.split('_')[1])
+
+    name_field = "publication_authorships_attributes_#{object_id}_name_on_paper".to_sym
+    email_field = "publication_authorships_attributes_#{object_id}_email".to_sym
+    institution_field = "publication_authorships_attributes_#{object_id}_institution".to_sym
     
-    authors_list.each do |author|      
-      @results << author unless users_list.include?(author.user)
+    yes_radio = "publication_authorships_attributes_#{object_id}_identify_author_yes".to_sym
+    
+    respond_to do |format|
+      format.js {logger.error { "JS" }
+        render(:update) {|page| 
+          page[name_field].value = item.name
+          page[email_field].value = item.mail
+          page[institution_field].value = item.institution
+
+          page[yes_radio].checked = true
+          page[name_field].readOnly = true
+          page[email_field].readOnly = true
+          page[institution_field].readOnly = true
+        }
+      }
     end
-                 
-    render :layout => false
   end
 
   def sort_author_order
@@ -235,26 +270,44 @@
     end
     render :nothing => true
   end
+
+  def add_project
+    @projects = Project.find(params[:publication][:project_ids])    
+    @publication.projects << @projects
+    @project = Project.find(params[:project_id])    
+    
+    # TODO luisf should also respond to HTML??? 
+    respond_to do |format|
+      format.html { redirect_to :back }
+      format.js { 
+        render(:update) {|page| 
+          page[:add_project_form].reset          
+          page.replace_html :list_projects, :partial => 'list_projects'
+        }
+      }
+    end
+  end
   
-  def remove_from_project_list
-    pub = Publication.find(params[:id])
-    proj = Project.find(params[:project_id])
+  
+  def remove_project
+    @project = Project.find(params[:project_id])
+    proj = Project.find(params[:remove_project_id])
 
-    if pub.projects.length > 1
-      if pub.projects.exists? proj
-        pub.projects.delete proj if request.post?
+    if @publication.projects.length > 1
+      if @publication.projects.exists? proj
+        @publication.projects.delete proj if request.post?
       end
     else
       logger.error { "Cannot remove project from publication list" }      
     end
     
-    respond_to do |format|
-      format.js { render(:update) {|page| page.replace_html "list_projects", :partial => 'list_projects', :id  => pub} } 
-    end
+    logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
+        
+    render(:update) {|page| 
+      page.replace_html "list_projects", :partial => 'list_projects', :id  => @publication
+    }    
+  end
     
-    
-  end
-  
   def destroy
     find_project_by_project_id
     
@@ -264,10 +317,6 @@
     redirect_to :controller => :publications, :action => 'index', :project_id => @project
   end
 
-  def identify_author
-    
-  end
-
   private
 
 end