Class: Knitkit::ErpApp::Desktop::WebsiteSectionController

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

Constant Summary

Constants inherited from AppController

AppController::KNIT_KIT_ROOT

Instance Method Summary collapse

Instance Method Details

#add_layoutObject



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

def add_layout
  begin
    current_user.with_capability('create', 'WebsiteSectionLayout') do
      @website_section.create_layout
      render :json => {:success => true}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#available_articlesObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 248

def available_articles
  website_id = params[:website_id]
  current_articles = Article.joins(:website_section_contents).where("website_section_id = #{params[:section_id]}").all

  # Defaults to retrieving all articles

  available_articles = Article.with_party_role(current_user.party.dba_organization,
                                               RoleType.iid('dba_org')).order('LOWER(contents.internal_identifier) ASC')

  # Orphaned Articles

  if !website_id.blank? and website_id.to_i == -1
    available_articles = available_articles.includes(:website_section_contents).where(:website_section_contents => {:content_id => nil})
  end

  # Website Articles

  if !website_id.blank? and website_id.to_i > 0
    available_articles = available_articles.joins(:website_sections).where("website_sections.website_id = #{website_id}")
  end

  available_articles = available_articles.all - current_articles

  render :inline => "{\"articles\":#{available_articles.to_json(:only => [:title, :internal_identifier, :id], :methods => [:combobox_display_value])}}"
end

#available_articles_filterObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 230

def available_articles_filter
  menu = []
  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')).all
  all_articles = [{:name => 'All Articles', :id => 0}]
  orphaned_articles = [{:name => 'Orphaned Articles Only', :id => -1}]

  websites_array = []
  websites.each do |w|
    websites_array << {:name => "Website: #{w.name}", :id => w.id}
  end

  menu = all_articles + orphaned_articles + websites_array

  render :inline => "{\"websites\":#{menu.to_json(:only => [:name, :id])}}"
end

#copyObject



74
75
76
77
78
79
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
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 74

def copy
  begin

    @website = Website.find(params[:website_id])
    parent_section = WebsiteSection.where('id = ?', params[:parent_section_id]).first
    section_to_copy = WebsiteSection.find(params[:id])

    new_section = section_to_copy.copy(params[:title].strip, true, true)

    if parent_section
      new_section.move_to_child_of(parent_section)
      new_section.save
    end

    # update all children paths after all are saved.

    new_section.self_and_descendants.each do |section|
      section.update_path!
    end

    result = {:success => true, :parentNodeId => params[:parent_section_id], :node => build_section_hash(new_section)}

  rescue => ex
    # TODO send error notification

    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    result = {:success => false, :message => 'Could not copy Section'}
  end

  render :json => result
end

#deleteObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 108

def delete
  begin
    ActiveRecord::Base.transaction do
      current_user.with_capability('delete', 'WebsiteSection') do
        section = WebsiteSection.find(params[:id])

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

#detach_articleObject



122
123
124
125
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 122

def detach_article
  success = WebsiteSectionContent.where(:website_section_id => @website_section.id, :content_id => params[:article_id]).first.destroy
  render :json => success ? {:success => true} : {:success => false}
end

#existing_sectionsObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 271

def existing_sections
  website = Website.find(params[:website_id])
  WebsiteSection.class_eval do
    def title_permalink
      "#{self.title} - #{self.path}"
    end
  end

  if params[:exclude_document_sections].present?
    render :inline => website.sections.where('type is null').to_json(:only => [:id], :methods => [:title_permalink])
  else
    render :inline => website.sections.to_json(:only => [:id], :methods => [:title_permalink])
  end
end

#get_layoutObject



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

def get_layout
  begin
    current_user.with_capability('edit', 'WebsiteSectionLayout') do
      render :text => @website_section.layout
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#newObject



7
8
9
10
11
12
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
43
44
45
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
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 7

