Commit 9526f3aa5d61aeb3ab5d95e3b5872c5429cf93a2

Authored by LeandroNunes
1 parent abc8b42c

ActionItem0: putting selection of themes and icons on system

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@110 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
@@ -2,8 +2,9 @@ @@ -2,8 +2,9 @@
2 # available in all controllers. 2 # available in all controllers.
3 class ApplicationController < ActionController::Base 3 class ApplicationController < ActionController::Base
4 4
5 - TEMPLATE_DIR_PATH = 'public/templates'  
6 - ICON_DIR_PATH = 'public/icons' 5 + ICONS_DIR_PATH = "#{RAILS_ROOT}/public/icons"
  6 + THEME_DIR_PATH = "#{RAILS_ROOT}/public/themes"
  7 +
7 8
8 before_filter :detect_stuff_by_domain 9 before_filter :detect_stuff_by_domain
9 attr_reader :virtual_community 10 attr_reader :virtual_community
@@ -17,49 +18,63 @@ class ApplicationController &lt; ActionController::Base @@ -17,49 +18,63 @@ class ApplicationController &lt; ActionController::Base
17 if Profile.exists?(1) 18 if Profile.exists?(1)
18 @owner = Profile.find(1) 19 @owner = Profile.find(1)
19 end 20 end
20 - @chosen_template = @owner.nil? ? "default" : @owner.template 21 + @chosen_template = @owner.template.nil? ? "default" : @owner.template
  22 + self.chosen_template = @chosen_template
21 end 23 end
22 24
23 before_filter :load_boxes 25 before_filter :load_boxes
24 - #Load a set of boxes belongs to a owner. We have to situations.  
25 - # 1 - The owner has equal or more boxes that boxes defined in template.  
26 - # The system limit the max number of boxes to the number permited in template  
27 - # 2 - The owner there isn't enough box that defined in template  
28 - # The system create the boxes needed to use the current template  
29 - def load_boxes  
30 - raise _('Template not found') if @chosen_template.nil?  
31 - n = boxes_by_template(@chosen_template)  
32 - @boxes = Array.new  
33 - if Profile.exists?(1)  
34 - owner = Profile.find(1)  
35 - @boxes = owner.boxes  
36 - end  
37 26
38 - if @boxes.length >= n  
39 - @boxes = @boxes.first(n)  
40 - else  
41 - @boxes = @boxes 27 + before_filter :load_theme
  28 + # Load the theme belongs to a Profile and set it at @chosen_theme variable.
  29 + # If no profile exist the @chosen_theme variable is set to 'default'
  30 + def load_theme
  31 + if Profile.exists?(1)
  32 + @owner = Profile.find(1)
42 end 33 end
43 - 34 + @chosen_theme = @owner.theme.nil? ? "default" : @owner.theme
44 end 35 end
45 36
46 - def boxes_by_template(template)  
47 - f = YAML.load_file("#{RAILS_ROOT}/public/templates/default/default.yml")  
48 - number_of_boxes = f[template.to_s]["number_of_boxes"]  
49 - raise _("The file #{template}.yml it's not a valid template filename") if number_of_boxes.nil?  
50 - number_of_boxes 37 + before_filter :load_icons_theme
  38 + # Load the icons belongs to a Profile and set it at @chosen_icons_theme variable.
  39 + # If no profile exist the @chosen_icons_theme variable is set to 'default'
  40 + def load_icons_theme
  41 + if Profile.exists?(1)
  42 + @owner = Profile.find(1)
  43 + end
  44 + @chosen_icons_theme = @owner.icons_theme.nil? ? "default" : @owner.icons_theme
51 end 45 end
52 46
53 47
  48 + # Set the default template to the profile
54 def set_default_template 49 def set_default_template
55 p = Profile.find(params[:object_id]) 50 p = Profile.find(params[:object_id])
56 set_template(p,params[:template_name]) 51 set_template(p,params[:template_name])
57 end 52 end
58 53
  54 + # Set the default theme to the profile
  55 + def set_default_theme
  56 + p = Profile.find(params[:object_id])
  57 + set_theme(p,params[:theme_name])
  58 + end
  59 +
  60 + # Set the default icons theme to the profile
  61 + def set_default_icons_theme
  62 + p = Profile.find(params[:object_id])
  63 + set_icons_theme(p,params[:icons_theme_name])
  64 + end
  65 +
  66 +
