From 3e9eb72bc655d989821d705851897312f7525186 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 24 Sep 2019 11:51:00 -0400
Subject: [PATCH] Add database column to track user language preference.
 (#1367)

- Based on work by Ere Maijala for Finna.
- Enables automated communication to be sent to users in the appropriate language (forthcoming feature).
---
 .../pgsql/6.1/001-modify-user-columns.sql           |  3 +++
 module/VuFind/sql/mysql.sql                         |  1 +
 module/VuFind/sql/pgsql.sql                         |  1 +
 module/VuFind/src/VuFind/Bootstrapper.php           |  8 ++++++++
 module/VuFind/src/VuFind/Db/Row/User.php            | 13 +++++++++++++
 5 files changed, 26 insertions(+)

diff --git a/module/VuFind/sql/migrations/pgsql/6.1/001-modify-user-columns.sql b/module/VuFind/sql/migrations/pgsql/6.1/001-modify-user-columns.sql
index 347c60e01b1..65d8f19f7a0 100644
--- a/module/VuFind/sql/migrations/pgsql/6.1/001-modify-user-columns.sql
+++ b/module/VuFind/sql/migrations/pgsql/6.1/001-modify-user-columns.sql
@@ -7,3 +7,6 @@ ALTER TABLE "user"
 
 ALTER TABLE "user"
   ADD COLUMN user_provided_email boolean NOT NULL DEFAULT '0';
+
+ALTER TABLE "user"
+  ADD COLUMN last_language varchar(30) NOT NULL DEFAULT '';
diff --git a/module/VuFind/sql/mysql.sql b/module/VuFind/sql/mysql.sql
index 3b1e0285172..e26390cb634 100644
--- a/module/VuFind/sql/mysql.sql
+++ b/module/VuFind/sql/mysql.sql
@@ -218,6 +218,7 @@ CREATE TABLE `user` (
   `verify_hash` varchar(42) NOT NULL DEFAULT '',
   `last_login` datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
   `auth_method` varchar(50) DEFAULT NULL,
+  `last_language` varchar(30) NOT NULL DEFAULT '',
   PRIMARY KEY (`id`),
   UNIQUE KEY `username` (`username`),
   UNIQUE KEY `cat_id` (`cat_id`)
diff --git a/module/VuFind/sql/pgsql.sql b/module/VuFind/sql/pgsql.sql
index 20f0a0f53c0..eb02493f63d 100644
--- a/module/VuFind/sql/pgsql.sql
+++ b/module/VuFind/sql/pgsql.sql
@@ -144,6 +144,7 @@ created timestamp NOT NULL DEFAULT '1970-01-01 00:00:00',
 verify_hash varchar(42) NOT NULL DEFAULT '',
 last_login timestamp NOT NULL DEFAULT '1970-01-01 00:00:00',
 auth_method varchar(50) DEFAULT NULL,
+last_language varchar(30) NOT NULL DEFAULT '',
 PRIMARY KEY (id),
 UNIQUE (username),
 UNIQUE (cat_id)
diff --git a/module/VuFind/src/VuFind/Bootstrapper.php b/module/VuFind/src/VuFind/Bootstrapper.php
index 09f9bf203fd..85c124bfef3 100644
--- a/module/VuFind/src/VuFind/Bootstrapper.php
+++ b/module/VuFind/src/VuFind/Bootstrapper.php
@@ -354,6 +354,14 @@ class Bootstrapper
                     );
                 }
             }
+
+            // Store last selected language in user account, if applicable:
+            if (($user = $sm->get(\VuFind\Auth\Manager::class)->isLoggedIn())
+                && $user->last_language != $language
+            ) {
+                $user->updateLastLanguage($language);
+            }
+
             // Send key values to view:
             $viewModel = $sm->get('ViewManager')->getViewModel();
             $viewModel->setVariable('userLang', $language);
diff --git a/module/VuFind/src/VuFind/Db/Row/User.php b/module/VuFind/src/VuFind/Db/Row/User.php
index 2c007ab088d..bc83e50df03 100644
--- a/module/VuFind/src/VuFind/Db/Row/User.php
+++ b/module/VuFind/src/VuFind/Db/Row/User.php
@@ -681,6 +681,19 @@ class User extends RowGateway implements \VuFind\Db\Table\DbTableAwareInterface,
     }
 
     /**
+     * Updated saved language
+     *
+     * @param string $language New language
+     *
+     * @return void
+     */
+    public function updateLastLanguage($language)
+    {
+        $this->last_language = $language;
+        $this->save();
+    }
+
+    /*
      * Update the user's email address, if appropriate. Note that this does NOT
      * automatically save the row; it assumes a subsequent call will be made to
      * the save() method.
-- 
GitLab