view vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb @ 424:b601a9e472f3 feature_36

Fixed minor bugs with the publications controller Started creating tests and fixtures.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Wed, 25 May 2011 17:46:49 +0100
parents 50474139cad4
children 4ecbc22579e2
line wrap: on
line source
# vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb

class PublicationsController < ApplicationController

  def new
    # we always try to create at least one publication
    @publication = Publication.new

    # the step we're at in the form
    @publication.current_step = session[:publication_step]

    @new_publications = []
    session[:publications] ||= {}
      
  end

  def create
    @publication = Publication.new(params[:publication])
    @publication.current_step = session[:publication_step]
    
    # contents of the paste text area
    bibtex_entry = params[:bibtex_entry]

    # method for creating "pasted" bibtex entries
    if bibtex_entry
      logger.error "ANTES PARSE"      
      parse_bibtex_list bibtex_entry    
      logger.error "DEPOIS PARSE"
    end

    # form's flow control
    if params[:back_button]
      @publication.previous_step
    else
      @publication.next_step
    end

    session[:publication_step] = @publication.current_step

    if @publication.new_record?
      render "new"
    else
      session[:publication_step] = session[:publication_params] = nil
      flash[:notice] = "New publication saved!"
      redirect_to @publication
    end
  end

  def index
    @publications = Publication.find(:all)
  end

  def edit
    logger.error "AAAA edit"

  end

  def update

    logger.error "AAAA update"

  end

  def show  
    @publication = Publication.find(params[id])
    @authors = @publication.authors
  end

  # parse string with bibtex authors
  def parse_authors(authors_entry)
    # in bibtex the authors are always seperated by "and"
    return authors_entry.split(" and ")
  end

  # parses a list of bibtex 
  def parse_bibtex_list(bibtex_list)
    bibliography = BibTeX.parse bibtex_list

    no_entries = bibliography.data.length

    # parses the bibtex entries
    bibliography.data.map do |d|

      if d.class == BibTeX::Entry
        create_bibtex_entry d
      end
    end
  end 

  def create_bibtex_entry(d)        
    @publication = Publication.new
    @bentry = BibtexEntry.new        
    authors = []
    institution = ""
    email = ""

    d.fields.keys.map do |field|
      case field.to_s
      when "author"
        authors = parse_authors d[field]
      when "title"
        @publication.title = d[field]
      when "institution"
        institution = d[field]
      when "email"
        email = d[field]
      else
        @bentry[field] = d[field]
      end
    end 

    @publication.bibtex_entry = @bentry
    @publication.save

    # what is this for??? 
    # @created_publications << @publication.id

    # need to save all authors
    #   and establish the author-publication association 
    #   via the authorships table 
    authors.each_with_index.map do |authorname, idx|
      author = Author.new(:name => authorname)
      if author.save!
        puts "SAVED"
      else
        puts "NOT SAVED"
      end

      author.authorships.create!(
      :publication => @publication,
      :institution => institution,
      :email => email,
      :order => idx)

    end
  end

  # parses the bibtex file
  def parse_bibtex_file

  end


  def review_new_entries

  end


end