parent
52ca82e154
commit
1175fe2459
@ -0,0 +1,124 @@ |
||||
<?php |
||||
|
||||
namespace Chamilo\UserBundle\Form\EventListener; |
||||
|
||||
use Sylius\Component\Attribute\Model\AttributeTypes; |
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
use Symfony\Component\Form\FormFactoryInterface; |
||||
|
||||
use Sylius\Bundle\AttributeBundle\Form\EventListener\BuildAttributeFormChoicesListener as BaseBuildAttributeFormChoicesListener; |
||||
|
||||
/** |
||||
* Form event listener that builds choices for attribute form. |
||||
* |
||||
* @author Leszek Prabucki <leszek.prabucki@gmail.com> |
||||
* @author Liverbool <liverbool@gmail.com> |
||||
*/ |
||||
class BuildAttributeFormChoicesListener extends BaseBuildAttributeFormChoicesListener |
||||
{ |
||||
/** |
||||
* Form factory. |
||||
* |
||||
* @var FormFactoryInterface |
||||
*/ |
||||
private $factory; |
||||
|
||||
/** |
||||
* Constructor. |
||||
* |
||||
* @param FormFactoryInterface $factory |
||||
*/ |
||||
public function __construct(FormFactoryInterface $factory) |
||||
{ |
||||
$this->factory = $factory; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function getSubscribedEvents() |
||||
{ |
||||
return array( |
||||
FormEvents::PRE_SET_DATA => 'buildChoices', |
||||
FormEvents::PRE_SUBMIT => 'buildConfiguration', |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Build configuration field for attribute form. |
||||
* |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function buildConfiguration(FormEvent $event) |
||||
{ |
||||
$data = $event->getData(); |
||||
$choices = array(); |
||||
|
||||
if (AttributeTypes::CHOICE === $data['type'] && !empty($data['choices'])) { |
||||
$choices = $data['choices']; |
||||
} |
||||
|
||||
$data['configuration'] = $choices; |
||||
|
||||
if (!$event->getForm()->has('configuration')) { |
||||
$event->getForm()->add( |
||||
$this->factory->createNamed( |
||||
'configuration', |
||||
'collection', |
||||
null, |
||||
array( |
||||
'allow_add' => true, |
||||
'allow_delete' => true, |
||||
'by_reference' => false, |
||||
'auto_initialize' => false, |
||||
) |
||||
) |
||||
); |
||||
} |
||||
|
||||
$event->setData($data); |
||||
} |
||||
|
||||
/** |
||||
* Builds choices for attribute form. |
||||
* |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function buildChoices(FormEvent $event) |
||||
{ |
||||
$attribute = $event->getData(); |
||||
if (null === $attribute) { |
||||
return; |
||||
} |
||||
|
||||
$type = $attribute->getType(); |
||||
|
||||
if (null === $type || AttributeTypes::CHOICE === $type) { |
||||
$data = null; |
||||
$config = $attribute->getConfiguration(); |
||||
|
||||
if (!empty($config['choices'])) { |
||||
$data = $config['choices']; |
||||
} |
||||
|
||||
$event->getForm()->add( |
||||
$this->factory->createNamed( |
||||
'choices', |
||||
'collection', |
||||
null, |
||||
array( |
||||
'type' => 'text', |
||||
'allow_add' => true, |
||||
'allow_delete' => true, |
||||
'by_reference' => false, |
||||
'auto_initialize' => false, |
||||
'mapped' => false, |
||||
'data' => $data, |
||||
) |
||||
) |
||||
); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,104 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form\EventListener; |
||||
|
||||
use Sylius\Component\Attribute\Model\AttributeValueInterface; |
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormFactoryInterface; |
||||
|
||||
/** |
||||
* Class BuildAttributeValueFormListener |
||||
* @package Chamilo\UserBundle\Form\EventListener |
||||
*/ |
||||
class BuildAttributeValueFormListener implements EventSubscriberInterface |
||||
{ |
||||
public static function getSubscribedEvents() |
||||
{ |
||||
return array( |
||||
FormEvents::PRE_SET_DATA => 'onPreSetData', |
||||
FormEvents::PRE_SUBMIT => 'onPreSubmit', |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Builds proper product form after setting the product. |
||||
* |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function buildForm(FormEvent $event) |
||||
{ |
||||
/** @var \Chamilo\CoreBundle\Entity\ExtraFieldValues $attributeValue */ |
||||
$attributeValue = $event->getData(); |
||||
|
||||
$form = $event->getForm(); |
||||
|
||||
if (null === $attributeValue) { |
||||
$form->add( |
||||
$this->factory->createNamed( |
||||
'value', |
||||
'text', |
||||
null, |
||||
array('auto_initialize' => false) |
||||
) |
||||
); |
||||
return; |
||||
} |
||||
|
||||
$type = $attributeValue->getType(); |
||||
$attributeValue->setAttribute($attributeValue->getField()); |
||||
|
||||
$options = array( |
||||
'label' => $attributeValue->getName(), |
||||
'auto_initialize' => false, |
||||
); |
||||
|
||||
if (is_array($attributeValue->getConfiguration())) { |
||||
$options = array_merge( |
||||
$options, |
||||
$attributeValue->getConfiguration() |
||||
); |
||||
} |
||||
|
||||
$this->verifyValue($attributeValue); |
||||
|
||||
// If we're editing the attribute value, let's just render the value field, not full selection. |
||||
$form |
||||
->remove('extraField') |
||||
->add($this->factory->createNamed('value', $type, null, $options)); |
||||
} |
||||
|
||||
/** |
||||
* Verify value before set to form |
||||
* |
||||
* @param AttributeValueInterface $attributeValue |
||||
*/ |
||||
protected function verifyValue(AttributeValueInterface $attributeValue) |
||||
{ |
||||
switch ($attributeValue->getType()) { |
||||
case AttributeTypes::CHECKBOX: |
||||
if (!is_bool($attributeValue->getValue())) { |
||||
$attributeValue->setValue(false); |
||||
} |
||||
|
||||
break; |
||||
|
||||
case AttributeTypes::CHOICE: |
||||
if (!is_array($attributeValue->getValue())) { |
||||
$attributeValue->setValue(null); |
||||
} |
||||
|
||||
break; |
||||
|
||||
case AttributeTypes::MONEY: |
||||
case AttributeTypes::NUMBER: |
||||
case AttributeTypes::PERCENTAGE: |
||||
if (!is_numeric($attributeValue->getValue())) { |
||||
$attributeValue->setValue(null); |
||||
} |
||||
|
||||
break; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
<?php |
||||
|
||||
/* |
||||
* This file is part of the Sylius package. |
||||
* |
||||
* (c) Paweł Jędrzejewski |
||||
* |
||||
* For the full copyright and license information, please view the LICENSE |
||||
* file that was distributed with this source code. |
||||
*/ |
||||
|
||||
namespace Chamilo\UserBundle\Form\EventSubscriber; |
||||
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||||
use Symfony\Component\Form\Exception\InvalidArgumentException; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
use Symfony\Component\Form\FormFactoryInterface; |
||||
|
||||
/** |
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> |
||||
*/ |
||||
class BuildAttributeFormSubscriber implements EventSubscriberInterface |
||||
{ |
||||
/** |
||||
* @var FormFactoryInterface |
||||
*/ |
||||
protected $factory; |
||||
|
||||
/** |
||||
* @param FormFactoryInterface $formFactory |
||||
*/ |
||||
public function __construct(FormFactoryInterface $formFactory) |
||||
{ |
||||
$this->factory = $formFactory; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function getSubscribedEvents() |
||||
{ |
||||
return array( |
||||
FormEvents::PRE_SET_DATA => 'addConfigurationFields', |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function addConfigurationFields(FormEvent $event) |
||||
{ |
||||
$attribute = $event->getData(); |
||||
$form = $event->getForm(); |
||||
|
||||
try { |
||||
$requiredFields = $this->factory->createNamed( |
||||
'configuration', |
||||
'sylius_attribute_type_configuration_'.$attribute->getType(), |
||||
null, |
||||
array( |
||||
'auto_initialize' => false, |
||||
'label' => 'sylius.attribute_type.configuration', |
||||
) |
||||
); |
||||
|
||||
$form->add($requiredFields); |
||||
} catch (InvalidArgumentException $exception) { |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,97 @@ |
||||
<?php |
||||
|
||||
/* |
||||
* This file is part of the Sylius package. |
||||
* |
||||
* (c) Paweł Jędrzejewski |
||||
* |
||||
* For the full copyright and license information, please view the LICENSE |
||||
* file that was distributed with this source code. |
||||
*/ |
||||
|
||||
namespace Chamilo\UserBundle\Form\EventSubscriber; |
||||
|
||||
use Sylius\Component\Attribute\Model\AttributeInterface; |
||||
use Sylius\Component\Resource\Repository\RepositoryInterface; |
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
use Symfony\Component\Form\FormInterface; |
||||
|
||||
/** |
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> |
||||
*/ |
||||
class BuildAttributeValueFormSubscriber implements EventSubscriberInterface |
||||
{ |
||||
/** |
||||
* @var RepositoryInterface |
||||
*/ |
||||
protected $attributeRepository; |
||||
|
||||
/** |
||||
* @param RepositoryInterface $attributeRepository |
||||
*/ |
||||
public function __construct(RepositoryInterface $attributeRepository) |
||||
{ |
||||
$this->attributeRepository = $attributeRepository; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function getSubscribedEvents() |
||||
{ |
||||
return array( |
||||
FormEvents::PRE_SET_DATA => 'preSetData', |
||||
FormEvents::PRE_SUBMIT => 'preSubmit', |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function preSetData(FormEvent $event) |
||||
{ |
||||
$attributeValue = $event->getData(); |
||||
|
||||
if (null === $attributeValue || null === $attributeValue->getAttribute()) { |
||||
return; |
||||
} |
||||
$this->addValueField($event->getForm(), $attributeValue->getAttribute()); |
||||
} |
||||
|
||||
/** |
||||
* @param FormEvent $event |
||||
*/ |
||||
public function preSubmit(FormEvent $event) |
||||
{ |
||||
$attributeValue = $event->getData(); |
||||
|
||||
if (!isset($attributeValue['value']) || !isset($attributeValue['attribute'])) { |
||||
throw new \InvalidArgumentException('Cannot create an attribute value form on pre submit event without "attribute" and "value" keys in data.'); |
||||
} |
||||
$form = $event->getForm(); |
||||
|
||||
$attribute = $this->attributeRepository->find($attributeValue['attribute']); |
||||
|
||||
$attribute = $attribute->getAttribute(); |
||||
|
||||
$this->addValueField($form, $attribute); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* @param FormInterface $form |
||||
* @param AttributeInterface $attribute |
||||
*/ |
||||
private function addValueField(FormInterface $form, AttributeInterface $attribute) |
||||
{ |
||||
$options = array( |
||||
'auto_initialize' => false, |
||||
'label' => $attribute->getName() |
||||
); |
||||
|
||||
$form->add('attribute'); |
||||
$form->add('value', 'sylius_attribute_type_'.$attribute->getType(), $options); |
||||
} |
||||
} |
@ -0,0 +1,62 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form; |
||||
|
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Symfony\Component\Form\AbstractType; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
||||
use Symfony\Component\Security\Core\SecurityContext; |
||||
use Symfony\Component\OptionsResolver\OptionsResolver; |
||||
|
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; |
||||
|
||||
/** |
||||
* Class ProfileType |
||||
* @deprecated is not being used |
||||
* @package Chamilo\UserBundle\Form |
||||
*/ |
||||
class ProfileFosUserType extends AbstractType |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
**/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$builder |
||||
->add('username') |
||||
->add('email', 'email') |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* |
||||
* @deprecated Remove it when bumping requirements to Symfony 2.7+ |
||||
*/ |
||||
public function setDefaultOptions(OptionsResolverInterface $resolver) |
||||
{ |
||||
$this->configureOptions($resolver); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function configureOptions(OptionsResolver $resolver) |
||||
{ |
||||
$resolver->setDefaults( |
||||
array( |
||||
'data_class' => 'Chamilo\UserBundle\Entity\User', |
||||
) |
||||
); |
||||
} |
||||
|
||||
public function getName() |
||||
{ |
||||
return 'chamilo_fos_user_profile'; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,173 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form; |
||||
|
||||
use Symfony\Component\Form\AbstractType; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
||||
use Symfony\Component\OptionsResolver\OptionsResolver; |
||||
|
||||
/** |
||||
* Class ProfileType |
||||
* Located in web/app_dev.php/profile/edit-profile |
||||
* @package Chamilo\UserBundle\Form |
||||
*/ |
||||
class ProfileType extends AbstractType |
||||
{ |
||||
/** |
||||
* @todo replace hardcode values of locale.preferred_choices |
||||
* @inheritdoc |
||||
**/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$builder |
||||
->add( |
||||
'firstname', |
||||
null, |
||||
array( |
||||
'label' => 'form.label_firstname', |
||||
'required' => false, |
||||
) |
||||
) |
||||
->add( |
||||
'lastname', |
||||
null, |
||||
array( |
||||
'label' => 'form.label_lastname', |
||||
'required' => false, |
||||
) |
||||
) |
||||
->add('official_code', 'text') |
||||
//->add('groups') |
||||
->add( |
||||
'locale', |
||||
'locale', |
||||
array( |
||||
'preferred_choices' => array( |
||||
'en', |
||||
'fr', |
||||
'es', |
||||
'pt', |
||||
'nl', |
||||
), |
||||
) |
||||
) |
||||
->add( |
||||
'dateOfBirth', |
||||
'birthday', |
||||
array( |
||||
'label' => 'form.label_date_of_birth', |
||||
'required' => false, |
||||
'widget' => 'single_text', |
||||
) |
||||
) |
||||
->add( |
||||
'website', |
||||
'url', |
||||
array( |
||||
'label' => 'form.label_website', |
||||
'required' => false, |
||||
) |
||||
) |
||||
->add( |
||||
'biography', |
||||
'textarea', |
||||
array( |
||||
'label' => 'form.label_biography', |
||||
'required' => false, |
||||
) |
||||
) |
||||
/*->add('locale', 'locale', array( |
||||
'label' => 'form.label_locale', |
||||
'required' => false, |
||||
))*/ |
||||
->add( |
||||
'timezone', |
||||
'timezone', |
||||
array( |
||||
'label' => 'form.label_timezone', |
||||
'required' => false, |
||||
//'preferred_choices' => array('Europe/Paris', 'America/Lima'), |
||||
) |
||||
) |
||||
->add( |
||||
'phone', |
||||
null, |
||||
array( |
||||
'label' => 'form.label_phone', |
||||
'required' => false, |
||||
) |
||||
) |
||||
->add( |
||||
'picture', |
||||
'sonata_media_type', |
||||
array( |
||||
'provider' => 'sonata.media.provider.image', |
||||
'context' => 'user', |
||||
'required' => false, |
||||
'data_class' => 'Chamilo\MediaBundle\Entity\Media', |
||||
) |
||||
) |
||||
/*->add( |
||||
'extraFieldValues', |
||||
CollectionType::class, |
||||
array( |
||||
'required' => false, |
||||
'allow_add' => true, |
||||
'allow_delete' => true, |
||||
'type' => 'chamilo_user_extra_field_value', |
||||
'by_reference' => false, |
||||
'prototype' => true, |
||||
'widget_add_btn' => ['label' => 'Add'], |
||||
'options' => array( // options for collection fields |
||||
'widget_remove_btn' => array('label' => 'Remove'), |
||||
'label_render' => false, |
||||
) |
||||
) |
||||
)*/ |
||||
//->add('save', 'submit', array('label' => 'Update') ) |
||||
; |
||||
|
||||
// Update Author id |
||||
/*$builder->addEventListener( |
||||
FormEvents::POST_SUBMIT, |
||||
function (FormEvent $event) use ($currentUser) { |
||||
// @var User $user |
||||
$user = $event->getData(); |
||||
$extraFields = $user->getExtrafields(); |
||||
foreach ($extraFields as $extraField) { |
||||
$extraField->setAuthor($currentUser); |
||||
} |
||||
} |
||||
);*/ |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* |
||||
* @deprecated Remove it when bumping requirements to Symfony 2.7+ |
||||
*/ |
||||
/*public function setDefaultOptions(OptionsResolverInterface $resolver) |
||||
{ |
||||
$this->configureOptions($resolver); |
||||
}*/ |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function configureOptions(OptionsResolver $resolver) |
||||
{ |
||||
$resolver->setDefaults( |
||||
array( |
||||
'data_class' => 'Chamilo\UserBundle\Entity\User', |
||||
) |
||||
); |
||||
} |
||||
|
||||
public function getName() |
||||
{ |
||||
return 'chamilo_sonata_user_profile'; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,64 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form\Type; |
||||
|
||||
use Sylius\Bundle\AttributeBundle\Form\EventSubscriber\BuildAttributeFormSubscriber; |
||||
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber; |
||||
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
|
||||
/** |
||||
* Attribute type. |
||||
* |
||||
* @author Paweł Jędrzejewski <pawel@sylius.org> |
||||
* @author Leszek Prabucki <leszek.prabucki@gmail.com> |
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> |
||||
*/ |
||||
class AttributeType extends AbstractResourceType |
||||
{ |
||||
/** |
||||
* @var string |
||||
*/ |
||||
protected $subjectName; |
||||
|
||||
/** |
||||
* @param string $dataClass |
||||
* @param array $validationGroups |
||||
* @param string $subjectName |
||||
*/ |
||||
public function __construct($dataClass, array $validationGroups, $subjectName) |
||||
{ |
||||
parent::__construct($dataClass, $validationGroups); |
||||
|
||||
$this->subjectName = $subjectName; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$builder |
||||
->addEventSubscriber(new BuildAttributeFormSubscriber($builder->getFormFactory())) |
||||
->addEventSubscriber(new AddCodeFormSubscriber()) |
||||
/*->add('translations', 'a2lix_translationsForms', array( |
||||
'form_type' => sprintf('sylius_%s_attribute_translation', $this->subjectName), |
||||
'label' => 'sylius.form.attribute.translations', |
||||
))*/ |
||||
->add('type', 'sylius_attribute_type_choice', array( |
||||
'label' => 'sylius.form.attribute.type', |
||||
'disabled' => true, |
||||
)) |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getName() |
||||
{ //chamilo_user_extra_field_choice |
||||
return 'chamilo_user_attribute_type'; |
||||
return sprintf('%s_extra_field', $this->subjectName); |
||||
} |
||||
} |
@ -0,0 +1,58 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form\Type; |
||||
|
||||
use Symfony\Component\Form\AbstractType; |
||||
use Symfony\Component\OptionsResolver\OptionsResolver; |
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
||||
|
||||
/** |
||||
* Attribute choice form type. |
||||
* |
||||
* @author Paweł Jędrzejewski <pawel@sylius.org> |
||||
*/ |
||||
class AttributeTypeChoiceType extends AbstractType |
||||
{ |
||||
/** |
||||
* @var array |
||||
*/ |
||||
private $attributeTypes; |
||||
|
||||
/** |
||||
* @param array $attributeTypes |
||||
*/ |
||||
public function __construct($attributeTypes) |
||||
{ |
||||
$this->attributeTypes = $attributeTypes; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function configureOptions(OptionsResolver $resolver) |
||||
{ |
||||
$resolver |
||||
->setDefaults( |
||||
array('choices' => $this->attributeTypes) |
||||
) |
||||
; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getParent() |
||||
{ |
||||
return 'choice'; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
//return sprintf('chamilo_%s_extra_field_choice', $this->subjectName); |
||||
return 'chamilo_user_attribute_choice'; |
||||
} |
||||
} |
@ -0,0 +1,136 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form\Type; |
||||
|
||||
use Chamilo\UserBundle\Form\EventSubscriber\BuildAttributeValueFormSubscriber; |
||||
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; |
||||
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; |
||||
//use Sylius\Component\Product\Model\AttributeInterface; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
use Symfony\Component\Form\FormInterface; |
||||
use Symfony\Component\Form\FormView; |
||||
|
||||
/** |
||||
* Attribute value form type. |
||||
* |
||||
* @author Paweł Jędrzejewski <pawel@sylius.org> |
||||
*/ |
||||
class AttributeValueType extends AbstractResourceType |
||||
{ |
||||
/** |
||||
* Attributes subject name. |
||||
* |
||||
* @var string |
||||
*/ |
||||
protected $subjectName; |
||||
|
||||
/** |
||||
* @var EntityRepository |
||||
*/ |
||||
protected $attributeRepository; |
||||
|
||||
/** |
||||
* @param string $dataClass |
||||
* @param array $validationGroups |
||||
* @param string $subjectName |
||||
* @param EntityRepository $attributeRepository |
||||
*/ |
||||
public function __construct($dataClass, array $validationGroups, $subjectName, EntityRepository $attributeRepository) |
||||
{ |
||||
parent::__construct($dataClass, $validationGroups); |
||||
|
||||
$this->subjectName = $subjectName; |
||||
$this->attributeRepository = $attributeRepository; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$builder |
||||
/*->add( |
||||
'attribute', |
||||
sprintf('chamilo_%s_attribute_choice', $this->subjectName), |
||||
['label' => sprintf('chamilo.form.attribute.%s_attribute_value.attribute', $this->subjectName)] |
||||
)*/ |
||||
->addEventSubscriber( |
||||
new BuildAttributeValueFormSubscriber($this->attributeRepository) |
||||
) |
||||
; |
||||
|
||||
// $prototypes = array(); |
||||
// $attributes = $this->getAttributes($builder); |
||||
// |
||||
// if ($attributes) { |
||||
// /** @var \Chamilo\CoreBundle\Entity\ExtraField $attribute */ |
||||
// foreach ($attributes as $attribute) { |
||||
// if (!empty($attribute)) { |
||||
// $configuration = $attribute->getConfiguration(); |
||||
// $type = $attribute->getTypeToString(); |
||||
// |
||||
// if (!is_array($configuration)) { |
||||
// $configuration = array(); |
||||
// } |
||||
// |
||||
// if (empty($type)) { |
||||
// continue; |
||||
// } |
||||
// } |
||||
// |
||||
// $prototypes[] = $builder->create( |
||||
// 'value', |
||||
// $type, |
||||
// $configuration |
||||
// )->getForm(); |
||||
// } |
||||
// } |
||||
// |
||||
// $builder->setAttribute('prototypes', $prototypes); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
/*public function buildView( |
||||
FormView $view, |
||||
FormInterface $form, |
||||
array $options |
||||
) { |
||||
$view->vars['prototypes'] = array(); |
||||
|
||||
foreach ($form->getConfig()->getAttribute('prototypes', array()) as $name => $prototype) { |
||||
$view->vars['prototypes'][$name] = $prototype->createView($view); |
||||
} |
||||
}*/ |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
//return sprintf('sylius_%s_attribute_value', $this->subjectName); |
||||
return sprintf('chamilo_%s_extra_field_value', $this->subjectName); |
||||
} |
||||
|
||||
/** |
||||
* Get attributes |
||||
* |
||||
* @param FormBuilderInterface $builder |
||||
* |
||||
* @return AttributeInterface[] |
||||
*/ |
||||
private function getAttributes(FormBuilderInterface $builder) |
||||
{ |
||||
/** @var \Symfony\Component\Form\FormBuilder $extraField */ |
||||
$extraField = $builder->get('extraField'); |
||||
|
||||
if ($extraField->hasOption('choice_list')) { |
||||
|
||||
return $extraField->getOption('choice_list')->getChoices(); |
||||
} |
||||
|
||||
return []; |
||||
} |
||||
} |
@ -0,0 +1,137 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form\Type; |
||||
|
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Symfony\Component\Form\AbstractType; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
||||
use Symfony\Component\Security\Core\SecurityContext; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
use Symfony\Component\OptionsResolver\OptionsResolver; |
||||
|
||||
/** |
||||
* Class RegistrationFormType |
||||
* Form located in web/app_dev.php/register/ |
||||
* @package Chamilo\UserBundle\Form\Type |
||||
*/ |
||||
class RegistrationFormType extends AbstractType |
||||
{ |
||||
/** |
||||
* @inheritdoc |
||||
**/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$builder |
||||
->add( |
||||
'username', |
||||
null, |
||||
array( |
||||
'label' => 'form.username', |
||||
'translation_domain' => 'FOSUserBundle', |
||||
) |
||||
) |
||||
->add('firstname', 'text') |
||||
->add('lastname', 'text') |
||||
->add( |
||||
'email', |
||||
'email', |
||||
array( |
||||
'label' => 'form.email', |
||||
'translation_domain' => 'FOSUserBundle', |
||||
) |
||||
) |
||||
->add('captcha', 'Gregwar\CaptchaBundle\Type\CaptchaType'); |
||||
; |
||||
|
||||
//$builder |
||||
/*->add('official_code', 'text') |
||||
->add('email', 'email') |
||||
->add('username', 'text') |
||||
->add('phone', 'text') |
||||
->add('password', 'password') |
||||
->add('groups') |
||||
->add('timezone', 'timezone') |
||||
->add( |
||||
'locale', |
||||
'locale', |
||||
array('preferred_choices' => array('en', 'fr', 'es')) |
||||
) |
||||
->add( |
||||
'picture_uri', |
||||
'sonata_media_type', |
||||
array( |
||||
'provider' => 'sonata.media.provider.image', |
||||
'context' => 'user_image', |
||||
'required' => false, |
||||
) |
||||
) |
||||
->add( |
||||
'extraFields', |
||||
'collection', |
||||
array( |
||||
'required' => false, |
||||
'type' => 'chamilo_user.form.type.attribute_value_type', |
||||
'allow_add' => true, |
||||
'allow_delete' => true, |
||||
'by_reference' => false, |
||||
) |
||||
) |
||||
->add('save', 'submit', array('label' => 'Update'))*/ |
||||
// ; |
||||
|
||||
// Update Author id |
||||
/*$builder->addEventListener( |
||||
FormEvents::POST_SUBMIT, |
||||
function (FormEvent $event) use ($currentUser) { |
||||
// @var User $user |
||||
$user = $event->getData(); |
||||
$extraFields = $user->getExtrafields(); |
||||
foreach ($extraFields as $extraField) { |
||||
$extraField->setAuthor($currentUser); |
||||
} |
||||
} |
||||
);*/ |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
* |
||||
* @deprecated Remove it when bumping requirements to Symfony 2.7+ |
||||
*/ |
||||
public function setDefaultOptions(OptionsResolverInterface $resolver) |
||||
{ |
||||
$this->configureOptions($resolver); |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function configureOptions(OptionsResolver $resolver) |
||||
{ |
||||
$resolver->setDefaults( |
||||
array( |
||||
'class' => 'Chamilo\UserBundle\Entity\User', |
||||
) |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getParent() |
||||
{ |
||||
return 'fos_user_registration'; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return 'chamilo_sonata_user_registration'; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,106 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\UserBundle\Form; |
||||
|
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Symfony\Component\Form\AbstractType; |
||||
use Symfony\Component\Form\FormBuilderInterface; |
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
||||
use Symfony\Component\Security\Core\SecurityContext; |
||||
use Symfony\Component\Form\FormEvent; |
||||
use Symfony\Component\Form\FormEvents; |
||||
|
||||
/** |
||||
* Class UserType |
||||
* @deprecated |
||||
* @package Chamilo\UserBundle\Form |
||||
*/ |
||||
class UserType extends AbstractType |
||||
{ |
||||
private $securityContext; |
||||
|
||||
public function __construct(SecurityContext $securityContext) |
||||
{ |
||||
$this->securityContext = $securityContext; |
||||
} |
||||
|
||||
/** |
||||
* @inheritdoc |
||||
**/ |
||||
public function buildForm(FormBuilderInterface $builder, array $options) |
||||
{ |
||||
$currentUser = $this->securityContext->getToken()->getUser(); |
||||
|
||||
$builder |
||||
->add('firstname', 'text') |
||||
->add('lastname', 'text') |
||||
->add('official_code', 'text') |
||||
->add('email', 'email') |
||||
->add('username', 'text') |
||||
->add('phone', 'text') |
||||
->add('password', 'password') |
||||
->add('groups') |
||||
->add('timezone', 'timezone') |
||||
->add( |
||||
'locale', |
||||
'locale', |
||||
array('preferred_choices' => array('en', 'fr', 'es')) |
||||
) |
||||
->add( |
||||
'picture_uri', |
||||
'sonata_media_type', |
||||
array( |
||||
'provider' => 'sonata.media.provider.image', |
||||
'context' => 'user_image', |
||||
'required' => false, |
||||
) |
||||
) |
||||
/*->add( |
||||
'extraFields', |
||||
'collection', |
||||
array( |
||||
'required' => false, |
||||
//'type' => 'chamilo_user.form.type.attribute_value_type', |
||||
'allow_add' => true, |
||||
'allow_delete' => true, |
||||
'by_reference' => false, |
||||
) |
||||
)*/ |
||||
->add('save', 'submit', array('label' => 'Update')); |
||||
|
||||
// Update Author id |
||||
$builder->addEventListener( |
||||
FormEvents::POST_SUBMIT, |
||||
function (FormEvent $event) use ($currentUser) { |
||||
/** @var User $user */ |
||||
$user = $event->getData(); |
||||
$extraFields = $user->getExtrafields(); |
||||
foreach ($extraFields as $extraField) { |
||||
$extraField->setAuthor($currentUser); |
||||
} |
||||
} |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param OptionsResolverInterface $resolver |
||||
*/ |
||||
public function setDefaultOptions(OptionsResolverInterface $resolver) |
||||
{ |
||||
$resolver->setDefaults( |
||||
array( |
||||
'data_class' => 'Chamilo\UserBundle\Entity\User', |
||||
) |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return 'user'; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue