Fixed formatting bugs in the outgoing emails

This commit is contained in:
Erik Thuning 2022-05-02 15:21:56 +02:00
parent 0ee0f81024
commit 15f4597637

@ -9,8 +9,8 @@ class Cron {
$this->now = new DateTimeImmutable(); $this->now = new DateTimeImmutable();
$this->sender = $sender; $this->sender = $sender;
$this->error = $error; $this->error = $error;
$warn_time = DateInterval::createFromDateString('3 days'); $this->warn_time = DateInterval::createFromDateString('3 days');
$this->warn_date = $this->now->add($warn_time); $this->warn_date = $this->now->add($this->warn_time);
$this->run_interval = DateInterval::createFromDateString('1 day'); $this->run_interval = DateInterval::createFromDateString('1 day');
$this->kvs = new Kvs(); $this->kvs = new Kvs();
$this->ldap = new Ldap(); $this->ldap = new Ldap();
@ -58,7 +58,7 @@ class Cron {
if(!$expiring) { if(!$expiring) {
return ''; return '';
} }
$days = $this->warn_date->d; $days = $this->warn_time->d;
switch($lang) { switch($lang) {
case 'sv': case 'sv':
$msg = "Följande lån går ut om mindre än ".$days." dagar:"; $msg = "Följande lån går ut om mindre än ".$days." dagar:";
@ -76,16 +76,16 @@ class Cron {
default: default:
throw new Exception("Invalid language: ".$lang); throw new Exception("Invalid language: ".$lang);
} }
$msg .= "\n\n"; $lines = array();
foreach($expiring as $loan) { foreach($expiring as $loan) {
$product = $loan->get_product(); $product = $loan->get_product();
$serial = $product->get_serial(); $serial = $product->get_serial();
$brand = $product->get_brand(); $brand = $product->get_brand();
$name = $product->get_name(); $name = $product->get_name();
$endtime = format_date($loan->get_endtime()); $endtime = format_date($loan->get_endtime());
$msg .= $serial.": ".$brand." ".$name.$itemglue.$endtime; $lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
} }
return $msg; return $msg."\n\n".implode("\n", $lines);
} }
private function make_overdue_notice($lang, $overdue) { private function make_overdue_notice($lang, $overdue) {
@ -108,16 +108,16 @@ class Cron {
default: default:
throw new Exception("Invalid language: ".$lang); throw new Exception("Invalid language: ".$lang);
} }
$msg .= "\n\n"; $lines = array();
foreach($overdue as $loan) { foreach($overdue as $loan) {
$product = $loan->get_product(); $product = $loan->get_product();
$serial = $product->get_serial(); $serial = $product->get_serial();
$brand = $product->get_brand(); $brand = $product->get_brand();
$name = $product->get_name(); $name = $product->get_name();
$endtime = format_date($loan->get_endtime()); $endtime = format_date($loan->get_endtime());
$msg .= $serial.": ".$brand." ".$name.$itemglue.$endtime; $lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
} }
return $msg; return $msg."\n\n".implode("\n", $lines);
} }
private function make_return_info($lang, $count) { private function make_return_info($lang, $count) {
@ -148,25 +148,31 @@ class Cron {
} }
private function send_reminder($user, $expiring, $expired) { private function send_reminder($user, $expiring, $overdue) {
$uid = $user->get_name(); $uid = $user->get_name();
$name = $this->ldap->get_firstname($uid); $name = $this->ldap->get_firstname($uid);
$subject = $this->make_subject(count($expiring), count($expired)); $expiring_count = count($expiring);
$overdue_count = count($overdue);
$total = $expiring_count + $overdue_count;
$subject = $this->make_subject($expiring_count, $overdue_count);
$info_sv = array(); $info_sv = array();
$info_sv[] = $this->make_expiring_notice('sv', $expiring);
$info_sv[] = $this->make_overdue_notice('sv', $expired);
$info_sv = implode("\n\n", $info_sv);
$returns_sv = $this->make_return_info(
'sv', count($expiring) + count($expired));
$info_en = array(); $info_en = array();
$info_en[] = $this->make_expiring_notice('en', $expiring); if($expiring_count > 0) {
$info_en[] = $this->make_overdue_notice('en', $expired); $info_sv[] = $this->make_expiring_notice('sv', $expiring);
$info_en[] = $this->make_expiring_notice('en', $expiring);
}
if($overdue_count > 0) {
$info_sv[] = $this->make_overdue_notice('sv', $overdue);
$info_en[] = $this->make_overdue_notice('en', $overdue);
}
$info_sv = implode("\n\n", $info_sv);
$returns_sv = $this->make_return_info('sv', $total);
$info_en = implode("\n\n", $info_en); $info_en = implode("\n\n", $info_en);
$returns_en = $this->make_return_info( $returns_en = $this->make_return_info('en', $total);
'en', count($expiring) + count($expired));
$message = <<<EOF $message = <<<EOF
Hej $name! Hej $name!