Skip to content

Commit

Permalink
Clean up mail and add back a couple missing features (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
williammck authored Mar 31, 2024
1 parent eeb579f commit 65233b3
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 454 deletions.
58 changes: 33 additions & 25 deletions app/Classes/EmailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,6 @@

class EmailHelper
{
/**
* Send Email, checking for facility template first
*
* @param string|array $email
* @param string $subject
* @param string $template
* @param array $data
*/
public static function sendEmailFacilityTemplate($email, $subject, $fac, $template, $data)
{
$global_templates = [
'examassigned' => "emails.exam.assign",
'exampassed' => 'emails.exam.passed',
'examfailed' => 'emails.exam.failed',
'transferpending' => 'emails.transfers.pending'
];
if (view()->exists("emails.facility.$fac." . $template)) {
$template = "emails.facility.$fac.$template";
} else {
$template = $global_templates[$template];
}

static::sendEmail($email, $subject, $template, $data);
}

/**
* Send an email from support
*
Expand Down Expand Up @@ -131,4 +106,37 @@ public static function sendEmailBCC($fromEmail, $fromName, $emails, $subject, $t
}
}

/**
* @param string $email
* @param string $from_email
* @param string $from_name
* @param string $subject
* @param string $template
* @param array $data
*/
public static function sendEmailFrom($email, $from_email, $from_name, $subject, $template, $data)
{
try {
Mail::send($template, $data, function ($msg) use ($data, $from_email, $from_name, $email, $subject) {
$msg->from("[email protected]", "$from_name");
$msg->replyTo($from_email, $from_name);
$msg->to($email);
$msg->subject("[VATUSA] $subject");
});
} catch (\Exception $exception) {
// Don't do anything - temporary workaround due to mail host failures
}
}

/**
* Check if user has opted in to broadcast emails.
* @param $cid
*
* @return int
*/
public static function isOptedIn($cid) {
$user = \App\Models\User::find($cid);

return $user && $user->flag_broadcastOptedIn;
}
}
5 changes: 5 additions & 0 deletions app/Helpers/ACLFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public function canViewEmail(): bool
return $this->isVATUSAStaff() || $this->isFacilitySeniorStaff();
}

public function canSendBroadcastEmail(): bool
{
return $this->isVATUSAStaff() || $this->isFacilityStaff();
}

