Class: Website

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/website.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_host(host) ⇒ Object



76
77
78
79
80
81
82
83
# File 'app/models/website.rb', line 76

def self.find_by_host(host)
  website = nil
  unless host.nil?
    website_host = WebsiteHost.find_by_host(host)
    website = website_host.website unless website_host.nil?
  end
  website
end

.import(file, current_user) ⇒ Object



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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
423
424
425
426
427
428
429
# File 'app/models/website.rb', line 297

def import(file, current_user)
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
  message = ''
  success = true

  file = ActionController::UploadedTempfile.new("uploaded-theme").tap do |f|
    f.puts file.read
    f.original_filename = file.original_filename
    f.read # no idea why we need this here, otherwise the zip can't be opened
  end unless file.path

  entries = []
  setup_hash = nil

  tmp_dir = Website.make_tmp_dir
  Zip::ZipFile.open(file.path) do |zip|
    zip.each do |entry|
      f_path = File.join(tmp_dir.to_s, entry.name)
      FileUtils.mkdir_p(File.dirname(f_path))
      zip.extract(entry, f_path) unless File.exist?(f_path)

      next if entry.name =~ /__MACOSX\//
      if entry.name =~ /setup.yml/
        data = ''
        entry.get_input_stream { |io| data = io.read }
        data = StringIO.new(data) if data.present?
        setup_hash = YAML.load(data)
      else
        type =  entry.name.split('/')[0]
        name = entry.name.split('/').last
        next if name.nil?

        if File.exist?(f_path) and !File.directory?(f_path)
          entry_hash = {:type => type, :name => name, :path => entry.name}
          entries << entry_hash unless name == 'sections' || name == 'articles' || name == 'excerpts' || name == 'documented contents'
          entry_hash[:data] = File.open(f_path,"rb") {|io| io.read}
        end
      end

    end
  end
  entries.uniq!
  FileUtils.rm_rf(tmp_dir.to_s)

  if Website.find_by_internal_identifier(setup_hash[:internal_identifier]).nil?
    website = Website.new(
      :name => setup_hash[:name],
      :title => setup_hash[:title],
      :subtitle => setup_hash[:subtitle],
      :internal_identifier => setup_hash[:internal_identifier]
    )

    #TODO update to handle configurations

    website.save!

    #set default publication published by user
    first_publication = website.published_websites.first
    first_publication.published_by = current_user
    first_publication.save

    begin
      #handle images
      # entries.each do |entry|
      #   puts "entry type '#{entry[:type]}'"
      #   puts "entry name '#{entry[:name]}'"
      #   puts "entry path '#{entry[:path]}'"
      #   puts "entry data #{!entry[:data].blank?}"
      # end
      # puts "------------------"
      setup_hash[:images].each do |image_asset|
        filename = 'images' + image_asset[:path] + '/' + image_asset[:name]
        #puts "image_asset '#{filename}'"
        content = entries.find{|entry| entry[:type] == 'images' and entry[:path] == filename }
        unless content.nil?
          website.add_file(content[:data], File.join(file_support.root, image_asset[:path], image_asset[:name]))
        end
      end

      #handle files
      setup_hash[:files].each do |file_asset|
        filename = 'files' + file_asset[:path] + '/' + file_asset[:name]
        #puts "file_asset '#{filename}'"
        content = entries.find{|entry| entry[:type] == 'files' and entry[:path] == filename }
        unless content.nil?
          website.add_file(content[:data], File.join(file_support.root, file_asset[:path], file_asset[:name]))
        end
      end

      #handle hosts
      setup_hash[:hosts].each do |host|
        website.hosts << WebsiteHost.create(:host => host)
        website.save
      end

      if !setup_hash[:hosts].blank? and !setup_hash[:hosts].empty?
        #set first host as primary host in configuration
        website.configurations.first.update_configuration_item(ConfigurationItemType.find_by_internal_identifier('primary_host'), setup_hash[:hosts].first)
        website.save
      end

      #handle sections
      setup_hash[:sections].each do |section_hash|
        build_section(section_hash, entries, website, current_user)
      end
      website.website_sections.update_paths!

      #handle website_navs
      setup_hash[:website_navs].each do |website_nav_hash|
        website_nav = WebsiteNav.new(:name => website_nav_hash[:name])
        website_nav_hash[:items].each do |item|
          website_nav.website_nav_items << build_menu_item(item)
        end
        website.website_navs << website_nav
      end

      website.publish("Website Imported", current_user)

    rescue Exception=>ex
      Rails.logger.error "#{ex.inspect} #{ex.backtrace}"
      website.destroy unless website.nil?
      raise ex
    end

    website.save
    success = true
  else
    message = 'Website already exists with that internal_identifier'
    success = false
  end

  return success, message
end

.make_tmp_dirObject



291
292
293
294
295
# File 'app/models/website.rb', line 291

def make_tmp_dir
  Pathname.new(Rails.root + "/tmp/website_export/tmp_#{Time.now.to_i.to_s}/").tap do |dir|
    FileUtils.mkdir_p(dir) unless dir.exist?
  end
