src/Platform/ControlPanelBundle/Form/TenantType.php line 93

Open in your IDE?
  1. <?php
  2. namespace Platform\ControlPanelBundle\Form;
  3. use Cms\CoreBundle\Form\Type\SlugType;
  4. use Cms\CoreBundle\Service\TeamworkPm;
  5. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  6. use Cms\TenantBundle\Doctrine\TenantRepository;
  7. use Cms\TenantBundle\Entity\Tenant;
  8. use Platform\SecurityBundle\Validator\Constraints\PasswordStrength;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  11. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Validator\Constraints\Callback;
  15. use Symfony\Component\Validator\Constraints\Email;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  18. /**
  19.  * Class TenantType
  20.  * @package Platform\ControlPanelBundle\Form
  21.  */
  22. final class TenantType extends AbstractType
  23. {
  24.     /**
  25.      * @var TeamworkPm
  26.      */
  27.     private $teamworkPm;
  28.     /**
  29.      * @var EntityManager
  30.      */
  31.     private $em;
  32.     /**
  33.      * TenantType constructor.
  34.      * @param EntityManager $em
  35.      * @param TeamworkPm $teamworkPm
  36.      * @internal param TenantRepository $tenantRepository
  37.      */
  38.     public function __construct(EntityManager $emTeamworkPm $teamworkPm)
  39.     {
  40.         $this->em $em;
  41.         $this->teamworkPm $teamworkPm;
  42.     }
  43.     /**
  44.      * @return TenantRepository
  45.      */
  46.     private function getTenantRepository()
  47.     {
  48.         return $this->em->getRepository(Tenant::class);
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function buildForm(FormBuilderInterface $builder, array $options)
  54.     {
  55.         $builder
  56.             ->add('name'TextType::class, array(
  57.                 'constraints' => array(
  58.                     new NotBlank(),
  59.                     new Callback(array(
  60.                         'callback' => array($this'nameValidate'),
  61.                     )),
  62.                 )
  63.             ))
  64.             ->add('slug'SlugType::class, array(
  65.                 'dependency' => array('name'),
  66.                 'helpText' => 'Attention - changing the wording on Slug field can cause broken links across your site. Proceed with caution.',
  67.                 'constraints' => array(
  68.                     new NotBlank(),
  69.                     new Callback(array(
  70.                         'callback' => array($this'slugValidate'),
  71.                     )),
  72.                 )
  73.             ))
  74.             ->add('teamworkTasklist'ChoiceType::class, array(
  75.                 'placeholder' => '...',
  76.                 'empty_data' => null,
  77.                 'required' => false,
  78.                 'choices' => array_flip(array_map(
  79.                     function (array $tasklist) {
  80.                         return $tasklist['name'];
  81.                     },
  82.                     $this->teamworkPm->getTasklists()
  83.                 )),
  84.                 'label' => 'Teamwork Task List',
  85.                 'helpText' => 'The Teamwork project that powers the Process List tab in the CMS dashboard.',
  86.                 'choice_label' => function ($value$label) {
  87.                     return $label;
  88.                 },
  89.                 'choice_value' => function ($value) {
  90.                     if ($value === null) {
  91.                         return '';
  92.                     }
  93.                     return $value;
  94.                 },
  95.             ))
  96.         ;
  97.     }
  98.     /**
  99.      * @param $value
  100.      * @param ExecutionContextInterface $context
  101.      */
  102.     public function slugValidate($valueExecutionContextInterface $context)
  103.     {
  104.         if (count($this->getTenantRepository()->findBy(array('slug' => $value))) !== 0) {
  105.             $context
  106.                 ->buildViolation('The tenant slug is already taken.')
  107.                 ->addViolation();
  108.         }
  109.     }
  110.     /**
  111.      * @param $value
  112.      * @param ExecutionContextInterface $context
  113.      */
  114.     public function nameValidate($valueExecutionContextInterface $context)
  115.     {
  116.         if (count($this->getTenantRepository()->findBy(array('name' => $value))) !== 0) {
  117.             $context
  118.                 ->buildViolation('The tenant name is already taken.')
  119.                 ->addViolation();
  120.         }
  121.     }
  122. }