Refactored the reminder generaion

This commit is contained in:
Erik Thuning 2022-05-16 13:49:22 +02:00
parent 15f4597637
commit 79d9f45c38

@ -14,6 +14,38 @@ class Cron {
$this->run_interval = DateInterval::createFromDateString('1 day');
$this->kvs = new Kvs();
$this->ldap = new Ldap();
$days = $this->warn_time->d;
$this->strings = array(
'en' => array(
'expiring' => array(
'single' => "The following loan expires in less than $days days:",
'multi' => "The following loans expire in less than $days days:",
'expiry' => "expires on",
'serial' => "serial number",
),
'overdue' => array(
'single' => "The following loan has expired:",
'multi' => "The following loans have expired:",
'expiry' => "expired on",
'serial' => "serial number",
),
),
'sv' => array(
'expiring' => array(
'single' => "Följande lån går ut om mindre än $days dagar:",
'multi' => "Följande lån går ut om mindre än $days dagar:",
'expiry' => "går ut",
'serial' => "artikelnummer",
),
'overdue' => array(
'single' => "Följande lån har gått ut:",
'multi' => "Följande lån har gått ut:",
'expiry' => "gick ut",
'serial' => "artikelnummer",
),
),
);
}
public function run() {
@ -54,68 +86,34 @@ class Cron {
return $subject.implode(" och ", $messages);
}
private function make_expiring_notice($lang, $expiring) {
if(!$expiring) {
private function make_notice($lang, $type, $list) {
if(!$list) {
return '';
}
$days = $this->warn_time->d;
switch($lang) {
case 'sv':
$msg = "Följande lån går ut om mindre än ".$days." dagar:";
$itemglue = ", går ut ";
break;
case 'en':
if(count($expiring) == 1) {
$loanstr = "loan expires";
} else {
$loanstr = "loans expire";
}
$msg = "The following ".$loanstr." in less than ".$days." days:";
$itemglue = ", expires on ";
break;
default:
throw new Exception("Invalid language: ".$lang);
if(!array_key_exists($lang, $this->strings)) {
throw new Exception("Invalid languange: $lang");
}
$lines = array();
foreach($expiring as $loan) {
$product = $loan->get_product();
$serial = $product->get_serial();
$brand = $product->get_brand();
$name = $product->get_name();
$endtime = format_date($loan->get_endtime());
$lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
$strings = $this->strings[$lang];
if(!array_key_exists($type, $strings)) {
throw new Exception("Invalid type: $type");
}
return $msg."\n\n".implode("\n", $lines);
}
$strings = $strings[$type];
private function make_overdue_notice($lang, $overdue) {
if(!$overdue) {
return '';
}
switch($lang) {
case 'sv':
$msg = "Följande lån har gått ut:";
$itemglue = ", gick ut ";
break;
case 'en':
if(count($overdue) == 1) {
$msg = "The following loan has expired:";
} else {
$msg = "The following loans have expired:";
}
$itemglue = ", expired on ";
break;
default:
throw new Exception("Invalid language: ".$lang);
$msg = $strings['single'];
if(count($list) > 1) {
$msg = $strings['multi'];
}
$lines = array();
foreach($overdue as $loan) {
foreach($list as $loan) {
$product = $loan->get_product();
$serial = $product->get_serial();
$brand = $product->get_brand();
$name = $product->get_name();
$endtime = format_date($loan->get_endtime());
$lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
$lines[] = "$brand $name, ".$strings['serial']
." $serial, ".$strings['expiry']." $endtime";
}
return $msg."\n\n".implode("\n", $lines);
}
@ -161,12 +159,12 @@ class Cron {
$info_sv = array();
$info_en = array();
if($expiring_count > 0) {
$info_sv[] = $this->make_expiring_notice('sv', $expiring);
$info_en[] = $this->make_expiring_notice('en', $expiring);
$info_sv[] = $this->make_notice('sv', 'expiring', $expiring);
$info_en[] = $this->make_notice('en', 'expiring', $expiring);
}
if($overdue_count > 0) {
$info_sv[] = $this->make_overdue_notice('sv', $overdue);
$info_en[] = $this->make_overdue_notice('en', $overdue);
$info_sv[] = $this->make_notice('sv', 'overdue', $overdue);
$info_en[] = $this->make_notice('en', 'overdue', $overdue);
}
$info_sv = implode("\n\n", $info_sv);
$returns_sv = $this->make_return_info('sv', $total);