Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

[GSoC 2019] allow users to configure notification settings #391

Merged
merged 3 commits into from Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions site/app/DoctrineMigrations/Version20190717152538.php
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190717152538 extends AbstractMigration
{
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE User ADD subscribedToEmailNotifications TINYINT(1) DEFAULT \'1\' NOT NULL');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE User DROP subscribedToEmailNotifications');
}
}
5 changes: 5 additions & 0 deletions site/app/Resources/views/user/settings_base.html.twig
Expand Up @@ -51,6 +51,11 @@ Settings for {{ user.getName() ? user.getName() : user.getUsername() }} @ LibreC
Change Password
</a>
</li>
<li class="list-group-item {{ settings_subpage == "notification" ? "active"}}">
<a href="{{ path('librecores.user.settings.notification') }}">
Notification Settings
</a>
</li>
</ul>
</div>
<div class="col-sm-10">
Expand Down
19 changes: 19 additions & 0 deletions site/app/Resources/views/user/settings_notification.html.twig
@@ -0,0 +1,19 @@
{#
Template for the user to edit notification settings
page path: /user/settings/notifications
#}
{% extends 'user/settings_base.html.twig' %}

{# Inform settings_base.html.twig about the page currently displayed #}
{% set settings_subpage = 'notifications' %}

{% block settings_content %}
<h2>Notification Settings</h2>
<p> By default, all users get notifications from the LibreCores community in the form of web notifications
on the website as well as email notifications to their registered email. The settings for each of these notifications
is down to personal preferences and can be edited in the panel down below</p>
<h3>Email Notifications</h3>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
32 changes: 31 additions & 1 deletion site/src/Controller/UserController.php
Expand Up @@ -6,6 +6,7 @@
use App\Entity\Notification;
use App\Form\Model\ResendConfirmationEmailRequest;
use App\Form\Type\ResendConfirmationEmailRequestType;
use App\Form\Type\NotificationSubscriptionType;
use App\Form\Type\UserProfileType;
use App\Security\Core\User\LibreCoresUserProvider;
use FOS\UserBundle\Form\Type\ChangePasswordFormType;
Expand All @@ -14,7 +15,6 @@
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Mgilet\NotificationBundle\Manager\NotificationManager;
use Mgilet\NotificationBundle\NotifiableInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -229,6 +229,36 @@ public function resendConfirmationEmailAction(
return $this->render('user/resend_confirmation_email.html.twig', [ 'form' => $form->createView() ]);
}

/**
* User Notification Settings
*
* @Route("/user/settings/notifications", name="librecores.user.settings.notification")
*
* @param Request $request
*
* @return Response
*/
public function notificationSettingsAction(Request $request)
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser();

$form = $this->createForm(NotificationSubscriptionType::class, $user);

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}

return $this->render(
'user/settings_notification.html.twig',
['user' => $user, 'form' => $form->createView()]
);
}

/**
* Mark notifications "seen" in the notification list
*
Expand Down
32 changes: 32 additions & 0 deletions site/src/Entity/User.php
Expand Up @@ -134,6 +134,14 @@ class User extends BaseUser implements NotifiableInterface
*/
protected $updatedAt;

/**
* Has the user subscribed to email notifications?
*
* @var $subscribedToEmailNotifications
*
* @ORM\Column(type="boolean", options={"default" : true})
*/
protected $subscribedToEmailNotifications = true;

public function __construct()
{
Expand Down Expand Up @@ -531,4 +539,28 @@ public function removeOrganizationsCreated(Organization $organizationsCreated)
{
$this->organizationsCreated->removeElement($organizationsCreated);
}

/**
* Has the user subscribed to email notifications?
*
* @return bool
*/
public function isSubscribedToEmailNotifications(): bool
{
return $this->subscribedToEmailNotifications;
}

/**
* Set email subscription for a given user
*
* @param bool $subscribedToEmailNotifications
*
* @return $this
*/
public function setSubscribedToEmailNotifications(bool $subscribedToEmailNotifications)
{
$this->subscribedToEmailNotifications = $subscribedToEmailNotifications;

return $this;
}
}
37 changes: 37 additions & 0 deletions site/src/Form/Type/NotificationSubscriptionType.php
@@ -0,0 +1,37 @@
<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NotificationSubscriptionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('subscribedToEmailNotifications', CheckboxType::class, [
'label' => 'Do you want to receive notifications via e-mail?',
'required' => false,
])
aquibbaig marked this conversation as resolved.
Show resolved Hide resolved
->add('save', SubmitType::class, array('label' => 'Save', 'attr' => ['class' => 'btn-primary']));
}

/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'App\Entity\User',
)
);
}
}