add privacy setting for file

This commit is contained in:
邱博亞 2020-12-19 18:28:18 +08:00
parent 55938d106a
commit 5448b69278
4 changed files with 179 additions and 135 deletions

View File

@ -233,7 +233,10 @@ class AnnouncementsController < ApplicationController
"tag" => tag.name , "tag" => tag.name ,
"url" => OrbitHelper.page_for_tag(tag) "url" => OrbitHelper.page_for_tag(tag)
} } rescue [] } } rescue []
files = announcement.bulletin_files.map{|file| { "file_url" => file.file.url, "file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '') } if file.enabled_for?(locale) } rescue [] files = announcement.bulletin_files.map do |file|
{ "file_url" => file.file.url,
"file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '') } if file.enabled_for?(locale) and file.can_access?(OrbitHelper.current_user)
end rescue []
files.delete(nil) files.delete(nil)
files.each do |file| files.each do |file|
if file["file_url"] =="" || file["file_url"] == nil if file["file_url"] =="" || file["file_url"] == nil

View File

@ -9,7 +9,7 @@ class BulletinFile
field :description, localize: true field :description, localize: true
field :title, localize: true field :title, localize: true
field :choose_lang, :type => Array, :default => ["en","zh_tw"] field :choose_lang, :type => Array, :default => ["en","zh_tw"]
field :privacy_type, type: String, default: 'public'
belongs_to :bulletin belongs_to :bulletin
@ -20,5 +20,11 @@ class BulletinFile
return self.choose_lang.include?(lang) return self.choose_lang.include?(lang)
end end
end end
def can_access?(user)
if user.nil? && self.privacy_type == 'logged_in'
return false
else
return true
end
end
end end

View File

@ -13,6 +13,12 @@
cursor: default; cursor: default;
display: inline-block; display: inline-block;
} }
.add-on.btn-group .dropdown-menu{
margin: 0;
}
.main-forms fieldset .input-area .controls .input-prepend a:hover{
color: #fff;
}
</style> </style>
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/bootstrap-fileupload" %> <%= javascript_include_tag "lib/bootstrap-fileupload" %>
@ -482,138 +488,147 @@
}) })
</script> </script>
<% end %> <% end %>
<% content_for :page_specific_javascript do %> <script>
<script> function Appendzero(obj)
function Appendzero(obj) {
{ if(obj<10) return "0" +""+ obj;
if(obj<10) return "0" +""+ obj; else return obj;
else return obj; }
}
$(function() {
$(function() { $('#bulletin_open_comment').change(function(){
$('#bulletin_open_comment').change(function(){ $(this).prop('checked') ? $('div[data-for="open_comment"]').removeClass('hide') : $('div[data-for="open_comment"]').addClass('hide')
$(this).prop('checked') ? $('div[data-for="open_comment"]').removeClass('hide') : $('div[data-for="open_comment"]').addClass('hide') })
}) $('.role_all_member').change(function(){
$('.role_all_member').change(function(){ if ($(this).prop('checked')){
if ($(this).prop('checked')){ $('.role').each(function() {
$('.role').each(function() { $(this).prop("checked", true);
$(this).prop("checked", true); })
}) } else{
} else{ $('.role').each(function() {
$('.role').each(function() { $(this).prop("checked", false);
$(this).prop("checked", false); })
})
}
})
$('.role').change(function(){
if (!$(this).prop('checked') && $('.role_all_member').prop('checked')){
$('.role_all_member').prop('checked',false)
}
})
if (location.pathname.substr(-3)=='new'){
var getDate = new Date();
var toDay = getDate.getFullYear()+"/"+ (Appendzero(getDate.getMonth()+1))+"/"+Appendzero(getDate.getDate())+" "+Appendzero(getDate.getHours())+":"+Appendzero(getDate.getMinutes());
$('input[name="bulletin[postdate]"]').val(toDay);
} }
})
$('.role').change(function(){
if (!$(this).prop('checked') && $('.role_all_member').prop('checked')){
$('.role_all_member').prop('checked',false)
}
})
if (location.pathname.substr(-3)=='new'){
var getDate = new Date();
var toDay = getDate.getFullYear()+"/"+ (Appendzero(getDate.getMonth()+1))+"/"+Appendzero(getDate.getDate())+" "+Appendzero(getDate.getHours())+":"+Appendzero(getDate.getMinutes());
$('input[name="bulletin[postdate]"]').val(toDay);
}
$("#main-wrap").after(""); $("#main-wrap").after("");
$(document).on('click', '#add_link', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_bulletin_links", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_link', f, :bulletin_links) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.tab-content').children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
formTip();
});
$(document).on('click', '#add_file', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_bulletin_files", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_file', f, :bulletin_files) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
$(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
});
formTip();
});
$(document).on('click', '.delete_link', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.delete_file', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.remove_existing_record', function(){
if(confirm("<%= I18n.t(:sure?)%>")){
$(this).children('.should_destroy').attr('value', 1);
$(this).parents('.start-line').hide();
}
});
$('#remind-check').prop('checked') ? '':$('.content-box').addClass('hide')
$('#remind-check').on('change', function() {
$(this).prop('checked') ? $('.content-box').removeClass('hide'):$('.content-box').addClass('hide')
})
$('#button_for_preview').click(function(){
var method = $('.main-forms input[name="_method"]').val();
$('.main-forms input[name="_method"]').val("post");
for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement();
var formData = new FormData( $('.main-forms')[0] );
formData.append("preview_type", ( (method==undefined) ? "new" : "edit" ));
formData.append("bulletin_id", '<%= @bulletin.id.to_s %>');
$.ajax({
type: "post",
url: '<%= admin_announcement_preview_path %>',
data : formData,
processData: false,
contentType: false
}).done(function(data){
if(window.location.protocol === "https:"){
data = data.replace("http:","https:");
}
$('.modal-body iframe').attr('src',data);
$('#show_preview .modal').modal();
$('#show_preview .modal').height(function() {
return $(window).height() * 0.7;
});
var slug = data.split('/')[(data.split('/').length-1)];
// $('#preview-iframe').on('load', function(){
// $.get('/admin/announcement/destroy_preview/'+slug,function(data){
// });
// });
});
$('.main-forms input[name="_method"]').val(method);
return false;
});
$("#bulletin_is_top").parent().on("click",function(){
setTimeout(function(){
if($("#bulletin_is_top").parent().hasClass("active")){
$("div[data-for=is_top]").removeClass("hide");
}else{
$("div[data-for=is_top]").addClass("hide");
$("div[data-for=is_top]").find("input[type=text]").val("");
}
},100)
})
$("#bulletin_is_external_link").on("click",function(){
if($(this).is(":checked")){
$("#external_link_box").show();
}else{
$("#external_link_box").hide();
}
})
$(document).on('click', '#add_link', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_bulletin_links", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_link', f, :bulletin_links) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.tab-content').children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
formTip();
}); });
</script> $(document).on('click', '#add_file', function(){
<% end %> var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_bulletin_files", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_file', f, :bulletin_files) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
$(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
});
formTip();
});
$(document).on('click', '.delete_link', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.delete_file', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.remove_existing_record', function(){
if(confirm("<%= I18n.t(:sure?)%>")){
$(this).children('.should_destroy').attr('value', 1);
$(this).parents('.start-line').hide();
}
});
$('#remind-check').prop('checked') ? '':$('.content-box').addClass('hide')
$('#remind-check').on('change', function() {
$(this).prop('checked') ? $('.content-box').removeClass('hide'):$('.content-box').addClass('hide')
})
$('#button_for_preview').click(function(){
var method = $('.main-forms input[name="_method"]').val();
$('.main-forms input[name="_method"]').val("post");
for ( instance in CKEDITOR.instances )
CKEDITOR.instances[instance].updateElement();
var formData = new FormData( $('.main-forms')[0] );
formData.append("preview_type", ( (method==undefined) ? "new" : "edit" ));
formData.append("bulletin_id", '<%= @bulletin.id.to_s %>');
$.ajax({
type: "post",
url: '<%= admin_announcement_preview_path %>',
data : formData,
processData: false,
contentType: false
}).done(function(data){
if(window.location.protocol === "https:"){
data = data.replace("http:","https:");
}
$('.modal-body iframe').attr('src',data);
$('#show_preview .modal').modal();
$('#show_preview .modal').height(function() {
return $(window).height() * 0.7;
});
var slug = data.split('/')[(data.split('/').length-1)];
// $('#preview-iframe').on('load', function(){
// $.get('/admin/announcement/destroy_preview/'+slug,function(data){
// });
// });
});
$('.main-forms input[name="_method"]').val(method);
return false;
});
$("#bulletin_is_top").parent().on("click",function(){
setTimeout(function(){
if($("#bulletin_is_top").parent().hasClass("active")){
$("div[data-for=is_top]").removeClass("hide");
}else{
$("div[data-for=is_top]").addClass("hide");
$("div[data-for=is_top]").find("input[type=text]").val("");
}
},100)
})
$("#bulletin_is_external_link").on("click",function(){
if($(this).is(":checked")){
$("#external_link_box").show();
}else{
$("#external_link_box").hide();
}
})
});
$(".privacy-changer-btn").click(function(){
var val = $(this).attr('for')
var parent_node = $(this).parents('.add-on.btn-group').eq(0)
parent_node.find('.privacy_type').val(val)
parent_node.find('i').eq(0).attr('class',$(this).find('i').attr('class'))
parent_node.find('.dropdown-menu').hide()
window.setTimeout(function(){
parent_node.find('.dropdown-menu').css('display','')
},1000)
})
</script>

