<?php
namespace Platform\ControlPanelBundle\Form;
use Cms\CoreBundle\Form\Type\SlugType;
use Cms\CoreBundle\Service\TeamworkPm;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Cms\TenantBundle\Doctrine\TenantRepository;
use Cms\TenantBundle\Entity\Tenant;
use Platform\SecurityBundle\Validator\Constraints\PasswordStrength;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Class TenantType
* @package Platform\ControlPanelBundle\Form
*/
final class TenantType extends AbstractType
{
/**
* @var TeamworkPm
*/
private $teamworkPm;
/**
* @var EntityManager
*/
private $em;
/**
* TenantType constructor.
* @param EntityManager $em
* @param TeamworkPm $teamworkPm
* @internal param TenantRepository $tenantRepository
*/
public function __construct(EntityManager $em, TeamworkPm $teamworkPm)
{
$this->em = $em;
$this->teamworkPm = $teamworkPm;
}
/**
* @return TenantRepository
*/
private function getTenantRepository()
{
return $this->em->getRepository(Tenant::class);
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'constraints' => array(
new NotBlank(),
new Callback(array(
'callback' => array($this, 'nameValidate'),
)),
)
))
->add('slug', SlugType::class, array(
'dependency' => array('name'),
'helpText' => 'Attention - changing the wording on Slug field can cause broken links across your site. Proceed with caution.',
'constraints' => array(
new NotBlank(),
new Callback(array(
'callback' => array($this, 'slugValidate'),
)),
)
))
->add('teamworkTasklist', ChoiceType::class, array(
'placeholder' => '...',
'empty_data' => null,
'required' => false,
'choices' => array_flip(array_map(
function (array $tasklist) {
return $tasklist['name'];
},
$this->teamworkPm->getTasklists()
)),
'label' => 'Teamwork Task List',
'helpText' => 'The Teamwork project that powers the Process List tab in the CMS dashboard.',
'choice_label' => function ($value, $label) {
return $label;
},
'choice_value' => function ($value) {
if ($value === null) {
return '';
}
return $value;
},
))
;
}
/**
* @param $value
* @param ExecutionContextInterface $context
*/
public function slugValidate($value, ExecutionContextInterface $context)
{
if (count($this->getTenantRepository()->findBy(array('slug' => $value))) !== 0) {
$context
->buildViolation('The tenant slug is already taken.')
->addViolation();
}
}
/**
* @param $value
* @param ExecutionContextInterface $context
*/
public function nameValidate($value, ExecutionContextInterface $context)
{
if (count($this->getTenantRepository()->findBy(array('name' => $value))) !== 0) {
$context
->buildViolation('The tenant name is already taken.')
->addViolation();
}
}
}