diff --git a/app/controllers/public/contact_controller.rb b/app/controllers/public/contact_controller.rb
new file mode 100644
index 0000000..c9a2491
--- /dev/null
+++ b/app/controllers/public/contact_controller.rb
@@ -0,0 +1,17 @@
+class ContactController < PublicController
+
+ needs_profile
+
+ def new
+ @contact = Contact.new(params[:contact])
+ if request.post?
+ if @contact.save
+ flash[:notice] = _('Contact successfully sent')
+ redirect_to :controller => 'profile', :profile => profile.identifier
+ else
+ flash[:notice] = _('Contact not sent')
+ end
+ end
+ end
+
+end
diff --git a/app/helpers/contact_helper.rb b/app/helpers/contact_helper.rb
new file mode 100644
index 0000000..83d146d
--- /dev/null
+++ b/app/helpers/contact_helper.rb
@@ -0,0 +1,2 @@
+module ContactHelper
+end
diff --git a/app/models/contact.rb b/app/models/contact.rb
new file mode 100644
index 0000000..2527bc3
--- /dev/null
+++ b/app/models/contact.rb
@@ -0,0 +1,13 @@
+class Contact < Task
+
+ validates_presence_of :target_id, :subject, :email, :message
+ validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT
+
+ acts_as_having_settings :field => :data
+ settings_items :subject, :message, :city_and_state, :email, :phone
+
+ def description
+ _('%s sent a new message') % (requestor ? requestor.name : _('Someone'))
+ end
+
+end
diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb
index 3cc387f..283845d 100644
--- a/app/models/enterprise.rb
+++ b/app/models/enterprise.rb
@@ -57,7 +57,6 @@ class Enterprise < Organization
end
end
-
def default_set_of_blocks
blocks = [
[MainBlock],
@@ -74,6 +73,8 @@ class Enterprise < Organization
environment.enterprise_template
end
+ settings_items :enable_contact_us, :type => :boolean, :default => true
+
protected
def default_homepage(attrs)
diff --git a/app/views/blocks/profile_info_actions/enterprise.rhtml b/app/views/blocks/profile_info_actions/enterprise.rhtml
index 5e736f6..98fe0e1 100644
--- a/app/views/blocks/profile_info_actions/enterprise.rhtml
+++ b/app/views/blocks/profile_info_actions/enterprise.rhtml
@@ -9,4 +9,7 @@
<%= _('New members must be approved:')%>
diff --git a/app/views/tasks/_contact.rhtml b/app/views/tasks/_contact.rhtml
new file mode 100644
index 0000000..0db37cb
--- /dev/null
+++ b/app/views/tasks/_contact.rhtml
@@ -0,0 +1,18 @@
+
<%= task.description %>
+
+<% form_for('task', task, :url => { :action => 'close', :id => task.id}) do |f| %>
+
+ <%= hidden_field_tag(:decision, :finish) %>
+
+ <% %w[ subject email phone city_and_state message ].each do |field| %>
+ <% if task.respond_to?(field) and !task.send(field).nil? and !task.send(field).empty? %>
+ <%= _(field.humanize) %> | <%= task.send(field) %> |
+ <% end %>
+ <% end %>
+
+
+ <% button_bar do %>
+ <%= submit_button(:ok, _('Ok!')) %>
+ <% end %>
+
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 4d7fbd4..defdc3d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -53,6 +53,9 @@ ActionController::Routing::Routes.draw do |map|
map.catalog 'catalog/:profile', :controller => 'catalog', :action => 'index', :profile => /#{Noosfero.identifier_format}/
map.product 'catalog/:profile/:id', :controller => 'catalog', :action => 'show', :profile => /#{Noosfero.identifier_format}/
+ # contact
+ map.contact 'contact/:profile/:action/:id', :controller => 'contact', :action => 'index', :id => /.*/, :profile => /#{Noosfero.identifier_format}/
+
######################################################
## Controllers that are profile-specific (for profile admins )
######################################################
diff --git a/public/designs/icons/default/style.css b/public/designs/icons/default/style.css
index 153a347..e1413df 100644
--- a/public/designs/icons/default/style.css
+++ b/public/designs/icons/default/style.css
@@ -5,6 +5,7 @@
.icon-open { background-image: url(folder-open.gif) }
.icon-cms { background-image: url(abiword_48.png) }
.icon-save { background-image: url(save-HC.gif) }
+.icon-send { background-image: url(mail-HC.gif) }
.icon-up { background-image: url(go-up-HC.gif) }
.icon-cancel { background-image: url(cancel-HC.gif) }
.icon-person { background-image: url(user_icon.png) }
diff --git a/test/functional/contact_controller_test.rb b/test/functional/contact_controller_test.rb
new file mode 100644
index 0000000..cfb8864
--- /dev/null
+++ b/test/functional/contact_controller_test.rb
@@ -0,0 +1,75 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'contact_controller'
+
+# Re-raise errors caught by the controller.
+class ContactController; def rescue_action(e) raise e end; end
+
+class ContactControllerTest < Test::Unit::TestCase
+
+ all_fixtures
+
+ def setup
+ @controller = ContactController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @profile = create_user('contact_test_user').person
+ @enterprise = Enterprise.create!(:identifier => 'contact_test_enterprise', :name => 'Test contact enteprise')
+ end
+ attr_reader :profile, :enterprise
+
+ should 'respond to new' do
+ get :new, :profile => enterprise.identifier
+ assert_response :success
+ end
+
+ should 'display destinatary name in title' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'h1', :content => "Contact #{enterprise.name}"
+ end
+
+ should 'add form to create contact via post' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'form', :attributes => { :action => "/contact/#{enterprise.identifier}/new", :method => 'post' }
+ end
+
+ should 'display input for destinatary email' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'input', :attributes => { :name => 'contact[email]', :type => 'text' }
+ end
+
+ should 'display input for message' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'textarea', :attributes => { :name => 'contact[message]' }
+ end
+
+ should 'add hidden field with target_id' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'input', :attributes => { :name => 'contact[target_id]', :value => enterprise.id, :type => 'hidden' }
+ end
+
+ should 'add requestor id if logged in' do
+ login_as(profile.identifier)
+ @controller.stubs(:current_user).returns(profile.user)
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => profile.id }
+ end
+
+ should 'nil requestor id if not logged in' do
+ get :new, :profile => enterprise.identifier
+ assert_tag :tag => 'input', :attributes => { :name => 'contact[requestor_id]', :value => nil }
+ end
+
+ should 'redirect to profile page after contact' do
+ post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id}
+ assert_response :redirect
+ assert_redirected_to :controller => 'profile', :profile => enterprise.identifier
+ end
+
+ should 'be able to send contact' do
+ assert_difference Contact, :count do
+ post :new, :profile => enterprise.identifier, :contact => {:subject => 'Hi', :email => 'visitor@mail.invalid', :message => 'Hi, all', :target_id => enterprise.id}
+ end
+ end
+
+end
diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb
index 6bc112b..9f77075 100644
--- a/test/functional/profile_controller_test.rb
+++ b/test/functional/profile_controller_test.rb
@@ -249,7 +249,7 @@ class ProfileControllerTest < Test::Unit::TestCase
get :index, :profile => 'my-test-enterprise'
assert_tag :tag => 'a', :attributes => { :href => '/catalog/my-test-enterprise'}, :content => /Products\/Services/
end
-
+
should 'not display "Products" link for enterprise if environment do not let' do
env = Environment.default
env.enable('disable_products_for_enterprises')
@@ -304,4 +304,21 @@ class ProfileControllerTest < Test::Unit::TestCase
assert_no_tag :content => /t2@t2.com/
end
+ should 'display contact us for enterprises' do
+ ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise')
+ get :index, :profile => 'my-test-enterprise'
+ assert_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us'
+ end
+
+ should 'not display contact us for non-enterprises' do
+ get :index, :profile => @profile.identifier
+ assert_no_tag :tag => 'a', :attributes => { :href => "/contact/#{@profile.identifier}/new" }, :content => 'Contact us'
+ end
+
+ should 'display contact us only if enabled' do
+ ent = Enterprise.create!(:name => 'my test enterprise', :identifier => 'my-test-enterprise', :enable_contact_us => false)
+ get :index, :profile => 'my-test-enterprise'
+ assert_no_tag :tag => 'a', :attributes => { :href => "/contact/my-test-enterprise/new" }, :content => 'Contact us'
+ end
+
end
diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb
index 043f645..c241514 100644
--- a/test/functional/profile_editor_controller_test.rb
+++ b/test/functional/profile_editor_controller_test.rb
@@ -517,4 +517,10 @@ class ProfileEditorControllerTest < Test::Unit::TestCase
:attributes => { :name=>'profile_data[email]', :value=>'teste_user@teste.com' }
end
+ should 'display enable contact us for enterprise' do
+ org = Enterprise.create!(:name => 'test org', :identifier => 'testorg')
+ get :edit, :profile => 'testorg'
+ assert_tag :tag => 'input', :attributes => {:name => 'profile_data[enable_contact_us]', :type => 'checkbox'}
+ end
+
end
diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb
index 24e6b32..0d5de8a 100644
--- a/test/functional/tasks_controller_test.rb
+++ b/test/functional/tasks_controller_test.rb
@@ -151,4 +151,16 @@ class TasksControllerTest < Test::Unit::TestCase
assert_equal f, assigns(:ticket).target
end
+
+ should 'list enterprise contacts' do
+ ent = Enterprise.create!(:identifier => 'contact_test_enterprise', :name => 'Test contact enteprise')
+ task = Contact.create!(:subject => 'test', :target_id => profile.id, :email => 'visitor@invalid.com', :message => 'Hi, all')
+
+ login_as(profile.identifier)
+ get :index, :profile => ent.identifier
+
+ assert_includes assigns(:tasks), task
+ assert_tag :tag => 'li', :attributes => { :class => 'task-Contact' }, :content => 'Someone sent a new message'
+ end
+
end
diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb
index bfadba0..cf02f10 100644
--- a/test/integration/routing_test.rb
+++ b/test/integration/routing_test.rb
@@ -186,4 +186,8 @@ class RoutingTest < ActionController::IntegrationTest
assert_routing('/myprofile/profile.withdot', :controller => 'profile_editor', :action => 'index', :profile => 'profile.withdot')
end
+ def test_contact_routing
+ assert_routing('/contact/wintermute/new', :controller => 'contact', :action => 'new', :profile => 'wintermute')
+ end
+
end
diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb
new file mode 100644
index 0000000..fdb760b
--- /dev/null
+++ b/test/unit/contact_test.rb
@@ -0,0 +1,25 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ContactTest < ActiveSupport::TestCase
+
+ should 'have serialized data' do
+ t = Contact.new
+ t.data[:test] = 'test'
+
+ assert_equal({:test => 'test'}, t.data)
+ end
+
+ should 'validates required fields' do
+ contact = Contact.new
+ assert !contact.valid?
+ contact.subject = 'Hi'
+ assert !contact.valid?
+ contact.email = 'visitor@invalid.com'
+ assert !contact.valid?
+ contact.message = 'Hi, all'
+ assert !contact.valid?
+ contact.target = create_user('contact_user_test').person
+ assert contact.save!
+ end
+
+end
diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb
index dfc4ee1..4e13433 100644
--- a/test/unit/enterprise_test.rb
+++ b/test/unit/enterprise_test.rb
@@ -211,5 +211,9 @@ class EnterpriseTest < Test::Unit::TestCase
assert_kind_of Enterprise, p.template
end
+ should 'contact us enabled by default' do
+ e = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => Environment.default)
+ assert e.enable_contact_us
+ end
end
--
libgit2 0.21.2