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->run_interval = DateInterval::createFromDateString('1 day');
$this->kvs = new Kvs(); $this->kvs = new Kvs();
$this->ldap = new Ldap(); $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() { public function run() {
@ -54,68 +86,34 @@ class Cron {
return $subject.implode(" och ", $messages); return $subject.implode(" och ", $messages);
} }
private function make_expiring_notice($lang, $expiring) { private function make_notice($lang, $type, $list) {
if(!$expiring) { if(!$list) {
return ''; return '';
} }
$days = $this->warn_time->d; if(!array_key_exists($lang, $this->strings)) {
switch($lang) { throw new Exception("Invalid languange: $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);
} }
$lines = array(); $strings = $this->strings[$lang];
foreach($expiring as $loan) { if(!array_key_exists($type, $strings)) {
$product = $loan->get_product(); throw new Exception("Invalid type: $type");
$serial = $product->get_serial();
$brand = $product->get_brand();
$name = $product->get_name();
$endtime = format_date($loan->get_endtime());
$lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
} }
return $msg."\n\n".implode("\n", $lines); $strings = $strings[$type];
}
private function make_overdue_notice($lang, $overdue) { $msg = $strings['single'];
if(!$overdue) { if(count($list) > 1) {
return ''; $msg = $strings['multi'];
}
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);
} }
$lines = array(); $lines = array();
foreach($overdue as $loan) { foreach($list 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());
$lines[] = $serial.": ".$brand." ".$name.$itemglue.$endtime;
$lines[] = "$brand $name, ".$strings['serial']
." $serial, ".$strings['expiry']." $endtime";
} }
return $msg."\n\n".implode("\n", $lines); return $msg."\n\n".implode("\n", $lines);
} }
@ -161,12 +159,12 @@ class Cron {
$info_sv = array(); $info_sv = array();
$info_en = array(); $info_en = array();
if($expiring_count > 0) { if($expiring_count > 0) {
$info_sv[] = $this->make_expiring_notice('sv', $expiring); $info_sv[] = $this->make_notice('sv', 'expiring', $expiring);
$info_en[] = $this->make_expiring_notice('en', $expiring); $info_en[] = $this->make_notice('en', 'expiring', $expiring);
} }
if($overdue_count > 0) { if($overdue_count > 0) {
$info_sv[] = $this->make_overdue_notice('sv', $overdue); $info_sv[] = $this->make_notice('sv', 'overdue', $overdue);
$info_en[] = $this->make_overdue_notice('en', $overdue); $info_en[] = $this->make_notice('en', 'overdue', $overdue);
} }
$info_sv = implode("\n\n", $info_sv); $info_sv = implode("\n\n", $info_sv);
$returns_sv = $this->make_return_info('sv', $total); $returns_sv = $this->make_return_info('sv', $total);