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

Methods inherited from AppController

#available_roles

Instance Method Details

#activate_publicationObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 78

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



16
17
18
19
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
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 16

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



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

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



181
182
183
184
185
186
187
188
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 181

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

#exporttemplateObject



190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 190

def exporttemplate
  zip_path = @website.export_template
  if zip_path
    begin
      send_file(zip_path, :stream => false)
    rescue Exception => 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

#has_active_themeObject



226
227
228
229
230
231
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 226

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



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

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
ensure
  FileUtils.rm_r File.dirname(zip_path) rescue nil
end

#importtemplateObject



216
217
218
219
220
221
222
223
224
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 216

def importtemplate
  website, message = Website.import_template_director(params[:website_data], User.first)

  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
end

#indexObject



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

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

#newObject



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
150
151
152
153
154
155
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 114

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.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)

        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



102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 102

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



90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 90

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 157

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



46
47
48
49
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
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 46

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