Class: Knitkit::ErpApp::Desktop::ArticlesController

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

Constant Summary collapse

@@datetime_format =
"%m/%d/%Y %l:%M%P"

Constants inherited from AppController

Knitkit::ErpApp::Desktop::AppController::KNIT_KIT_ROOT

Instance Method Summary collapse

Methods inherited from AppController

#available_roles

Instance Method Details

#add_existingObject



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

def add_existing
  begin
    current_user.with_capability('add_existing', 'Content') do
      website_section = WebsiteSection.find(params[:section_id])
      website_section.contents << Article.find(params[:article_id])
      website_section.save

      website_section_content = website_section.website_section_contents.where('content_id = ?', params[:article_id]).first

      render :json => {:success => true, :article => build_article_hash(website_section_content, website_section.website, website_section.is_blog?)}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#allObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 166

def all
  Article.include_root_in_json = false
  sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
  sort = sort_hash[:property] || 'title'
  dir = sort_hash[:direction] || 'ASC'
  limit = params[:limit] || 20
  start = params[:start] || 0

  articles = Article.includes(:website_section_contents)
  articles = articles.where(:website_section_contents => {:content_id => nil}) if params[:show_orphaned] == 'true'
  articles = articles.where("UPPER(contents.internal_identifier) LIKE UPPER('%#{params[:iid]}%')") unless params[:iid].blank?
  articles = articles.where("UPPER(contents.title) LIKE UPPER('%#{params[:title]}%')") unless params[:title].blank?
  articles = articles.where("UPPER(contents.body_html) LIKE UPPER('%#{params[:content]}%')
                          OR UPPER(contents.excerpt_html) LIKE UPPER('%#{params[:content]}%')") unless params[:content].blank?
  articles = articles.order("contents.#{sort} #{dir}")
  total_count = articles.count
  articles = articles.limit(limit).offset(start)

  articles_array = []
  articles.each do |a|
    articles_hash = {}
    articles_hash[:id] = a.id
    articles_hash[:sections] = a.website_sections.collect { |section| section.title }.join(',')
    articles_hash[:title] = a.title
    articles_hash[:tag_list] = a.tag_list.join(', ')
    articles_hash[:body_html] = a.body_html
    articles_hash[:internal_identifier] = a.internal_identifier
    articles_hash[:display_title] = a.display_title
    articles_hash[:excerpt_html] = a.excerpt_html
    articles_hash[:created_by] = a.created_by. rescue ''
    articles_hash[:last_update_by] = a.updated_by. rescue ''
    articles_hash[:created_at] = a.created_at.getlocal.strftime(@@datetime_format)
    articles_hash[:updated_at] = a.updated_at.getlocal.strftime(@@datetime_format)


    articles_array << articles_hash
  end

  render :inline => "{total:#{total_count},data:#{articles_array.to_json}}"
end

#article_attributesObject



207
208
209
210
211
212
213
214
215
216
217
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
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 207

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

  article = Article.find(params[:article_id])
  attributes = article.attribute_values
  attributes = attributes.slice(start.to_i, limit.to_i)

  if dir == "DESC"
    if sort == "data_type" or sort == "description"
      attributes = attributes.sort { |x, y| x.attribute_type.send(sort) <=> y.attribute_type.send(sort) }
    else
      attributes = attributes.sort { |x, y| x.send(sort) <=> y.send(sort) }
    end
  else
    if sort == "data_type" or sort == "description"
      attributes = attributes.sort { |x, y| y.attribute_type.send(sort) <=> x.attribute_type.send(sort) }
    else
      attributes = attributes.sort { |x, y| y.send(sort) <=> x.send(sort) }
    end
  end

  attributes_array = []
  attributes.each do |attribute|
    attributes_hash = {}
    attributes_hash[:id] = attribute.id
    attributes_hash[:description] = attribute.attribute_type.description
    attributes_hash[:data_type] = attribute.attribute_type.data_type
    attributes_hash[:value] = attribute.value

    attributes_array << attributes_hash
  end

  render :inline => "{data:#{attributes_array.to_json(:only => [:id, :description, :data_type, :value])}}"
end

#attribute_typesObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 246

def attribute_types
  attribute_types = AttributeType.find(:all)
  attribute_types_array = []
  attribute_types_array << {:description => 'Search By Article', :iid => 'search_by_article'}
  attribute_types.each do |attribute_type|
    attribute_types_hash = {}
    attribute_types_hash[:description] = attribute_type.description
    attribute_types_hash[:iid] = attribute_type.internal_identifier

    attribute_types_array << attribute_types_hash
  end

  render :inline => "{data:#{attribute_types_array.to_json(:only => [:description, :iid])}}"
end

#deleteObject



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

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

#delete_attributeObject



261
262
263
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 261

def delete_attribute
  render :json => AttributeValue.destroy(params[:id]) ? {:success => true} : {:success => false}
end

#existing_articlesObject



109
110
111
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 109

def existing_articles
  render :inline => Article.all.to_json(:only => [:internal_identifier, :id])
end

#getObject



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

def get
  website_section_id = params[:section_id]
  section = WebsiteSection.find(website_section_id)

  if section.type == 'Blog'
    sort_default = 'contents.created_at'
    dir_default = 'DESC'
  else
    sort_default = 'title'
    dir_default = 'ASC'
  end

  sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
  sort = sort_hash[:property] || sort_default
  dir = sort_hash[:direction] || dir_default
  limit = params[:limit] || 10
  start = params[:start] || 0

  articles = Article.joins("INNER JOIN website_section_contents ON website_section_contents.content_id = contents.id").where("website_section_id = #{website_section_id}")
  total_count = articles.count
  articles = articles.order("#{sort} #{dir}").limit(limit).offset(start)

  Article.class_exec(website_section_id) do
    @@website_section_id = website_section_id

    def website_section_position
      self.website_section_contents.find_by_website_section_id(@@website_section_id).position
    end
  end

  articles_array = []
  articles.each do |a|
    articles_hash = {}
    articles_hash[:content_area] = a.content_area_by_website_section(WebsiteSection.find(website_section_id))
    articles_hash[:id] = a.id
    articles_hash[:title] = a.title
    articles_hash[:tag_list] = a.tag_list.join(', ')
    articles_hash[:internal_identifier] = a.internal_identifier
    articles_hash[:display_title] = a.display_title
    articles_hash[:position] = a.position(website_section_id)
    articles_hash[:created_at] = a.created_at.getlocal.strftime(@@datetime_format)
    articles_hash[:updated_at] = a.updated_at.getlocal.strftime(@@datetime_format)
    articles_array << articles_hash
  end

  render :inline => "{total:#{total_count},data:#{articles_array.to_json}}"
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
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 7

def new
  ActiveRecord::Base.transaction do
    begin
      current_user.with_capability('create', 'Content') do
        result = {}
        website_section_id = params[:section_id]
        article = Article.new

        article.tag_list = params[:tags].split(',').collect { |t| t.strip } unless params[:tags].blank?
        article.title = params[:title]
        article.internal_identifier = params[:internal_identifier]
        article.display_title = params[:display_title] == 'yes'
        article.created_by = current_user

        if article.save
          result[:node] = if website_section_id.blank?
                            {:text => params[:title],
                             :id => article.id,
                             :objectType => 'Article',
                             :parentItemId => params[:section_id],
                             :siteId => nil,
                             :iconCls => 'x-column-header-wysiwyg',
                             :leaf => true
                            }
                          else
                            website_section = WebsiteSection.find(website_section_id)
                            article.website_sections << website_section
                            website_section_content = article.update_content_area_and_position_by_section(website_section, params['content_area'], params['position'])

                            build_article_hash(website_section_content, website_section.website, website_section.is_blog?)
                          end

          result[:success] = true
        else
          result[:success] = false
        end

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

#new_attributeObject



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 265

def new_attribute
  result = {:success => true}
  article = Article.find(params[:article_id])
  attribute_type = AttributeType.find_by_iid_with_description(params[:description])

  if attribute_type == nil
    attribute_type = AttributeType.new
    attribute_type.description = params[:description]
    attribute_type.data_type = params[:data_type]
    result[:success] = false unless attribute_type.save

    attribute_value = AttributeValue.new
    attribute_value.value = params[:value]

    attribute_type.attribute_values << attribute_value
    article.attribute_values << attribute_value

    result[:success] = false unless attribute_value.save
  else
    if attribute_type.data_type == params[:data_type]
      attribute_value = AttributeValue.new
      attribute_value.value = params[:value]

      attribute_type.attribute_values << attribute_value
      article.attribute_values << attribute_value

      result[:sucess] = false unless attribute_value.save
    else
      result[:success] = false
    end
  end

  render :json => result
end

#showObject



113
114
115
116
# File 'app/controllers/knitkit/erp_app/desktop/articles_controller.rb', line 113

def show
  article = Article.find(params[:section_id])
  render :json => article.to_json
end

#updateObject



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

def update
  begin
    current_user.with_capability('edit', 'Content') do
      result = {}
      website_section_id = params[:section_id]
      article = Article.find(params[:id])

      article.tag_list = params[:tags].split(',').collect { |t| t.strip } unless params[:tags].blank?
      article.title = params[:title]
      article.internal_identifier = params[:internal_identifier]
      article.display_title = params[:display_title] == 'yes'
      article.updated_by = current_user

      if article.save
        unless website_section_id.blank?
          website_section = WebsiteSection.find(website_section_id)
          article.update_content_area_and_position_by_section(website_section, params['content_area'], params['position'])
        end

        result[:success] = true
      else
        result[:success] = false
      end

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