Revision 1078:b9e34a051f82 vendor/plugins/redmine_bibliography/app/controllers

View differences:

vendor/plugins/redmine_bibliography/app/controllers/publications_controller.rb
3 3

  
4 4
class PublicationsController < ApplicationController
5 5
  unloadable
6
  
6

  
7 7
  model_object Publication
8
  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 ]  
8
  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 ]
9 9
  before_filter :find_project_by_project_id, :authorize, :only => [ :edit, :new, :update, :create ]
10
    
10

  
11 11
  def new
12 12
    find_project_by_project_id
13 13
    @publication = Publication.new
14
    
14

  
15 15
    # we'll always want a new publication to have its bibtex entry
16 16
    @publication.build_bibtex_entry
17
    
17

  
18 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.partition('@')[2]})", "#{User.current.class.to_s}_#{User.current.id.to_s}"]]
21

  
22

  
23 21
  end
24 22

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

  
28 26
    @author_options = []
29 27

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

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

  
36 34
      flash[:notice] = "Successfully created publication."
37 35
      redirect_to :action => :show, :id => @publication, :project_id => @project
38 36
    else
......
52 50

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

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

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

  
65 63
  def get_bibtex_required_fields
66 64

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

  
71 69
    respond_to do |format|
72 70
      format.js {
73
        render(:update) {|page|       
71
        render(:update) {|page|
74 72
          if params[:value].empty?
75 73
            page << "hideOnLoad();"
76 74
          else
......
78 76
          end
79 77
        }
80 78
      }
81
    
79

  
82 80
    end
83 81
  end
84 82

  
......
92 90
    end
93 91
  end
94 92

  
95
  def edit   
93
  def edit
96 94
    find_project_by_project_id unless params[:project_id].nil?
97
    
95

  
98 96
    @edit_view = true;
99 97
    @publication = Publication.find(params[:id])
100 98
    @selected_bibtex_entry_type_id = @publication.bibtex_entry.entry_type
101 99

  
102
    @author_options = []  
103
    
104
    @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)    
100
    @author_options = []
101

  
102
    @bibtype_fields = BibtexEntryType.fields(@selected_bibtex_entry_type_id)
105 103
  end
106 104

  
107
  def update    
108
    @publication = Publication.find(params[:id])        
109

  
105
  def update
106
    @publication = Publication.find(params[:id])
110 107
    @author_options = []
111 108

  
112
    logger.error { "INSIDE THE UPDATE ACTION IN THE PUBLICATION CONTROLLER" }
113

  
114 109
    if @publication.update_attributes(params[:publication])
115 110
      flash[:notice] = "Successfully updated Publication."
116 111

  
112
      # expires the previosly cached entries
113
      Rails.cache.delete "publication-#{@publication.id}-ieee"
114
      Rails.cache.delete "publication-#{@publication.id}-bibtex"
115

  
117 116
      if !params[:project_id].nil?
118 117
        redirect_to :action => :show, :id => @publication, :project_id => params[:project_id]
119 118
      else
......
121 120
      end
122 121
    else
123 122
      render :action => 'edit'
124
    end   
123
    end
125 124
  end
126 125

  
126

  
127 127
  def show
128 128
    find_project_by_project_id unless params[:project_id].nil?
129
        
129

  
130 130
    if @publication.nil?
131 131
      @publications = Publication.all
132 132
      render "index", :alert => 'The publication was not found!'
......
142 142
    return authors_entry.split(" and ")
143 143
  end
144 144

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

  
......
155 155
        create_bibtex_entry d
156 156
      end
157 157
    end
158
  end 
158
  end
159 159

  
160
  def create_bibtex_entry(d)        
160
  def create_bibtex_entry(d)
161 161
    @publication = Publication.new
162
    @bentry = BibtexEntry.new        
162
    @bentry = BibtexEntry.new
163 163
    authors = []
164 164
    institution = ""
165 165
    email = ""
......
177 177
      else
178 178
        @bentry[field] = d[field]
179 179
      end
180
    end 
180
    end
181 181

  
182 182
    @publication.bibtex_entry = @bentry
183 183
    @publication.save
184 184

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

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

  
199 200
      author.authorships.create!(
200
        :publication => @publication,
201
        :institution => institution,
202
        :email => email,
203
        :order => idx)
201
      :publication => @publication,
202
      :institution => institution,
203
      :email => email,
204
      :order => idx)
204 205
    end
205 206
  end
206 207

  
......
211 212

  
212 213
  def import
213 214
    @publication = Publication.new
214
    
215
    
215

  
216

  
216 217
  end
217
  