end

Instance Method Details

#active_publicationObject



103
104
105
# File 'app/models/website.rb', line 103

def active_publication
  self.published_websites.where(:active => true).first
end

#add_party_with_role(party, role_type) ⇒ Object



58
59
60
61
# File 'app/models/website.rb', line 58

def add_party_with_role(party, role_type)
  self.website_party_roles << WebsitePartyRole.create(:party => party, :role_type => role_type)
  self.save
end

#all_section_pathsObject



63
64
65
# File 'app/models/website.rb', line 63

def all_section_paths
  WebsiteSection.select(:path).where(:website_id => self.id).collect{|row| row['path']}
end

#auto_activate_publication?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
# File 'app/models/website.rb', line 107

def auto_activate_publication?
  configuration_item = self.configurations.first.get_item(:auto_active_publications)
  unless configuration_item.nil?
    configuration_item.options.first.value == 'yes'
  else
    false
  end
end

#config_value(config_item_type_iid) ⇒ Object



67
68
69
70
# File 'app/models/website.rb', line 67

def config_value(config_item_type_iid)
  primary_host_config_item_type = ConfigurationItemType.find_by_internal_identifier(config_item_type_iid)
  self.configurations.first.get_configuration_item(primary_host_config_item_type).options.first.value
end

#deactivate_themes!Object



85
86
87
88
89
# File 'app/models/website.rb', line 85

def deactivate_themes!
  themes.each do |theme|
    theme.deactivate!
  end
end

#email_inquiries?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'app/models/website.rb', line 72

def email_inquiries?
  config_value('email_inquiries') == 'yes'
end

#exportObject



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
# File 'app/models/website.rb', line 218

