Commit aa47fe88392602658ea693cb762e6dc5c50ed674

Authored by Joenio Costa
1 parent 8791c34e

added piwik plugin

added new hotstop "body_ending"
app/views/layouts/application-ng.html.erb
... ... @@ -83,5 +83,10 @@
83 83 <%= noosfero_layout_features %>
84 84 <%= theme_javascript_ng %>
85 85 <%= addthis_javascript %>
  86 + <%=
  87 + @plugins.dispatch(:body_ending).map do |content|
  88 + if content.respond_to?(:call) then instance_exec(&content).html_safe else content.html_safe end
  89 + end.join("\n")
  90 + %>
86 91 </body>
87 92 </html>
... ...
lib/noosfero/plugin.rb
... ... @@ -277,6 +277,12 @@ class Noosfero::Plugin
277 277 nil
278 278 end
279 279  
  280 + # -> Adds content to the ending of the page
  281 + # returns = lambda block that creates html code or raw rhtml/html.erb
  282 + def body_ending
  283 + nil
  284 + end
  285 +
280 286 # -> Adds content to the ending of the page head
281 287 # returns = lambda block that creates html code or raw rhtml/html.erb
282 288 def head_ending
... ...
plugins/piwik/controllers/piwik_plugin_admin_controller.rb 0 → 100644
... ... @@ -0,0 +1,16 @@
  1 +class PiwikPluginAdminController < AdminController
  2 +
  3 + append_view_path File.join(File.dirname(__FILE__) + '/../views')
  4 +
  5 + def index
  6 + if request.post?
  7 + if @environment.update_attributes(params[:environment])
  8 + session[:notice] = _('Piwik plugin settings updated successfully.')
  9 + else
  10 + session[:notice] = _('Piwik plugin settings could not be saved.')
  11 + end
  12 + redirect_to :controller => 'plugins', :action => 'index'
  13 + end
  14 + end
  15 +
  16 +end
... ...
plugins/piwik/lib/ext/environment.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +require_dependency 'environment'
  2 +
  3 +class Environment
  4 + settings_items :piwik_domain
  5 + settings_items :piwik_site_id
  6 + attr_accessible :piwik_domain, :piwik_site_id
  7 +end
... ...
plugins/piwik/lib/piwik_plugin.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +class PiwikPlugin < Noosfero::Plugin
  2 +
  3 + include ActionView::Helpers::JavaScriptHelper
  4 + include ActionView::Helpers::FormHelper
  5 + include ActionView::Helpers::UrlHelper
  6 + include ActionView::Helpers::TagHelper
  7 +
  8 + def self.plugin_name
  9 + "Piwik"
  10 + end
  11 +
  12 + def self.plugin_description
  13 + _("Tracking and web analytics to your Noosfero's environment")
  14 + end
  15 +
  16 + def body_ending
  17 + domain = context.environment.piwik_domain
  18 + site_id = context.environment.piwik_site_id
  19 + unless domain.blank? || site_id.blank?
  20 + expanded_template('tracking-code.rhtml',{:domain => domain, :site_id => site_id})
  21 + end
  22 + end
  23 +
  24 +end
... ...
plugins/piwik/test/functional/piwik_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/piwik_plugin_admin_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class PiwikPluginAdminController; def rescue_action(e) raise e end; end
  6 +
  7 +class PiwikPluginAdminControllerTest < ActionController::TestCase
  8 +
  9 + def setup
  10 + @environment = Environment.default
  11 + user_login = create_admin_user(@environment)
  12 + login_as(user_login)
  13 + @environment.enabled_plugins = ['PiwikPlugin']
  14 + @environment.save!
  15 + end
  16 +
  17 + should 'access index action' do
  18 + get :index
  19 + assert_template 'index'
  20 + assert_response :success
  21 + end
  22 +
  23 + should 'update piwik plugin settings' do
  24 + assert_nil @environment.reload.piwik_domain
  25 + assert_nil @environment.reload.piwik_site_id
  26 + post :index, :environment => { :piwik_domain => 'http://something', :piwik_site_id => 10 }
  27 + assert_not_nil @environment.reload.piwik_domain
  28 + assert_not_nil @environment.reload.piwik_site_id
  29 + end
  30 +
  31 +end
... ...
plugins/piwik/test/unit/piwik_plugin_test.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class PiwikPluginTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + @plugin = PiwikPlugin.new
  7 + @context = mock()
  8 + @plugin.context = @context
  9 + @environment = Environment.new
  10 + @context.stubs(:environment).returns(@environment)
  11 + end
  12 +
  13 + should 'add content at the body ending unless domain and site_id are blank' do
  14 + @environment.piwik_domain = 'piwik.domain.example.com'
  15 + @environment.piwik_site_id = 5
  16 + @plugin.stubs(:expanded_template).returns('content')
  17 + assert_equal 'content', @plugin.body_ending
  18 + end
  19 +
  20 + should 'not add any content at the body ending if domain is blank' do
  21 + @environment.piwik_domain = nil
  22 + @environment.piwik_site_id = 5
  23 + @plugin.stubs(:expanded_template).returns('content')
  24 + assert_equal nil, @plugin.body_ending
  25 + end
  26 +
  27 + should 'not add any content at the body ending if site_id is blank' do
  28 + @environment.piwik_domain = 'piwik.domain.example.com'
  29 + @environment.piwik_site_id = nil
  30 + @plugin.stubs(:expanded_template).returns('content')
  31 + assert_equal nil, @plugin.body_ending
  32 + end
  33 +
  34 + should 'extends Environment with attr piwik_domain' do
  35 + assert_respond_to Environment.new, :piwik_domain
  36 + end
  37 +
  38 + should 'extends Environment with attr piwik_site_id' do
  39 + assert_respond_to Environment.new, :piwik_site_id
  40 + end
  41 +
  42 +end
... ...
plugins/piwik/views/piwik_plugin_admin/index.html.erb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +<h1><%= _("Piwik plugin settings") %></h1>
  2 +
  3 +<%= form_for(:environment) do |f| %>
  4 +
  5 + <%= labelled_form_field _('Piwik domain'), f.text_field(:piwik_domain) %>
  6 +
  7 + <%= labelled_form_field _('Piwik site id'), f.text_field(:piwik_site_id) %>
  8 +
  9 + <% button_bar do %>
  10 + <%= submit_button(:save, _('Save'), :cancel => {:controller => 'plugins', :action => 'index'}) %>
  11 + <% end %>
  12 +
  13 +<% end %>
... ...
plugins/piwik/views/tracking-code.rhtml 0 → 100644
... ... @@ -0,0 +1,15 @@
  1 +<!-- Piwik -->
  2 +<script type="text/javascript">
  3 + var _paq = _paq || [];
  4 + _paq.push(['trackPageView']);
  5 + _paq.push(['enableLinkTracking']);
  6 + (function() {
  7 + var u=(("https:" == document.location.protocol) ? "https" : "http") + "://<%= escape_javascript locals[:domain] %>/piwik/";
  8 + _paq.push(['setTrackerUrl', u+'piwik.php']);
  9 + _paq.push(['setSiteId', <%= escape_javascript locals[:site_id] %>]);
  10 + var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript';
  11 + g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  12 + })();
  13 +</script>
  14 +<noscript><p><img src="http://<%= escape_javascript locals[:domain] %>/piwik/piwik.php?idsite=<%= escape_javascript locals[:site_id] %>" style="border:0;" alt="" /></p></noscript>
  15 +<!-- End Piwik Code -->
... ...