218

  
218 219
  def autocomplete_for_project
219 220
    @publication = Publication.find(params[:id])
220
        
221
    @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects            
221

  
222
    @projects = Project.active.like(params[:q]).find(:all, :limit => 100) - @publication.projects
222 223
    logger.debug "Query for \"#{params[:q]}\" returned \"#{@projects.size}\" results"
223 224
    render :layout => false
224 225
  end
225 226

  
226
  def autocomplete_for_author    
227
  def autocomplete_for_author
227 228
    @results = []
228
    
229

  
229 230
    object_id = params[:object_id]
230 231
    @object_name = "publications[authorships_attributes][#{object_id}][search_results]"
231
        
232

  
232 233
    # cc 20110909 -- revert to like instead of like_unique -- see #289
233 234
    authorships_list = Authorship.like(params[:q]).find(:all, :limit => 100)
234 235
    users_list = User.active.like(params[:q]).find(:all, :limit => 100)
235 236

  
236 237
    logger.debug "Query for \"#{params[:q]}\" returned \"#{authorships_list.size}\" authorships and \"#{users_list.size}\" users"
237
    
238

  
238 239
    @results = users_list
239 240

  
240
    # TODO: can be optimized…    
241
    authorships_list.each do |authorship|      
241
    # TODO: can be optimized…
242
    authorships_list.each do |authorship|
242 243
      flag = true
243
      
244

  
244 245
      users_list.each do |user|
245 246
        if authorship.name == user.name && authorship.email == user.mail && authorship.institution == user.institution
246 247
          Rails.logger.debug { "Rejecting Authorship #{authorship.id}" }
......
252 253
      @results << authorship if flag
253 254
    end
254 255

  
255
    render :layout => false    
256
    render :layout => false
256 257
  end
257
  
258
  
258

  
259 259
  def get_user_info
260 260
    object_id = params[:object_id]
261 261
    value = params[:value]
......
266 266
    name_field = "publication_authorships_attributes_#{object_id}_name_on_paper".to_sym
267 267
    email_field = "publication_authorships_attributes_#{object_id}_email".to_sym
268 268
    institution_field = "publication_authorships_attributes_#{object_id}_institution".to_sym
269
    
269

  
270 270
    yes_radio = "publication_authorships_attributes_#{object_id}_identify_author_yes".to_sym
271
    
271

  
272 272
    respond_to do |format|
273 273
      format.js {
274
        render(:update) {|page| 
274
        render(:update) {|page|
275 275
          page[name_field].value = item.name
276 276
          page[email_field].value = item.mail
277 277
          page[institution_field].value = item.institution
......
293 293
  end
294 294

  
295 295
  def add_project
296
    @projects = Project.find(params[:publication][:project_ids])    
296
    @projects = Project.find(params[:publication][:project_ids])
297 297
    @publication.projects << @projects
298
    @project = Project.find(params[:project_id])    
299
    
300
    # TODO luisf should also respond to HTML??? 
298
    @project = Project.find(params[:project_id])
299

  
300
    # TODO luisf should also respond to HTML???
301 301
    respond_to do |format|
302 302
      format.html { redirect_to :back }
303
      format.js { 
304
        render(:update) {|page| 
305
          page[:add_project_form].reset          
303
      format.js {
304
        render(:update) {|page|
305
          page[:add_project_form].reset
306 306
          page.replace_html :list_projects, :partial => 'list_projects'
307 307
        }
308 308
      }
309 309
    end
310 310
  end
311
  
312
  
311

  
312

  
313 313
  def remove_project
314 314
    @project = Project.find(params[:project_id])
315 315
    proj = Project.find(params[:remove_project_id])
......
319 319
        @publication.projects.delete proj if request.post?
320 320
      end
321 321
    else
322
      logger.error { "Cannot remove project from publication list" }      
322
      logger.error { "Cannot remove project from publication list" }
323 323
    end
324
    
324

  
325 325
    logger.error { "CURRENT project name#{proj.name} and wanna delete #{@project.name}" }
326
        
327
    render(:update) {|page| 
326

  
327
    render(:update) {|page|
328 328
      page.replace_html "list_projects", :partial => 'list_projects', :id  => @publication
329
    }    
329
    }
330 330
  end
331
    
331

  
332 332
  def destroy
333 333
    find_project_by_project_id
334
    
334

  
335 335
    @publication.destroy
336
        
336

  
337 337
    flash[:notice] = "Successfully deleted Publication."
338 338
    redirect_to :controller => :publications, :action => 'index', :project_id => @project
339 339
  end

Also available in: Unified diff