View File

@ -40,7 +40,7 @@
<% end %> <% end %>
</span> </span>
</span> </span>
<span class="add-on btn-group btn" title="<%= t('archive.show_lang') %>"> <span class="add-on btn-group btn" title="<%= t('archive.show_lang') %>">
<i class="icons-earth"></i> <span class="caret"></span> <i class="icons-earth"></i> <span class="caret"></span>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<% @site_in_use_locales.each do |locale| %> <% @site_in_use_locales.each do |locale| %>
@ -65,5 +65,25 @@
<%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %>
</span> </span>
<% end %> <% end %>
<span class="add-on btn-group btn" style="background: white;border: 0;margin-left: 1em;">
<% case form_file.privacy_type
when "public"
icon = "icons-earth"
when "logged_in"
icon = "icons-users"
end
%>
<i class="<%= icon %>"></i> <span class="caret"></span>
<ul class="dropdown-menu">
<li>
<a class="privacy-changer-btn" for="public"><i class="icons-earth"></i><%= t('privacy_type.public') %></a>
</li>
<li class="divider"></li>
<li>
<a class="privacy-changer-btn" for="logged_in"><i class="icons-users"></i><%= t('privacy_type.logged_in') %></a>
</li>
</ul>
<%= f.hidden_field :privacy_type, class: 'privacy_type' %>
</span>
</div> </div>
</div> </div>