def new
  begin
    result = {success: false}

    current_user.with_capability('create', 'WebsiteSection') do
      begin
        ActiveRecord::Base.transaction do

          @website = Website.find(params[:website_id])
          @website_primary_host = @website.nil? ? nil : @website.config_value('primary_host')

          if params[:title].to_s.downcase == 'blog' && params[:type] == 'Blog'
            result = {:success => false, :message => 'Blog can not be the title of a Blog'}
          else
            website_section = WebsiteSection.new
            website_section.parent_id = params[:website_section_id] if params[:website_section_id]
            website_section.website_id = @website.id
            website_section.in_menu = params[:in_menu] == 'yes'
            website_section.title = params[:title]
            website_section.render_base_layout = params[:render_with_base_layout] == 'yes'
            website_section.type = params[:type] unless params[:type] == 'Page'
            website_section.internal_identifier = params[:internal_identifier]
            website_section.position = 0 # explicitly set position null, MS SQL doesn't always honor column default


            if website_section.save
              if params[:website_section_id]
                parent_website_section = WebsiteSection.find(params[:website_section_id])
                website_section.move_to_child_of(parent_website_section)
              end

              if params[:type] == "OnlineDocumentSection"
                documented_content = DocumentedContent.create(:title => website_section.title,
                                                              :created_by => current_user,
                                                              :body_html => website_section.title)
                DocumentedItem.create(:documented_content_id => documented_content.id,
                                      :online_document_section_id => website_section.id)
              end

              website_section.update_path!
              result = {:success => true, :node => build_section_hash(website_section)}
            else
              message = "<ul>"
              website_section.errors.collect do |e, m|
                message << "<li>#{e} #{m}</li>"
              end
              message << "</ul>"
              result = {:success => false, :message => message}
            end

          end
        end
      rescue => ex
        Rails.logger.error ex.message
        Rails.logger.error ex.backtrace.join("\n")

        ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

        result = {:success => false, :message => 'Could not create Section'}
      end

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

#save_layoutObject



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

def save_layout
  begin
    current_user.with_capability('edit', 'WebsiteSectionLayout') do
      result = Knitkit::SyntaxValidator.validate_content(:erb, params[:content])
      unless result
        website = @website_section.website
        @website_section.layout = params[:content]

        #TODO this should probably be moved into the view

        if @website_section.altered?
          saved = @website_section.save
          @website_section.publish(website, 'Auto Publish', @website_section.version, current_user) if saved and website.publish_on_save?

          render :json => saved ? {:success => true} : {:success => false}
        else

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

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

#updateObject



148
149
150
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
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 148

def update
  begin
    current_user.with_capability('edit', 'WebsiteSection') do
      @website_section.in_menu = params[:in_menu] == 'yes'
      @website_section.title = params[:title]
      @website_section.render_base_layout = params[:render_with_base_layout] == 'yes'
      @website_section.internal_identifier = params[:internal_identifier]

      #check if this is a OnlineDocumentSection if so set markdown

      if @website_section.is_a?(OnlineDocumentSection) || @website_section.type == 'OnlineDocumentSection'
        @website_section.use_markdown = (params[:use_markdown] == 'yes')
      end

      #TODO this should probably be moved into the view

      if @website_section.altered?
        website = @website_section.website
        if @website_section.save
          @website_section.publish(website, 'Auto Publish', @website_section.version, current_user) if website.publish_on_save?

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

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

#update_securityObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/knitkit/erp_app/desktop/website_section_controller.rb', line 127

def update_security
  if current_user.has_capability?('secure', 'WebsiteSection') or current_user.has_capability?('unsecure', 'WebsiteSection')
    roles = JSON.parse(params[:security])

    if roles.blank? || roles.empty?
      @website_section.remove_capability(:view)
    else
      capability = @website_section.add_capability(:view)
      capability.remove_all_roles
      roles.each do |r|
        role = SecurityRole.find_by_internal_identifier(r)
        role.add_capability(capability)
      end
    end

    render :json => {:success => true, :secured => @website_section.is_secured?, :roles => @website_section.roles.collect { |item| item.internal_identifier }}
  else
    render :json => {:success => false, :message => "User does not have capability."}
  end
end