def export
  tmp_dir = Website.make_tmp_dir
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)

  sections_path = Pathname.new(File.join(tmp_dir,'sections'))
  FileUtils.mkdir_p(sections_path) unless sections_path.exist?

  articles_path = Pathname.new(File.join(tmp_dir,'articles'))
  FileUtils.mkdir_p(articles_path) unless articles_path.exist?

  documented_contents_path = Pathname.new(File.join(tmp_dir, 'documented contents'))
  FileUtils.mkdir_p(documented_contents_path) unless documented_contents_path.exist?

  excerpts_path = Pathname.new(File.join(tmp_dir,'excerpts'))
  FileUtils.mkdir_p(excerpts_path) unless excerpts_path.exist?

  image_assets_path = Pathname.new(File.join(tmp_dir,'images'))
  FileUtils.mkdir_p(image_assets_path) unless image_assets_path.exist?

  file_assets_path = Pathname.new(File.join(tmp_dir,'files'))
  FileUtils.mkdir_p(file_assets_path) unless file_assets_path.exist?

  sections.each do |website_section|
    unless website_section.layout.blank?
      File.open(File.join(sections_path,"#{website_section.internal_identifier}.rhtml"), 'wb+') {|f| f.puts(website_section.layout) }
    end
  end

  contents = sections.collect(&:contents).flatten.uniq
  contents.each do |content|
    File.open(File.join(articles_path,"#{content.internal_identifier}.html"), 'wb+') {|f| f.puts(content.body_html) }
    unless content.excerpt_html.blank?
      File.open(File.join(excerpts_path,"#{content.internal_identifier}.html"), 'wb+') {|f| f.puts(content.excerpt_html) }
    end
  end

  online_document_sections.each do |online_documented_section|
    File.open(File.join(documented_contents_path,"#{online_documented_section.internal_identifier}.html"), 'wb+') {|f| f.puts(online_documented_section.documented_item_published_content_html(active_publication)) }
  end

  self.files.where("directory like '%/sites/#{self.iid}/images%'").all.each do |image_asset|
    contents = file_support.get_contents(File.join(file_support.root,image_asset.directory,image_asset.name))
    FileUtils.mkdir_p(File.join(image_assets_path,image_asset.directory))
    File.open(File.join(image_assets_path,image_asset.directory,image_asset.name), 'wb+') {|f| f.puts(contents) }
  end

  self.files.where("directory like '%/#{Rails.application.config.erp_tech_svcs.file_assets_location}/sites/#{self.iid}%'").all.each do |file_asset|
    contents = file_support.get_contents(File.join(file_support.root,file_asset.directory,file_asset.name))
    FileUtils.mkdir_p(File.join(file_assets_path,file_asset.directory))
    File.open(File.join(file_assets_path,file_asset.directory,file_asset.name), 'wb+') {|f| f.puts(contents) }
  end

  files = []
  Dir.glob("#{tmp_dir.to_s}/**/*").each do |entry|
    next if entry =~ /^\./
    path = entry
    entry = entry.gsub(Regexp.new(tmp_dir.to_s), '')
    entry = entry.gsub(Regexp.new(/^\//), '')
    files << {:path => path, :name => entry}
  end

  File.open(tmp_dir + 'setup.yml', 'wb+') { |f| f.puts(export_setup.to_yaml) }

  (tmp_dir + "#{name}.zip").tap do |file_name|
    file_name.unlink if file_name.exist?
    Zip::ZipFile.open(file_name, Zip::ZipFile::CREATE) do |zip|
      files.each { |file| zip.add(file[:name], file[:path]) if File.exists?(file[:path]) }
      zip.add('setup.yml', tmp_dir + 'setup.yml')
    end
  end
end

#export_setupObject



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
# File 'app/models/website.rb', line 181

def export_setup
  setup_hash = {
    :name => name,
    :hosts => hosts.collect(&:host),
    :title => title,
    :subtitle => subtitle,
    :internal_identifier => internal_identifier,
    :sections => [],
    :images => [],
    :files => [],
    :website_navs => []
  }

  #TODO update to handle configurations

  setup_hash[:sections] = sections.positioned.collect do |website_section|
    website_section.build_section_hash
  end

  setup_hash[:website_navs] = website_navs.collect do |website_nav|
    {
      :name => website_nav.name,
      :items => website_nav.items.positioned.map{|website_nav_item| build_menu_item_hash(website_nav_item)}
    }
  end

  self.files.where("directory like '%/sites/#{self.iid}/images%'").all.each do |image_asset|
    setup_hash[:images] << {:path => image_asset.directory, :name => image_asset.name}
  end

  self.files.where("directory like '%/#{Rails.application.config.erp_tech_svcs.file_assets_location}/sites/#{self.iid}%'").all.each do |file_asset|
    setup_hash[:files] << {:path => file_asset.directory, :name => file_asset.name}
  end

  setup_hash
end

#iidObject

creating method because we only want a getter, not a setter for iid



54
55
56
# File 'app/models/website.rb', line 54

def iid
  self.internal_identifier
end

#publish(comment, current_user) ⇒ Object



95
96
97
# File 'app/models/website.rb', line 95

def publish(comment, current_user)
  self.published_websites.last.publish(comment, current_user)
end

#publish_element(comment, element, version, current_user) ⇒ Object



91
92
93
# File 'app/models/website.rb', line 91

def publish_element(comment, element, version, current_user)
  self.published_websites.last.publish_element(comment, element, version, current_user)
end

#publish_on_save?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
# File 'app/models/website.rb', line 116

def publish_on_save?
  configuration_item = self.configurations.first.get_item(:publish_on_save)
  unless configuration_item.nil?
    configuration_item.options.first.value == 'yes'
  else
    false
  end
end

#remove_sites_directoryObject



141
142
143
144
145
146
147
148
# File 'app/models/website.rb', line 141

def remove_sites_directory
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
  begin
    file_support.delete_file(File.join(file_support.root,"sites/#{self.iid}"), :force => true)
  rescue
    #do nothing it may not exist
  end
end

#remove_website_roleObject



150
151
152
# File 'app/models/website.rb', line 150

def remove_website_role
  role.destroy if role
end

#roleObject



125
126
127
# File 'app/models/website.rb', line 125

def role
  SecurityRole.iid(website_role_iid)
end

#set_publication_version(version, current_user) ⇒ Object



99
100
101
# File 'app/models/website.rb', line 99

def set_publication_version(version, current_user)
  PublishedWebsite.activate(self, version, current_user)
end

#setup_default_pagesObject



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
# File 'app/models/website.rb', line 154

def setup_default_pages
  # create default sections for each widget using widget layout
  widget_classes = [
    ::Widgets::ContactUs::Base,
    ::Widgets::Search::Base,
    ::Widgets::ManageProfile::Base,
    ::Widgets::Login::Base,
    ::Widgets::Signup::Base,
    ::Widgets::ResetPassword::Base
  ]
  profile_page = nil
  widget_classes.each do |widget_class|
    website_section = WebsiteSection.new
    website_section.title = widget_class.title
    website_section.in_menu = true unless ["Login", "Sign Up", "Reset Password"].include?(widget_class.title)
    website_section.layout = widget_class.base_layout
    website_section.save

    profile_page = website_section if widget_class.title == 'Manage Profile'

    self.website_sections << website_section
  end
  self.save
  self.website_sections.update_paths!
  profile_page.secure unless profile_page.nil?
end

#setup_websiteObject



129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/website.rb', line 129

def setup_website
  PublishedWebsite.create(:website => self, :version => 0, :active => true, :comment => 'New Site Created')
  SecurityRole.create(:description => "Website #{self.title}", :internal_identifier => website_role_iid) if self.role.nil?
  configuration = ::Configuration.find_template('default_website_configuration').clone(true)
  configuration.description = "Website #{self.name} Configuration"
  configuration.internal_identifier = configuration.description.underscore
  configuration.update_configuration_item(ConfigurationItemType.find_by_internal_identifier('login_url'), '/login')
  configuration.update_configuration_item(ConfigurationItemType.find_by_internal_identifier('homepage_url'), '/home')
  self.configurations << configuration
  self.save
end

#to_labelObject



49
50
51
# File 'app/models/website.rb', line 49

def to_label
  self.name
end

#website_role_iidObject



518
519
520
# File 'app/models/website.rb', line 518

def website_role_iid
  "website_#{self.iid}_access"
end