Skip to content

Commit

Permalink
Store multiple comms opt-ins in database
Browse files Browse the repository at this point in the history
- $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
  • Loading branch information
ajparsons authored and dracos committed Dec 15, 2023
1 parent a19a770 commit 74e0cc7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 28 deletions.
35 changes: 33 additions & 2 deletions classes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions www/docs/style/sass/pages/_legacy.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@
}
}

.join-form {
.join-form, .edit-form {
.row {
margin-bottom: 1.1em;

label {
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;
Expand Down
30 changes: 23 additions & 7 deletions www/includes/easyparliament/templates/html/user/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="main">
<?php if ($facebook_user) { ?>
<h1>Edit your details</h1>
<form method="post" action="/user/index.php">
<form method="post" class="edit-form" action="/user/index.php">
<?php if (isset($errors['postcode'])) { ?>
<p class="error">
<?= $errors['postcode'] ?>
Expand Down Expand Up @@ -40,7 +40,7 @@
<h1><?= gettext('Edit your details') ?></h1>
<?php } ?>

<form method="post" action="/user/index.php">
<form method="post" class="edit-form" action="/user/index.php">
<?php if (isset($errors['firstname'])) { ?>
<p class="error">
<?= $errors['firstname'] ?>
Expand Down Expand Up @@ -157,21 +157,37 @@

<?php } ?>


<?php
$optin_options = array(
"optin_service" => "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;
?>

<div class="row">
&nbsp;<br><?= gettext("Do you want to receive the monthly newsletter from mySociety, with news on TheyWorkForYou and our other projects?") ?>
&nbsp;<br><?= $optin_txt ?>
</div>

<?php if (isset($errors['optin'])) { ?>
<?php if (isset($errors[$optin_key])) { ?>
<p class="error">
<?= $errors['optin'] ?>
<?= $errors[$optin_key] ?>
</p>
<?php } ?>

<div class="row">
<span class="formw"><input type="radio" name="optin" id="optintrue" value="true" <?= $optin == 'Yes' ? ' checked' : '' ?>> <label for="optintrue">Yes</label><br>
<input type="radio" name="optin" id="optinfalse" value="false" <?= $optin == 'No' ? ' checked' : '' ?>> <label for="optinfalse">No</label></span>
<span class="formw"><input type="radio" name="<?= $optin_key ?>" id="<?= $optin_key ?>true" value="true" <?= $optin_value == 'Yes' ? ' checked' : '' ?>> <label class="option_yesno" for="<?= $optin_key ?>true">Yes</label><br>
<input type="radio" name="<?= $optin_key ?>" id="<?= $optin_key ?>false" value="false" <?= $optin_value == 'No' ? ' checked' : '' ?>> <label class="option_yesno" for="<?= $optin_key ?>false">No</label></span>
</div>

<?php } ?>

<div class="row">
<span class="formw"><input type="submit" class="submit" value="<?= gettext('Update details') ?>"></span>
</div>
Expand Down
29 changes: 22 additions & 7 deletions www/includes/easyparliament/templates/html/user/join.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,44 @@
</p>
<?php } ?>

<?php
$optin_options = array(
"optin_service" => "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;
?>

<div class="row">
&nbsp;<br><?= gettext("Do you want to receive the monthly newsletter from mySociety, with news on TheyWorkForYou and our other projects?") ?>
&nbsp;<br><?= $optin_txt ?>
</div>

<?php if (isset($errors['optin'])) { ?>
<?php if (isset($errors[$optin_key])) { ?>
<p class="error">
<?= $errors['optin'] ?>
<?= $errors[$optin_key] ?>
</p>
<?php } ?>

<div class="row">
<span class="formw"><input type="radio" name="optin" id="optintrue" value="true" <?= isset($optin) && $optin == 'Yes' ? ' checked' : '' ?>> <label for="optintrue">Yes</label><br>
<input type="radio" name="optin" id="optinfalse" value="false" <?= isset($optin) && $optin == 'No' ? ' checked' : !isset($optin) ? ' checked' : '' ?>> <label for="optinfalse">No</label></span>
<span class="formw"><input type="radio" name="<?= $optin_key ?>" id="<?= $optin_key ?>true" value="true" <?= $optin_value == 'Yes' ? ' checked' : '' ?>> <label class="option_yesno" for="<?= $optin_key ?>true">Yes</label><br>
<input type="radio" name="<?= $optin_key ?>" id="<?= $optin_key ?>false" value="false" <?= $optin_value == 'No' ? ' checked' : !isset($mp_alert) ? ' checked' : '' ?>> <label class="option_yesno" for="<?= $optin_key ?>false">No</label></span>
</div>

<?php } ?>

<div class="row">
<?= gettext("Would you like to receive email updates on your MP&rsquo;s activity in Parliament?") ?>
<br><small><?= gettext("(if you&rsquo;re already getting email alerts to your address, don&rsquo;t worry about this)") ?></small>
</div>

<div class="row">
<span class="formw"><input type="radio" name="mp_alert" id="mp_alerttrue" value="true" <?= isset($mp_alert) && $mp_alert == 'Yes' ? ' checked' : '' ?>> <label for="mp_alerttrue">Yes</label><br>
<input type="radio" name="mp_alert" id="mp_alertfalse" value="false" <?= isset($mp_alert) && $mp_alert == 'No' ? ' checked' : !isset($mp_alert) ? ' checked' : '' ?>> <label for="mp_alertfalse">No</label></span>
<span class="formw"><input type="radio" name="mp_alert" id="mp_alerttrue" value="true" <?= isset($mp_alert) && $mp_alert == 'Yes' ? ' checked' : '' ?>> <label class="option_yesno" for="mp_alerttrue">Yes</label><br>
<input type="radio" name="mp_alert" id="mp_alertfalse" value="false" <?= isset($mp_alert) && $mp_alert == 'No' ? ' checked' : !isset($mp_alert) ? ' checked' : '' ?>> <label class="option_yesno" for="mp_alertfalse">No</label></span>
</div>

<div class="row">
Expand Down
14 changes: 4 additions & 10 deletions www/includes/easyparliament/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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,
Expand All @@ -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']
)));

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 74e0cc7

Please sign in to comment.