added settings for approver election and also for sending emails

This commit is contained in:
Harry Bomrah 2016-08-05 20:00:34 +08:00
parent 239eca2f0d
commit f8f70e0ebe
7 changed files with 185 additions and 23 deletions

View File

@ -4,7 +4,7 @@ class Admin::AnnouncementsController < OrbitAdminController
include Admin::AnnouncementsHelper include Admin::AnnouncementsHelper
before_action ->(module_app = @app_title) { set_variables module_app } before_action ->(module_app = @app_title) { set_variables module_app }
before_action :set_bulletin, only: [:edit, :destroy] before_action :set_bulletin, only: [:edit, :destroy]
before_action :load_access_level before_action :load_access_level, :load_settings
def initialize def initialize
super super
@ -38,10 +38,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def settings def settings
@setting = AnnouncementSetting.first rescue nil @setting = @announcement_setting
if @setting.nil?
@setting = AnnouncementSetting.new
end
end end
def import def import
@ -86,7 +83,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def updatesettings def updatesettings
setting = AnnouncementSetting.first setting = @announcement_setting
setting.update_attributes(settings_params) setting.update_attributes(settings_params)
setting.save setting.save
redirect_to admin_announcement_settings_path redirect_to admin_announcement_settings_path
@ -339,6 +336,13 @@ class Admin::AnnouncementsController < OrbitAdminController
private private
def load_settings
@announcement_setting = AnnouncementSetting.first rescue nil
if @announcement_setting.nil?
@announcement_setting = AnnouncementSetting.create
end
end
def set_bulletin def set_bulletin
@bulletin = Bulletin.find(params[:id]) @bulletin = Bulletin.find(params[:id])
end end

View File

@ -255,7 +255,9 @@ class AnnouncementsController < ApplicationController
access_level = OrbitHelper.user_access_level? access_level = OrbitHelper.user_access_level?
if !announcement.approved && (access_level != "manager" && access_level != "admin") if !announcement.approved && (access_level != "manager" && access_level != "admin")
return {} if !(access_level == "sub_manager" && AnnouncementSetting.first.approvers.include?(OrbitHelper.current_user.id.to_s))
return {}
end
end end
return {} if announcement.category.disable return {} if announcement.category.disable

View File

@ -133,14 +133,23 @@ module Admin::AnnouncementsHelper
end end
def send_notification_mail_to_managers(announcement, type) def send_notification_mail_to_managers(announcement, type)
authorizations = Authorization.where(:module_app_id => @module_app.id) users = []
users = authorizations.collect do |auth| if @announcement_setting.email_to.include?("managers")
auth.user authorizations = Authorization.where(:module_app_id => @module_app.id)
users = authorizations.collect do |auth|
auth.user
end
end
if @announcement_setting.email_to.include?("admins")
wg = Workgroup.where(:key => "admin").first
admins = User.where(:workgroup_id => wg.id)
users.delete(nil)
users = users.concat(admins.to_a)
end
if @announcement_setting.email_to.include?("approvers")
approvers = User.find(@announcement_setting.approvers) rescue []
users = users.concat(approvers.to_a)
end end
wg = Workgroup.where(:key => "admin").first
admins = User.where(:workgroup_id => wg.id)
users.delete(nil)
users = users.concat(admins.to_a)
users.each do |user| users.each do |user|
email = user.member_profile.email email = user.member_profile.email
if !email.nil? && email != "" if !email.nil? && email != ""
@ -210,15 +219,29 @@ module Admin::AnnouncementsHelper
end end
end end
def user_can_approve? def user_can_approve?(anns=nil)
can_approve = false
setting = AnnouncementSetting.first
case @access_level case @access_level
when "admin" when "admin"
return true can_approve = true
when "manager" when "manager"
return true can_approve = true
else else
return false can_approve = false
end end
if !can_approve
if !anns.nil?
if setting.approvers.include?(current_user.id.to_s)
if (current_user.approved_categories_for_module(@module_app).include?(anns.category) rescue false)
can_approve = true
end
end
else
can_approve = setting.approvers.include?(current_user.id.to_s)
end
end
can_approve
end end
end end

View File

@ -4,6 +4,8 @@ class AnnouncementSetting
field :top_limit, type: Integer, :default => 0 field :top_limit, type: Integer, :default => 0
field :pro_enabled, type: Boolean, :default => false field :pro_enabled, type: Boolean, :default => false
field :approvers, type: Array, :default => []
field :email_to, type: Array, :default => ["admins","managers","approvers"]
def self.check_limit_for_user(user_id, b_id = nil) def self.check_limit_for_user(user_id, b_id = nil)
limit = self.first.top_limit rescue 0 limit = self.first.top_limit rescue 0

View File

@ -48,9 +48,9 @@
<% if can_edit_or_delete?(b) %> <% if can_edit_or_delete?(b) %>
<li><a href="/admin/announcements/<%=b.id.to_s%>/edit"><%= t(:edit) %></a></li> <li><a href="/admin/announcements/<%=b.id.to_s%>/edit"><%= t(:edit) %></a></li>
<li><a href="#" class="delete text-error" rel="/admin/announcements/<%=b.id.to_s%>"><%= t(:delete_) %></a></li> <li><a href="#" class="delete text-error" rel="/admin/announcements/<%=b.id.to_s%>"><%= t(:delete_) %></a></li>
<% if (!b.approved || b.reapproval) && user_can_approve? %> <% end %>
<li><a href="<%= page_for_bulletin(b) %>" class="approval_button" data-id="<%= b.id.to_s %>" ><%= t("announcement.approval_waiting") %></a></li> <% if ((!b.approved && !b.rejected && !b.reapproval) || (b.rejected && b.reapproval)) && user_can_approve?(b) %>
<% end %> <li><a href="<%= page_for_bulletin(b) %>" class="approval_button" data-id="<%= b.id.to_s %>" ><%= t("announcement.approval_waiting") %></a></li>
<% end %> <% end %>
</ul> </ul>
</div> </div>