59 private 67 private
60 68
61 - def set_template(object, template_name)  
62 - object.template = template_name 69 + # Set to the owner the theme choosed
  70 + def set_theme(object, theme_name)
  71 + object.theme = theme_name
  72 + object.save
  73 + end
  74 +
  75 + # Set to the owner the icons theme choosed
  76 + def set_icons_theme(object,icons_theme_name)
  77 + object.icons_theme = icons_theme_name
63 object.save 78 object.save
64 end 79 end
65 80
app/controllers/edit_template_controller.rb
@@ -2,9 +2,4 @@ class EditTemplateController &lt; ApplicationController @@ -2,9 +2,4 @@ class EditTemplateController &lt; ApplicationController
2 2
3 uses_manage_template :edit => true 3 uses_manage_template :edit => true
4 4
5 - def test  
6 - @bli = true  
7 - render :action => 'index'  
8 - end  
9 -  
10 end 5 end
app/helpers/application_helper.rb
@@ -9,51 +9,72 @@ module ApplicationHelper @@ -9,51 +9,72 @@ module ApplicationHelper
9 .svn 9 .svn
10 ] 10 ]
11 11
12 - # Generate a select option to choose one of the available templates.  
13 - # The available templates are those in 'public/templates'  
14 - def select_template(object, chosen_template = nil) 12 + ICONS_DIR_PATH = "#{RAILS_ROOT}/public/icons"
  13 + THEME_DIR_PATH = "#{RAILS_ROOT}/public/themes"
  14 +
  15 +
  16 + # Generate a select option to choose one of the available themes.
  17 + # The available themes are those in 'public/themes'
  18 + def select_theme(object, chosen_theme = nil)
15 return '' if object.nil? 19 return '' if object.nil?
16 - available_templates = Dir.new('public/templates').to_a - REJECTED_DIRS  
17 - template_options = options_for_select(available_templates.map{|template| [template, template] }, chosen_template)  
18 - select_tag('template_name', template_options ) +  
19 - change_tempate('template_name', object) 20 + available_themes = Dir.new("#{THEME_DIR_PATH}").to_a - REJECTED_DIRS
  21 + theme_options = options_for_select(available_themes.map{|theme| [theme, theme] }, chosen_theme)
  22 + select_tag('theme_name', theme_options ) +
  23 + change_theme('theme_name', object)
20 end 24 end
21 25
22 - def change_tempate(observed_field, object) 26 + # Generate a observer to reload a page when a theme is selected
  27 + def change_theme(observed_field, object)
23 observe_field( observed_field, 28 observe_field( observed_field,
24 - :url => {:action => 'set_default_template'},  
25 - :with =>"'template_name=' + escape(value) + '&object_id=' + escape(#{object.id})", 29 + :url => {:action => 'set_default_theme'},
  30 + :with =>"'theme_name=' + escape(value) + '&object_id=' + escape(#{object.id})",
26 :complete => "document.location.reload();" 31 :complete => "document.location.reload();"
27 ) 32 )
28 end 33 end
29 34
30 - # Load all the css files of a existing template with the template_name passed as argument.  
31 - #  
32 - # The files loaded are in the path:  
33 - #  
34 - # 'public/templates/#{template_name}/stylesheets/*'  
35 - #TODO I think that implements this idea describe above it's good. Let's discuss about it.  
36 - # OBS: If no files are found in path the default template is used  
37 - def stylesheet_link_tag_template(template_name)  
38 - d = Dir.new("public/templates/#{template_name}/stylesheets/").to_a - REJECTED_DIRS  
39 - d.map do |filename|  
40 - stylesheet_link_tag("/templates/#{template_name}/stylesheets/#{filename}")  
41 - end 35 +
  36 + # Generate a select option to choose one of the available icons themes.
  37 + # The available icons themes are those in 'public/icons'
  38 + def select_icons_theme(object, chosen_icons_theme = nil)
  39 + return '' if object.nil?
  40 + available_icons_themes = Dir.new("#{ICONS_DIR_PATH}").to_a - REJECTED_DIRS
  41 + icons_theme_options = options_for_select(available_icons_themes.map{|icons_theme| [icons_theme, icons_theme] }, chosen_icons_theme)
  42 + select_tag('icons_theme_name', icons_theme_options ) +
  43 + change_icons_theme('icons_theme_name', object)
