From 3547aa7d470f0af634159f3f36912387e0f4db11 Mon Sep 17 00:00:00 2001 From: Alex Parsons Date: Wed, 18 Oct 2023 14:36:05 +0000 Subject: [PATCH] Store multiple comms opt-ins in database - $optin is decoded and encoded to three seperate booleans - basic conversions from bool to int removed - Multiple options added to join and edit page - Form formatting tidied up --- classes/User.php | 35 +++++++++++++++++-- www/docs/style/sass/pages/_legacy.scss | 4 +-- .../templates/html/user/form.php | 30 ++++++++++++---- .../templates/html/user/join.php | 29 +++++++++++---- www/includes/easyparliament/user.php | 14 +++----- 5 files changed, 84 insertions(+), 28 deletions(-) diff --git a/classes/User.php b/classes/User.php index b6ccecf3c8..6eb0b03265 100644 --- a/classes/User.php +++ b/classes/User.php @@ -11,6 +11,30 @@ * User */ + function calculateOptinValue($optin_service, $optin_stream, $optin_org) { + // combine three booleans into a single integer to store in the database + // +1 = optin_service + // +2 = optin_stream + // +4 = optin_org + + $value = 0; + + $value += $optin_service ? 1 : 0; + $value += $optin_stream ? 2 : 0; + $value += $optin_org ? 4 : 0; + + return $value; +} + +function extractOptinValues($value) { + // convert an integer into three seperate optin values ('Yes', 'No') + return [ + 'optin_service' => ($value & 1) ? "Yes" : "No", + 'optin_stream' => ($value & 2) ? "Yes" : "No", + 'optin_org' => ($value & 4) ? "Yes" : "No", + ]; + } + class User { public function getUserDetails($user_id = false) { global $THEUSER; @@ -31,7 +55,10 @@ public function getUserDetails($user_id = false) { $data['name'] = $user->firstname() . " " . $user->lastname(); $data['url'] = $user->url(); $data['email'] = $user->email(); - $data['optin'] = $user->optin() == true ? "Yes" : "No"; + $optin_values = extractOptinValues($user->optin()); + $data['optin_service'] = $optin_values['optin_service']; + $data['optin_stream'] = $optin_values['optin_stream']; + $data['optin_org'] = $optin_values['optin_org']; $data['postcode'] = $user->postcode(); $data['website'] = $user->url(); $data['registrationtime'] = $user->registrationtime(); @@ -61,7 +88,11 @@ public function getUpdateDetails($this_page, $user) { $details["url"] = trim(get_http_var("url")); - $details["optin"] = get_http_var("optin") == "true" ? true : false; + $optin_service = get_http_var("optin_service") == "true" ? true : false; + $optin_stream = get_http_var("optin_stream") == "true" ? true : false; + $optin_org = get_http_var("optin_org") == "true" ? true : false; + + $details["optin"] = calculateOptinValue($optin_service, $optin_stream, $optin_org); if (get_http_var("remember") != "") { $remember = get_http_var("remember"); diff --git a/www/docs/style/sass/pages/_legacy.scss b/www/docs/style/sass/pages/_legacy.scss index 220fcde4fa..6d1f68ef75 100644 --- a/www/docs/style/sass/pages/_legacy.scss +++ b/www/docs/style/sass/pages/_legacy.scss @@ -63,7 +63,7 @@ } } - .join-form { + .join-form, .edit-form { .row { margin-bottom: 1.1em; @@ -71,7 +71,7 @@ margin-bottom: -.8em; } - label[for="optintrue"], label[for="optinfalse"], label[for="mp_alerttrue"], label[for="mp_alertfalse"] { + label.option_yesno { display: inline; font-size: inherit; margin-left: .5em; diff --git a/www/includes/easyparliament/templates/html/user/form.php b/www/includes/easyparliament/templates/html/user/form.php index 2af2224e42..92add8c0d2 100644 --- a/www/includes/easyparliament/templates/html/user/form.php +++ b/www/includes/easyparliament/templates/html/user/form.php @@ -8,7 +8,7 @@

Edit your details

-
+

@@ -40,7 +40,7 @@

- +

@@ -157,21 +157,37 @@ + + "Do you wish to receive occasional update emails about TheyWorkForYou.com?", + "optin_stream" => "Do you want to receive our newsletter about our wider democracy work, including our research and campaigns?", + "optin_org" => gettext("Do you want to receive the monthly newsletter from mySociety, with news on TheyWorkForYou and our other projects?"), + ); + + for ($i = 0; $i < count($optin_options); $i++) { + $optin_key = array_keys($optin_options)[$i]; + $optin_txt = array_values($optin_options)[$i]; + $optin_value = isset($$optin_key) ? $$optin_key : null; + ?> +

-  
+  
- +

- +

- >
- >
+ >
+ >
+ +
diff --git a/www/includes/easyparliament/templates/html/user/join.php b/www/includes/easyparliament/templates/html/user/join.php index a089532230..fb62411afc 100644 --- a/www/includes/easyparliament/templates/html/user/join.php +++ b/www/includes/easyparliament/templates/html/user/join.php @@ -85,29 +85,44 @@

+ "Do you wish to receive occasional update emails about TheyWorkForYou.com?", + "optin_stream" => "Do you want to receive our newsletter about our wider democracy work, including our research and campaigns?", + "optin_org" => gettext("Do you want to receive the monthly newsletter from mySociety, with news on TheyWorkForYou and our other projects?"), + ); + + for ($i = 0; $i < count($optin_options); $i++) { + $optin_key = array_keys($optin_options)[$i]; + $optin_txt = array_values($optin_options)[$i]; + $optin_value = isset($$optin_key) ? $$optin_key : null; + ?> +
-  
+  
- +

- +

- >
- >
+ >
+ >
+ +

- >
- >
+ >
+ >
diff --git a/www/includes/easyparliament/user.php b/www/includes/easyparliament/user.php index cf8e7d0961..19222f8d97 100644 --- a/www/includes/easyparliament/user.php +++ b/www/includes/easyparliament/user.php @@ -66,7 +66,7 @@ class USER { public $lastvisit = ""; // Last time the logged-in user loaded a page (GMT). public $registrationtime = ""; // When they registered (GMT). public $registrationip = ""; // Where they registered from. - public $optin = ""; // boolean - Do they want emails from us? + public $optin = ""; // Int containing multiple binary opt-ins. (See top of User.php) public $deleted = ""; // User can't log in or have their info displayed. public $confirmed = ''; // boolean - Has the user confirmed via email? public $facebook_id = ''; // Facebook ID for users who login with FB @@ -128,7 +128,7 @@ public function init($user_id) { $this->registrationtoken = $q['registrationtoken']; $this->registrationtime = $q["registrationtime"]; $this->registrationip = $q["registrationip"]; - $this->optin = $q["optin"] == 1 ? true : false; + $this->optin = $q["optin"]; $this->status = $q["status"]; $this->deleted = $q["deleted"] == 1 ? true : false; $this->confirmed = $q["confirmed"] == 1 ? true : false; @@ -169,8 +169,6 @@ public function add($details, $confirmation_required=true) { $details["facebook_id"] = ""; } - $optin = $details["optin"] == true ? 1 : 0; - $q = $this->db->query("INSERT INTO users ( firstname, lastname, @@ -205,7 +203,7 @@ public function add($details, $confirmation_required=true) { ':postcode' => $details["postcode"], ':url' => $details["url"], ':password' => $passwordforDB, - ':optin' => $optin, + ':optin' => $details["optin"], ':status' => $details["status"], ':registrationtime' => $registrationtime, ':facebook_id' => $details["facebook_id"], @@ -766,9 +764,6 @@ public function _update($details) { $params[':email'] = $details['email']; } - // Convert internal true/false variables to MySQL BOOL 1/0 variables. - $optin = $details["optin"] == true ? 1 : 0; - $q = $this->db->query("UPDATE users SET firstname = :firstname, lastname = :lastname, @@ -786,7 +781,7 @@ public function _update($details) { ':lastname' => $details['lastname'], ':postcode' => $details['postcode'], ':url' => $details['url'], - ':optin' => $optin, + ':optin' => $details["optin"], ':user_id' => $details['user_id'] ))); @@ -1445,7 +1440,6 @@ public function update_self($details, $confirm_email = true) { unset($details['email']); } $details["user_id"] = $this->user_id; - $newdetails = $this->_update($details); // $newdetails will be an array of details if all went well,