Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/UserBundle/Form/Type/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/UserBundle/Form/Type/UserRegisterType.php

<?php

namespace App\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Email;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type;
use App\UserBundle\Validator\Constraints\UserExists;
use App\UserBundle\Entity\UserList;

class UserRegisterType extends AbstractType {

    private $serviceContainer = null;
    private $myDetails = false;

    public function __construct(ContainerInterface $serviceContainer, $myDetails = false) {
        $this->serviceContainer = $serviceContainer;
        $this->myDetails = $myDetails;
    }

    public function getServiceContainer() {
        return $this->serviceContainer;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $userListManager = $this->getServiceContainer()->get('user_list.manager');
        $systemSettingManager = $this->serviceContainer->get('system_setting.manager');

        $canEdit = true;

        if ($this->myDetails) {

            if (isset($options['user_list']) && is_object($options['user_list'])) {
                $userList = $options['user_list'];
            } else {
                $userList = $userListManager->getLoggedInUser();
            }

            $canEdit = $userListManager->canEdit($userList, $userListManager->getLoggedInUser());
        } else {
            $userList = new UserList();
        }

        $subscribedAttr = array('class' => 'form-control');

        if ($userList->isEMailAddressSubscribed() || strlen($userList->getId()) <= 0) {
            $subscribedAttr = array('checked' => '');
        }

        $builder
                ->add(
                        'first_name_string', TextType::class, array(
                    'constraints' => array(
                        new Length(array('min' => 0, 'max' => 255)),
                    ),
                    'label' => 'Name: *',
                    'attr' => array('placeholder' => 'Name', 'class' => 'form-control'),
                    'data' => $userList->getFirstNameString(),
                    'required' => true,
                        )
                )
                ->add(
                        'last_name_string', TextType::class, array(
                    'constraints' => array(
                        new Length(array('min' => 0, 'max' => 255)),
                    ),
                    'label' => 'Surname: *',
                    'attr' => array('placeholder' => 'Surname', 'class' => 'form-control'),
                    'data' => $userList->getLastNameString(),
                    'required' => true,
                        )
                )
                ->add(
                        'username', TextType::class, array(
                    'constraints' => array(
                        new NotBlank(),
                        new Length(array('min' => 3, 'max' => 255)),
                        new Regex(
                                array(
                            'pattern' => '/[^-a-z0-9_.-@]/i',
                            'message' => 'Your username may only contain a - z, 0 - 9, "-" and "_"',
                            'match' => false,
                                )
                        ),
                        new UserExists(array('userList' => $userList)),
                    ),
                    'label' => 'Username: *',
                    'attr' => array('placeholder' => 'Username', 'class' => 'form-control'),
                    'data' => $userList->getUsername(),
                        )
                )
                ->add(
                        'e_mail_address_string', EmailType::class, array(
                    'constraints' => array(
                        new Length(array('min' => 0, 'max' => 255)),
                        new Email(),
                    ),
                    'label' => 'E-Mail: *',
                    'attr' => array('placeholder' => 'E-Mail', 'class' => 'form-control'),
                    'required' => true,
                        )
                )
                ->add(
                        'e_mail_address_subscribed',
                        CheckboxType::class,
                        array(
                            'required' => false,
                            'attr' => $subscribedAttr,
                            'label' => 'Receive Notifications',
                        )
                )
        ;

        if (!$canEdit) {
            $builder->get('first_name_string')->setAttribute('disabled', true);
            $builder->get('last_name_string')->setAttribute('disabled', true);
            $builder->get('username')->setAttribute('disabled', true);
            $builder->get('e_mail_address_string')->setAttribute('disabled', true);
            $builder->get('e_mail_address_subscribed')->setAttribute('disabled', true);
        }

        if (!$this->myDetails) {
            //User register form
            $builder->add(
                            'password', RepeatedType::class, array(
                        'first_name' => 'password',
                        'second_name' => 'confirm',
                        'type' => PasswordType::class,
                        'first_options' => array(
                            'attr' => array('placeholder' => 'Password', 'class' => 'form-control'),
                            'label' => 'Password: *',
                            'constraints' => array(
                                new NotBlank(),
                                new Length(array('min' => 6, 'max' => 255)),
                                new Regex(
                                        array(
                                    'pattern' => '#[0-9]+#',
                                    'message' => 'Your password must contain a number',
                                    'match' => true
                                        )
                                ),
                                new Regex(
                                        array(
                                    'pattern' => '#[A-Z]+#',
                                    'message' => 'Your password must contain a uppercase character',
                                    'match' => true
                                        )
                                ),
                                new Regex(
                                        array(
                                    'pattern' => '#[a-z]+#',
                                    'message' => 'Your password must contain a lowercase character',
                                    'match' => true
                                        )
                                ),
                            ),
                        ),
                        'second_options' => array(
                            'attr' => array(
                                'placeholder' => 'Confirm password', 'class' => 'form-control'
                            ),
                            'label' => 'Confirm Password: *',
                            'constraints' => array(
                                new NotBlank(),
                                new Length(array('min' => 6, 'max' => 255)),
                                new Regex(
                                        array(
                                    'pattern' => '#[0-9]+#',
                                    'message' => 'Your password must contain a number',
                                    'match' => true
                                        )
                                ),
                                new Regex(
                                        array(
                                    'pattern' => '#[A-Z]+#',
                                    'message' => 'Your password must contain a uppercase character',
                                    'match' => true
                                        )
                                ),
                                new Regex(
                                        array(
                                    'pattern' => '#[a-z]+#',
                                    'message' => 'Your password must contain a lowercase character',
                                    'match' => true
                                        )
                                ),
                            ),
                        ),
                        'invalid_message' => 'Password and Confirm Password values do not match'
                            )
                    )
                    ->add(
                            'accept_conditions',
                            CheckboxType::class,
                            array('required' => true, 'mapped' => false, 'label' => ' ')
            );

            $builder->add(
                    'captcha', EWZRecaptchaV3Type:: class, array(
                'mapped' => false,
                    )
            );

            if (!$canEdit) {
                $builder->get('password')->setAttribute('disabled', true);
                $builder->get('accept_conditions')->setAttribute('disabled', true);
            }
        } else {
            $builder->add(
                    'password', RepeatedType::class, array(
                'first_name' => 'password',
                'second_name' => 'confirm',
                'type' => PasswordType::class,
                'first_options' => array(
                    'attr' => array('placeholder' => 'Password', 'class' => 'form-control'),
                    'label' => 'Password: *',
                    'constraints' => array(
                        new Length(array('min' => 0, 'max' => 255)),
                        new Regex(
                                array(
                            'pattern' => '#[0-9]+#',
                            'message' => 'Your password must contain a number',
                            'match' => true
                                )
                        ),
                        new Regex(
                                array(
                            'pattern' => '#[A-Z]+#',
                            'message' => 'Your password must contain a uppercase character',
                            'match' => true
                                )
                        ),
                        new Regex(
                                array(
                            'pattern' => '#[a-z]+#',
                            'message' => 'Your password must contain a lowercase character',
                            'match' => true
                                )
                        ),
                    ),
                    'required' => false,
                ),
                'second_options' => array(
                    'attr' => array(
                        'placeholder' => 'Confirm password', 'class' => 'form-control'
                    ),
                    'label' => 'Confirm Password: *',
                    'constraints' => array(
                        new Length(array('min' => 0, 'max' => 255)),
                        new Regex(
                                array(
                            'pattern' => '#[0-9]+#',
                            'message' => 'Your password must contain a number',
                            'match' => true
                                )
                        ),
                        new Regex(
                                array(
                            'pattern' => '#[A-Z]+#',
                            'message' => 'Your password must contain a uppercase character',
                            'match' => true
                                )
                        ),
                        new Regex(
                                array(
                            'pattern' => '#[a-z]+#',
                            'message' => 'Your password must contain a lowercase character',
                            'match' => true
                                )
                        ),
                    ),
                    'required' => false,
                ),
                'invalid_message' => 'Password and Confirm Password values do not match',
                'required' => false
                    )
            );

            if (!$canEdit) {
                $builder->get('password')->setAttribute('disabled', true);
            }
        }

        $companyNameOnRegister = 1; //$systemSettingManager->getSetting('company_name_register', 1);
        $session = $this->serviceContainer->get('sessions.manager')->getSession();
        $templateId = $session->getSites()->getTemplateId();

        if ($companyNameOnRegister > 0) {

            $attrCR = array();

            if (true || $systemSettingManager->getSetting('company_name_register_compulsory', 1) > 0) {
                $attrCR['checked'] = 'checked';
            }

            $builder->add(
                    'company_name', TextType::class, array(
                'constraints' => array(
                    new Length(array('min' => 3, 'max' => 255)),
                ),
                'label' => 'Company Name: *',
                'attr' => array('placeholder' => 'Company Name', 'class' => 'form-control'),
                'required' => false,
                    )
            )
            ;
        }
    }

    public function getParent() {
        return \Symfony\Component\Form\Extension\Core\Type\FormType::class;
    }

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

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'user_list' => null,
            'user_list_edit' => false,
            'user_type_id' => null,
            'is_new' => false,
            'owner' => null,
        ));
    }
}