Skip to content

Commit

Permalink
new: Allow user to accept to be contacted
Browse files Browse the repository at this point in the history
  • Loading branch information
marienfressinaud committed Mar 10, 2024
1 parent bbbc5cb commit eabce91
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 75 deletions.
Binary file modified locales/fr_FR/LC_MESSAGES/main.mo
Binary file not shown.
153 changes: 89 additions & 64 deletions locales/fr_FR/LC_MESSAGES/main.po

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function run(\Minz\Request $request): mixed
$autoload_modal_url = \Minz\Url::for('showcase', ['id' => 'navigation']);
} elseif ($current_user->autoload_modal === 'showcase link') {
$autoload_modal_url = \Minz\Url::for('showcase', ['id' => 'link']);
} elseif ($current_user->autoload_modal === 'showcase contact') {
$autoload_modal_url = \Minz\Url::for('showcase', ['id' => 'contact']);
}

// Force CSRF token to avoid weird issues when user did nothing for a while
Expand Down
1 change: 1 addition & 0 deletions src/cli/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function show(): Response
$usage .= " --url=TEXT where TEXT is an external URL\n";
$usage .= "\n";
$usage .= " users List all the users\n";
$usage .= " [--to-contact=BOOL] list only the users who accepted to be contacted (default: false)\n";
$usage .= " users create Create a user\n";
$usage .= " --email=EMAIL\n";
$usage .= " --password=PASSWORD\n";
Expand Down
11 changes: 10 additions & 1 deletion src/cli/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ class Users
/**
* List all the users ordered by created_at.
*
* @request_param bool to-contact
*
* @response 200
*/
public function index(Request $request): Response
{
$users = models\User::listAll();
$to_contact = $request->paramBoolean('to-contact');

if ($to_contact) {
$users = models\User::listBy(['accept_contact' => true]);
} else {
$users = models\User::listAll();
}

usort($users, function ($user1, $user2) {
if ($user1->created_at == $user2->created_at) {
return 0;
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/Registrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function new(): Response
* @request_param string email
* @request_param string username
* @request_param string password
* @request_param string accept_terms
* @request_param bool accept_terms
* @request_param bool accept_contact
*
* @response 302 / if already connected
* @response 302 /login if registrations are closed
Expand All @@ -68,10 +69,6 @@ public function new(): Response
* @response 400 if the service has terms of service and accept_terms is false
* @response 400 if email already exists
* @response 302 /onboarding
*
* @param \Minz\Request $request
*
* @return \Minz\Response
*/
public function create(Request $request): Response
{
Expand All @@ -91,7 +88,8 @@ public function create(Request $request): Response
$username = $request->param('username', '');
$email = $request->param('email', '');
$password = $request->param('password', '');
$accept_terms = $request->param('accept_terms', false);
$accept_terms = $request->paramBoolean('accept_terms');
$accept_contact = $request->paramBoolean('accept_contact');
$csrf = $request->param('csrf', '');

if (!\Minz\Csrf::validate($csrf)) {
Expand Down Expand Up @@ -134,6 +132,8 @@ public function create(Request $request): Response
]);
}

$user->accept_contact = $accept_contact;

// Initialize the validation token
$validation_token = new models\Token(1, 'day', 16);
$validation_token->save();
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/Showcases.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function show(Request $request): Response
return Response::ok('showcases/show_navigation.phtml');
} elseif ($id === 'link') {
return Response::ok('showcases/show_link.phtml');
} elseif ($id === 'contact') {
return Response::ok('showcases/show_contact.phtml');
} else {
return Response::notFound('not_found.phtml');
}
Expand Down
14 changes: 10 additions & 4 deletions src/controllers/my/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function edit(Request $request): Response
return Response::ok('my/preferences/edit.phtml', [
'locale' => $user->locale,
'option_compact_mode' => $user->option_compact_mode,
'accept_contact' => $user->accept_contact,
// Don't name it "beta_enabled" because there's already a global
// view variable named like this.
'is_beta_enabled' => models\FeatureFlag::isEnabled('beta', $user->id),
Expand All @@ -45,8 +46,9 @@ public function edit(Request $request): Response
*
* @request_param string csrf
* @request_param string locale
* @request_param boolean option_compact_mode
* @request_param boolean beta_enabled
* @request_param bool option_compact_mode
* @request_param bool accept_contact
* @request_param bool beta_enabled
* @request_param string from
*
* @response 302 /login?redirect_to=:from
Expand All @@ -59,8 +61,9 @@ public function edit(Request $request): Response
public function update(Request $request): Response
{
$locale = $request->param('locale', '');
$option_compact_mode = $request->paramBoolean('option_compact_mode', false);
$beta_enabled = $request->paramBoolean('beta_enabled', false);
$option_compact_mode = $request->paramBoolean('option_compact_mode');
$accept_contact = $request->paramBoolean('accept_contact');
$beta_enabled = $request->paramBoolean('beta_enabled');
$csrf = $request->param('csrf', '');
$from = $request->param('from', '');

Expand All @@ -73,6 +76,7 @@ public function update(Request $request): Response
return Response::badRequest('my/preferences/edit.phtml', [
'locale' => $locale,
'option_compact_mode' => $option_compact_mode,
'accept_contact' => $accept_contact,
'is_beta_enabled' => $beta_enabled,
'from' => $from,
'error' => _('A security verification failed: you should retry to submit the form.'),
Expand All @@ -82,13 +86,15 @@ public function update(Request $request): Response
$old_locale = $user->locale;
$user->locale = trim($locale);
$user->option_compact_mode = $option_compact_mode;
$user->accept_contact = $accept_contact;

$errors = $user->validate();
if ($errors) {
$user->locale = $old_locale;
return Response::badRequest('my/preferences/edit.phtml', [
'locale' => $locale,
'option_compact_mode' => $option_compact_mode,
'accept_contact' => $accept_contact,
'is_beta_enabled' => $beta_enabled,
'from' => $from,
'errors' => $errors,
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/Migration202403100001AddAcceptContactToUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace flusio\migrations;

class Migration202403100001AddAcceptContactToUsers
{
public function migrate(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
ALTER TABLE users
ADD COLUMN accept_contact BOOLEAN NOT NULL DEFAULT false;
SQL);

return true;
}

public function rollback(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
ALTER TABLE users
DROP COLUMN accept_contact;
SQL);

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace flusio\migrations;

class Migration202403100002SetAutoloadModalToShowcaseContact
{
public function migrate(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
UPDATE users SET autoload_modal = 'showcase contact';
SQL);

return true;
}

public function rollback(): bool
{
$database = \Minz\Database::get();

$database->exec(<<<'SQL'
UPDATE users SET autoload_modal = '';
SQL);

return true;
}
}
3 changes: 3 additions & 0 deletions src/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class User
#[Database\Column]
public bool $option_compact_mode;

#[Database\Column]
public bool $accept_contact;

public function __construct(string $username, string $email, string $password)
{
$this->id = \Minz\Random::timebased();
Expand Down
1 change: 1 addition & 0 deletions src/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CREATE TABLE users (
csrf TEXT NOT NULL DEFAULT '',
autoload_modal TEXT NOT NULL DEFAULT '',
option_compact_mode BOOLEAN NOT NULL DEFAULT false,
accept_contact BOOLEAN NOT NULL DEFAULT false,

validated_at TIMESTAMPTZ,
validation_token TEXT REFERENCES tokens ON DELETE SET NULL ON UPDATE CASCADE,
Expand Down
13 changes: 13 additions & 0 deletions src/views/my/preferences/edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
</p>
</div>

<div class="form-group">
<input
type="checkbox"
id="accept-contact"
name="accept_contact"
<?= $accept_contact ? 'checked' : '' ?>
/>

<label class="label--checkbox" for="accept-contact">
<?= _('Accept to be contacted by email to help improve the service (optional).') ?>
</label>
</div>

<div class="form-group">
<input
type="checkbox"
Expand Down
12 changes: 12 additions & 0 deletions src/views/registrations/new.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@
</div>
</div>

<div class="form-group">
<input
type="checkbox"
id="accept-contact"
name="accept_contact"
/>

<label class="label--checkbox" for="accept-contact">
<?= _('Accept to be contacted by email to help improve the service (optional).') ?>
</label>
</div>

<?php if ($has_terms): ?>
<div class="form-group form-group--terms <?= isset($errors['accept_terms']) ? 'form-group--invalid' : '' ?>">
<div id="accept-terms-desc">
Expand Down
31 changes: 31 additions & 0 deletions src/views/showcases/show_contact.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
$this->layout('base.phtml', [
'title' => _('You can help to improve the service'),
'canonical' => url_full('showcase', ['id' => 'contact']),
'modal_enabled' => true,
]);
?>

<div class="section section--small">
<div class="section__title">
<h1><?= _('You can help to improve the service') ?></h1>
</div>

<p>
<?= _f('To help us improve %s, you can now accept to be contacted by email.', $brand) ?>
</p>

<p class="text--centered">
<a href="<?= url('preferences') ?>" class="anchor--action" data-turbo-frame="_top">
<?= _('Go to your preferences') ?>
</a>
</p>

<form method="get" action="" data-turbo-frame="_top">
<div class="form__actions">
<button type="submit" class="button--ghost">
<?= _('No thanks!') ?>
</button>
</div>
</form>
</div>

0 comments on commit eabce91

Please sign in to comment.