Class: Admin::DashboardController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/dashboard_controller.rb

Constant Summary collapse

@@to_search =
[ "Page", "User", "Help", "Category", "Asset", "TopicPost", "TopicThread", "FormSubmission"]

Instance Method Summary collapse

Instance Method Details

#activityObject



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'app/controllers/admin/dashboard_controller.rb', line 424

def activity
  @activity = Activity

  if params[:for]
    @activity = @activity.where(Activity.arel_table[:name].matches("%#{params[:for]}%").or(Activity.arel_table[:name].matches("%#{params[:for]}%"))).sys(_sid)
  else
  @activity = @activity.includes(:user)
  end
  @activity = @activity.where(["category = ?", params[:cat]]) if params[:cat]
  @activity = @activity.sys(_sid)
  @activity = @activity.order("created_at desc").page(params[:page]).per(100)
  

  if request.xhr?
    render :partial=>"activity_list"
  else
    render "activity", :layout=>"cms"
  end
end

#build_systemObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/admin/dashboard_controller.rb', line 112

def build_system
  raise Exception("permissions") unless can?(:dashboard, :super)

  if request.post?  
    if params[:password] != params[:password_confirm]
      flash[:notice] = "Passwords don't match"
      return
    end

    if params[:password].length < 8
      flash[:notice] = "This password controls the whole system - it must be at least 8 characters"
      return
    end

    if params[:name].strip.length < 1
      flash[:notice] = "You must enter a system name"
      return
    end

    if params[:domain_name].strip.length < 1
      flash[:notice] = "You must enter a domain name"
      return
    end
  
    params[:full_name] ||= params[:name]

      system = System.new(:name=>params[:name])
      system.save
      SystemIdentifier.create(:system_id=>system.id, :ident_type=>"hostname", :ident_value=>params[:domain_name])
      user = User.create(:system_id=>system.id, :email=>params[:email], :password=>params[:password])
      role = Role.create(:system_id=>system.id, :name=>"SuperAdmin")
      user.roles << role
      Preference.get!(system.id, 'app_name', params[:name], nil)
      Preference.get!(system.id, 'site_name', params[:full_name], nil)
      Preference.get!(system.id, 'host', "http://#{params[:domain_name]}")
      redirect_to "/admin/integrity?system_id=#{system.id}"
  end
end

#errorObject



44
45
46
# File 'app/controllers/admin/dashboard_controller.rb', line 44

def error
 raise Exception("Simulated error")
end

#error_message(s) ⇒ Object



466
467
468
# File 'app/controllers/admin/dashboard_controller.rb', line 466

def error_message(s)
  "<span class='check_error'>#{s}</span>".html_safe
end

#eventObject



97
98
99
100
# File 'app/controllers/admin/dashboard_controller.rb', line 97

def event
  raise Exception("permissions") unless can?(:dashboard, :super)
  @event = Event.where(:id=>params[:id]).first
end

#eventsObject



88
89
90
91
92
93
94
95
# File 'app/controllers/admin/dashboard_controller.rb', line 88

def events
  raise Exception("permissions") unless can?(:dashboard, :super)

  if request.delete?
    Event.delete_all
  end
  @events = Event.order("created_at desc").page(params[:page]).per(50)
end

#helpObject



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'app/controllers/admin/dashboard_controller.rb', line 444

def help 
  if params[:search]
    search_for = params[:search]

    search = Tire.search "kit_#{app_name}_helps" do
      query do
        string search_for, :fields=>["name", "body", "path"]
      end
    end
    @helps = search.results
    render "helps"
    return
  else      
   params[:url] = "index" unless params[:url]
   @help = Help.where(:path=>params[:url]).first
   if @help==nil
     render "help_not_found"
     return
   end
  end 
end

#indexObject



102
103
# File 'app/controllers/admin/dashboard_controller.rb', line 102

def index
end

#integrityObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'app/controllers/admin/dashboard_controller.rb', line 151

