To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / plugins / redmine_bibliography / app / controllers / publications_controller.rb @ 1375:c28ce1a71a22

History | View | Annotate | Download (9.04 KB)

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
    # and at least one author
19
    # @publication.authorships.build.build_author
20
    @author_options = [["#{User.current.name} (@#{User.current.mail.partition('@')[2]})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
21
  end
22

    
23
  def create
24
    @project = Project.find(params[:project_id])
25

    
26
    @author_options = []
27

    
28
    @publication = Publication.new(params[:publication])
29
    @publication.projects << @project unless @project.nil?
30

    
31
    if @publication.save
32
      @publication.notify_authors_publication_added(@project)
33

    
34
      flash[:notice] = "Successfully created publication."
35
      redirect_to :action => :show, :id => @publication, :project_id => @project
36
    else
37
      render :action => 'new', :project_id => @project
38
    end
39
  end
40

    
41
  def index
42
    if !params[:project_id].nil?
43
      find_project_by_project_id
44
      @project = Project.find(params[:project_id])
45
      @publications = Publication.find :all, :joins => :projects, :conditions => ["project_id = ?", @project.id]
46
    else
47
      @publications = Publication.find :all
48
    end
49
  end
50

    
51
  def new_from_bibfile
52
    @publication.current_step = session[:publication_step]
53

    
54
    # contents of the paste text area
55
    bibtex_entry = params[:bibtex_entry]
56

    
57
    # method for creating "pasted" bibtex entries
58
    if bibtex_entry
59
      parse_bibtex_list bibtex_entry
60
    end
61
  end
62

    
63
  def show_bibtex_fields
64
    @fields = []
65

    
66
    unless params[:value].empty?
67
      @fields = BibtexEntryType.fields(params[:value])
68
    end
69

    
70
    respond_to do |format|
71
      format.js {
72
        render :show_bibtex_fields
73
      }
74
    end
75
  end
76

    
77
  def add_author
78
    if (request.xhr?)
79
      render :text => User.find(params[:user_id]).name
80
    else
81
      # No?  Then render an action.
82
      #render :action => 'view_attribute', :attr => @name
83
      logger.error { "Error while adding Author to publication." }
84
    end
85
  end
86

    
87
  def edit
88
    find_project_by_project_id unless params[:project_id].nil?
89

    
90
    @edit_view = true;
91
    @publication = Publication.find(params[:id])
92
    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
93

    
94
    @author_options = []
95

    
96
    @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)
97
  end
98

    
99
  def update
100
    @publication = Publication.find(params[:id])
101
    @author_options = []
102

    
103
    if @publication.update_attributes(params[:publication])
104
      flash[:notice] = "Successfully updated Publication."
105

    
106
      # expires the previosly cached entries
107
      Rails.cache.delete "publication-#{@publication.id}-ieee"
108
      Rails.cache.delete "publication-#{@publication.id}-bibtex"
109

    
110
      if !params[:project_id].nil?
111
        redirect_to :action => :show, :id => @publication, :project_id => params[:project_id]
112
      else
113
        redirect_to :action => :show, :id => @publication
114
      end
115
    else
116
      render :action => 'edit'
117
    end
118
  end
119

    
120

    
121
  def show
122
    find_project_by_project_id unless params[:project_id].nil?
123

    
124
    if @publication.nil?
125
      @publications = Publication.all
126
      render "index", :alert => 'The publication was not found!'
127
    else
128
      @authors = @publication.authors
129
      @bibtext_entry = @publication.bibtex_entry
130
    end
131
  end
132

    
133
  # parse string with bibtex authors
134
  def parse_authors(authors_entry)
135
    # in bibtex the authors are always seperated by "and"
136
    return authors_entry.split(" and ")
137
  end
138

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

    
143
    no_entries = bibliography.data.length
144

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

    
148
      if d.class == BibTeX::Entry
149
        create_bibtex_entry d
150
      end
151
    end
152
  end
153

    
154
  def create_bibtex_entry(d)
155
    @publication = Publication.new
156
    @bentry = BibtexEntry.new
157
    authors = []
158
    institution = ""
159
    email = ""
160

    
161
    d.fields.keys.map do |field|
162
      case field.to_s
163
      when "author"
164
        authors = parse_authors d[field]
165
      when "title"
166
        @publication.title = d[field]
167
      when "institution"
168
        institution = d[field]
169
      when "email"
170
        email = d[field]
171
      else
172
        @bentry[field] = d[field]
173
      end
174
    end
175

    
176
    @publication.bibtex_entry = @bentry
177
    @publication.save
178

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

    
182
    # need to save all authors
183
    #   and establish the author-publication association
184
    #   via the authorships table
185
    authors.each_with_index.map do |authorname, idx|
186
      author = Author.new(:name => authorname)
187
      if author.save!
188
        # todo: catch the errors...
189
        puts "SAVED"
190
      else
191
        puts "NOT SAVED"
192
      end
193

    
194
      author.authorships.create!(
195
      :publication => @publication,
196
      :institution => institution,
197
      :email => email,
198
      :order => idx)
199
    end
200
  end
201

    
202
  # parses the bibtex file
203
  def parse_bibtex_file
204

    
205
  end
206

    
207
  def import
208
    @publication = Publication.new
209

    
210

    
211
  end
212

    
213
  def autocomplete_for_project
214
    @publication = Publication.find(params[:id])
215

    
216
    @projects = Project.active.name_or_homepage_like(params[:q]).find(:all, :limit => 100) - @publication.projects
217
    logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
218
    render :layout => false
219
  end
220

    
221
  def autocomplete_for_author
222
    @results = []
223

    
224
    object_id = params[:object_id]
225
    @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
226

    
227
    # cc 20110909 -- revert to like instead of like_unique -- see #289
228
    authorships_list = Authorship.like(params[:term]).find(:all, :limit => 100)
229
    users_list = User.active.like(params[:term]).find(:all, :limit => 100)
230

    
231
    logger.debug "Query for \"#{params[:term]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
232

    
233
    @results = users_list
234

    
235
    # TODO: can be optimized…
236
    authorships_list.each do |authorship|
237
      flag = true
238

    
239
      users_list.each do |user|
240
        if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
241
          Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
242
          flag = false
243
          break
244
        end
245
      end
246

    
247
      @results << authorship if flag
248
    end
249

    
250
    render :layout => false
251
  end
252

    
253
  def get_user_info
254
    object_id = params[:object_id]
255
    value = params[:value]
256
    classname = Kernel.const_get(value.split('_')[0])
257

    
258
    item = classname.find(value.split('_')[1])
259

    
260
    name_field = "publication_authorships_attributes_#{object_id}_name_on_paper".to_sym
261
    email_field = "publication_authorships_attributes_#{object_id}_email".to_sym
262
    institution_field = "publication_authorships_attributes_#{object_id}_institution".to_sym
263

    
264
    yes_radio = "publication_authorships_attributes_#{object_id}_identify_author_yes".to_sym
265

    
266
    respond_to do |format|
267
      format.js {
268
        render(:update) {|page|
269
          page[name_field].value = item.name
270
          page[email_field].value = item.mail
271
          page[institution_field].value = item.institution
272

    
273
          page[yes_radio].checked = true
274
          page[name_field].readOnly = true
275
          page[email_field].readOnly = true
276
          page[institution_field].readOnly = true
277
        }
278
      }
279
    end
280
  end
281

    
282
  def sort_author_order
283
    params[:authorships].each_with_index do |id, index|
284
      Authorship.update_all(['auth_order=?', index+1], ['id=?', id])
285
    end
286
    render :nothing => true
287
  end
288

    
289
  def add_project
290
    @projects = Project.find(params[:publication][:project_ids])
291
    @publication.projects << @projects
292
    @project = Project.find(params[:project_id])
293

    
294
    # TODO luisf should also respond to HTML???
295
    respond_to do |format|
296
      format.html { redirect_to :back }
297
      format.js {
298
        render(:update) {|page|
299
          page[:add_project_form].reset
300
          page.replace_html :list_projects, :partial => 'list_projects'
301
        }
302
      }
303
    end
304
  end
305

    
306

    
307
  def remove_project
308
    @project = Project.find(params[:project_id])
309
    proj = Project.find(params[:remove_project_id])
310

    
311
    if @publication.projects.length > 1
312
      if @publication.projects.exists? proj
313
        @publication.projects.delete proj if request.post?
314
      end
315
    else
316
      logger.error { "Cannot remove project from publication list" }
317
    end
318

    
319
    logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
320

    
321
    render(:update) {|page|
322
      page.replace_html "list_projects", :partial => 'list_projects', :id  => @publication
323
    }
324
  end
325

    
326
  def destroy
327
    find_project_by_project_id
328

    
329
    @publication.destroy
330

    
331
    flash[:notice] = "Successfully deleted Publication."
332
    redirect_to :controller => :publications, :action => 'index', :project_id => @project
333
  end
334

    
335
  private
336

    
337
end