Class: Knitkit::ErpApp::Desktop::WebsiteController

Inherits:
AppController
  • Object
show all
Defined in:
app/controllers/knitkit/erp_app/desktop/website_controller.rb

Constant Summary collapse

IGNORED_PARAMS =
%w{action controller id}

Constants inherited from AppController

AppController::KNIT_KIT_ROOT

Instance Method Summary collapse

Instance Method Details

#activate_publicationObject



82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 82

def activate_publication
  begin
    current_user.with_capability('activate', 'Website') do
      @website.set_publication_version(params[:version].to_f, current_user)

      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#build_content_treeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 20

def build_content_tree
  nodes = []

  if @website
    if params[:record_type].blank?
      @website.website_sections.positioned.each do |website_section|
        nodes << build_section_hash(website_section)
      end
    else
      case params[:record_type]
        when 'WebsiteSection'
          website_section = WebsiteSection.find(params[:record_id])

          # get child sections

          nodes = website_section.positioned_children.map { |child| build_section_hash(child) }

          # get child articles

          website_section.website_section_contents.order('position').each do |website_section_content|
            nodes << build_article_hash(website_section_content, @website, website_section.is_blog?)
          end

        else
          raise 'Unknown Node Type'
      end
    end
  end

  render :json => nodes
end

#deleteObject



185
186
187
188
189
190
191
192
193
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 185

def delete
  begin
    current_user.with_capability('delete', 'Website') do
      render :json => @website.destroy ? {:success => true} : {:success => false}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#exportObject



195
196
197
198
199
200
201
202
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 195

def export
  zip_path = @website.export
  begin
    send_file(zip_path.to_s, :stream => false)
  rescue StandardError => ex
    raise "Error sending #{zip_path} file"
  end
end

#exporttemplateObject



204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 204

def exporttemplate
  zip_path = @website.export_template
  if zip_path
    begin
      send_file(zip_path, :stream => false)
    rescue StandardError => ex
      raise "Error sending file. Make sure you have a website and an active theme."
    end
  else
    render :inline => {:success => false, :message => 'test'}.to_json
  end
end

#get_current_hostObject



250
251
252
253
254
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 250

def get_current_host
  current_host = request.host_with_port
  existing_website_host = WebsiteHost.find_by_host(current_host)
  render :json => {:success => !existing_website_host.present?, :host => current_host}
end

#has_active_themeObject



243
244
245
246
247
248
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 243

def has_active_theme
  !!Website.find_by_id(params[:website_id]).themes.active.first ? response = 'true' : response = 'false'
  render :json => {:success => true, :message => response}

  # example found in knitkit module.js

end

#importObject

TODO add role restriction to this



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 218

def import
  website, message = Website.import(params[:website_data], current_user)

  if website
    render :inline => {:success => true, :website => website.to_hash(:only => [:id, :name])}.to_json
  else
    render :inline => {:success => false, :message => message}.to_json
  end
  WebsitePartyRole.create(website: website,
                          party: current_user.party.dba_organization,
                          role_type: RoleType.iid('dba_org'))
ensure
  FileUtils.rm_r File.dirname(zip_path) rescue nil
end

#import_templateObject



233
234
235
236
237
238
239
240
241
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 233

def import_template
  result = Website.import_template(params[:website_data], current_user)

  if result[:success]
    render :inline => {:success => true, :website => result[:website].to_hash(:only => [:id, :name])}.to_json
  else
    render :inline => {:success => false, :message => result[:message]}.to_json
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 10

def index
  websites = Website.joins(:website_party_roles)
                 .where('website_party_roles.party_id = ?', current_user.party.dba_organization.id)
                 .where('website_party_roles.role_type_id = ?', RoleType.iid('dba_org'))

  render :json => {:sites => websites.all.collect { |item| item.to_hash(:only => [:id, :name, :title, :subtitle],
                                                                        :configuration_id => item.configurations.first.id,
                                                                        :url => "#{request.protocol}#{item.config_value('primary_host')}") }}
end

#newObject



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 118

def new
  begin
    Website.transaction do
      current_user.with_capability('create', 'Website') do
        website = Website.new
        website.subtitle = params[:subtitle]
        website.title = params[:title]
        website.name = params[:name]

        # create homepage

        website_section = WebsiteSection.new
        website_section.title = "Home"
        website_section.in_menu = true
        website.website_sections << website_section

        website.save
        website.setup_default_pages

        #set default publication published by user

        first_publication = website.published_websites.first
        first_publication.published_by = current_user
        first_publication.save

        website_host = WebsiteHost.find_by_host(params[:host])
        if website_host
          website_name = website_host.website.name
          raise "Host #{website_host.host} already used by #{website_name}"
        end

        website.hosts << WebsiteHost.create(:host => params[:host])
        website.configurations.first.update_configuration_item(ConfigurationItemType.find_by_internal_identifier('primary_host'), params[:host])
        website.save

        website.publish("Publish Default Sections", current_user)

        PublishedWebsite.activate(website, 1, current_user)

        # set the currents users dba_org as the dba_org for this website

        WebsitePartyRole.create(website: website,
                                party: current_user.party.dba_organization,
                                role_type: RoleType.iid('dba_org'))

        render :json => {:success => true, :website => website.to_hash(:only => [:id, :name],
                                                                       :configuration_id => website.configurations.first.id,
                                                                       :url => "http://#{website.config_value('primary_host')}")}
      end
    end
  rescue => ex
    Rails.logger.error("#{ex.message} + #{ex.backtrace.join("\n")}")
    render :json => {:success => false, :message => ex.message}
  end
end

#publishObject



106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 106

def publish
  begin
    current_user.with_capability('publish', 'Website') do
      @website.publish(params[:comment], current_user)

      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#set_viewing_versionObject



94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 94

def set_viewing_version
  if session[:website_version].blank?
    session[:website_version] = []
    session[:website_version] << {:website_id => @website.id, :version => params[:version]}
  else
    session[:website_version].delete_if { |item| item[:website_id] == @website.id }
    session[:website_version] << {:website_id => @website.id, :version => params[:version]}
  end

  render :json => {:success => true}
end

#updateObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 171

def update
  begin
    current_user.with_capability('edit', 'Website') do
      @website.name = params[:name]
      @website.title = params[:title]
      @website.subtitle = params[:subtitle]

      render :json => @website.save ? {:success => true} : {:success => false}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#website_publicationsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 50

def website_publications
  sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
  sort = sort_hash[:property] || 'version'
  dir = sort_hash[:direction] || 'DESC'
  limit = params[:limit] || 9
  start = params[:start] || 0

  published_websites = @website.published_websites.order("#{sort} #{dir}").limit(limit).offset(start)

  #set site_version. User can view different versions. Check if they are viewing another version

  site_version = @website.active_publication.version
  if !session[:website_version].blank? && !session[:website_version].empty?
    site_version_hash = session[:website_version].find { |item| item[:website_id] == @website.id }
    site_version = site_version_hash[:version].to_f unless site_version_hash.nil?
  end

  PublishedWebsite.class_exec(site_version) do
    cattr_accessor :site_version
    self.site_version = site_version

    def viewing
      self.version == self.site_version
    end
  end

  render :inline => "{\"success\":true, \"results\":#{published_websites.count},
                    \"totalCount\":#{@website.published_websites.count},
                    \"data\":#{published_websites.to_json(
             :only => [:comment, :id, :version, :created_at, :active],
             :methods => [:viewing, :published_by_username])} }"
end