From e1ab4612e1133ff198ea04d42954b8aab3b18419 Mon Sep 17 00:00:00 2001 From: Eduardo Vital Date: Tue, 21 Jul 2015 10:58:30 -0300 Subject: [PATCH] Create environment notification plugin --- plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb | 10 ++++++++++ plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb | 26 ++++++++++++++++++++++++++ plugins/environment_notification/lib/environment_notification_helper.rb | 9 +++++++++ plugins/environment_notification/lib/environment_notification_plugin.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/lib/environment_notifications_user.rb | 10 ++++++++++ plugins/environment_notification/lib/ext/application_controller.rb | 10 ++++++++++ plugins/environment_notification/lib/ext/environment.rb | 5 +++++ plugins/environment_notification/lib/ext/user.rb | 6 ++++++ plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/models/environment_notification_plugin/information_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/success_notification.rb | 2 ++ plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb | 2 ++ plugins/environment_notification/po/environment_notification.pot | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/po/pt/environment_notification.po | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/public/environment_notification_plugin.js | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/public/images/close.png | Bin 0 -> 240 bytes plugins/environment_notification/public/images/greenhide.png | Bin 0 -> 794 bytes plugins/environment_notification/public/images/hide.png | Bin 0 -> 389 bytes plugins/environment_notification/public/images/redclose.png | Bin 0 -> 552 bytes plugins/environment_notification/public/images/show.png | Bin 0 -> 364 bytes plugins/environment_notification/public/public | 1 + plugins/environment_notification/style.css | 224 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/functional/account_controller_test.rb | 35 +++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/functional/home_controller_test.rb | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/unit/environment_notification_helper_test.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/test/unit/environment_notification_test.rb | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb | 36 ++++++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb | 1 + plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb | 40 ++++++++++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb | 1 + plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb | 33 +++++++++++++++++++++++++++++++++ plugins/environment_notification/views/environment_notification_plugin_public/notifications_with_popup.html.erb | 22 ++++++++++++++++++++++ 35 files changed, 1635 insertions(+), 0 deletions(-) create mode 100644 plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb create mode 100644 plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb create mode 100644 plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb create mode 100644 plugins/environment_notification/lib/environment_notification_helper.rb create mode 100644 plugins/environment_notification/lib/environment_notification_plugin.rb create mode 100644 plugins/environment_notification/lib/environment_notifications_user.rb create mode 100644 plugins/environment_notification/lib/ext/application_controller.rb create mode 100644 plugins/environment_notification/lib/ext/environment.rb create mode 100644 plugins/environment_notification/lib/ext/user.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/information_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/success_notification.rb create mode 100644 plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb create mode 100644 plugins/environment_notification/po/environment_notification.pot create mode 100644 plugins/environment_notification/po/pt/environment_notification.po create mode 100644 plugins/environment_notification/public/environment_notification_plugin.js create mode 100644 plugins/environment_notification/public/images/close.png create mode 100644 plugins/environment_notification/public/images/greenhide.png create mode 100644 plugins/environment_notification/public/images/hide.png create mode 100644 plugins/environment_notification/public/images/redclose.png create mode 100644 plugins/environment_notification/public/images/show.png create mode 120000 plugins/environment_notification/public/public create mode 100644 plugins/environment_notification/style.css create mode 100644 plugins/environment_notification/test/functional/account_controller_test.rb create mode 100644 plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb create mode 100644 plugins/environment_notification/test/functional/home_controller_test.rb create mode 100644 plugins/environment_notification/test/unit/environment_notification_helper_test.rb create mode 100644 plugins/environment_notification/test/unit/environment_notification_test.rb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb create mode 100644 plugins/environment_notification/views/environment_notification_plugin_public/notifications_with_popup.html.erb diff --git a/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb new file mode 100644 index 0000000..d336352 --- /dev/null +++ b/plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb @@ -0,0 +1,98 @@ +class EnvironmentNotificationPluginAdminController < AdminController + before_filter :admin_required, :except => [:close_notification, :hide_notification] + def index + @notifications = environment.environment_notifications.order('updated_at DESC') + end + + def new + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new + if request.post? + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.new(params[:notifications]) + @notification.message = @notification.message.html_safe + @notification.environment_id = environment.id + if @notification.save + session[:notice] = _("Notification successfully created") + redirect_to :action => :index + else + session[:notice] = _("Notification couldn't be created") + end + end + end + + def destroy + if request.delete? + notification = environment.environment_notifications.find_by_id(params[:id]) + if notification && notification.destroy + session[:notice] = _('The notification was deleted.') + else + session[:notice] = _('Could not remove the notification') + end + end + redirect_to :action => :index + end + + def edit + @notification = environment.environment_notifications.find_by_id(params[:id]) + if request.post? + if @notification.update_attributes(params[:notifications]) + session[:notice] = _('The notification was edited.') + else + session[:notice] = _('Could not edit the notification.') + end + redirect_to :action => :index + end + end + + def change_status + @notification = environment.environment_notifications.find_by_id(params[:id]) + + @notification.active = !@notification.active + + if @notification.save! + session[:notice] = _('The status of the notification was changed.') + else + session[:notice] = _('Could not change the status of the notification.') + end + + redirect_to :action => :index + end + + def close_notification + result = false + + if logged_in? + @notification = environment.environment_notifications.find_by_id(params[:notification_id]) + + if @notification + @notification.users << current_user + result = @notification.users.include?(current_user) + end + end + + render json: result + end + + def hide_notification + result = false + + if logged_in? + @notification = environment.environment_notifications.find_by_id(params[:notification_id]) + + if @notification + current_notificaions = [] + current_notificaions = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? + current_notificaions << @notification.id unless current_notificaions.include? @notification.id + cookies[:hide_notifications] = JSON.generate(current_notificaions) + result = current_notificaions.include? @notification.id + end + end + + render json: result + end + + protected + def admin_required + redirect_to :root unless current_user.person.is_admin? + end + +end diff --git a/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb b/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb new file mode 100644 index 0000000..1bd4ca0 --- /dev/null +++ b/plugins/environment_notification/controllers/public/environment_notification_plugin_public_controller.rb @@ -0,0 +1,10 @@ +class EnvironmentNotificationPluginPublicController < PublicController + def notifications_with_popup + @hide_notifications = hide_notifications + if params[:previous_path] + @previous_path = params[:previous_path] + else + @previous_path = nil + end + end +end diff --git a/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb b/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb new file mode 100644 index 0000000..2833b07 --- /dev/null +++ b/plugins/environment_notification/db/migrate/20150721132025_create_notification_table.rb @@ -0,0 +1,26 @@ +class CreateNotificationTable < ActiveRecord::Migration + def up + create_table :environment_notifications do |t| + t.text :message + t.integer :environment_id + t.string :type + t.string :title + t.boolean :active + t.boolean :display_only_in_homepage, :default => false + t.boolean :display_to_all_users, :default => false + t.boolean :display_popup, :default => false + t.column :created_at, :datetime + t.column :updated_at, :datetime + end + + create_table :environment_notifications_users, id: false do |t| + t.belongs_to :environment_notification, index: true + t.belongs_to :user, index: true + end + end + + def down + drop_table :environment_notifications + drop_table :environment_notifications_users + end +end diff --git a/plugins/environment_notification/lib/environment_notification_helper.rb b/plugins/environment_notification/lib/environment_notification_helper.rb new file mode 100644 index 0000000..47720ba --- /dev/null +++ b/plugins/environment_notification/lib/environment_notification_helper.rb @@ -0,0 +1,9 @@ +module EnvironmentNotificationHelper + def self.substitute_variables(message, user) + if user + message = message.gsub("%{email}", user.person.email).gsub("%{name}", user.person.name) + end + + message + end +end \ No newline at end of file diff --git a/plugins/environment_notification/lib/environment_notification_plugin.rb b/plugins/environment_notification/lib/environment_notification_plugin.rb new file mode 100644 index 0000000..2b94af1 --- /dev/null +++ b/plugins/environment_notification/lib/environment_notification_plugin.rb @@ -0,0 +1,46 @@ +class EnvironmentNotificationPlugin < Noosfero::Plugin + + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::TagHelper + + def self.plugin_name + "Environment Notifications Plugin" + end + + def self.plugin_description + _("A plugin for environment notifications.") + end + + def stylesheet? + true + end + + def js_files + %w( + public/environment_notification_plugin.js + ) + end + + def body_beginning + expanded_template('environment_notification_plugin_admin/show_notification.html.erb') + end + + def admin_panel_links + {:title => _('Notification Manager'), :url => {:controller => 'environment_notification_plugin_admin', :action => 'index'}} + end + + def account_controller_filters + block = proc do + if !logged_in? + cookies[:hide_notifications] = nil + end + end + + [{ + :type => "after_filter", + :method_name => "clean_hide_notifications_cookie", + :options => { }, + :block => block + }] + end +end diff --git a/plugins/environment_notification/lib/environment_notifications_user.rb b/plugins/environment_notification/lib/environment_notifications_user.rb new file mode 100644 index 0000000..69d63b8 --- /dev/null +++ b/plugins/environment_notification/lib/environment_notifications_user.rb @@ -0,0 +1,10 @@ +class EnvironmentNotificationsUser < ActiveRecord::Base + self.table_name = "environment_notifications_users" + + belongs_to :user + belongs_to :environment_notification, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' + + attr_accessible :user_id, :environment_notification_id + + validates_uniqueness_of :user_id, :scope => :environment_notification_id +end diff --git a/plugins/environment_notification/lib/ext/application_controller.rb b/plugins/environment_notification/lib/ext/application_controller.rb new file mode 100644 index 0000000..445bf0e --- /dev/null +++ b/plugins/environment_notification/lib/ext/application_controller.rb @@ -0,0 +1,10 @@ +require_dependency 'application_controller' + +class ApplicationController + def hide_notifications + invalid_id = -1 + hide_notifications_ids = [-1] + hide_notifications_ids = JSON.parse(cookies[:hide_notifications]) unless cookies[:hide_notifications].blank? + hide_notifications_ids + end +end diff --git a/plugins/environment_notification/lib/ext/environment.rb b/plugins/environment_notification/lib/ext/environment.rb new file mode 100644 index 0000000..0fd9264 --- /dev/null +++ b/plugins/environment_notification/lib/ext/environment.rb @@ -0,0 +1,5 @@ +require_dependency 'environment' + +class Environment + has_many :environment_notifications, class_name: 'EnvironmentNotificationPlugin::EnvironmentNotification' +end diff --git a/plugins/environment_notification/lib/ext/user.rb b/plugins/environment_notification/lib/ext/user.rb new file mode 100644 index 0000000..2cb316e --- /dev/null +++ b/plugins/environment_notification/lib/ext/user.rb @@ -0,0 +1,6 @@ +require_dependency 'user' + +class User + has_many :environment_notifications_users + has_many :environment_notifications, :through => :environment_notifications_users +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb new file mode 100644 index 0000000..35a6ce1 --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/danger_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::DangerNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb new file mode 100644 index 0000000..ad91678 --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb @@ -0,0 +1,50 @@ +class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Base + + self.table_name = "environment_notifications" + + TYPE_LIST = [ + "EnvironmentNotificationPlugin::WarningNotification", + "EnvironmentNotificationPlugin::SuccessNotification", + "EnvironmentNotificationPlugin::InformationNotification", + "EnvironmentNotificationPlugin::DangerNotification" + ] + + attr_accessible :message, :environment_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title + + has_many :environment_notifications_users + has_many :users, :through => :environment_notifications_users + + validates_presence_of :message + validates_presence_of :environment_id + validate :notification_type_must_be_in_type_list + + def notification_type_must_be_in_type_list + unless TYPE_LIST.include?(type) + errors.add(:type, "invalid notification type") + end + end + + scope :active, lambda{|environment| { :conditions => { :environment_id => environment.id, :active => true } } } + + def self.visibles(environment, user, controller_path) + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(environment).order('updated_at DESC') + + if user + active_notifications_ids = notifications.pluck(:id) - user.environment_notifications.pluck(:id) + + notifications = notifications.where(id: active_notifications_ids) + else + notifications = notifications.where(display_to_all_users: true) + end + + if controller_path != "home" + notifications = notifications.where(display_only_in_homepage: false) + end + + notifications + end + + def self.with_popup(environment, user, previous_path) + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(environment, user, previous_path).where(display_popup: true) + end +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb new file mode 100644 index 0000000..661ecdb --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/information_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::InformationNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb new file mode 100644 index 0000000..22db359 --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/success_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::SuccessNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb b/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb new file mode 100644 index 0000000..378bd5a --- /dev/null +++ b/plugins/environment_notification/models/environment_notification_plugin/warning_notification.rb @@ -0,0 +1,2 @@ +class EnvironmentNotificationPlugin::WarningNotification < EnvironmentNotificationPlugin::EnvironmentNotification +end diff --git a/plugins/environment_notification/po/environment_notification.pot b/plugins/environment_notification/po/environment_notification.pot new file mode 100644 index 0000000..f4d9746 --- /dev/null +++ b/plugins/environment_notification/po/environment_notification.pot @@ -0,0 +1,177 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 1.2-514-gc258618\n" +"POT-Creation-Date: 2015-09-24 19:03-0000\n" +"PO-Revision-Date: 2015-09-14 12:12-0000\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: plugins/environment_notification/lib/environment_notification_plugin.rb:11 +msgid "A plugin for environment notifications." +msgstr "" + +#: plugins/environment_notification/lib/environment_notification_plugin.rb:29 +msgid "Notification Manager" +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:14 +msgid "Notification successfully created" +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:17 +msgid "Notification couldn't be created" +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:26 +msgid "The notification was deleted." +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:28 +msgid "Could not remove the notification" +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:38 +msgid "The notification was edited." +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:40 +msgid "Could not edit the notification." +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:52 +msgid "The status of the notification was changed." +msgstr "" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:54 +msgid "Could not change the status of the notification." +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:8 +msgid "There are active notifications in this environment! You can " +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:10 +msgid "manage all notifications here." +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:28 +msgid "Do not show anymore" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:29 +msgid "Hide for now" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:3 +msgid "Environment Notifications" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:7 +msgid "New Notification" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:10 +msgid "Back to control panel" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:16 +msgid "Notifications" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:19 +msgid "Actions" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:30 +msgid "Deactivate" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:30 +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:32 +msgid "Do you want to change the status of this notification?" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:32 +msgid "Activate" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:34 +msgid "Edit" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:35 +msgid "Delete" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:35 +msgid "Do you want to delete this notification?" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:3 +msgid "Back" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:9 +msgid "Optional Title:" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:11 +msgid "Enter your message here:" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:13 +msgid "" +"Obs: You can use %{name} and %{email} variables to put the user's name and " +"email in the message." +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:16 +msgid "Notifications Status" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Notifications Color/Type" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Blue - Information" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Yellow - Warning" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Green - Success" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Red - Danger" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:21 +msgid "Display only in the homepage" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:25 +msgid "Display to not logged users too" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:29 +msgid "Display popup until user close the notification" +msgstr "" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:33 +msgid "Save" +msgstr "" diff --git a/plugins/environment_notification/po/pt/environment_notification.po b/plugins/environment_notification/po/pt/environment_notification.po new file mode 100644 index 0000000..8acc049 --- /dev/null +++ b/plugins/environment_notification/po/pt/environment_notification.po @@ -0,0 +1,178 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: 1.2-514-gbe9e36b\n" +"POT-Creation-Date: 2015-09-24 18:52-0000\n" +"PO-Revision-Date: 2015-09-14 12:12-0000\n" +"Last-Translator: FULL NAME \n" +"Language-Team: Portuguese\n" +"Language: Portuguese\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: plugins/environment_notification/lib/environment_notification_plugin.rb:11 +msgid "A plugin for environment notifications." +msgstr "Plugin para notificações do ambiente" + +#: plugins/environment_notification/lib/environment_notification_plugin.rb:29 +msgid "Notification Manager" +msgstr "Gerenciador de Notificações" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:14 +msgid "Notification successfully created" +msgstr "Notificação criada com sucesso" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:17 +msgid "Notification couldn't be created" +msgstr "Não foi possível criar notificação" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:26 +msgid "The notification was deleted." +msgstr "A notificação foi removida" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:28 +msgid "Could not remove the notification" +msgstr "Não foi possível remover a notificação" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:38 +msgid "The notification was edited." +msgstr "A notificação foi editada." + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:40 +msgid "Could not edit the notification." +msgstr "Não foi possível editar a notificação." + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:52 +msgid "The status of the notification was changed." +msgstr "O status da notificação foi modificado" + +#: plugins/environment_notification/controllers/environment_notification_plugin_admin_controller.rb:54 +msgid "Could not change the status of the notification." +msgstr "Não foi possível alterar o status da notificação" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:8 +msgid "There are active notifications in this environment! You can " +msgstr "Existem notificações ativas neste ambiente! Você pode " + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:10 +msgid "manage all notifications here." +msgstr "gerenciar todas as notificações aqui." + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:28 +msgid "Do not show anymore" +msgstr "Não mostrar mais" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb:29 +msgid "Hide for now" +msgstr "Ocultar momentaneamente" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:3 +msgid "Environment Notifications" +msgstr "Notificações do Ambiente" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:7 +msgid "New Notification" +msgstr "Nova Notificação" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:10 +msgid "Back to control panel" +msgstr "Voltar ao Painel de Controle" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:16 +msgid "Notifications" +msgstr "Notificações" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:19 +msgid "Actions" +msgstr "Ações" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:30 +msgid "Deactivate" +msgstr "Desativar" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:30 +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:32 +msgid "Do you want to change the status of this notification?" +msgstr "Você quer alterar o status dessa notificação?" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:32 +msgid "Activate" +msgstr "Ativar" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:34 +msgid "Edit" +msgstr "Editar" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:35 +msgid "Delete" +msgstr "Remover" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb:35 +msgid "Do you want to delete this notification?" +msgstr "Você quer remover essa notificação?" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:3 +msgid "Back" +msgstr "Voltar" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:9 +msgid "Optional Title:" +msgstr "Título Opcional:" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:11 +msgid "Enter your message here:" +msgstr "Entre com a sua mensagem aqui:" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:13 +msgid "" +"Obs: You can use %{name} and %{email} variables to put the user's name and " +"email in the message." +msgstr "" +"Obs: Você pode usar as variáveis %{name} e %{email} para inserir o nome e " +"email do usuário logado na mensagem." + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:16 +msgid "Notifications Status" +msgstr "Status da Notificação" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Notifications Color/Type" +msgstr "Cor/Tipo de Notificação" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Blue - Information" +msgstr "Azul - Informação" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Yellow - Warning" +msgstr "Amarelo - Atenção" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Green - Success" +msgstr "Verde - Sucesso" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:18 +msgid "Red - Danger" +msgstr "Vermelho - Perigo" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:21 +msgid "Display only in the homepage" +msgstr "Apresentar apenas na página inicial" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:25 +msgid "Display to not logged users too" +msgstr "Apresentar notificação para usuários não logados também" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:29 +msgid "Display popup until user close the notification" +msgstr "Apresentar Popup da notificação até que o usuário a feche" + +#: plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb:33 +msgid "Save" +msgstr "Salvar" diff --git a/plugins/environment_notification/public/environment_notification_plugin.js b/plugins/environment_notification/public/environment_notification_plugin.js new file mode 100644 index 0000000..fc0943c --- /dev/null +++ b/plugins/environment_notification/public/environment_notification_plugin.js @@ -0,0 +1,95 @@ +(function($) { + "use strict"; + + function notificationBar() { + var completeMessage = $(".notification-bar").remove(); + $("#content-inner").before(completeMessage); + } + + function closeNotification(){ + var notification = $(this).parent(); + var id = notification.attr("data-notification"); + + $.ajax({ + url: noosfero_root()+"/admin/plugin/environment_notification/close_notification", + type: "POST", + data: {notification_id: id}, + success: function(response) { + notification.fadeOut(); + } + }); + } + + function hideNotification(){ + var notification = $(this).parent(); + var id = notification.attr("data-notification"); + + $.ajax({ + url: noosfero_root()+"/admin/plugin/environment_notification/hide_notification", + type: "POST", + data: {notification_id: id}, + success: function(response) { + notification.fadeOut(); + } + }); + } + + function hideUserNotification(){ + var ids = $.cookie('hide_notifications'); + if(ids === null) { + return null; + } + + if(ids.startsWith('[') && ids.endsWith(']')){ + ids = ids.substring(1, ids.length - 1); + ids = ids.split(","); + + for(var i = 0; i < ids.length; i++) { + $('[data-notification="' + ids[i] + '"]').fadeOut(); + } + } + } + + function mceRestrict() { + tinyMCE.init({ + menubar : false, + selector: "textarea", + plugins: [ + "autolink link" + ], + toolbar: "bold italic underline | link" + }); + } + + function showPopup() { + if($('.action-home-index').length > 0) { + jQuery(function($){ + $.colorbox({href: noosfero_root()+'/plugin/environment_notification/public/notifications_with_popup?previous_path=home'}); + }); + } + else { + jQuery(function($){ + $.colorbox({href: noosfero_root()+'/plugin/environment_notification/public/notifications_with_popup'}); + }); + } + } + + $(document).ready(function(){ + notificationBar(); + $(".notification-close").on("click", closeNotification); + $(".notification-hide").on("click", hideNotification); + + if($('.environment-notification-plugin-message').length > 0){ + mceRestrict(); + } + + if($('.notification-bar').length > 0){ + hideUserNotification(); + } + + if($('[notification-display-popup="true"]').length > 0){ + showPopup(); + } + }); + +})($); \ No newline at end of file diff --git a/plugins/environment_notification/public/images/close.png b/plugins/environment_notification/public/images/close.png new file mode 100644 index 0000000..e51bbb9 Binary files /dev/null and b/plugins/environment_notification/public/images/close.png differ diff --git a/plugins/environment_notification/public/images/greenhide.png b/plugins/environment_notification/public/images/greenhide.png new file mode 100644 index 0000000..c69cd9d Binary files /dev/null and b/plugins/environment_notification/public/images/greenhide.png differ diff --git a/plugins/environment_notification/public/images/hide.png b/plugins/environment_notification/public/images/hide.png new file mode 100644 index 0000000..5804c92 Binary files /dev/null and b/plugins/environment_notification/public/images/hide.png differ diff --git a/plugins/environment_notification/public/images/redclose.png b/plugins/environment_notification/public/images/redclose.png new file mode 100644 index 0000000..d65e786 Binary files /dev/null and b/plugins/environment_notification/public/images/redclose.png differ diff --git a/plugins/environment_notification/public/images/show.png b/plugins/environment_notification/public/images/show.png new file mode 100644 index 0000000..477815e Binary files /dev/null and b/plugins/environment_notification/public/images/show.png differ diff --git a/plugins/environment_notification/public/public b/plugins/environment_notification/public/public new file mode 120000 index 0000000..73787ba --- /dev/null +++ b/plugins/environment_notification/public/public @@ -0,0 +1 @@ +/home/arthurmde/Documentos/projects/noosfero/config/plugins/environment_notification/public \ No newline at end of file diff --git a/plugins/environment_notification/style.css b/plugins/environment_notification/style.css new file mode 100644 index 0000000..e2f6c37 --- /dev/null +++ b/plugins/environment_notification/style.css @@ -0,0 +1,224 @@ +.notification-bar { + display: block; +} + +.notification:hover { + opacity: 0.8; +} + +#notification-manager { + overflow: auto; +} + +.notification .notification-close { + background: url(public/images/close.png) no-repeat; + background-position: center; + width: 20px; + height: 20px; +} + +.warningnotification, +.informationnotification, +.successnotification, +.dangernotification, +.adminnotification { + margin-bottom: 10px; + padding: 7px 10px; + border-radius: 5px; + border: 1px solid blue; + font-size: 16px; + color: white; + overflow: auto; +} + +.warningnotification p, +.informationnotification p, +.successnotification p, +.dangernotification p, +.adminnotification p { + margin: 0px; +} + +.warningnotification { + background: #fcf8e3; + border: 1px solid #faebcc; + color: #8a6d3b; +} + +.warningnotification p a{ + font-weight: bold; + color: #8a6d3b; +} + + +.informationnotification { + background: #d9edf7; + border: 1px solid #bce8f1; + color: #31708f; +} + +.informationnotification p a{ + font-weight: bold; + color: #31708f; +} + +.successnotification { + background: #dff0d8; + border: 1px solid #d6e9c6; + color: #3c763d; +} + +.successnotification p a{ + font-weight: bold; + color: #3c763d; +} + +.dangernotification { + background: #f2dede; + border: 1px solid #ebccd1; + color: #a94442; +} + +.dangernotification p a{ + font-weight: bold; + color: #a94442; +} + +.adminnotification { + background: #9a959a; + border: 1px solid #9a959a; +} + +.adminnotification p a{ + font-weight: bold; + color: white; +} + +a.button.icon-deactivate { + background: url(public/images/hide.png) no-repeat; + background-position: center; +} + +a.button.icon-activate { + background: url(public/images/show.png) no-repeat; + background-position: center; +} + +.notification-line { + display: inline; + padding-top: 10px; + vertical-align: middle; + border-bottom: 1px solid #ccc; +} + +.notification-title-bar { + float: left; + width: 100%; + font-style: 14px; + font-weight: 700; + border-bottom: 2px solid black; + padding: 9px 0; +} + +.notification-title { + width: 80%; + float: left; + text-align: center; +} + +.notification-modal .notification-with-title { + margin-bottom: 0px; +} + +.notification-modal .notification .notification-title { + width: 100%; + float: left; + font-weight: bold; + text-align: left; +} + +.notification-modal .notification-with-title-message { + width: 100%; + float: left; + border-radius: 3px; + margin-bottom: 10px; + background-color: #f5f5f5; + font-size: 14px; + overflow: auto; +} + +.notification-modal .notification-with-title-message p{ + padding: 0px 7px; +} + +.notification-modal .notification-with-title-message p a{ + color: black; + font-weight: bold; +} + + +.action-title { + width: 20%; + float: left; + text-align: center; +} + +.notification-action { + width: 18%; + float: left; + height: 30px; + padding-top: 9px; +} + +.main-bar .button, +.notification-action .button { + border-radius: 3px; +} + +.notification-message { + width: 82%; + float: left; +} + +.new-notification { + float: right; + width: auto; +} + +.back-button { + float: left; +} + +.main-bar { + display: inline; + width: 100%; +} + +.notification-bar .notification .notification-message { + width: 90%; + float: left; +} + +.notification-bar .notification .notification-close { + background: url(public/images/redclose.png) no-repeat; + background-position: center; + width: 20px; + height: 20px; + float: right; + cursor: pointer; +} + +.notification-bar .notification .notification-hide { + background: url(public/images/greenhide.png) no-repeat; + background-position: center; + width: 20px; + height: 20px; + float: right; + cursor: pointer; +} + +.notification-modal { + display: block; + min-width: 400px; + max-width: 700px; +} \ No newline at end of file diff --git a/plugins/environment_notification/test/functional/account_controller_test.rb b/plugins/environment_notification/test/functional/account_controller_test.rb new file mode 100644 index 0000000..43891fe --- /dev/null +++ b/plugins/environment_notification/test/functional/account_controller_test.rb @@ -0,0 +1,35 @@ +require File.expand_path(File.dirname(__FILE__)) + '/../../../../test/test_helper' +require 'account_controller' + +class AccountController; def rescue_action(e) raise e end; +end + +class AccountControllerTest < ActionController::TestCase + def setup + @controller = AccountController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + + @environment = Environment.default + @environment.enable_plugin('EnvironmentNotificationPlugin') + @environment.save! + + login_as(@person.user.login) + end + + attr_accessor :person + + should 'clean hide_notifications cookie after logout' do + @request.cookies[:hide_notifications] = JSON.generate([1,2]) + get :index + assert !@request.cookies[:hide_notifications].blank? + + @request.cookies[:hide_notifications] = nil + get :logout + assert_nil session[:user] + assert_response :redirect + assert_equal 1, @controller.hide_notifications.count + assert @controller.hide_notifications.include?(-1) + end +end diff --git a/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb b/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb new file mode 100644 index 0000000..1ff4671 --- /dev/null +++ b/plugins/environment_notification/test/functional/environment_notification_plugin_admin_controller_test.rb @@ -0,0 +1,186 @@ +require File.expand_path(File.dirname(__FILE__)) + '/../../../../test/test_helper' +require( + File.expand_path(File.dirname(__FILE__)) + + '/../../controllers/environment_notification_plugin_admin_controller' +) + +class EnvironmentNotificationPluginAdminController; def rescue_action(e) raise e end; +end + +class EnvironmentNotificationPluginAdminControllerTest < ActionController::TestCase + def setup + @controller = EnvironmentNotificationPluginAdminController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + + @environment = Environment.default + @environment.enable_plugin('EnvironmentNotificationPlugin') + @environment.save! + + login_as(@person.user.login) + end + + attr_accessor :person + + should 'an admin be able to create a notification' do + @environment.add_admin(@person) + post :new, :notifications => { + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + assert_redirected_to :action => 'index' + notification = EnvironmentNotificationPlugin::EnvironmentNotification.last + assert_equal "Message", notification.message + assert notification.active + assert_equal "EnvironmentNotificationPlugin::DangerNotification", notification.type + end + + should 'an user not to be able to create a notification' do + post :new, :notifications => { + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + assert_redirected_to :root + assert_nil EnvironmentNotificationPlugin::EnvironmentNotification.last + end + + should 'an admin be able to edit a notification' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :edit, :id => @notification.id, :notifications => { + :message => "Edited Message", + :active => false, + :type => "EnvironmentNotificationPlugin::WarningNotification" + } + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.last + assert_redirected_to :action => 'index' + assert_equal "Edited Message", @notification.message + assert !@notification.active + assert_equal "EnvironmentNotificationPlugin::WarningNotification", @notification.type + end + + should 'an user not to be able to edit a notification' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :edit, :notifications => { + :message => "Edited Message", + :active => false, + :type => "EnvironmentNotificationPlugin::DangerNotification" + } + @notification.reload + assert_redirected_to :root + assert_equal "Message", @notification.message + assert @notification.active + end + + should 'an admin be able to destroy a notification' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + delete :destroy, :id => @notification.id + assert_nil EnvironmentNotificationPlugin::EnvironmentNotification.find_by_id(@notification.id) + end + + should 'an user not to be able to destroy a notification' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + delete :destroy, :id => @notification.id + + assert_redirected_to :root + assert_not_nil EnvironmentNotificationPlugin::EnvironmentNotification.find_by_id(@notification.id) + end + + should 'an admin be able to change Notification status' do + @environment.add_admin(@person) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :change_status, :id => @notification.id + assert_redirected_to :action => 'index' + + @notification.reload + assert !@notification.active + end + + should 'an user not be able to change Notification status' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :change_status, :id => @notification.id + assert_redirected_to :root + + @notification.reload + assert @notification.active + end + + should 'a logged in user be able to permanently hide notifications' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + post :close_notification, :notification_id => @notification.id + assert_equal "true", @response.body + assert @notification.users.include?(@person.user) + end + + should 'a logged in user be able to momentarily hide notifications' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + @another_notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Another Message", + :active => true, + :type => "EnvironmentNotificationPlugin::WarningNotification" + ) + post :hide_notification, :notification_id => @notification.id + assert_equal "true", @response.body + assert @controller.hide_notifications.include?(@notification.id) + assert !@controller.hide_notifications.include?(@another_notification.id) + end + + should 'not momentarily hide any notification if its id is not found' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + post :hide_notification, :notification_id => nil + assert_equal "false", @response.body + assert !@controller.hide_notifications.include?(@notification.id) + end +end diff --git a/plugins/environment_notification/test/functional/home_controller_test.rb b/plugins/environment_notification/test/functional/home_controller_test.rb new file mode 100644 index 0000000..73dfd33 --- /dev/null +++ b/plugins/environment_notification/test/functional/home_controller_test.rb @@ -0,0 +1,120 @@ +require File.expand_path(File.dirname(__FILE__)) + '/../../../../test/test_helper' +require 'home_controller' + +class HomeController; def rescue_action(e) raise e end; +end + +class HomeControllerTest < ActionController::TestCase + def setup + @controller = HomeController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @person = create_user('person').person + + @environment = Environment.default + @environment.enable_plugin('EnvironmentNotificationPlugin') + @environment.save! + end + + attr_accessor :person + + should 'an active notification be displayed on home page for a logged in user' do + login_as(@person.user.login) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_match /Hello, this is a Notification Message/, @response.body + end + + + should 'an active notification not be displayed on home page for unlogged user' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end + + should 'an active notification be displayed on home page for unlogged user' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :display_to_all_users => true, + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_match /Hello, this is a Notification Message/, @response.body + end + + should 'only display the notification with display_to_all_users option for unlogged user ' do + @notification1 = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is an old Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + @notification2 = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a new Notification Message", + :display_to_all_users => true, + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + + + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + assert_match /Hello, this is a new Notification Message/, @response.body + end + + should 'an inactive notification not be displayed on home page' do + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => false, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end + + + should 'an active notification not be displayed to a logged in user after been closed by him' do + login_as(@person.user.login) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Hello, this is a Notification Message", + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + @notification.users << @person.user + @notification.save! + assert_equal true, @notification.users.include?(@person.user) + get :index + assert_no_match /Hello, this is a Notification Message/, @response.body + end + + should 'a notification be displayed with a Popup' do + login_as(@person.user.login) + @notification = EnvironmentNotificationPlugin::EnvironmentNotification.create( + :environment_id => @environment.id, + :message => "Message", + :display_popup => true, + :active => true, + :type => "EnvironmentNotificationPlugin::DangerNotification" + ) + assert_equal true, @notification.display_popup? + + get :index + assert_no_match /div id="cboxWrapper"/, @response.body + end +end diff --git a/plugins/environment_notification/test/unit/environment_notification_helper_test.rb b/plugins/environment_notification/test/unit/environment_notification_helper_test.rb new file mode 100644 index 0000000..aada279 --- /dev/null +++ b/plugins/environment_notification/test/unit/environment_notification_helper_test.rb @@ -0,0 +1,46 @@ +require_relative '../../../../test/test_helper' + +class EnvironmentNotificationHelperTest < ActiveSupport::TestCase + def setup + @env = Environment.default + @env.enable_plugin('EnvironmentNotificationPlugin') + + @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login => "new_user", :password => "test", :password_confirmation => "test", :name => "UserName") + end + + should 'substitute all email variables to the current user email' do + message = "Hello user with email %{email}! please, update your current email (%{email})." + + new_message = EnvironmentNotificationHelper.substitute_variables(message, @user) + + assert message != new_message + assert_equal new_message, "Hello user with email user@domain.com! please, update your current email (user@domain.com)." + end + + should 'not substitute emails variables if there is no current user' do + message = "Hello user with email %{email}! please, update your current email (%{email})." + + new_message = EnvironmentNotificationHelper.substitute_variables(message, nil) + + assert_equal message, new_message + assert_not_include new_message, "user@domain.com" + end + + should 'substitute all name variables to the current user name' do + message = "Hello %{name}! is %{name} your real name?." + + new_message = EnvironmentNotificationHelper.substitute_variables(message, @user) + + assert message != new_message + assert_equal new_message, "Hello UserName! is UserName your real name?." + end + + should 'not substitute name variables if there is no current user' do + message = "Hello %{name}! is %{name} your real name?." + + new_message = EnvironmentNotificationHelper.substitute_variables(message, nil) + + assert_equal message, new_message + assert_not_include new_message, "UserName" + end +end \ No newline at end of file diff --git a/plugins/environment_notification/test/unit/environment_notification_test.rb b/plugins/environment_notification/test/unit/environment_notification_test.rb new file mode 100644 index 0000000..b2e0aab --- /dev/null +++ b/plugins/environment_notification/test/unit/environment_notification_test.rb @@ -0,0 +1,162 @@ +require_relative '../../../../test/test_helper' + +class EnvironmentNotificationTest < ActiveSupport::TestCase + + def setup + @env = Environment.default + @env.enable_plugin('EnvironmentNotificationPlugin') + + User.destroy_all + EnvironmentNotificationPlugin::EnvironmentNotification.destroy_all + EnvironmentNotificationsUser.destroy_all + + @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login => "new_user", :password => "test", :password_confirmation => "test") + @danger_notification = EnvironmentNotificationPlugin::DangerNotification.create!( + :environment_id => @env.id, + :message => "Danger Message", + :active => true, + ) + + @warning_notification = EnvironmentNotificationPlugin::WarningNotification.create!( + :environment_id => @env.id, + :message => "Warning Message", + :active => true, + ) + + @information_notification = EnvironmentNotificationPlugin::InformationNotification.create!( + :environment_id => @env.id, + :message => "Information Message", + :active => true, + ) + end + + should 'get all notifications that a user did not closed' do + @information_notification.users << @user + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, nil) + + assert notifications.include?(@danger_notification) + assert notifications.include?(@warning_notification) + assert !notifications.include?(@information_notification) + end + + should 'get only notifications configured to be displayed to all users' do + @information_notification.display_to_all_users = true + @information_notification.save! + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, nil, nil) + + assert !notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + end + + should 'get only notifications configured to be displayed to all users and in all pages' do + @information_notification.display_to_all_users = true + @information_notification.display_only_in_homepage = true + @information_notification.save! + + @danger_notification.display_to_all_users = true + @danger_notification.save! + + @warning_notification.display_only_in_homepage = true + @warning_notification.save! + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, nil, 'not_home') + + assert notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert !notifications.include?(@information_notification) + end + + should 'get only notifications configured to be displayed in all pages' do + @danger_notification.display_to_all_users = true + @danger_notification.display_only_in_homepage = true + @danger_notification.save! + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, "not_home") + + assert !notifications.include?(@danger_notification) + assert notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, nil, "home") + + assert notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert !notifications.include?(@information_notification) + end + + should 'get only notifications configured to be displayed to all users and in all pages and not closed by an user' do + @information_notification.display_to_all_users = true + @information_notification.save! + + @danger_notification.display_to_all_users = true + @danger_notification.display_only_in_homepage = true + @danger_notification.save! + + @warning_notification.display_to_all_users = true + @warning_notification.save! + + @warning_notification.users << @user + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, 'not_home') + + assert !notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + end + + should 'get only active notifications' do + @information_notification.active = false + @information_notification.save! + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, 'home') + + assert notifications.include?(@danger_notification) + assert notifications.include?(@warning_notification) + assert !notifications.include?(@information_notification) + end + + should 'get only notifications with popup' do + @information_notification.display_popup = true + @information_notification.display_to_all_users = true + @information_notification.save! + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.with_popup(@env, @user, 'home') + + assert !notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.with_popup(@env, nil, nil) + + assert !notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + end + + should 'get only notifications with popup not closed by an user' do + @information_notification.display_popup = true + @information_notification.display_to_all_users = true + @information_notification.save! + + @danger_notification.display_popup = true + @danger_notification.display_to_all_users = true + @danger_notification.save! + + @danger_notification.users << @user + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.with_popup(@env, @user, 'home') + + assert !notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.with_popup(@env, nil, nil) + + assert notifications.include?(@danger_notification) + assert !notifications.include?(@warning_notification) + assert notifications.include?(@information_notification) + end +end diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb new file mode 100644 index 0000000..d8327f5 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/_form.html.erb @@ -0,0 +1,36 @@ +<% abstract_options = {:value => @notification.message, :style => 'width: 100%; height: 200px;', :class => "environment-notification-plugin-message" } %> + +<%= button :back, _('Back'), :controller => 'environment_notification_plugin_admin' %> + +<%= form_for :notifications do |f| %> + + <%= render :file => 'shared/tiny_mce' %> + + <%= labelled_form_field(_("Optional Title:"), f.text_field(:title, value: @notification.title)) %> + + <%= labelled_form_field(_("Enter your message here:"), f.text_area(:message, abstract_options)) %> + + <%= _("Obs: You can use %{name} and %{email} variables to put the user's name and email in the message.") %> + + + <%= labelled_form_field(_('Notifications Status'), select(:notifications, :active, options_for_select_with_title({"Active" => true, "Inactive" => false}, @notification.active))) %> + + <%= labelled_form_field(_('Notifications Color/Type'), select(:notifications, :type, options_for_select_with_title({_("Blue - Information") => "EnvironmentNotificationPlugin::InformationNotification", _("Yellow - Warning") => "EnvironmentNotificationPlugin::WarningNotification", _("Green - Success") => "EnvironmentNotificationPlugin::SuccessNotification", _("Red - Danger") => "EnvironmentNotificationPlugin::DangerNotification"}, @notification.type))) %> + +
+ <%= labelled_check_box(_("Display only in the homepage"), 'notifications[display_only_in_homepage]', '1', @notification.display_only_in_homepage?) %> +
+ +
+ <%= labelled_check_box(_("Display to not logged users too"), 'notifications[display_to_all_users]', '1', @notification.display_to_all_users?) %> +
+ +
+ <%= labelled_check_box(_("Display popup until user close the notification"), 'notifications[display_popup]', '1', @notification.display_popup?) %> +
+ + <% button_bar do %> + <%= submit_button 'save', _('Save'), :cancel => { :action => 'index' } %> + <% end %> + +<% end %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb new file mode 100644 index 0000000..2872e82 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/edit.html.erb @@ -0,0 +1 @@ +<%= render :partial => "form" %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb new file mode 100644 index 0000000..6978526 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/index.html.erb @@ -0,0 +1,40 @@ +
+
+