def integrity
  raise Exception("permissions") unless can?(:dashboard, :super)

  [ "js", "css", "other" ].each do |d|
    dir = File.join(Rails.root, "public", "kit", d)
    Dir::mkdir(dir) unless File.exists?(dir)
  end 
 
  @checks = []

  sid = params[:system_id]

  HtmlAsset.sys(sid).each do |ha|
    HtmlAsset.fetch(sid, ha.name, ha.file_type)
  end


  @system_id = sid
  prefs = {
    date_time_format: "%H:%M %d-%m-%y",
    app_name: System.find(sid).name.downcase,
    spam_points_to_ban_user: 5,
    error_layout: "application",
    notify: "[email protected]"
  }

  prefs.each do |name, value|
    @checks << "Check preference '#{name}' exists and creating it if not"
    Preference.get!(sid, name.to_s, value, nil)
  end

  if Topic.sys(sid).count == 0 
    @checks << warning_message('There are no forum topics')
  end

  Topic.sys(sid).find_each do |topic|
    @checks << "Setting thread count and last post on topic '#{topic.name}'"
    topic.thread_count = topic.topic_threads.count
    last_post = TopicPost.joins(:topic_thread).order("topic_posts.id desc").where("topic_threads.topic_id = #{topic.id}").limit(1).first
    topic.last_post_at = last_post.updated_at rescue nil
    topic.save
  end

  if params[:forums]
    @checks << "Setting post count and last post info on threads"

    Topic.find_each do |topic|
      topic.last_thread_id = nil
      topic.post_count = 0
      topic.last_post_at = nil
      topic.post_count = 0
      topic.save
    end

    last_post_per_topic = {}

    TopicThread.find_each do |thread|
      posts = thread.topic_posts.order("topic_posts.created_at")
      n = 0

      posts.reverse.each do |post|
        if post.is_visible==1
          n += 1 
          post.post_number = n
        else
          post.post_number = 0
        end
        begin
        post.save
        rescue 
        end
      end 
      last_post = posts.first
      first_post = posts.last
      thread.post_count = n 
      thread.last_post_at = last_post.created_at
      thread.last_post_by_user_id = last_post.created_by_user_id
      thread.created_by_user_display_name = thread.created_by_user.display_name rescue ''
      thread.last_post_by_user_display_name = last_post.created_by_user.display_name

      thread.first_post_id = first_post.id
      thread.save
      thread.topic.thread_count += 1 rescue 1
      thread.topic.post_count += n rescue 1
        thread.topic.last_thread_id = thread.id
      if last_post_per_topic[thread.topic_id]==nil || last_post.id > last_post_per_topic[thread.topic_id]
        last_post_per_topic[thread.topic_id] = last_post.id
      end
      thread.topic.save
    end

    last_post_per_topic.each do |topic_id, last_post_id|
      topic = Topic.find(topic_id)
      next unless topic
      topic.last_post_id = last_post_id
      topic.last_post_at = TopicPost.find(last_post_id).created_at
      topic.save
    end


    @checks << "Forum votes"
    post_scores = {}
    user_scores = {}
    user_votes = {}
    TopicPostVote.find_each do |topic_post_vote|
      post_scores[topic_post_vote.topic_post_id] ||= 0
      post_scores[topic_post_vote.topic_post_id] += topic_post_vote.score
      user_scores[topic_post_vote.topic_post.created_by_user_id] ||= 0
      user_scores[topic_post_vote.topic_post.created_by_user_id] += topic_post_vote.score
      user_votes[topic_post_vote.topic_post.created_by_user_id] ||= 0
      user_votes[topic_post_vote.topic_post.created_by_user_id] += 1
    end 
    post_scores.each do |k,v|
      TopicPost.connection.execute("update topic_posts set score = #{v} where id = #{k}")
    end
    user_scores.each do |k,v|
      User.connection.execute("update users set forum_points = #{v}, forum_votes = #{user_votes[k]} where id = #{k}")
    end
  end

  @checks << "Checking statuses"
  i = 0
  ["Editing", "For review", "Ready for publication", "Published", "Withdrawn", "Stub"].each do |status|
    i += 1
    if Status.sys(sid).where(:name=>status).count==0
      @checks << "Creating '#{status}'"
      Status.create(:name=>status, :order_by=>i, :is_published=>(status=="Published"), :is_default=>(status=='Editing'), :is_stub=>(status=='Stub'), :system_id=>sid)
    end
  end
  @checks << "Check editor-styles block exists and creating if not"
  Block.ensure(sid, "editor-styles", "<div class='mercury-select-options'><div class='red' data-class='red'>Red Text</div></div>")
  @checks << "Check editor-blocks block exists and creating if not"
  Block.ensure(sid, "editor-blocks", "<div class='mercury-select-options'><h1 data-tag='h1'>Heading 1 <span>&lt;&gt;</span></h1><div data-tag='p'>Paragraph <span>&lt;&gt;</span></div>")

  @home_page = Pagebase.sys(sid).where(:id=>Preference.getCached(sid, "home_page")).first rescue nil

  @checks << "Check default layout"
  if Layout.sys(sid).count < 1
    @checks << "Creating default layout"
    Layout.create_default(sid, current_user.id)
  end
  
  @checks << "Check default stylesheet"
  if HtmlAsset.sys(sid).count < 1
    @checks << "Creating default stylesheet"
    HtmlAsset.create_default(sid, current_user.id, 'css')
  end
 
  @checks << "Check default page template"
  if PageTemplate.sys(sid).count < 1
    @checks << "Creating default page template"
    PageTemplate.create_default(sid, current_user.id)
  else
    if PageTemplate.sys(sid).where(:is_default=>1).count<1
      @checks << "Making first template the default"
      PageTemplate.order(:created_at).first.update_attributes(:is_default=>1)
    end
  end
 
   
  @checks << "Check system identifier"
  if SystemIdentifier.sys(sid).count < 1
    @checks << error_message("No system identifier exists - nobody will be able to access this system")
  end

  @checks << "Check default category"
  if Category.sys(sid).count < 1
    @checks << "Creating default category"
    Category.create_default(sid)
  end

  [ "Moderator", "Editor", "Designer" ].each do |role|
    @checks << "Check role #{role}"
    if Role.sys(sid).where(:name=>role).count==0
      @checks << "Creating role #{role}"
      Role.create(:system_id=>sid, :name=>role)
    end
  end