public function canViewController(User $user): bool
{
if ($this->isVATUSAStaff() ||
Expand Down
178 changes: 178 additions & 0 deletions app/Http/Controllers/EmailMgtController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php namespace App\Http\Controllers;

use App\Helpers\AuthHelper;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Facility;
use App\Classes\RoleHelper;
use App\Classes\EmailHelper;
use Auth;
use App\Classes\Helper;

class EmailMgtController extends Controller {
public function getIndividual($cid) {
if (!AuthHelper::authACL()->canSendBroadcastEmail()) {
abort(401);
}
return view('mgt.mail.broadcast', ['cid' => $cid]);
}

public function getBroadcast($cid = null) {
if (!AuthHelper::authACL()->canSendBroadcastEmail()) {
abort(401);
}
return view('mgt.mail.broadcast', ['cid' => $cid]);
}

public function postBroadcast(Request $request) {
if (!AuthHelper::authACL()->canSendBroadcastEmail()) {
abort(401);
}
$rcpts = $request->recipients;
$subj = $request->subject;
$msg = $request->email;
$single = $request->single;

if ((empty($rcpts) && empty($single)) || empty($subj) || empty($msg)) {
return back()->with('broadcastError', 'All fields are required.');
}

// Send to single person.
if (empty($rcpts) && !empty($single)) {
if (!EmailHelper::isOptedIn($single)) {
return redirect("/mgt/mail/$single")
->with('broadcastError', 'Could not send email. User is not opted in to broadcast emails.');
}
$email = Helper::emailFromCID($single);
EmailHelper::sendEmailFrom($email, Auth::user()->email, Auth::user()->fname . " " . Auth::user()->lname, $subj, 'emails.mass', array('msg' => nl2br(strip_tags($msg, '<b><i>')), 'init' => Auth::user()->fname . " " . Auth::user()->lname . " (" . Auth::user()->cid . ")"));
return redirect("/mgt/mail/$single")->with('broadcastSuccess', 'Email sent.');
} else {
// Handle Recipients
// STAFF = All Staff
// ALL = VATUSA Controllers
// SRSTAFF = Facility Senior Staff
// VATUSA = VATUSA Staff
// INS = Instructional Staff (I1 + I3)
// Z--/HCF[SJU/GUM] = Facility

$emails = array();

if ($rcpts == "STAFF") {
$rcpts = "Facility Staff";
foreach (Facility::get() as $f) {
if ($f->atm && EmailHelper::isOptedIn($f->atm)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->datm && EmailHelper::isOptedIn($f->datm)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->ta && EmailHelper::isOptedIn($f->ta)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->ec && EmailHelper::isOptedIn($f->ec)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->fe && EmailHelper::isOptedIn($f->fe)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->wm && EmailHelper::isOptedIn($f->wm)) {
$emails[] = $f->id . "[email protected]";
}
}
} else if ($rcpts == "ALL") {
$rcpts = "VATUSA";
foreach (User::where('facility', 'NOT LIKE', 'ZZN')
->where('facility', 'NOT LIKE', 'ZAE')
->where('flag_broadcastOptedIn', true)->get() as $u) {
if ($u->email) {
$emails[] = $u->email;
}
}
} else if ($rcpts == "SRSTAFF") {
$rcpts = "Facility Senior Staff";
foreach (Facility::get() as $f) {
if ($f->atm && EmailHelper::isOptedIn($f->atm)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->datm && EmailHelper::isOptedIn($f->datm)) {
$emails[] = $f->id . "[email protected]";
}
if ($f->ta && EmailHelper::isOptedIn($f->ta)) {
$emails[] = $f->id . "[email protected]";
}
}
} else if ($rcpts == "DRCTR") {
$rcpts = "Facility ATM/DATM";
foreach (Facility::get() as $f) {
if (EmailHelper::isOptedIn($f->atm)) {
$emails[] = $f->id . "[email protected]";
}
if (EmailHelper::isOptedIn($f->datm)) {
$emails[] = $f->id . "[email protected]";
}
}
} else if ($rcpts == "WM") {
$rcpts = "Facility Webmasters";
foreach (Facility::get() as $f) {
if (EmailHelper::isOptedIn($f->wm)) {
$emails[] = $f->id . "[email protected]";
}
}
} else if ($rcpts == "VATUSA") {
$rcpts = "VATUSA Staff";
foreach (Role::where('facility', 'ZHQ')->get() as $f) {
if (EmailHelper::isOptedIn($f->cid)) {
$emails[] = Helper::emailFromCID($f->cid);
}
}
} else if ($rcpts == "INS") {
$rcpts = "Instructional staff";
foreach (User::where('facility', 'NOT LIKE', 'ZZN')
->where('facility', 'NOT LIKE', 'ZAE')
->where('flag_broadcastOptedIn', true)->get() as $u) {
if (RoleHelper::isInstructor($u->cid, $u->facility, false) && $u->email) {
$emails[] = $u->email;
}
}
} else if ($rcpts == "ACE") {
$rcpts = "ACE Team Members";
foreach (Role::where('facility', 'ZHQ')->where('role', 'ACE')->get() as $f) {
if (EmailHelper::isOptedIn($f->cid)) {
$emails[] = Helper::emailFromCID($f->cid);
}
}
} else {
foreach (User::where('facility', $rcpts)->where('flag_broadcastOptedIn', true)->get() as $u) {
$emails[] = $u->email;
}
}
EmailHelper::sendEmailBCC(Auth::user()->email, Auth::user()->fname . " " . Auth::user()->lname, $emails, $subj, 'emails.mass', array('msg' => nl2br(strip_tags($msg, '<b><i>')), 'init' => Auth::user()->fname . " " . Auth::user()->lname . " (" . Auth::user()->cid . ")"));
}
$count = count($emails);
if (!$count) {
return redirect("/mgt/mail/broadcast")->with('broadcastError', 'No emails were sent.');
}
return redirect("/mgt/mail/broadcast")->with('broadcastSuccess', 'Email sent to ' . count($emails) . ' member' . ($count == 1 ? '' : 's'));
}

public function getWelcome() {
if (!AuthHelper::authACL()->canManageFacilityTechConfig()) {
abort(401);
}

$fac = Facility::find(Auth::user()->facility);
return view('mgt.mail.welcome', ['welcome' => $fac->welcome_text]);
}

public function postWelcome(Request $request) {
if (!AuthHelper::authACL()->canManageFacilityTechConfig()) {
abort(401);
}

$fac = Facility::find(Auth::user()->facility);
$fac->welcome_text = $request->input("welcome");
$fac->save();
return redirect("/mgt/mail/welcome")->with("success", "Welcome email set.");
}
}
9 changes: 9 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@
Route::post('{fac}/mapping/{id}', 'TMUController@postMgtMapping');
});

// * * * * * * Mail * * * * * *
Route::group(['prefix' => 'mail'], function () {
Route::get('broadcast', 'EmailMgtController@getBroadcast');
Route::post('broadcast', 'EmailMgtController@postBroadcast');
Route::get('{cid}', 'EmailMgtController@getIndividual')->where('cid', '[0-9]+');
Route::get('welcome', 'EmailMgtController@getWelcome');
Route::post('welcome', 'EmailMgtController@postWelcome');
});

/* * * * * * * * *
* Statistics *
* * * * * * * * */
Expand Down
1 change: 0 additions & 1 deletion public/js/email.js

This file was deleted.

1 change: 0 additions & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"/js/email.js": "/js/email.js?id=2aa9d558c844e79504573de371ecd41f",
"/js/privacy.js": "/js/privacy.js?id=d6478b87fe44415810b79dcb9ea90531",
"/css/vatusa.css": "/css/vatusa.css?id=f11d8db90cdca03664e60da5ee1439cc"
}
32 changes: 0 additions & 32 deletions resources/assets/js/components/emails/emailPage.js

This file was deleted.

Loading

0 comments on commit 65233b3

Please sign in to comment.