42 end 44 end
43 45
44 - # Load all the javascript files of a existing template with the template_name passed as argument. 46 + # Generate a observer to reload a page when a icons theme is selected
  47 + def change_icons_theme(observed_field, object)
  48 + observe_field( observed_field,
  49 + :url => {:action => 'set_default_icons_theme'},
  50 + :with =>"'icons_theme_name=' + escape(value) + '&object_id=' + escape(#{object.id})",
  51 + :complete => "document.location.reload();"
  52 + )
  53 + end
  54 +
  55 + #Display a given icon passed as argument
  56 + #The icon path should be '/icons/{icons_theme}/{icon_image}'
  57 + def display_icon(icon , icons_theme = "default", options = {})
  58 + image_tag("/icons/#{icons_theme}/#{icon}", options)
  59 + end
  60 +
  61 + # Load all the css files of a existing theme with the theme_name passed as argument.
45 # 62 #
46 # The files loaded are in the path: 63 # The files loaded are in the path:
47 # 64 #
48 - # 'public/templates/#{template_name}/javascripts/*'  
49 - #  
50 - #TODO I think that implements this idea describe above it's good. Let's discuss about it.  
51 - # OBS: If no files are found in path the default template is used  
52 - def javascript_include_tag_template(template_name)  
53 - d = Dir.new("public/templates/#{template_name}/javascripts/").to_a - REJECTED_DIRS 65 + # 'public/themes/#{theme_name}/*'
  66 + # If a invalid theme it's passed the 'default' theme is applied
  67 + def stylesheet_link_tag_theme(theme_name)
  68 + if !File.exists? "#{THEME_DIR_PATH}/#{theme_name}"
  69 + flash[:notice] = _("The theme %s it's not a valid theme") % theme_name
  70 + theme_name = 'default'
  71 + end
  72 +
  73 + d = Dir.new("#{THEME_DIR_PATH}/#{theme_name}/").to_a - REJECTED_DIRS
54 d.map do |filename| 74 d.map do |filename|
55 - javascript_include_tag("/templates/#{template_name}/javascripts/#{filename}") 75 + stylesheet_link_tag("/themes/#{theme_name}/#{filename}")
56 end 76 end
57 end 77 end
58 78
  79 +
59 end 80 end
app/views/edit_template/index.rhtml
@@ -1 +0,0 @@ @@ -1 +0,0 @@
1 -<h1> something</h1>  
app/views/home/index.rhtml
  1 +<%= flash[:notice] %>
1 Start page of Virtual Community <%= @virtual_community.name %> 2 Start page of Virtual Community <%= @virtual_community.name %>
app/views/layouts/application.rhtml
@@ -3,15 +3,23 @@ @@ -3,15 +3,23 @@
3 <%= javascript_include_tag :defaults %> 3 <%= javascript_include_tag :defaults %>
4 <%= javascript_include_tag_template @chosen_template %> 4 <%= javascript_include_tag_template @chosen_template %>
5 <%= stylesheet_link_tag_template @chosen_template %> 5 <%= stylesheet_link_tag_template @chosen_template %>
  6 + <%= stylesheet_link_tag_theme @chosen_theme %>
6 7
7 </head> 8 </head>
8 <body> 9 <body>
  10 +
9 <%= image_tag 'loading.gif', :id => 'spinner', :style => "display:none; float:right;" %> 11 <%= image_tag 'loading.gif', :id => 'spinner', :style => "display:none; float:right;" %>
10 12
11 <%= link_to _('Show Layout'), :controller => 'home' %> 13 <%= link_to _('Show Layout'), :controller => 'home' %>
12 <%= link_to _('Edit Layout'), :controller => 'edit_template' %> 14 <%= link_to _('Edit Layout'), :controller => 'edit_template' %>
13 15
14 <%= select_template(@owner, @chosen_template) %> 16 <%= select_template(@owner, @chosen_template) %>
  17 + <%= select_theme(@owner, @chosen_theme) %>
  18 + <%= select_icons_theme(@owner, @chosen_icons_theme) %>
  19 +
  20 + <%= display_icon('back', @chosen_icons_theme )%>
  21 +
  22 + <%= flash[:notice] %>
15 23
16 <%= display_boxes(@boxes, yield) %> 24 <%= display_boxes(@boxes, yield) %>
17 25