diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 4d0aaa3..1a3bdf0 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -20,22 +20,34 @@ class CmsController < MyProfileController def available_article_types articles = [ - Folder, TinyMceArticle, TextileArticle, - RssFeed, UploadedFile, Event ] + parent_id = params ? params[:parent_id] : nil + if !parent_id or !Article.find(parent_id).blog? + articles += [ + Folder, + RssFeed + ] + end if profile.enterprise? articles << EnterpriseHomepage end articles end + def special_article_types + [Blog] + end + def view @article = profile.articles.find(params[:id]) @subitems = @article.children.reject {|item| item.folder? } + if @article.blog? + @subitems.reject! {|item| item.class == RssFeed } + end @folders = @article.children.select {|item| item.folder? } end @@ -80,7 +92,7 @@ class CmsController < MyProfileController return end - raise "Invalid article type #{@type}" unless available_article_types.map {|item| item.name}.include?(@type) + raise "Invalid article type #{@type}" unless valid_article_type?(@type) klass = @type.constantize @article = klass.new(params[:article]) @@ -192,5 +204,9 @@ class CmsController < MyProfileController [url, url.sub('https:', 'http:')] end + def valid_article_type?(type) + (available_article_types + special_article_types).map {|item| item.name}.include?(type) + end + end diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index a351dae..94a43bd 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -29,6 +29,11 @@ class ProfileDesignController < BoxOrganizerController blocks << ProductsBlock end + # block exclusive to profile has blog + if profile.has_blog? + blocks << BlogArchivesBlock + end + blocks end diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 49cf3f9..8ed1b9c 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -14,6 +14,10 @@ class ContentViewerController < ApplicationController return end else + path.gsub!(/\/(\d{4})\/(\d{2})\Z/, '') + year = $1 + month = $2 + @page = profile.articles.find_by_path(path) unless @page page_from_old_path = profile.articles.find_by_old_path(path) @@ -66,6 +70,10 @@ class ContentViewerController < ApplicationController remove_comment end + if @page.blog? + @page.filter = {:year => year, :month => month} + end + @comments = @page.comments(true) end diff --git a/app/helpers/article_helper.rb b/app/helpers/article_helper.rb new file mode 100644 index 0000000..8afe0c8 --- /dev/null +++ b/app/helpers/article_helper.rb @@ -0,0 +1,14 @@ +module ArticleHelper + + def custom_options_for_article(article) + @article = article + content_tag('h4', _('Options')) + + content_tag('div', + check_box(:article, :published) + + content_tag('label', _('Published'), :for => 'article_published') + + check_box(:article, :accept_comments) + + content_tag('label', _('Accept Comments'), :for => 'article_accept_comments') + ) + end + +end diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb new file mode 100644 index 0000000..00c113f --- /dev/null +++ b/app/helpers/blog_helper.rb @@ -0,0 +1,9 @@ +module BlogHelper + + def custom_options_for_article(article) + @article = article + hidden_field_tag('article[published]', 1) + + hidden_field_tag('article[accept_comments]', 0) + end + +end diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index ff94bdc..cb5548b 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -11,4 +11,16 @@ module CmsHelper attr_reader :environment + def options_for_article(article) + article_helper = ActionView::Base.new + article_helper.extend ArticleHelper + begin + class_name = article.class.name + 'Helper' + klass = class_name.constantize + article_helper.extend klass + rescue + end + article_helper.custom_options_for_article(article) + end + end diff --git a/app/helpers/content_viewer_helper.rb b/app/helpers/content_viewer_helper.rb index 2fcdbf6..bf12cfb 100644 --- a/app/helpers/content_viewer_helper.rb +++ b/app/helpers/content_viewer_helper.rb @@ -1,2 +1,55 @@ module ContentViewerHelper + + include GetText + + def number_of_comments(article) + n = article.comments.size + if n == 0 + _('No comments yet') + else + n_('One comment', '%{comments} comments', n) % { :comments => n } + end + end + + def article_title(article, args = {}) + title = content_tag('h1', article.title, :class => 'title') + if article.belongs_to_blog? + unless args[:no_link] + title = content_tag('h3', link_to(article.name, article.url), :class => 'title') + end + title << content_tag('span', _("%s, by %s" % [show_date(article.created_at), article.profile.name]), :class => 'created-at') + end + title + end + + def list_posts(articles) + pagination = will_paginate(articles, { + :param_name => 'npage', + :page_links => false, + :prev_label => _('Newer posts »'), + :next_label => _('« Older posts') + }) + articles.map{ |i| content_tag('div', display_post(i), :class => 'blog-post', :id => "post-#{i.id}") }.join("\n") + + (pagination or '') + end + + def display_post(article) + article_title(article) + content_tag('p', article.to_html) + + content_tag('p', link_to( number_of_comments(article), article.url ), :class => 'metadata') + end + + def article_to_html(article) + if article.blog? + children = if article.filter and article.filter[:year] and article.filter[:month] + filter_date = DateTime.parse("#{article.filter[:year]}-#{article.filter[:month]}-01") + article.posts.paginate :page => params[:npage], :per_page => article.posts_per_page, :conditions => [ 'created_at between ? and ?', filter_date, filter_date + 1.month - 1.day ] + else + article.posts.paginate :page => params[:npage], :per_page => article.posts_per_page + end + article.to_html + (children.compact.empty? ? content_tag('em', _('(no posts)')) : list_posts(children)) + else + article.to_html + end + end + end diff --git a/app/models/article.rb b/app/models/article.rb index 38da5d0..ab716f2 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -147,6 +147,10 @@ class Article < ActiveRecord::Base name end + def belongs_to_blog? + self.parent and self.parent.blog? + end + def url self.profile.url.merge(:page => path.split('/')) end @@ -159,6 +163,10 @@ class Article < ActiveRecord::Base false end + def blog? + false + end + def display_to?(user) if self.public_article self.profile.display_info_to?(user) diff --git a/app/models/blog.rb b/app/models/blog.rb new file mode 100644 index 0000000..897acbe --- /dev/null +++ b/app/models/blog.rb @@ -0,0 +1,54 @@ +class Blog < Folder + + has_many :posts, :class_name => 'Article', :foreign_key => 'parent_id', :source => :children, :conditions => [ 'type != ?', 'RssFeed' ], :order => 'created_at DESC' + + attr_accessor :feed_attrs + attr_accessor :filter + + after_create do |blog| + blog.children << RssFeed.new(:name => 'feed', :profile => blog.profile, :include => 'parent_and_children') + blog.feed = blog.feed_attrs + end + + settings_items :posts_per_page, :type => :integer, :default => 20 + settings_items :title, :type => :string, :default => _('My blog') + + before_save do |blog| + blog.name = 'blog' + end + + def self.short_description + _('Blog') + end + + def self.description + _('A blog, inside which you can put other articles.') + end + + # FIXME isn't this too much including just to be able to generate some HTML? + include ActionView::Helpers::TagHelper + def to_html + content_tag('div', body) + tag('hr') + end + + def folder? + true + end + + def blog? + true + end + + def feed + self.children.find(:first, :conditions => {:type => 'RssFeed'}) + end + + def feed=(attrs) + if self.feed + self.feed.update_attributes(attrs) + else + self.feed_attrs = attrs + end + end + +end diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb new file mode 100644 index 0000000..b414469 --- /dev/null +++ b/app/models/blog_archives_block.rb @@ -0,0 +1,31 @@ +class BlogArchivesBlock < Block + + include ActionView::Helpers::TagHelper + include ActionView::Helpers::UrlHelper + include ActionController::UrlWriter + + def self.description + _('List posts of your blog') + end + + def default_title + _('Blog posts') + end + + def content + return nil unless owner.has_blog? + results = '' + posts = owner.blog.posts + posts.group_by{|i| i.created_at.year}.each do |year, results_by_year| + results << content_tag('li', content_tag('strong', "#{year} (#{results_by_year.size})")) + results << "