<%= _("Environment Notifications") %>

+
+
+
+ <%= button :new, _('New Notification'), {:action => :new}, :method => :get %> +
+
+ <%= button :back, _('Back to control panel'), {:controller => 'admin_panel', :action => :index}, :method => :get %> +
+
+ +
+
+ <%= _('Notifications') %> +
+
+ <%= _('Actions') %> +
+
+ + <% @notifications.each do |notification| %> +
+
+ <%= truncate(notification.message, length: 50) %> +
+
+ <% if notification.active? %> + <%= button_without_text :deactivate, _('Deactivate'), {:action => :change_status, :id => notification}, :method => :post, :confirm => _("Do you want to change the status of this notification?") %> + <% else %> + <%= button_without_text :activate, _('Activate'), {:action => :change_status, :id => notification}, :method => :post, :confirm => _("Do you want to change the status of this notification?") %> + <% end %> + <%= button_without_text :edit, _('Edit'), {:action => 'edit', :id => notification.id} if !remove_content_button(:edit, notification) %> + <%= button_without_text :delete, _('Delete'), {:action => :destroy, :id => notification}, :method => :delete, :confirm => _("Do you want to delete this notification?") %> +
+
+ <% end %> +
+ diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb new file mode 100644 index 0000000..2872e82 --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/new.html.erb @@ -0,0 +1 @@ +<%= render :partial => "form" %> diff --git a/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb b/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb new file mode 100644 index 0000000..189e42d --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_admin/show_notification.html.erb @@ -0,0 +1,33 @@ +<% if current_user && current_user.person.is_admin? %> + <% active_notifications = EnvironmentNotificationPlugin::EnvironmentNotification.active(environment) %> + <% unless active_notifications.blank? %> +
+
+
+

