diff --git a/app/controllers/my_profile/cms/README b/app/controllers/my_profile/cms/README new file mode 100644 index 0000000..6bd4cf2 --- /dev/null +++ b/app/controllers/my_profile/cms/README @@ -0,0 +1,46 @@ +CMS editors for Noosfero +======================== + +This directory contains code for custom content editors in Noosfero. + +Creating a new editor +===================== + +:: File structure + +Let's say that you are implementingn and editor for type, say, "foo/bar". Then +you have to create: + + * app/controllers/my_profile/cms/foo_bar.html + * app/views/cms/foo_bar_*.rhtml (one view for each action you define in the + controller class, if applicable). + +:: Coding conventions + +You file must add methods to CmsController class. They are going to be loaded +after the cms_controller.rb file itself, to you don't need to care about +declaring the superclass: + + class CmsController + def foo_bar_edit + # code for preparing the "edit" view + end + + def foo_bar_new + # code for preparing the "new" view + end + + # etc ... + end + +Note that *all* of your actions must be prefixed with the content type (e.g. +"foor_bar_" for "foo/bar"), in order to avoid conflicts. + +The views for those actions can be thrown in app/views/cms/, just like other +views for this controller. + +Limitations +=========== + + + diff --git a/app/controllers/my_profile/cms/text_html.rb b/app/controllers/my_profile/cms/text_html.rb new file mode 100644 index 0000000..a96a105 --- /dev/null +++ b/app/controllers/my_profile/cms/text_html.rb @@ -0,0 +1,23 @@ +class CmsController + + def text_html_new + @article = Article.new(params[:article]) + @article.parent = profile.articles.find(params[:parent_id]) + @article.profile = profile + if request.post? + if @article.save + redirect_to :action => 'view', :id => @article.id + end + end + end + + def text_html_edit + @article = Article.find(params[:id]) + if request.post? + if @article.update_attributes(params[:article]) + redirect_to :action => 'view', :id => @article.id + end + end + end + +end diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 6847684..bf1f603 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -3,6 +3,8 @@ class CmsController < MyProfileController # FIXME add the access control again # protect 'post_content', :profile, :only => [:edit, :new, :reorder, :delete] + include CmsHelper + def view @article = profile.articles.find(params[:id]) @subitems = @article.children @@ -14,6 +16,11 @@ class CmsController < MyProfileController render :action => 'view' end + def edit + article = profile.articles.find(params[:id]) + redirect_to(url_for_edit_article(article)) + end + post_only :set_home_page def set_home_page @article = profile.articles.find(params[:id]) @@ -24,4 +31,18 @@ class CmsController < MyProfileController protected + class << self + def available_editors + Dir.glob(File.join(File.dirname(__FILE__), 'cms', '*.rb')) + end + + def available_types + available_editors.map {|item| File.basename(item).gsub(/\.rb$/, '').gsub('_', '/') } + end + end + +end + +CmsController.available_editors.each do |item| + load item end diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index f4436f8..85c4c63 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -7,6 +7,7 @@ class ContentViewerController < PublicController if path.blank? @page = profile.home_page + # FIXME need to do something when the user didn't set a homepage else @page = profile.articles.find_by_path(path) end diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index e476188..ee88335 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -1,3 +1,17 @@ module CmsHelper + def link_to_edit_article(article) + link_to(_("Edit"), url_for_edit_article(article)) + end + + def url_for_edit_article(article) + action = article.mime_type.gsub('/', '_') + '_edit' + url_for(:action => action, :id => article.id) + end + + def link_to_new_article(mime_type) + action = mime_type.gsub('/', '_') + '_new' + link_to(_("New %s") % mime_type, :action => action, :parent_id => params[:parent_id]) + end + end diff --git a/app/models/article.rb b/app/models/article.rb index e7ff79c..6140e41 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -31,4 +31,12 @@ class Article < ActiveRecord::Base 'text-html' end + def mime_type + 'text/html' + end + + def title + name + end + end diff --git a/app/views/cms/new.rhtml b/app/views/cms/new.rhtml new file mode 100644 index 0000000..f5ed00b --- /dev/null +++ b/app/views/cms/new.rhtml @@ -0,0 +1,3 @@ +<% CmsController.available_types.each do |item| %> + <%= link_to_new_article(item) %> +<% end %> diff --git a/app/views/cms/text_html_edit.rhtml b/app/views/cms/text_html_edit.rhtml new file mode 120000 index 0000000..ee9726e --- /dev/null +++ b/app/views/cms/text_html_edit.rhtml @@ -0,0 +1 @@ +text_html_new.rhtml \ No newline at end of file diff --git a/app/views/cms/text_html_new.rhtml b/app/views/cms/text_html_new.rhtml new file mode 100644 index 0000000..6203abf --- /dev/null +++ b/app/views/cms/text_html_new.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'article' %> + +<% labelled_form_for 'article', @article do |f| %> + + <%= hidden_field_tag 'parent_id', params[:parent_id] %> + + <%= f.text_field 'name' %> + + <%= f.text_area('body') %> + + <%= submit_tag _('Save') %> + +<% end %> diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index b676991..743905e 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -30,6 +30,7 @@ <%# display the article content %> <% if @article %> + <%= link_to_edit_article(@article) %>