app/controllers/knitkit/erp_app/desktop/articles_controller.rb in knitkit-2.1.15 vs app/controllers/knitkit/erp_app/desktop/articles_controller.rb in knitkit-3.0.0

- old
+ new

@@ -3,49 +3,62 @@ module Desktop class ArticlesController < Knitkit::ErpApp::Desktop::AppController @@datetime_format = "%m/%d/%Y %l:%M%P" def new - begin - current_user.with_capability('create', 'Content') do - result = {} - website_section_id = params[:section_id] - article = Article.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 + 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 - unless website_section_id.blank? - website_section = WebsiteSection.find(website_section_id) - article.website_sections << website_section - article.update_content_area_and_position_by_section(website_section, params['content_area'], params['position']) + 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 - result[:success] = true - else - result[:success] = false + render :json => result end - - render :json => result + rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex + render :json => {:success => false, :message => ex.message} end - rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex - render :json => {:success => false, :message => ex.message} end end 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.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 @@ -60,42 +73,50 @@ result[:success] = false end render :json => result end - rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability=>ex + rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex render :json => {:success => false, :message => ex.message} end end 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 + rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex render :json => {:success => false, :message => ex.message} end end 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 - render :json => {:success => true} + 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 + rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex render :json => {:success => false, :message => ex.message} end end def existing_articles render :inline => Article.all.to_json(:only => [:internal_identifier, :id]) end + def show + article = Article.find(params[:section_id]) + render :json => article.to_json + end + def get website_section_id = params[:section_id] section = WebsiteSection.find(website_section_id) if section.type == 'Blog' @@ -106,20 +127,21 @@ 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 + 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 @@ -128,12 +150,10 @@ 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[:body_html] = a.body_html - articles_hash[:excerpt_html] = a.excerpt_html 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) @@ -145,16 +165,16 @@ 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' + 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 = 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}") @@ -163,11 +183,11 @@ 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[: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 @@ -185,29 +205,29 @@ end 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' + 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)} + 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)} + 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)} + 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)} + attributes = attributes.sort { |x, y| y.send(sort) <=> x.send(sort) } end end attributes_array = [] attributes.each do |attribute| @@ -275,10 +295,10 @@ end render :json => result end - end#ArticlesController - end#Desktop - end#ErpApp -end#Knitkit + end #ArticlesController + end #Desktop + end #ErpApp +end #Knitkit