Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form options, controller template arrays #95

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Controller/ProfileManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PUGX\MultiUserBundle\Controller;

use FOS\UserBundle\Controller\ProfileController;
Expand Down Expand Up @@ -30,10 +31,10 @@ class ProfileManager
protected $formFactory;

/**
* @param UserDiscriminator $userDiscriminator
* @param UserDiscriminator $userDiscriminator
* @param ContainerInterface $container
* @param ProfileController $controller
* @param FormFactory $formFactory
* @param ProfileController $controller
* @param FormFactory $formFactory
*/
public function __construct(
UserDiscriminator $userDiscriminator,
Expand All @@ -49,16 +50,17 @@ public function __construct(

/**
* @param string $class
*
* @return RedirectResponse
*/
public function edit($class)
public function edit($class, $templates = array())
{
$this->userDiscriminator->setClass($class);

$this->controller->setContainer($this->container);
$result = $this->controller->editAction($this->container->get('request'));
if ($result instanceof RedirectResponse) {
return $this->controller->redirect($this->controller->get('request')->getRequestUri());
return $result;
}

$template = $this->userDiscriminator->getTemplate('profile');
Expand All @@ -67,8 +69,11 @@ public function edit($class)
}

$form = $this->formFactory->createForm();
return $this->container->get('templating')->renderResponse($template, array(
'form' => $form->createView(),

return $this->container->get('templating')->renderResponse($template,
array(
'form' => $form->createView(),
'templates' => $templates,
));
}
}
45 changes: 21 additions & 24 deletions Controller/RegistrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,33 @@
class RegistrationManager
{
/**
*
* @var \PUGX\MultiUserBundle\Model\UserDiscriminator
* @var \PUGX\MultiUserBundle\Model\UserDiscriminator
*/
protected $userDiscriminator;

/**
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;

/**
*
* @var \FOS\UserBundle\Controller\RegistrationController
* @var \FOS\UserBundle\Controller\RegistrationController
*/
protected $controller;

/**
*
* @var \PUGX\MultiUserBundle\Form\FormFactory
*/
protected $formFactory;

/**
*
* @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator
* @param \PUGX\MultiUserBundle\Model\UserDiscriminator $userDiscriminator
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \FOS\UserBundle\Controller\RegistrationController $controller
* @param \PUGX\MultiUserBundle\Form\FormFactory $formFactory
* @param \FOS\UserBundle\Controller\RegistrationController $controller
* @param \PUGX\MultiUserBundle\Form\FormFactory $formFactory
*/
public function __construct(UserDiscriminator $userDiscriminator,
ContainerInterface $container,
ContainerInterface $container,
RegistrationController $controller,
FormFactory $formFactory)
{
Expand All @@ -51,30 +46,32 @@ public function __construct(UserDiscriminator $userDiscriminator,
$this->controller = $controller;
$this->formFactory = $formFactory;
}

/**
*
* @param string $class
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function register($class)
public function register($class, $templates = array())
{
$this->userDiscriminator->setClass($class);

$this->controller->setContainer($this->container);
$result = $this->controller->registerAction($this->container->get('request'));
$result = $this->controller->registerAction($this->container->get('request'));
if ($result instanceof RedirectResponse) {
return $result;
}

$template = $this->userDiscriminator->getTemplate('registration');
if (is_null($template)) {
$template = 'FOSUserBundle:Registration:register.html.twig';
}

$form = $this->formFactory->createForm();

$form = $this->formFactory->createForm();

return $this->container->get('templating')->renderResponse($template, array(
'form' => $form->createView(),
'templates' => $templates,
));
}
}
10 changes: 10 additions & 0 deletions DependencyInjection/Compiler/OverrideServiceCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public function process(ContainerBuilder $container)
$container,
'fos_user.profile.form.factory',
'pugx_multi_user.profile_form_factory');

$this->changeService(
$container,
'fos_user.registration.form.type',
'pugx_multi_user.registration.form.type');

$this->changeService(
$container,
'fos_user.profile.form.type',
'pugx_multi_user.profile.form.type');
}

private function changeService($container, $serviceName, $newServiceName)
Expand Down
6 changes: 5 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()

->children()
->arrayNode('options')
->prototype('scalar')->defaultValue(null)->end()
->end()
->end()
->end()
->end()
->end();
Expand Down
78 changes: 78 additions & 0 deletions Form/ProfileFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\MultiUserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;

class ProfileFormType extends AbstractType
{
protected $class;
protected $options;

/**
* @param string $class The User class name
*/
public function __construct($class, $options = null)
{
$this->class = $class;
$this->options = $options;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->buildUserForm($builder, $options);

$builder->add('current_password', 'password', array(
'label' => 'form.current_password',
'translation_domain' => 'FOSUserBundle',
'mapped' => false,
'constraints' => new UserPassword(),
));
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class,
'intention' => 'profile',
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

public function getName()
{
return 'fos_user_profile';
}

/**
* Builds the embedded form representing the user.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
protected function buildUserForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
;
}
}
68 changes: 68 additions & 0 deletions Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PUGX\MultiUserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationFormType extends AbstractType
{
private $class;
protected $options;

/**
* @param string $class The User class name
*/
public function __construct($class, $options = null)
{
$this->class = $class;
$this->options = $options;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->class,
'intention' => 'registration',
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

public function getName()
{
return 'fos_user_registration';
}


}
23 changes: 18 additions & 5 deletions Model/UserDiscriminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getClasses()
foreach ($this->conf as $entity => $conf) {
$classes[] = $entity;
}

return $classes;
}

Expand Down Expand Up @@ -136,6 +136,18 @@ public function getUserFactory()
{
return $this->conf[$this->getClass()]['factory'];
}

/**
*
* @return array
*/
public function getUserOptions()
{
$class = $this->getClass();
$options = $this->conf[$class]['options'];

return $options;
}

/**
*
Expand All @@ -147,13 +159,13 @@ public function getFormType($name)
{
$class = $this->getClass();
$className = $this->conf[$class][$name]['form']['type'];
$options = $this->conf[$class]['options'];

if (!class_exists($className)) {
throw new \InvalidArgumentException(sprintf('UserDiscriminator, error getting form type : "%s" not found', $className));
}

$type = new $className($class);

$type = new $className($class, $options);
return $type;
}

Expand Down Expand Up @@ -185,7 +197,7 @@ public function getTemplate($name)
{
return $this->conf[$this->getClass()][$name]['template'];
}

/**
*
* @param array $entities
Expand Down Expand Up @@ -219,7 +231,8 @@ protected function buildConfig(array $users)
'validation_groups' => $user['profile']['form']['validation_groups'],
),
'template' => $user['profile']['template'],
)
),
'options' => $user['options']
);
}
}
Expand Down
Loading