changeset 1026:b42553f6df71 bibplugin_bibtex

Merge
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Tue, 13 Nov 2012 13:23:29 +0000
parents b52621512b47 (diff) 02ee54197879 (current diff)
children 72294a3b0647
files vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb vendor/plugins/redmine_bibliography/assets/stylesheets/bibliography.css
diffstat 7 files changed, 121 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb	Tue Nov 13 13:23:29 2012 +0000
@@ -1,11 +1,13 @@
 # -*- coding: utf-8 -*-
 # vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
 
+class BibtexParsingError < Exception; end
+
 class PublicationsController < ApplicationController
   unloadable
 
   model_object Publication
-  before_filter :find_model_object, :except => [:new, :create, :index, :get_bibtex_required_fields, :autocomplete_for_project, :add_author, :sort_author_order, :autocomplete_for_author, :get_user_info ]  
+  before_filter :find_model_object, :except => [:parse_bibtex, :new, :create, :index, :get_bibtex_required_fields, :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
@@ -18,11 +20,67 @@
     # and at least one author
     # @publication.authorships.build.build_author        
     @author_options = [["#{User.current.name} (@#{User.current.mail.partition('@')[2]})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
+  end
 
 
+  def parse_bibtex
+    find_project_by_project_id
+
+    @bibtex_paste = params[:bibtex_paste]
+
+    begin
+      bib = BibTeX.parse(@bibtex_paste)
+
+      if bib.errors.present? or bib[0].class == NilClass
+        raise BibtexParsingError, "Bibtex Parsing Error"
+      end
+
+      respond_to do |format| 
+        format.js {
+          render(:update) {|page|
+            flash[:notice] = "Correctly parsed BibTeX entry"
+
+            bibtex_entry_no = BibtexEntryType.find_by_name(bib[0].type.to_s).id
+            page["publication_title"].value = bib[0][:title]
+            page["publication_bibtex_entry_attributes_entry_type"].value = bibtex_entry_no
+            
+            BibtexEntryType.fields(bibtex_entry_no).each do |field|
+              page["publication_bibtex_entry_attributes_#{field}"].value = bib[0][field]
+            end
+
+            # for each author simulates a click and fills the author info                        
+            bib[0].authors.each do |author|            
+              page["add_another_author"].click
+#              page.alert(bib[0].authors.length)
+#              page.alert(page["authors"].first.id)
+            
+            end
+            
+
+
+          }
+        }
+      end
+
+    rescue BibtexParsingError => e
+      logger.error { "Bibtex Parsing Error #{bib.errors}" }
+
+      # todo: not showing... should be inside render?
+      flash[:error] = e.message
+      
+      respond_to do |format|
+       format.js{ 
+         render(:update) {|page|
+         }
+       }
+      end
+
+    end
+
   end
 
-  def create    
+
+  def create            
     @project = Project.find(params[:project_id])
 
     @author_options = []
@@ -64,14 +122,12 @@
 
   def get_bibtex_required_fields
 
-    unless params[:value].empty?
-      fields = BibtexEntryType.fields(params[:value]) 
-    end
+    fields = BibtexEntryType.fields(params[:q]) 
 
     respond_to do |format|
       format.js {
         render(:update) {|page|       
-          if params[:value].empty?
+          if params[:q].empty?
             page << "hideOnLoad();"
           else
             page << "show_required_bibtex_fields(#{fields.to_json()});"
@@ -109,10 +165,8 @@
 
     @author_options = []
 
-    logger.error { "INSIDE THE UPDATE ACTION IN THE PUBLICATION CONTROLLER" }
-
     if @publication.update_attributes(params[:publication])
-      flash[:notice] = "Successfully updated Publication."
+      flash[:notice] = "Successfully Updated Publication."
 
       if !params[:project_id].nil?
         redirect_to :action => :show, :id => @publication, :project_id => params[:project_id]
@@ -224,6 +278,7 @@
     logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
     render :layout => false
   end
+  
 
   def autocomplete_for_author    
     @results = []
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.html.erb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_authorship_fields.html.erb	Tue Nov 13 13:23:29 2012 +0000
@@ -1,9 +1,4 @@
-<%= stylesheet_link_tag 'bibliography', :plugin => 'redmine_bibliography' %>
-
-
-<div id="authors" class="fields">
-<!--  <h4><%= l("label_author_1") %></h4> -->
-
+<fieldset> 
   <div id="<%= form_tag_id( f.object_name, :search_author ) %>" style=<%= "display:none;" unless params[:action] == "new" %> >
       <p>
         <%= f.text_field :search_name, :size => 25 %>
@@ -12,10 +7,10 @@
       <%# link_to_function l(:label_author_is_me), "update_author_info(this," + User.current.get_author_info.to_json + ")", :id => "add_me_as_author" %>
 
       <p>   
-      <%= f.select :search_results, options_for_select(@author_options), {}, {:size => 5, 
-        :onChange => remote_function( :url => { :controller => :publications, :action => :get_user_info, :object_id => form_object_id(f.object_name) }, :with => "'value=' + 
-        value" )} %>
-	</p>  
+        <%= f.select :search_results, options_for_select(@author_options), {}, {:size => 5, 
+          :onChange => remote_function( :url => { :controller => :publications, :action => :get_user_info, :object_id => form_object_id(f.object_name) }, :with => "'value=' + 
+          value" )} %>
+	    </p>  
 
       <p style="margin-bottom: -2.5em; padding-bottom; 0"><label><%= l(:identify_author_question) %></label></p>
       <p class="author_identify">
@@ -25,7 +20,7 @@
         
         <label class='inline'><%= radio_button_tag(:identify_author, "no", true, :name => form_tag_name(f.object_name,:identify_author ), :id => form_tag_id( f.object_name, :identify_author_no ), :onchange => "identify_author_status($(this).value, #{form_object_id(f.object_name) });") %> <%= l(:identify_author_no) %> </label><br />
       </p>
-    </div>	
+  </div>	
   
   <div class='author_edit' id="<%= form_tag_id( f.object_name, :edit_author_info ) %>">
     <p>
@@ -38,21 +33,18 @@
     </p>
   </div>
   
-
   <div class="box" id="<%= form_tag_id( f.object_name, :show_author_info ) %>" style="display: none">
-	
   </div>
-
+  
   <p>
-
-  <%- if params[:action] == 'new' -%>
-    <%= button_to_function l(:label_save_author), {}, { :onclick => "toggle_save_author(#{form_object_id(f.object_name)}); return false;", :id => form_tag_id( f.object_name, :edit_save_button )} %>
-  <%- else -%>
-    <%= button_to_function l(:label_edit_author), {}, { :onclick => "toggle_save_author(#{form_object_id(f.object_name)}); return false;", :id => form_tag_id( f.object_name, :edit_save_button )} %>
-  <%- end -%>
-
+    <%- if params[:action] == 'new' -%>
+      <%= button_to_function l(:label_save_author), {}, { :onclick => "toggle_save_author(#{form_object_id(f.object_name)}); return false;", :id => form_tag_id( f.object_name, :edit_save_button )} %>
+    <%- else -%>
+      <%= button_to_function l(:label_edit_author), {}, { :onclick => "toggle_save_author(#{form_object_id(f.object_name)}); return false;", :id => form_tag_id( f.object_name, :edit_save_button )} %>
+    <%- end -%>
 
   <%= link_to_remove_fields l("remove_author"), f %>
   </p>
-</div>
-<br/>
+<br />
+</fieldset>
+
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_bibtex_fields.html.erb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_bibtex_fields.html.erb	Tue Nov 13 13:23:29 2012 +0000
@@ -3,12 +3,17 @@
 	<%= f.collection_select :entry_type, 
 	        BibtexEntryType.find(:all).reject { |x| x.redundant? }, 
 	        :id, 
-	        :label,  
-	        { :selected => @selected_bibtex_entry_type_id, :prompt => true },	           
-	        :onChange => remote_function( :url => { :controller => :publications, :action => :get_bibtex_required_fields}, :with => "'value=' + value" )	              
+	        :label,
+	        { :prompt => true }
 	%>
 </p>
 
+
+<%= observe_field :publication_bibtex_entry_attributes_entry_type, :url => { :controller => :publications, :action => :get_bibtex_required_fields },
+     :frequency => 0.25,
+     :with => 'q'
+     %>
+
 <p class="bibtex hol"> 
   <%= f.text_field :year, :size => 4 %> 
 </p>
--- a/vendor/plugins/redmine_bibliography/app/views/publications/_form.html.erb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/_form.html.erb	Tue Nov 13 13:23:29 2012 +0000
@@ -1,9 +1,9 @@
-<%= f.error_messages %>  
+<%= f.error_messages %> 
+<%= stylesheet_link_tag 'bibliography', :plugin => 'redmine_bibliography' %>
 
 <h3><%= f.text_field :title, :required => true, :size => 70 %></h3>
 
-<div class="splitcontentleft">      
-  <h3><%= l(:label_publication_other_details) %></h3>
+<h3><%= l(:label_publication_other_details) %></h3>
   <div class="box tabular">
     <% f.fields_for :bibtex_entry do |builder| -%>
       <%= render :partial => 'bibtex_fields', :locals => { :f => builder}  %>
@@ -14,18 +14,15 @@
       <br />
       <em><%= l(:text_external_url) %></em>
     </p>
+  </div>
 
-  </div>
-</div>
-
-<div class="splitcontentright">
-  <h3><%= l(:authors) %></h3>    
+<h3><%= l(:authors) %></h3>    
   <div class="box tabular">
-    <% f.fields_for :authorships do |builder| -%>
-      <%= render "authorship_fields", :f => builder %>
-    <%- end -%>
+    <div id="authors" class="fields">      
+      <% f.fields_for :authorships do |builder| -%>
+        <%= render "authorship_fields", :f => builder %>
+      <%- end -%>
+    </div>
     <%= link_to_add_author_fields l(:label_add_an_author), f, :authorships, params[:action] %>
   </div>
-</div>
-
-
+</div>
\ No newline at end of file
--- a/vendor/plugins/redmine_bibliography/app/views/publications/new.html.erb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/new.html.erb	Tue Nov 13 13:23:29 2012 +0000
@@ -5,8 +5,21 @@
 
 <h2><%=l(:label_publication_new)%></h2>
 
-<% form_for @publication, :url => { :project_id  => @project, :action => :create }, :builder => TabularFormBuilder do |f| -%>
-  <%= render :partial => 'form', :locals => { :f => f }  %>
-  <div style="clear:both"></div>
-  <%= f.submit %>
-<% end %>
+<p>To create a publication you can either parse a BibTeX entry or enter the publication details manually.<p>
+
+
+<div class="splitcontentleft">
+  <%- remote_form_for @publication, :url =>  { :project_id  => @project, :action => :parse_bibtex } do |f| -%>
+    <%= text_area_tag :bibtex_paste, "Please paste your bibtex entry here", :rows => 6, :style => 'width:90%' %>
+    <br />
+    <%= f.submit "Parse BibTex" %>
+  <%- end -%>
+</div>
+
+<div class="splitcontentright">
+  <% form_for @publication, :url => { :project_id  => @project, :action => :create }, :builder => TabularFormBuilder do |f| -%>
+    <%= render :partial => 'form', :locals => { :f => f }  %>
+    <div style="clear:both"></div>
+    <%= f.submit %>
+  <% end %>
+</div>
--- a/vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/app/views/publications/show.html.erb	Tue Nov 13 13:23:29 2012 +0000
@@ -1,3 +1,5 @@
+<%= stylesheet_link_tag 'bibliography', :plugin => 'redmine_bibliography' %>
+
 <h2><%=l(:label_publication_show)%></h2>
 
 <div class="box">
--- a/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Tue Nov 13 12:10:15 2012 +0000
+++ b/vendor/plugins/redmine_bibliography/assets/javascripts/authors.js	Tue Nov 13 13:23:29 2012 +0000
@@ -6,9 +6,9 @@
 function add_author_fields(link, association, content, action) {
 	var new_id = new Date().getTime();
   var regexp = new RegExp("new_" + association, "g");
-  $(link).insert({
-		before: content.replace(regexp, new_id)
-  });
+
+  $('authors').insert(content.replace(regexp, new_id));
+
 	if(action != "new"){
 		toggle_save_author(new_id, $(link));
 	};