2017-08-24 11:47:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Roundmail;
|
|
|
|
|
use App\User;
|
2017-08-26 15:15:54 +02:00
|
|
|
use App\Round;
|
2017-08-28 16:34:19 +02:00
|
|
|
use App\Season;
|
2017-08-24 11:47:16 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
|
|
use App\Mail\RoundmailMailer;
|
|
|
|
|
use Auth;
|
|
|
|
|
use DB;
|
|
|
|
|
use Log;
|
|
|
|
|
use Carbon;
|
2018-08-17 17:16:01 +02:00
|
|
|
use App\MyClasses\TimeLogger;
|
2017-08-24 11:47:16 +02:00
|
|
|
|
|
|
|
|
class RoundmailController extends Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
|
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
|
|
2018-08-17 17:16:01 +02:00
|
|
|
$a=new TimeLogger("RoundmailController::index()");
|
|
|
|
|
Log::info($a->getStartMessage());
|
|
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
$mails_db = Roundmail::all();
|
|
|
|
|
$mails=array();
|
|
|
|
|
foreach($mails_db as $mail_db) {
|
|
|
|
|
$fields=array();
|
|
|
|
|
$fields['id'] = $mail_db->id;
|
|
|
|
|
$fields['send_date'] = $mail_db->send_date;
|
|
|
|
|
$fields['topic'] = $mail_db->topic;
|
|
|
|
|
|
|
|
|
|
$sender = User::where('id','=',$mail_db->user_id)->first();
|
2017-08-28 16:34:19 +02:00
|
|
|
$sender_name="Automailer";
|
2017-08-24 11:47:16 +02:00
|
|
|
if ($sender) { $sender_name=$sender->name; }
|
|
|
|
|
$fields['sender'] = $sender_name;
|
|
|
|
|
|
|
|
|
|
array_push($mails,$fields);
|
|
|
|
|
}
|
|
|
|
|
$mails=array_reverse($mails);
|
|
|
|
|
|
2018-08-17 17:16:01 +02:00
|
|
|
Log::info($a->getEndMessage());
|
|
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
return view('roundmails.index',compact('mails'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function show($mail_id) {
|
|
|
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
|
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
|
|
2018-08-17 17:16:01 +02:00
|
|
|
$a=new TimeLogger("RoundmailController::show start");
|
|
|
|
|
Log::info($a->getStartMessage());
|
|
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
$user_id=Auth::user()->id;
|
|
|
|
|
|
2019-06-23 09:18:36 +02:00
|
|
|
$users = DB::table('users')->orderBy('lastname')->get();
|
2017-08-24 11:47:16 +02:00
|
|
|
|
|
|
|
|
$active_users=array();
|
|
|
|
|
$activeMailList=array();
|
|
|
|
|
|
|
|
|
|
$inactive_users=array();
|
|
|
|
|
$inactiveMailList=array();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$missingTippMailList=array();
|
|
|
|
|
$missingJokerMailList=array();
|
|
|
|
|
|
|
|
|
|
$adminMailList=array();
|
|
|
|
|
$topic="";
|
|
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
$send_date = date("d.m.Y h:i:s", time());
|
2017-08-24 11:47:16 +02:00
|
|
|
|
|
|
|
|
Log::info("Users to check for missing tipps:" . sizeof($users));
|
|
|
|
|
foreach($users as $user) {
|
2019-06-23 09:18:36 +02:00
|
|
|
Log::info("Season paytest user_id=".$user->id);
|
|
|
|
|
if (User::is_season_paid($user->id)=="Ja") {
|
|
|
|
|
Log::info("Season is paid.");
|
2017-08-24 11:47:16 +02:00
|
|
|
array_push($active_users,$user);
|
|
|
|
|
array_push($activeMailList,$user->email);
|
|
|
|
|
} else {
|
2019-06-23 09:18:36 +02:00
|
|
|
Log::info("Season is NOT paid.");
|
2017-08-24 11:47:16 +02:00
|
|
|
array_push($inactive_users, $user);
|
|
|
|
|
array_push($inactiveMailList,$user->email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Log::info("user-id=".$user->id.", ".$user->name);
|
|
|
|
|
//Log::info("Tipp: ".User::hasMissingTipp($user->id));
|
|
|
|
|
|
2019-06-23 09:18:36 +02:00
|
|
|
if (!$user->deleted) {
|
|
|
|
|
if (User::hasMissingTipp($user->id)) {
|
|
|
|
|
array_push($missingTippMailList, $user->email);
|
|
|
|
|
}
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2019-06-23 09:18:36 +02:00
|
|
|
if (User::hasMissingJoker($user->id)) {
|
|
|
|
|
array_push($missingJokerMailList, $user->email);
|
|
|
|
|
}
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2019-06-23 09:18:36 +02:00
|
|
|
if ($user->admin == true) {
|
|
|
|
|
array_push($adminMailList, $user->email);
|
|
|
|
|
}
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|
2019-06-23 09:18:36 +02:00
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log::info("Size of missingMailist=".sizeof($missingTippMailList));
|
|
|
|
|
|
|
|
|
|
$activeMailList = implode(",",$activeMailList);
|
|
|
|
|
$inactiveMailList = implode(",",$inactiveMailList);
|
|
|
|
|
$missingTippMailList = implode(",",$missingTippMailList);
|
|
|
|
|
$missingJokerMailList = implode(",",$missingJokerMailList);
|
|
|
|
|
$adminMailList = implode(",",$adminMailList);
|
2017-08-28 16:34:19 +02:00
|
|
|
$tippSpielLink = Round::getTippSpielLink();
|
2017-08-24 11:47:16 +02:00
|
|
|
|
|
|
|
|
$topic="";
|
|
|
|
|
$mail_message="";
|
2017-08-28 16:34:19 +02:00
|
|
|
$sender_name="Automailer";
|
2017-08-24 11:47:16 +02:00
|
|
|
$recipients="";
|
|
|
|
|
$viewmode=false;
|
|
|
|
|
if ($mail_id>-1) {
|
|
|
|
|
$viewmode=true;
|
2017-08-28 16:34:19 +02:00
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
$mail = Roundmail::where('id','=',$mail_id)->first();
|
2017-08-28 16:34:19 +02:00
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
$send_date = $mail->send_date;
|
|
|
|
|
$topic = $mail->topic;
|
2017-08-28 16:34:19 +02:00
|
|
|
|
|
|
|
|
Log::info("Before:");
|
|
|
|
|
Log::info($mail->message);
|
|
|
|
|
//$mail_message = str_replace('<br/>','\n', $mail->message);
|
|
|
|
|
$mail_message = $mail->message;
|
|
|
|
|
Log::info("After:");
|
|
|
|
|
Log::info($mail_message);
|
2017-08-24 11:47:16 +02:00
|
|
|
$recipients = $mail->recipients;
|
|
|
|
|
$sender = User::where('id','=',$mail->user_id)->first();
|
2017-08-28 16:34:19 +02:00
|
|
|
if ($sender) {$sender_name = $sender->name;}
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-23 09:18:36 +02:00
|
|
|
|
|
|
|
|
Log::info("!!! inactiveMailList content=". $inactiveMailList);
|
|
|
|
|
|
2018-08-17 17:16:01 +02:00
|
|
|
Log::info($a->getEndMessage());
|
|
|
|
|
|
2017-08-26 15:15:54 +02:00
|
|
|
return view('roundmails.show',compact('mail_id','activeMailList','inactiveMailList','missingTippMailList','missingJokerMailList','adminMailList','send_date','user_id','topic', 'mail_message', 'recipients', 'sender_name','viewmode','tippSpielLink'));
|
2017-08-24 11:47:16 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send() {
|
|
|
|
|
if (Auth::guest()) { return redirect("/"); }
|
|
|
|
|
if (!Auth::user()->admin) { return redirect("/"); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Validate form
|
|
|
|
|
$this->validate(request(), [
|
|
|
|
|
'user_id' => 'required',
|
|
|
|
|
'send_date' => 'required',
|
|
|
|
|
'topic' => 'required',
|
|
|
|
|
'message' => 'required',
|
|
|
|
|
'recipients'=> 'required'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Log::info("Preparing to send from user_id ".request('user_id'));
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
foreach($users as $user) {
|
|
|
|
|
|
|
|
|
|
Log::info("Sending RoundmailMailer to: ".$user->name);
|
|
|
|
|
\Mail::to($user)->send(new RoundmailMailer);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
$r = new RoundmailMailer();
|
|
|
|
|
$r->user_id = Auth::user()->id;
|
2017-08-28 16:34:19 +02:00
|
|
|
$r->sender_email = Auth::user()->email;
|
|
|
|
|
$r->sender_name = Auth::user()->name;
|
2017-08-25 12:57:58 +02:00
|
|
|
$r->send_date = Carbon\Carbon::now();
|
|
|
|
|
$r->recipients = request('recipients');
|
2017-08-28 16:34:19 +02:00
|
|
|
|
|
|
|
|
Log::info("Recipients:".$r->recipients);
|
|
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
$r->topic = request('topic');
|
2017-08-28 16:34:19 +02:00
|
|
|
$r->mail_message = str_replace("\r","",str_replace("\n","<br/>", request('message')));
|
|
|
|
|
Log::info("Recipients:");
|
|
|
|
|
Log::info($r->recipients);
|
|
|
|
|
\Mail::to(explode(",",$r->recipients))->send($r);
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
// Die hier ist nur für die DB
|
|
|
|
|
$rm = new RoundMail();
|
|
|
|
|
$rm->send_date=$r->send_date;
|
|
|
|
|
$rm->recipients=$r->recipients;
|
|
|
|
|
$rm->user_id=$r->user_id;
|
|
|
|
|
$rm->topic=$r->topic;
|
|
|
|
|
$rm->message=$r->mail_message;
|
|
|
|
|
$rm->save();
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-28 16:34:19 +02:00
|
|
|
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
// TODO: refactor, loop is also used in index method.
|
|
|
|
|
// COPY/PASTE, please refactor!
|
|
|
|
|
$mails_db = Roundmail::all();
|
|
|
|
|
$mails=array();
|
|
|
|
|
foreach($mails_db as $mail_db) {
|
|
|
|
|
$fields=array();
|
|
|
|
|
$fields['id'] = $mail_db->id;
|
|
|
|
|
$fields['send_date'] = $mail_db->send_date;
|
|
|
|
|
$fields['topic'] = $mail_db->topic;
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
$sender = User::where('id','=',$mail_db->user_id)->first();
|
2017-08-28 16:34:19 +02:00
|
|
|
$sender_name="Automailer";
|
2017-08-25 12:57:58 +02:00
|
|
|
if ($sender) { $sender_name=$sender->name; }
|
|
|
|
|
$fields['sender'] = $sender_name;
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
array_push($mails,$fields);
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|
2017-08-25 12:57:58 +02:00
|
|
|
$mails=array_reverse($mails);
|
2017-08-24 11:47:16 +02:00
|
|
|
|
2017-08-25 12:57:58 +02:00
|
|
|
return view('roundmails.index',compact('mails'));
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-26 15:15:54 +02:00
|
|
|
public function checkReminder() {
|
|
|
|
|
Log::info("RoundmailController::checkReminder was triggered");
|
2017-08-28 16:34:19 +02:00
|
|
|
|
|
|
|
|
$currentSeason = Season::getCurrent();
|
|
|
|
|
$users = User::whereNull('deleted')->get();
|
|
|
|
|
|
|
|
|
|
Log::info("Usercount: ".sizeof($users));
|
|
|
|
|
$emailsOfUsersWithIncompleteTipps=array();
|
|
|
|
|
foreach($users as $user) {
|
|
|
|
|
if (User::hasMissingTipp($user->id) || User::hasMissingJoker($user->id)) {
|
|
|
|
|
if ($user->gets_reminders) {
|
|
|
|
|
array_push($emailsOfUsersWithIncompleteTipps, $user->email);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$firstTippableRound = Round::getFirstTippableRound();
|
|
|
|
|
if (!$firstTippableRound) {
|
|
|
|
|
Log::info("RoundmailController::checkReminder: No tippable round detected, automailing aborted");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$WARNING_HOURS_OFFSET=48;
|
|
|
|
|
if (!$firstTippableRound->isInLockWarningTime($WARNING_HOURS_OFFSET)) {
|
|
|
|
|
Log::info("RoundmailController->checkRemninder: skipping Reminder Mail because Lock is further away than ".$WARNING_HOURS_OFFSET." hours.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$roundLockTime = $firstTippableRound->getLockTime();
|
|
|
|
|
|
|
|
|
|
$r = new RoundmailMailer();
|
|
|
|
|
$r->user_id = 0;
|
|
|
|
|
$r->sender_email="info@hoschis-tippspiel.de";
|
|
|
|
|
$r->sender_name="Hoschis Tippspiel";
|
|
|
|
|
$r->send_date = Carbon\Carbon::now();
|
|
|
|
|
$r->recipients = implode(",", $emailsOfUsersWithIncompleteTipps);
|
|
|
|
|
$r->topic = "Fehlende Tipps/Joker";
|
2017-08-30 14:52:21 +02:00
|
|
|
$r->mail_message = 'Es fehlen noch Tipps/Joker für: ' . $firstTippableRound->name . '<br/>Die Runde sperrt sich: ' . $roundLockTime . '<br/><br/>Link zum Spiel: <a href="' . Round::getTippSpielLink(). '">( Hier klicken )</a>';
|
2017-08-28 16:34:19 +02:00
|
|
|
Log::info("Sending Reminder Mails");
|
|
|
|
|
|
|
|
|
|
\Mail::to($emailsOfUsersWithIncompleteTipps)->send($r);
|
|
|
|
|
|
|
|
|
|
// Die hier ist nur für die DB
|
|
|
|
|
$rm = new RoundMail();
|
|
|
|
|
$rm->send_date=$r->send_date;
|
|
|
|
|
$rm->recipients=$r->recipients;
|
|
|
|
|
$rm->user_id=$r->user_id;
|
|
|
|
|
$rm->topic=$r->topic;
|
|
|
|
|
$rm->message=$r->mail_message;
|
|
|
|
|
$rm->save();
|
|
|
|
|
|
2017-08-26 15:15:54 +02:00
|
|
|
}
|
2017-08-24 11:47:16 +02:00
|
|
|
}
|