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, #websites

Instance Method Details

#activate_publicationObject



44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 44

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

#add_hostObject



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

def add_host
  begin
    current_user.with_capability('create', 'WebsiteHost') do
      website = Website.find(params[:id])
      website_host = WebsiteHost.create(:host => params[:host])
      website.hosts << website_host
      website.save

      render :json => {
        :success => true,
        :node => {
          :text => website_host.attributes['host'],
          :websiteHostId => website_host.id,
          :host => website_host.attributes['host'],
          :iconCls => 'icon-globe',
          :url => "http://#{website_host.attributes['host']}",
          :isHost => true,
          :leaf => true,
          :children => []}
      }
    end
  rescue Exception => ex
    Rails.logger.error("#{ex.message} + #{ex.backtrace}")
    render :json => {:success => false, :message => ex.message}
  end
end

#deleteObject



136
137
138
139
140
141
142
143
144
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 136

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

#delete_hostObject



201
202
203
204
205
206
207
208
209
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 201

def delete_host
  begin
    current_user.with_capability('delete', 'WebsiteHost') do
      render :json => WebsiteHost.destroy(params[:id]) ? {:success => true} : {:success => false}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex
    render :json => {:success => false, :message => ex.message}
  end
end

#exportObject



146
147
148
149
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 146

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

#importObject

TODO add role restriction to this



152
153
154
155
156
157
158
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 152

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

  render :inline => {:success => result, :message => message}.to_json
ensure
  FileUtils.rm_r File.dirname(zip_path) rescue nil
end

#indexObject



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

def index
  render :json => {:sites => Website.all}
end

#newObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 80

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}
      end
    end
  rescue Exception => ex
    Rails.logger.error("#{ex.message} + #{ex.backtrace.join("\n")}")
    render :json => {:success => false, :message => ex.message}
  end

end

#publishObject



68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 68

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



56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 56

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



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 121

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

#update_hostObject



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

def update_host
  begin
    current_user.with_capability('edit', 'WebsiteHost') do
      website_host = WebsiteHost.find(params[:id])
      website_host.host = params[:host]
      website_host.save

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

#website_publicationsObject



13
14
15
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
# File 'app/controllers/knitkit/erp_app/desktop/website_controller.rb', line 13

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