end

#logfileObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/admin/dashboard_controller.rb', line 71

def logfile
  filename = Rails.root.join('log', Rails.env + '.log')

  if params[:grep].not_blank?
    @output = `grep "#{params[:grep]}" #{filename}`
  else
    @lines = []
    File.open(filename) do |file|
      @file = file
      @file.extend(File::Tail)
      @file.backward((params[:lines] || "500").to_i )
      render "logfile"
    end
  end

end

#maintenanceObject



52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/admin/dashboard_controller.rb', line 52

def maintenance
  message = params[:message]
  if message.not_blank?
    Preference.set(_sid, "down_for_maintenance_message", message)
  else
    Preference.delete(_sid, "down_for_maintenance_message")
  end

  redirect_to "/admin/system", :notice=>"Message set"
end

#plumbObject



48
49
50
# File 'app/controllers/admin/dashboard_controller.rb', line 48

def plumb
  
end

#recent_activityObject



359
360
361
# File 'app/controllers/admin/dashboard_controller.rb', line 359

def recent_activity
  render :partial=>"recent_activity"
end

#recent_pagesObject



355
356
357
# File 'app/controllers/admin/dashboard_controller.rb', line 355

def recent_pages
  render :partial=>"recent_pages"
end

#reindexObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'app/controllers/admin/dashboard_controller.rb', line 365

def reindex
  @@to_search.each do |model|
    modelk = Kernel.const_get(model)

    modelk.index.delete rescue nil
    modelk.create_elasticsearch_index

    if params[:slow]
      modelk.find_each do |record|
        logger.info "Importing #{model} #{record.id}"
        modelk.index.store  record
      end
    else
      modelk.import :per_page=>1000
    end
  end 

  flash[:notice] = "Reindex complete"
  redirect_to "/db"
end

#salesforceObject



63
64
65
66
67
68
69
# File 'app/controllers/admin/dashboard_controller.rb', line 63

