From 16f48cd28d7dd693232b9a8065608f1c62399bfa Mon Sep 17 00:00:00 2001
From: Erik Thuning <boooink@gmail.com>
Date: Thu, 13 Mar 2025 11:27:43 +0100
Subject: [PATCH] Made the deafult loan length configurable in config.php

---
 config.php.example       | 3 +++
 include/CheckoutPage.php | 2 +-
 include/ProductPage.php  | 4 ++--
 include/functions.php    | 8 ++++++--
 4 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/config.php.example b/config.php.example
index 02408e6..a0633ee 100644
--- a/config.php.example
+++ b/config.php.example
@@ -20,6 +20,9 @@ $language = 'en';
 # Site name
 $name = 'My product tracker';
 
+# Default loan length
+$default_loan_length = '1 day';
+
 # Email subject prefix
 # Will be prepended without change, so should probably end with a space
 $email_subject_prefix = "System name: ";
diff --git a/include/CheckoutPage.php b/include/CheckoutPage.php
index 797b047..17cdd2a 100644
--- a/include/CheckoutPage.php
+++ b/include/CheckoutPage.php
@@ -85,7 +85,7 @@ class CheckoutPage extends Page {
             $email = $this->user->get_email($this->ldap);
             $displayname = $this->user->get_displayname($this->ldap);
             $notes = $this->user->get_notes();
-            $enddate = format_date(default_loan_end(time()));
+            $enddate = format_date(default_loan_end());
             $disabled = '';
             $loans = $this->user->get_loans('active');
             $loan_table = i18n('No active loans.');
diff --git a/include/ProductPage.php b/include/ProductPage.php
index 3d0f72f..c22c72e 100644
--- a/include/ProductPage.php
+++ b/include/ProductPage.php
@@ -84,7 +84,7 @@ class ProductPage extends Page {
                         'service'     => i18n('Start service'),
                         'history'     => $history,
                         'attachments' => $attachments,
-                        'end'         => format_date(default_loan_end(time())));
+                        'end'         => format_date(default_loan_end()));
         if(class_exists('QRcode')) {
             $fields['label'] = replace($fields,
                                        $this->fragments['product_label']);
@@ -123,7 +123,7 @@ class ProductPage extends Page {
                                     $this->fragments['item_link']);
                 if(!$end) {
                     $end = $event->get_endtime();
-                    $extend = format_date(default_loan_end(time()));
+                    $extend = format_date(default_loan_end());
                     $note = replace(array('id' => $product->get_id(),
                                           'end_new' => $extend),
                                     $this->fragments['loan_extend_form']);
diff --git a/include/functions.php b/include/functions.php
index 49f2ce1..be4eae9 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -287,8 +287,12 @@ function format_date($date) {
     return $date;
 }
 
-function default_loan_end($start) {
-    return $start + 604800; # 1 week later
+function default_loan_end() {
+    global $default_loan_length;
+    $now = new DateTimeImmutable();
+    $duration = DateInterval::createFromDateString($default_loan_length);
+    $end = $now->add($duration);
+    return $end->getTimestamp();
 }