Skip to content

Commit

Permalink
add completion form and handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Jun 21, 2016
1 parent 3f335eb commit c136bae
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 10 deletions.
86 changes: 86 additions & 0 deletions Controller/CompletionController.php
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\CommunityBundle\EventListener\CompletionListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

/**
* Handles the completion page.
*/
class CompletionController extends AbstractController
{
const TYPE = Configuration::TYPE_COMPLETION;

/**
* Handle registration form.
*
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
$user = $this->getUser();

if (!$user) {
throw new HttpException(403, 'You need to be logged in for completion form.');
}

$communityManager = $this->getCommunityManager($this->getWebspaceKey());

// Create Form
$form = $this->createForm(
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE),
$user,
$communityManager->getConfigTypeProperty(self::TYPE, Configuration::FORM_TYPE_OPTIONS)
);

$form->handleRequest($request);
$success = false;

// Handle Form Success
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();

// Completion User
$communityManager->completion($user);

// Redirect
$session = $request->getSession();
$redirectTo = $session->get(CompletionListener::SESSION_STORE);

if (!$redirectTo) {
$redirectTo = $communityManager->getConfigTypeProperty(self::TYPE, Configuration::REDIRECT_TO);
} else {
$session->remove(CompletionListener::SESSION_STORE);
}

if ($redirectTo) {
return $this->redirect($redirectTo);
}

$success = true;
}

return $this->renderTemplate(
self::TYPE,
[
'form' => $form->createView(),
'success' => $success,
]
);
}
}
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Expand Up @@ -184,13 +184,13 @@ public function getConfigTreeBuilder()
->arrayNode(self::FORM_TYPE_OPTIONS)
->addDefaultsIfNotSet()
->end()
->scalarNode(self::REDIRECT_TO)->defaultValue('?send=true')->end()
->scalarNode(self::REDIRECT_TO)->defaultValue('/')->end()
->arrayNode(self::EMAIL)
->addDefaultsIfNotSet()
->children()
->scalarNode(self::EMAIL_SUBJECT)->defaultValue('Completion')->end()
->scalarNode(self::EMAIL_ADMIN_TEMPLATE)->defaultValue(null)->end()
->scalarNode(self::EMAIL_USER_TEMPLATE)->defaultValue('SuluCommunityBundle:community:completion-email.html.twig')->end()
->scalarNode(self::EMAIL_USER_TEMPLATE)->defaultValue(null)->end()
->end()
->end()
->end()
Expand Down
17 changes: 15 additions & 2 deletions EventListener/CompletionListener.php
Expand Up @@ -24,6 +24,11 @@
*/
class CompletionListener
{
/**
* @var string
*/
const SESSION_STORE = 'sulu_community/completion/redirect_to';

/**
* @var RequestAnalyzerInterface
*/
Expand Down Expand Up @@ -83,8 +88,13 @@ public function onRequest(GetResponseEvent $event)
return;
}

if ($request->isXmlHttpRequest()) {
// don't do anything if it's a ajax request
return;
}

$completionUrl = $this->router->generate('sulu_community.completion');
if ($request->getUri() === $completionUrl) {
if ($request->getPathInfo() === $completionUrl) {
// don't do anything if it's the completion url
return;
}
Expand All @@ -94,11 +104,14 @@ public function onRequest(GetResponseEvent $event)
/** @var User $user */
$user = $token->getUser();

if (!$user) {
if (!$user instanceof User) {
// don't do anything if no user is login
return;
}

$session = $request->getSession();
$session->set(self::SESSION_STORE, $request->getUri());

$webspaceKey = $this->requestAnalyzer->getWebspace()->getKey();
$validator = $this->getValidator($webspaceKey);

Expand Down
10 changes: 10 additions & 0 deletions EventListener/MailListener.php
Expand Up @@ -94,6 +94,16 @@ public function sendPasswordResetEmails(CommunityEvent $event)
$this->sendTypeEmails($event, Configuration::TYPE_PASSWORD_RESET);
}

/**
* Send password reset emails.
*
* @param CommunityEvent $event
*/
public function sendCompletionEmails(CommunityEvent $event)
{
$this->sendTypeEmails($event, Configuration::TYPE_COMPLETION);
}

/**
* Send emails for specific type.
*
Expand Down
4 changes: 2 additions & 2 deletions Form/Type/CompletionContactType.php
Expand Up @@ -28,11 +28,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$contact = $builder->getData();

if (!$contact->getFirstname()) {
if (!$contact->getFirstName()) {
$builder->add('first_name', 'text');
}

if (!$contact->getLastname()) {
if (!$contact->getLastName()) {
$builder->add('last_name', 'text');
}
}
Expand Down
10 changes: 7 additions & 3 deletions Form/Type/CompletionType.php
Expand Up @@ -14,7 +14,6 @@
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -43,9 +42,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add(
'contact',
new $options['contact_type'](),
$options['contact_type_options']
array_merge(
$options['contact_type_options'],
[
'data' => $user->getContact(),
]
)
);

$builder->add('submit', SubmitType::class);
}

Expand Down
22 changes: 22 additions & 0 deletions Manager/CommunityManager.php
Expand Up @@ -38,6 +38,7 @@ class CommunityManager
const EVENT_CONFIRMED = 'sulu.community.confirmed';
const EVENT_PASSWORD_FORGOT = 'sulu.community.password_forgot';
const EVENT_PASSWORD_RESETED = 'sulu.community.password_reseted';
const EVENT_COMPLETED = 'sulu.community.completed';

/**
* @var array
Expand Down Expand Up @@ -188,6 +189,27 @@ public function register(User $user)
return $user;
}

/**
* @param User $user
*
* @return User
*/
public function completion($user)
{
// TODO use user mmanager
$contact = $user->getContact();

$this->entityManager->persist($contact);
$this->entityManager->persist($user);
$this->entityManager->flush();

// Event
$event = new CommunityEvent($user, $this->config);
$this->eventDispatcher->dispatch(self::EVENT_COMPLETED, $event);

return $user;
}

/**
* @param User $user
* @param Request $request
Expand Down
105 changes: 104 additions & 1 deletion README.md
Expand Up @@ -77,6 +77,7 @@ security:

access_control:
- { path: /profile, roles: ROLE_USER }
- { path: /completion, roles: ROLE_USER }

firewalls:
<webspace_key>:
Expand Down Expand Up @@ -104,7 +105,9 @@ sulu_security:
app/console sulu:community:init
```

## Embedded Login
## Additional Features

### Embedded Login

Activate ESI to use the `login-embed.html.twig`.

Expand All @@ -121,3 +124,103 @@ framework:
{{ render_esi(controller('SuluCommunityBundle:Login:embed')) }}
```

### Completion Form

You can add an additional completion form to complete the user registration after confirmation.

**Create Service**

```php
<?php

namespace AppBundle\Validator;

use Sulu\Bundle\CommunityBundle\Validator\User\CompletionInterface;
use Sulu\Bundle\SecurityBundle\Entity\User;

/**
* Validates the user before he can access pages.
*/
class CompletionValidator implements CompletionInterface
{
/**
* {@inheritdoc}
*/
public function validate(User $user, $webspaceKey)
{
$contact = $user->getContact();

if (!$contact
|| !$contact->getFirstName()
|| !$contact->getLastName()
|| !$user->getUsername()
|| !$user->getEmail()
) {
return false;
}

return true;
}
}
```

**Register Service**

```xml
<service id="app.completion_validator" class="AppBundle\Validator\CompletionValidator" />
```

**Create Form**

```php
<?php

namespace AppBundle\Form\Type\CompletionType;

use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Create the registration form type.
*/
class CompletionType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Create your form

$builder->add('submit', SubmitType::class);
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => User::class,
'validation_groups' => ['completion'],
]
);
}
}
```

**Configuration**

```yml
# Community Configuration
sulu_community:
webspaces:
<webspace_key>:
completion:
form: AppBundle\Form\Type\CompletionType
service: app.completion_validator
```
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Expand Up @@ -10,6 +10,7 @@
<parameter type="constant" key="sulu_community.event.confirmed">Sulu\Bundle\CommunityBundle\Manager\CommunityManager::EVENT_CONFIRMED</parameter>
<parameter type="constant" key="sulu_community.event.password_forgot">Sulu\Bundle\CommunityBundle\Manager\CommunityManager::EVENT_PASSWORD_FORGOT</parameter>
<parameter type="constant" key="sulu_community.event.password_reseted">Sulu\Bundle\CommunityBundle\Manager\CommunityManager::EVENT_PASSWORD_RESETED</parameter>
<parameter type="constant" key="sulu_community.event.completed">Sulu\Bundle\CommunityBundle\Manager\CommunityManager::EVENT_COMPLETED</parameter>
</parameters>

<services>
Expand All @@ -23,6 +24,7 @@
<tag name="kernel.event_listener" event="%sulu_community.event.confirmed%" method="sendConfirmationEmails" />
<tag name="kernel.event_listener" event="%sulu_community.event.password_forgot%" method="sendPasswordForgetEmails" />
<tag name="kernel.event_listener" event="%sulu_community.event.password_reseted%" method="sendPasswordResetEmails" />
<tag name="kernel.event_listener" event="%sulu_community.event.completed%" method="sendCompletionEmails" />
</service>

<!-- Completion Listener -->
Expand Down
13 changes: 13 additions & 0 deletions Resources/views/community/completion.html.twig
@@ -0,0 +1,13 @@
{% extends "master.html.twig" %}

{% block content %}
<h1>{{ 'Completion'|trans }}</h1>

{% if app.request.get('send') == 'true' %}
<p>
{{ 'completion_finished'|trans }}
</p>
{% else %}
{{ form(form) }}
{% endif %}
{% endblock %}

0 comments on commit c136bae

Please sign in to comment.