def salesforce
  client = Databasedotcom::Client.new :client_id => "3MVG99qusVZJwhsnYBQ82sOeClHKBKrjEb5GTvhWfI_QwP72acY3t0TkXkjjFp7SadN3j7uywOrS5S8fAMYhX", :client_secret => "6488955455249765126"

  client.authenticate :username => "[email protected]", :password => "sales1016c0S0Bc5EXrX2dRnRs8vb2bOm"

  @cats = client.materialize("Cat__c") 
end

#searchObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'app/controllers/admin/dashboard_controller.rb', line 386

def search
  search_for = params[:for]
  indexes = []
  search_fields = []

  @@to_search.each do |model|
    if params["include_#{model.pluralize.downcase}".to_sym]
      indexes << "kit_#{app_name.downcase}_" + model.tableize
      KitIndexed.indexed_columns(model).collect { |ic| 
        next if ic[:include_in_all]==false
        next if ic[:index]==:not_analyzed
        search_fields << ic[:name] 
      }
    end
  end
  
  if params[:include_pages]  
    search_fields << "old_content" if params[:include_old]
    search_fields << "draft_content" if params[:include_draft]
    search_fields << "autosave_content" if params[:include_autosave]
  end
  
  system_id = _sid

  search = Tire.search indexes.join(',') do
    query do
      string search_for, :fields=>search_fields.uniq
    end
    filter :term, :system_id=>system_id

  end
  logger.debug "SEARCH: #{indexes.join(',')} #{search.to_json}"

  @results = search.results
  logger.debug "RESULTS: #{@results.size}"
  @to_search = @@to_search
end

#sysadminObject



332
333
# File 'app/controllers/admin/dashboard_controller.rb', line 332

def sysadmin
end

#systemObject



105
106
107
108
109
110
# File 'app/controllers/admin/dashboard_controller.rb', line 105

def system
  raise Exception("permissions") unless can?(:dashboard, :super)

  @preferences = Preference.sys(_sid).where("user_id is null").order(:name)
  @preferences = @preferences.all
end

#system_settingsObject



345
346
347
348
349
350
351
352
353
# File 'app/controllers/admin/dashboard_controller.rb', line 345

def system_settings
  raise Exception("permissions") unless can?(:dashboard, :super)
  
  if ["eu_cookies", "mobile_dif_by_default"].include?(params[:attr])
    Preference.set(_sid, params[:attr], params[:value], nil)
  end

  redirect_to "/admin/system"
end

#update_preferenceObject



335
336
337
338
339
340
341
# File 'app/controllers/admin/dashboard_controller.rb', line 335

def update_preference
  raise Exception("permissions") unless can?(:dashboard, :super)

  Preference.set(_sid, params[:attr], params[:preference][:value], nil)
    
  respond_with_bip(Preference.where(:name=>params[:attr]).sys(_sid).where("user_id is null").first)
end

#user_commentObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/admin/dashboard_controller.rb', line 4

def user_comment
  comment = Comment.sys(_sid).where(:id=>params[:id]).first

  if comment
    if params[:delete]
      comment.destroy
      render :js=>"$('tr.comment_#{comment.id}').remove();"
    else
      comment.update_attributes(params[:comment])
      render :js=>"updated(#{comment.id}, #{comment.is_moderated}, #{comment.is_visible});"
    end
    return
  end

  render :js=>""
end

#user_commentsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/admin/dashboard_controller.rb', line 21

def user_comments
  if params.size==2
    params[:unmoderated] = "1"
    params[:visible] = "1"
    params[:invisible] = "1"
  end

  @comments = Comment.sys(_sid).order("created_at desc")
  conditions = []
  conditions << "is_moderated = -1"
  conditions << "is_moderated = 1" if params[:moderated]
  conditions << "is_moderated = 0" if params[:unmoderated]
  @comments = @comments.where(conditions.join(" or "))

  conditions = []
  conditions << "is_visible = -1"
  conditions << "is_visible = 1" if params[:visible]
  conditions << "is_visible = 0" if params[:invisible]
  @comments = @comments.where(conditions.join(" or "))

  @comments = @comments.page(params[:page]).per(params[:per] || 50)
end

#warning_message(s) ⇒ Object



470
471
472
# File 'app/controllers/admin/dashboard_controller.rb', line 470

def warning_message(s)
  "<span class='check_warning'>#{s}</span>".html_safe
end