diff --git a/models/service_space.rb b/models/service_space.rb index e0018b8449db18e8e1c160657a323840883fab94..197c4041edb43576b9211340f921bd6baa123b5f 100644 --- a/models/service_space.rb +++ b/models/service_space.rb @@ -34,4 +34,8 @@ class ServiceSpace < ActiveRecord::Base def admin_users_href "/#{self.url_name}/admin/users/" end + + def admin_email_href + "/#{self.url_name}/admin/email/" + end end \ No newline at end of file diff --git a/routes/admin/emails.rb b/routes/admin/emails.rb new file mode 100644 index 0000000000000000000000000000000000000000..769e181ca9a4facf5d67a297be3ca1b9550847db --- /dev/null +++ b/routes/admin/emails.rb @@ -0,0 +1,64 @@ +require 'models/user' +require 'models/permission' + +before '/:service_space_url_name/admin/email*' do + unless @user.has_permission?(Permission::MANAGE_EMAILS, @space) + raise Sinatra::NotFound + end +end + +get '/:service_space_url_name/admin/email/?' do + @breadcrumbs << {:text => 'Admin Emails', :href => @space.admin_email_href} + @breadcrumbs << {:text => 'Send Email'} + users = User.all.select{|user| user.in_space?(@space)} + + erb :'admin/send_email', :layout => :fixed, :locals => { + :users => users + } +end + +post '/:service_space_url_name/admin/email/?' do + users_to_send_to = [] + + # compile the list based on what was checked + if params.checked?('send_to_all_non_admins') + users = User.all.select{|user| user.in_space?(@space) && !user.is_admin?(@space)} + users_to_send_to += users + end + if params.checked?('send_to_all_users') + users = User.all.select{|user| user.in_space?(@space)} + users_to_send_to += users + end + if params.checked?('send_to_specific_user') + user = User.find_by(:id => params[:specific_user]) + users_to_send_to << user unless user.nil? + end + + # compact and uniqify the list + users_to_send_to = users_to_send_to.compact.uniq do |user| + user.id + end + + # check on attachments + attachments = {} + params.select {|k,v| k.starts_with?('file_')}.each do |key, file_hash| + attachments[file_hash[:filename]] = file_hash[:tempfile].read + end + attachments = nil if attachments.empty? + + # correctly choose how to send + if users_to_send_to.count == 1 + Emailer.mail(users_to_send_to[0].email, params[:subject], params[:body], '', attachments) + output = users_to_send_to[0].full_name + elsif users_to_send_to.count > 1 + bcc = users_to_send_to.map(&:email).join(',') + Emailer.mail('', params[:subject], params[:body], bcc, attachments) + output = "#{users_to_send_to.count} users" + else + flash :error, 'No Users Selected', 'This email was not sent to any users' + redirect back + end + + flash :success, 'Email sent', "Your email was sent to #{output}." + redirect @space.admin_email_href +end \ No newline at end of file diff --git a/routes/resources.rb b/routes/resources.rb index 4c86d4a6532979eee61616072ac3a05389840f27..2a6582ec55bee25fc77ef7b964cc7a6fbdcebd90 100644 --- a/routes/resources.rb +++ b/routes/resources.rb @@ -159,10 +159,7 @@ post '/:service_space_url_name/resources/:resource_id/reserve/?' do if (start_time >= reservation.start_time && start_time < reservation.end_time) || (end_time > reservation.start_time && end_time <= reservation.end_time) || (start_time < reservation.start_time && end_time > reservation.end_time) - flash :alert, "Tool is being used.", "Sorry, that resource is reserved during that time period. Please try another time slot." - redirect back - elsif reservation.user_id == @user.id - flash :alert, "Over Limit", "Sorry, you can only reserve this resource once per day. Please try reserving another time slot on another day." + flash :alert, "Resource is being used.", "Sorry, that resource is reserved during that time period. Please try another time slot." redirect back end end @@ -332,10 +329,7 @@ post '/:service_space_url_name/resources/:resource_id/edit_reservation/:reservat if (start_time >= reservation.start_time && start_time < reservation.end_time) || (end_time >= reservation.start_time && end_time < reservation.end_time) || (start_time < reservation.start_time && end_time > reservation.end_time) - flash :alert, "Tool is being used.", "Sorry, that resource is reserved during that time period. Please try another time slot." - redirect back - elsif reservation.user_id == @user.id - flash :alert, "Over Limit", "Sorry, you can only reserve this resource once per day. Please try reserving another time slot on another day." + flash :alert, "Resource is being used.", "Sorry, that resource is reserved during that time period. Please try another time slot." redirect back end end diff --git a/views/admin/send_email.erb b/views/admin/send_email.erb new file mode 100644 index 0000000000000000000000000000000000000000..5e6b04d97c387f558a77da502bda755034dc026d --- /dev/null +++ b/views/admin/send_email.erb @@ -0,0 +1,72 @@ +<div id="pagetitle"> + <h3>Send Email</h3> +</div> + +<form action="" method="POST" enctype="multipart/form-data"> + <div class="wdn-grid-set"> + <fieldset class="bp2-wdn-col-one-half"> + <legend>Send to:</legend> + <input type="checkbox" id="send-to-all-non-admins" name="send_to_all_non_admins"> <label for="send-to-all-non-admins">All Non-Admins of <%= @space.name %></label><br> + <input type="checkbox" id="send-to-all-users" name="send_to_all_users"> <label for="send-to-all-users">All Users of <%= @space.name %></label><br> + <input type="checkbox" id="send-to-specific-user" name="send_to_specific_user"> + <label for="specific-user">Specific User:</label> + <select id="specific-user" name="specific_user" style="width: auto;"> + <% users.sort{|x,y| x.sortable_name.downcase <=> y.sortable_name.downcase}.each do |user| %> + <option value="<%= user.id %>"><%= user.sortable_name %></option> + <% end %> + </select><br> + </fieldset> + + <fieldset class="bp2-wdn-col-one-half"> + <legend>Attachments</legend> + <div id="files"> + <div id="file-container" class="file-container" style="padding-right: 60px; position: relative;"> + <input class="file-input" type="file" name="file_1" /> + <button style="display: none; position: absolute; right: 5px; top: 5px;" type="button" class="remove-file wdn-button">×</button> + </div> + </div> + <br> + <button id="add-file" type="button" class="wdn-button wdn-button-triad">Add another file</button> + </fieldset> + + </div> + + <fieldset> + <legend>Compose</legend> + <label for="subject">Subject:</label> + <input type="text" id="subject" name="subject" /> + + <label for="body">Body:</label> + <textarea class="ckeditor" style="min-height: 150px" id="body" name="body"></textarea> + <br><br> + <button type="submit" class="wdn-button wdn-button-brand">Send</button> + </fieldset> +</form> + +<script type="text/javascript" src="/js/lib/ckeditor/ckeditor.js"></script> + +<script type="text/javascript"> +var files = 1; + +require(['jquery'], function ($) { + $(document).ready(function () { + $('#specific-user').change(function (change) { + $('#send-to-specific-user').attr('checked', true); + }); + + $('#add-file').click(function (click) { + click.preventDefault(); + $input_div = $('#file-container').clone(); + $input_div.removeAttr('id'); + $input_div.find('.file-input').attr('name', 'file_' + ++files); + $input_div.find('.remove-file').show(); + $('#files').append($input_div); + }); + + $('#files').on('click', '.remove-file', function (click) { + click.preventDefault(); + $(this).closest('.file-container').remove(); + }); + }); +}); +</script> \ No newline at end of file diff --git a/views/template_partials/navigation.erb b/views/template_partials/navigation.erb index 7ba36e9b0ce5c666f38fa31ef6c940712806e4c1..f980c61d45e72f179fadf176a853cdc4dfd4646f 100644 --- a/views/template_partials/navigation.erb +++ b/views/template_partials/navigation.erb @@ -2,6 +2,7 @@ <li><a href="/" title="UNL Resource Scheduler">Home</a></li> <% unless @space.nil? %> <li><a href="<%= @space.calendar_href %>">Calendar</a></li> + <li><a href="<%= @space.resources_href %>">Resources</a></li> <% unless @user.nil? || !@user.is_admin?(@space) %> <li><a href="<%= @space.admin_href %>" title="Admin">Admin</a> <ul>