View File

@ -5,7 +5,7 @@
<%= render 'layouts/delete_modal', delete_options: @delete_options %> <%= render 'layouts/delete_modal', delete_options: @delete_options %>
<% if AnnouncementSetting.is_pro? && (@access_level == "admin" || @access_level == "manager") %> <% if AnnouncementSetting.is_pro? && user_can_approve? %>
<%= render :partial=> "approval_modal" %> <%= render :partial=> "approval_modal" %>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -1,3 +1,53 @@
<style type="text/css">
#notification{
background-color: #ececec;
font-size: 14px;
left: 40%;
padding: 10px;
position: absolute;
text-align: center;
top: 40px;
width: auto;
z-index: 1200;
display: none;
}
.badge-info{
margin-left: 10px;
}
#approver-list{
list-style: none;
margin-left: 5px;
}
#approver-list li {
border-bottom: 1px solid #efefef;
margin-bottom: 10px;
padding-bottom: 10px;
}
#approver-list .approver-avatar{
width: 60px;
height: 60px;
border-radius: 50px;
margin-right: 20px;
}
#approver-list .approver-check{
vertical-align: middle;
margin-top: 25px;
}
#approver-list .approver-check input{
margin-right: 5px;
vertical-align: middle;
}
#approver-list .approver-check label{
display: inline;
vertical-align: middle;
}
#approver-list .approver-title{
font-size: 14px;
}
</style>
<% sub_managers = @module_app.sub_managers %>
<div id="notification">Click on Submit to save the changes.</div>
<%= form_for @setting, url: (@setting.new_record? ? admin_announcement_createsettings_path : admin_announcement_updatesettings_path), html: {class: "form-horizontal main-forms"} do |f| %> <%= form_for @setting, url: (@setting.new_record? ? admin_announcement_createsettings_path : admin_announcement_updatesettings_path), html: {class: "form-horizontal main-forms"} do |f| %>
<div class="input-area"> <div class="input-area">
<div class="control-group"> <div class="control-group">
@ -7,8 +57,89 @@
<span class="help-block">Put 0 for unlimited.</span> <span class="help-block">Put 0 for unlimited.</span>
</div> </div>
</div> </div>
<% if !sub_managers.blank? %>
<div class="control-group">
<%= f.label "Approver Setting", :class => "control-label muted" %>
<div class="controls">
<a href="#approverModal" role="button" class="btn" data-toggle="modal">Approvers List</a>
<span class="badge badge-info"><%= @setting.approvers.count %></span>
</div>
</div>
<% else %>
<div class="control-group">
<a href="/admin/authorizations/announcement">Click here to set Sub Managers for this module</a>
</div>
<% end %>
<div class="control-group">
<%= f.label "Send emails to", :class => "control-label muted" %>
<div class="controls">
<input type="checkbox" name="announcement_setting[email_to][]" value="admins" <%= @setting.email_to.include?("admins") ? "checked=checked" : "" %>> Admins
<input type="checkbox" name="announcement_setting[email_to][]" value="managers" <%= @setting.email_to.include?("managers") ? "checked=checked" : "" %>> Managers
<input type="checkbox" name="announcement_setting[email_to][]" value="approvers" <%= @setting.email_to.include?("approvers") ? "checked=checked" : "" %>> Approvers
</div>
</div>
</div>
<div class="hidden-approver-list">
<% sub_managers.each do |sm| %>
<% if @setting.approvers.include?(sm.id.to_s) %>
<input type="hidden" id="check_<%= sm.id.to_s %>" value="<%= sm.id.to_s %>" name="announcement_setting[approvers][]">
<% end %>
<% end %>
</div> </div>
<div class="form-actions"> <div class="form-actions">
<%= f.submit t('submit'), class: 'btn btn-primary' %> <%= f.submit t('submit'), class: 'btn btn-primary' %>
</div> </div>
<% end %> <% end %>
<div id="approverModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="approverModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="approverModalLabel">Sub Managers</h3>
</div>
<div class="modal-body">
<ul id="approver-list">
<% sub_managers.each do |sm| %>
<li>
<%= image_tag sm.member_profile.avatar.thumb, :class => "approver-avatar" %>
<span class="approver-title"><%= sm.name %></span>
<span class="pull-right approver-check">
<input id="checkbox_<%= sm.id %>" type="checkbox" value="<%= sm.id %>" <%= @setting.approvers.include?(sm.id.to_s) ? "checked=checked" : "" %>>
<label for="checkbox_<%= sm.id %>">Approver</label>
</span>
</li>
<% end %>
</ul>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Ok</button>
</div>
</div>
<script type="text/javascript">
var approverList = $(".hidden-approver-list");
$(".approver-check input").on("click",function(){
var el = $(this);
if(el.is(":checked")){
var t = $("<input type='hidden'>");
t.val(el.val());
t.attr("name", "announcement_setting[approvers][]");
t.attr("id", "check_" + el.val());
approverList.append(t);
}else{
approverList.find("#check_" + el.val()).remove();
}
})
$("#approverModal").on("hidden",function(){
$("#notification").slideDown();
$(".badge-info").text($(".hidden-approver-list input").length);
})
</script>