diff --git a/app/controllers/admin/admin_panel_controller.rb b/app/controllers/admin/admin_panel_controller.rb index f62c526..933e686 100644 --- a/app/controllers/admin/admin_panel_controller.rb +++ b/app/controllers/admin/admin_panel_controller.rb @@ -16,7 +16,22 @@ class AdminPanelController < AdminController end end - def edit_templates + def manage_templates + @person_templates = environment.templates('person') + @community_templates = environment.templates('community') + @enterprise_templates = environment.templates('enterprise') + @templates = @person_templates + @community_templates + @enterprise_templates end + def set_template + environment.person_template = Person.find(params[:environment][:person_template]) if params[:environment][:person_template] + environment.enterprise_template = Enterprise.find(params[:environment][:enterprise_template]) if params[:environment][:enterprise_template] + environment.community_template = Community.find(params[:environment][:community_template]) if params[:environment][:community_template] + if environment.save! + flash[:notice] = _('Template updated successfully') + else + flash[:error] = _('Could not update template') + end + redirect_to :action => 'manage_templates' + end end diff --git a/app/models/environment.rb b/app/models/environment.rb index 23ab916..35d27c1 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -335,18 +335,57 @@ class Environment < ActiveRecord::Base settings[:layout_template] = value end - def enterprise_template - Enterprise.find_by_id settings[:enterprise_template_id] - end - def community_template Community.find_by_id settings[:community_template_id] end + def community_template=(value) + settings[:community_template_id] = value.id + end + def person_template Person.find_by_id settings[:person_template_id] end + def person_template=(value) + settings[:person_template_id] = value.id + end + + def enterprise_template + Enterprise.find_by_id settings[:enterprise_template_id] + end + + def enterprise_template=(value) + settings[:enterprise_template_id] = value.id + end + + def inactive_enterprise_template + Enterprise.find_by_id settings[:inactive_enterprise_template_id] + end + + def inactive_enterprise_template=(value) + settings[:inactive_enterprise_template_id] = value.id + end + + def templates(profile = 'profile') + klass = profile.classify.constantize + templates = [] + if settings[:templates_ids] + settings[:templates_ids].each do |template_id| + templates << klass.find_by_id(template_id) + end + end + templates.compact + end + + def add_templates=(values) + if settings[:templates_ids] + settings[:templates_ids].concat(values.map(&:id)) + else + settings[:templates_ids] = values.map(&:id) + end + end + after_create :create_templates def create_templates diff --git a/app/models/profile.rb b/app/models/profile.rb index 5a091ea..886fe86 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -213,6 +213,12 @@ class Profile < ActiveRecord::Base nil end + def apply_template(template) + copy_blocks_from(template) + copy_articles_from(template) + self.update_attributes!(:custom_footer => template[:custom_footer], :custom_header => template[:custom_header]) + end + xss_terminate :only => [ :name, :nickname, :address, :contact_phone ] # returns the contact email for this profile. By default returns the the @@ -355,6 +361,16 @@ class Profile < ActiveRecord::Base end def copy_article_tree(article, parent=nil) + original_article = self.articles.find_by_name(article.name) + if original_article + num = 2 + new_name = original_article.name + ' ' + num.to_s + while self.articles.find_by_name(new_name) + num = num + 1 + new_name = original_article.name + ' ' + num.to_s + end + original_article.update_attributes!(:name => new_name) + end article_copy = article.copy(:profile => self, :parent => parent, :advertise => false) if article.profile.home_page == article self.home_page = article_copy @@ -440,11 +456,26 @@ class Profile < ActiveRecord::Base end def custom_header - self[:custom_header] || environment.custom_header + header = self[:custom_header] || environment.custom_header + if header + if self.respond_to?(:name) && header.include?('{name}') + header.gsub('{name}', self.name) + else + header + end + end end def custom_footer - self[:custom_footer] || environment.custom_footer + footer = self[:custom_footer] || environment.custom_footer + if footer + %w[contact_person contact_email contact_phone location address economic_activity].each do |att| + if self.respond_to?(att) && footer.include?("{#{att}}") + footer.gsub!("{#{att}}", self.send(att).to_s) + end + end + end + footer end def theme diff --git a/app/views/admin_panel/index.rhtml b/app/views/admin_panel/index.rhtml index e66e50a..ab666d0 100644 --- a/app/views/admin_panel/index.rhtml +++ b/app/views/admin_panel/index.rhtml @@ -10,5 +10,5 @@
  • <%= link_to _('Manage Categories'), :controller => 'categories'%>
  • <%= link_to _('Manage User roles'), :controller => 'role' %>
  • <%= link_to _('Manage Validators by region'), :controller => 'region_validators' %>
  • -
  • <%= link_to _('Edit Templates'), :action => 'edit_templates' %>
  • +
  • <%= link_to _('Manage Templates'), :action => 'manage_templates' %>
  • diff --git a/app/views/admin_panel/manage_templates.rhtml b/app/views/admin_panel/manage_templates.rhtml new file mode 100644 index 0000000..0abcd52 --- /dev/null +++ b/app/views/admin_panel/manage_templates.rhtml @@ -0,0 +1,39 @@ +

    <%= _('Set Templates') %>

    + +<% labelled_form_for(:environment, @environment, :url => {:action => 'set_template'}) do |f| %> +<%= current_user.name %> +

    +
    + <%= f.select :person_template, @person_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.person_template.nil? ? nil : environment.person_template.id) %> +

    + +

    +
    + <%= f.select :community_template, @community_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.community_template.nil? ? nil : environment.community_template.id) %> +

    + +

    +
    + <%= f.select :enterprise_template, @enterprise_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.enterprise_template.nil? ? nil : environment.enterprise_template.id) %> +

    + +

    +
    + <%= f.select :inactive_enterprise_template, @enterprise_templates.map {|item| [ item.identifier, item.id]}, :selected => (environment.inactive_enterprise_template.nil? ? nil : environment.inactive_enterprise_template.id) %> +

    + + <% button_bar do %> + <%= submit_button(:save, _('Save')) %> + <%= button(:cancel, _('Cancel'), :action => 'index') %> + <% end %> + +<% end %> + +

    <%= _('Edit Templates') %>

    + + + diff --git a/test/functional/admin_panel_controller_test.rb b/test/functional/admin_panel_controller_test.rb index fac6a0b..89439af 100644 --- a/test/functional/admin_panel_controller_test.rb +++ b/test/functional/admin_panel_controller_test.rb @@ -110,4 +110,39 @@ class AdminPanelControllerTest < Test::Unit::TestCase assert_equal "This is my new environment", Environment.default.message_for_disabled_enterprise end + should 'list templates' do + get :manage_templates + + assert_kind_of Array, assigns(:person_templates) + assert_kind_of Array, assigns(:community_templates) + assert_kind_of Array, assigns(:enterprise_templates) + end + + should 'display environment template options' do + e = Environment.default + @controller.stubs(:environment).returns(e) + profile_template = Profile.create!(:name =>'template_test', :identifier => 'template_test', :environment => e) + e.settings[:templates_ids] = [profile_template.id] + e.save! + e.stubs(:templates).with('person').returns([profile_template]) + e.stubs(:templates).with('community').returns([profile_template]) + e.stubs(:templates).with('enterprise').returns([profile_template]) + + assert_equal [profile_template], e.templates('person') + + get :manage_templates + ['person_template', 'community_template', 'enterprise_template'].each do |template| + assert_tag :tag => 'select', :attributes => { :id => "environment_#{template}"}, :descendant => { :tag => 'option', :content => 'template_test'} + end + end + + should 'set template' do + e = Environment.default + @controller.stubs(:environment).returns(e) + profile_template = Enterprise.create!(:name =>'template_test', :identifier => 'template_test') + + post :set_template, :environment => {:enterprise_template => profile_template.id} + + assert_equal profile_template, e.enterprise_template + end end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 4cc7689..85daa0b 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -440,6 +440,58 @@ class EnvironmentTest < Test::Unit::TestCase assert !e.person_template.public? end + should 'set a template in community_template' do + e = Environment.create!(:name => 'test_env') + template = Community.create!(:name => 'Community template 2', :identifier => e.name.to_slug + 'community_template_2', :environment => e, :public_profile => false) + e.community_template = template + + assert_equal template, e.community_template + end + + should 'set a template in person_template' do + e = Environment.create!(:name => 'test_env') + template = create_user('person_template_2').person + e.person_template = template + + assert_equal template, e.person_template + end + + should 'set a template in enterprise_template' do + e = Environment.create!(:name => 'test_env') + template = Enterprise.create!(:name => 'Enterprise template 2', :identifier => e.name.to_slug + 'enterprise_template', :environment => e, :public_profile => false) + e.enterprise_template = template + + assert_equal template, e.enterprise_template + end + + should 'add templates when it is empty' do + e = Environment.create!(:name => 'test_env') + ent_template_a = Enterprise.create!(:name => 'Enterprise template A', :identifier => e.name.to_slug + 'enterprise_template_a', :environment => e, :public_profile => false) + ent_template_b = Enterprise.create!(:name => 'Enterprise template B', :identifier => e.name.to_slug + 'enterprise_template_b', :environment => e, :public_profile => false) + + e.add_templates = [ent_template_a, ent_template_b] + assert_equal [ent_template_a, ent_template_b], e.templates + end + + should 'add templates when it is not empty' do + e = Environment.create!(:name => 'test_env') + + ent_template_example = Enterprise.create!(:name => 'Enterprise template example', :identifier => e.name.to_slug + 'enterprise_template_example', :environment => e, :public_profile => false) + + e.settings[:templates_ids] = [ent_template_example.id] + e.save + + ent_template_a = Enterprise.create!(:name => 'Enterprise template A', :identifier => e.name.to_slug + 'enterprise_template_a', :environment => e, :public_profile => false) + + e.add_templates = [ent_template_a] + + assert_equal [ent_template_example, ent_template_a], e.templates + end + + should 'have an empty array of templates by default' do + assert_equal [], Environment.new.templates + end + should 'not disable ssl by default' do e = Environment.new assert !e.disable_ssl diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 80aa6c9..b7ccb73 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -767,10 +767,18 @@ class ProfileTest < Test::Unit::TestCase assert_equal 'my custom header', Profile.new(:custom_header => 'my custom header').custom_header end + should 'provide custom header with variables' do + assert_equal 'Custom header of Test', Profile.new(:custom_header => 'Custom header of {name}', :name => 'Test').custom_header + end + should 'provide custom footer' do assert_equal 'my custom footer', Profile.new(:custom_footer => 'my custom footer').custom_footer end + should 'provide custom footer with variables' do + assert_equal 'Address: Address for test', Profile.new(:custom_footer => 'Address: {address}', :address => 'Address for test').custom_footer + end + should 'provide environment header if profile header is blank' do profile = Profile.new env = mock @@ -893,6 +901,53 @@ class ProfileTest < Test::Unit::TestCase assert_equal 1, p.boxes[0].blocks.size end + should 'apply template' do + template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.save! + + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + + p.apply_template(template) + + assert_equal 1, p.boxes.size + assert_equal 1, p.boxes[0].blocks.size + end + + should 'copy articles when applying template' do + template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.articles.create(:name => 'template article') + template.save! + + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + + p.apply_template(template) + + assert_not_nil p.articles.find_by_name('template article') + end + + should 'rename existing articles when applying template' do + template = Profile.create!(:name => 'test template', :identifier => 'test_template') + template.boxes.destroy_all + template.boxes << Box.new + template.boxes[0].blocks << Block.new + template.articles.create(:name => 'some article') + template.save! + + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof') + p.articles.create(:name => 'some article') + + p.apply_template(template) + + assert_not_nil p.articles.find_by_name('some article 2') + assert_not_nil p.articles.find_by_name('some article') + end + TMP_THEMES_DIR = RAILS_ROOT + '/test/tmp/profile_themes' should 'have themes' do Theme.stubs(:user_themes_dir).returns(TMP_THEMES_DIR) -- libgit2 0.21.2