+ <%= _("There are active notifications in this environment! You can ") %> + + <%= _("manage all notifications here.") %> + +

+
+
+
+ <% end %> +<% end %> + +<% @notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(environment, current_user, controller_path).where("id NOT IN (?)", hide_notifications) %> + +
+ <% @notifications.each do |notification| %> +
notification" data-notification="<%=notification.id%>" notification-display-popup="<%=notification.display_popup?%>"> +
+ <%= EnvironmentNotificationHelper.substitute_variables(notification.message, current_user) %> +
+ <% if logged_in? %> +
+
+ <% end %> +
+ <% end %> +
diff --git a/plugins/environment_notification/views/environment_notification_plugin_public/notifications_with_popup.html.erb b/plugins/environment_notification/views/environment_notification_plugin_public/notifications_with_popup.html.erb new file mode 100644 index 0000000..8f232cf --- /dev/null +++ b/plugins/environment_notification/views/environment_notification_plugin_public/notifications_with_popup.html.erb @@ -0,0 +1,22 @@ +<% @notifications = EnvironmentNotificationPlugin::EnvironmentNotification.with_popup(environment, current_user, @previous_path).where("id NOT IN (?)", @hide_notifications) %> + +
+ <% @notifications.each do |notification| %> + <% if !notification.title.blank? %> +
notification notification-with-title" data-notification="<%=notification.id%>"> +
+ <%= notification.title %> +
+
+
+ <%= EnvironmentNotificationHelper.substitute_variables(notification.message, current_user) %> +
+ <% else %> +
notification notification-without-title" data-notification="<%=notification.id%>"> +
+ <%= EnvironmentNotificationHelper.substitute_variables(notification.message, current_user) %> +
+
+ <% end %> + <% end %> +
-- libgit2 0.21.2