-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAP-13377: Change email compose form to choose origin instead from em…
…ail (#8282)
- 6.1.0-rc.3
- 6.1.0-rc.2
- 6.1.0-rc
- 6.0.8
- 6.0.7
- 6.0.6
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 6.0.0-rc
- 6.0.0-beta.1
- 5.1.8
- 5.1.7
- 5.1.6
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.1.0-rc.2
- 5.1.0-rc.1
- 5.1.0-beta.2
- 5.1.0-beta.1
- 5.1.0-alpha.2
- 5.1.0-alpha.1
- 5.0.12
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 5.0.0-rc
- 5.0.0-beta.2
- 5.0.0-beta.1
- 5.0.0-alpha.2
- 5.0.0-alpha.1
- 4.2.10
- 4.2.9
- 4.2.8
- 4.2.7
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-rc
- 4.2.0-beta
- 4.2.0-alpha.3
- 4.2.0-alpha.2
- 4.2.0-alpha.1
- 4.2.0-alpha
- 4.1.13
- 4.1.12
- 4.1.11
- 4.1.10
- 4.1.9
- 4.1.8
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.1-rc2
- 4.1.1-rc
- 4.1.0
- 4.1.0-rc4
- 4.1.0-rc3
- 4.1.0-rc2
- 4.1.0-rc
- 4.1.0-beta
- 4.0.0
- 4.0.0-rc
- 4.0.0-beta
- 3.1.20
- 3.1.19
- 3.1.18
- 3.1.17
- 3.1.16
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.1.0-rc
- 3.1.0-beta
- 3.0.0
- 3.0.0-rc
- 3.0.0-beta
- 2.6.39
- 2.6.38
- 2.6.37
- 2.6.36
- 2.6.35
- 2.6.34
- 2.6.33
- 2.6.32
- 2.6.31
- 2.6.30
- 2.6.29
- 2.6.28
- 2.6.27
- 2.6.26
- 2.6.25
- 2.6.24
- 2.6.23
- 2.6.22
- 2.6.21
- 2.6.20
- 2.6.19
- 2.6.18
- 2.6.17
- 2.6.16
- 2.6.15
- 2.6.14
- 2.6.13
- 2.6.12
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.12
- 2.3.11
- 2.3.10
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
Showing
13 changed files
with
504 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/Oro/Bundle/EmailBundle/Form/DataTransformer/OriginTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\EmailBundle\Form\DataTransformer; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
use Oro\Bundle\EmailBundle\Entity\EmailOrigin; | ||
use Oro\Bundle\EmailBundle\Tools\EmailOriginHelper; | ||
use Oro\Bundle\SecurityBundle\SecurityFacade; | ||
|
||
/** | ||
* Transforms between entity and id | ||
*/ | ||
class OriginTransformer implements DataTransformerInterface | ||
{ | ||
/** @var EntityManager */ | ||
protected $em; | ||
|
||
/** @var string */ | ||
protected $className; | ||
|
||
/** @var SecurityFacade */ | ||
protected $securityFacade; | ||
|
||
/** @var EmailOriginHelper */ | ||
protected $emailOriginHelper; | ||
|
||
/** | ||
* OriginTransformer constructor. | ||
* | ||
* @param EntityManager $em | ||
* @param SecurityFacade $securityFacade | ||
* @param EmailOriginHelper $emailOriginHelper | ||
* | ||
* @throws UnexpectedTypeException When $queryBuilderCallback is set and not callable | ||
*/ | ||
public function __construct( | ||
EntityManager $em, | ||
SecurityFacade $securityFacade, | ||
EmailOriginHelper $emailOriginHelper | ||
) { | ||
$this->em = $em; | ||
$this->securityFacade = $securityFacade; | ||
$this->emailOriginHelper = $emailOriginHelper; | ||
$this->className = EmailOrigin::class; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!is_object($value)) { | ||
throw new UnexpectedTypeException($value, 'object'); | ||
} | ||
|
||
return $value->getId(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (!$value) { | ||
return null; | ||
} | ||
$values = explode('|', $value); | ||
if (!array_key_exists(0, $values) || !$values[0]) { | ||
$origin = $this->findByOwner($this->securityFacade->getLoggedUser()); | ||
|
||
if (!$origin) { | ||
$origin = $this->createInternalOrigin($values[1]); | ||
} | ||
|
||
return $origin; | ||
} | ||
|
||
return $this->loadEntityById($value); | ||
} | ||
|
||
/** | ||
* Load entity by id | ||
* | ||
* @param mixed $id | ||
* @return object | ||
* @throws UnexpectedTypeException if query builder callback returns invalid type | ||
* @throws TransformationFailedException if value not matched given $id | ||
*/ | ||
protected function loadEntityById($id) | ||
{ | ||
$repository = $this->em->getRepository($this->className); | ||
$result = $repository->find($id); | ||
if (!$result) { | ||
throw new TransformationFailedException(sprintf('The value "%s" does not exist or not unique.', $id)); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @param $owner | ||
* | ||
* @return null|object | ||
*/ | ||
protected function findByOwner($owner) | ||
{ | ||
$repository = $this->em->getRepository($this->className); | ||
|
||
return $repository->findOneBy(['owner'=> $owner]); | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* | ||
* @return EmailOrigin | ||
*/ | ||
protected function createInternalOrigin($email) | ||
{ | ||
return $this->emailOriginHelper->getEmailOrigin($email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
src/Oro/Bundle/EmailBundle/Form/Type/EmailOriginFromType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\EmailBundle\Form\Type; | ||
|
||
use Doctrine\Common\Util\ClassUtils; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
|
||
use Oro\Bundle\EmailBundle\Form\DataTransformer\OriginTransformer; | ||
use Oro\Bundle\EmailBundle\Builder\Helper\EmailModelBuilderHelper; | ||
use Oro\Bundle\EmailBundle\Tools\EmailOriginHelper; | ||
use Oro\Bundle\EntityBundle\ORM\Registry; | ||
use Oro\Bundle\EmailBundle\Provider\RelatedEmailsProvider; | ||
use Oro\Bundle\ImapBundle\Entity\UserEmailOrigin; | ||
use Oro\Bundle\SecurityBundle\SecurityFacade; | ||
use Oro\Bundle\UserBundle\Entity\User; | ||
use Oro\Bundle\EmailBundle\Entity\Manager\MailboxManager; | ||
|
||
class EmailOriginFromType extends AbstractType | ||
{ | ||
const NAME = 'oro_email_email_origin_from'; | ||
|
||
/** @var SecurityFacade */ | ||
protected $securityFacade; | ||
|
||
/** @var EmailModelBuilderHelper */ | ||
protected $helper; | ||
|
||
/** @var RelatedEmailsProvider */ | ||
protected $relatedEmailsProvider; | ||
|
||
/** @var MailboxManager */ | ||
protected $mailboxManager; | ||
|
||
/** @var Registry */ | ||
protected $registry; | ||
|
||
/** @var EmailOriginHelper */ | ||
protected $emailOriginHelper; | ||
|
||
/** | ||
* @param SecurityFacade $securityFacade | ||
* @param RelatedEmailsProvider $relatedEmailsProvider | ||
* @param EmailModelBuilderHelper $helper | ||
* @param MailboxManager $mailboxManager | ||
* @param Registry $registry | ||
* @param EmailOriginHelper $emailOriginHelper | ||
*/ | ||
public function __construct( | ||
SecurityFacade $securityFacade, | ||
RelatedEmailsProvider $relatedEmailsProvider, | ||
EmailModelBuilderHelper $helper, | ||
MailboxManager $mailboxManager, | ||
Registry $registry, | ||
EmailOriginHelper $emailOriginHelper | ||
) { | ||
$this->securityFacade = $securityFacade; | ||
$this->relatedEmailsProvider = $relatedEmailsProvider; | ||
$this->helper = $helper; | ||
$this->mailboxManager = $mailboxManager; | ||
$this->registry = $registry; | ||
$this->emailOriginHelper = $emailOriginHelper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$builder->addModelTransformer( | ||
new OriginTransformer( | ||
$this->registry->getManager(), | ||
$this->securityFacade, | ||
$this->emailOriginHelper | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setDefaultOptions(OptionsResolverInterface $resolver) | ||
{ | ||
$choices = $this->createChoices(); | ||
$resolver->setDefaults([ | ||
'choices' => $choices, | ||
'read_only' => count($choices) === 1, | ||
]); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function createChoices() | ||
{ | ||
$user = $this->securityFacade->getLoggedUser(); | ||
if (!$user instanceof User) { | ||
return []; | ||
} | ||
$origins = []; | ||
$origins = $this->fillUserOrigins($user, $origins); | ||
if (count($origins) === 0) { | ||
$origins = $this->fillUserEmails($user, $origins); | ||
} | ||
$origins = $this->fillMailboxOrigins($user, $origins); | ||
|
||
return $origins; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return 'genemu_jqueryselect2_choice'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->getBlockPrefix(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBlockPrefix() | ||
{ | ||
return static::NAME; | ||
} | ||
|
||
/** | ||
* @param User $user | ||
* @param array $origins | ||
* | ||
* @return array | ||
*/ | ||
protected function fillUserOrigins(User $user, $origins) | ||
{ | ||
$userOrigins = $user->getEmailOrigins(); | ||
foreach ($userOrigins as $origin) { | ||
if ($origin instanceof UserEmailOrigin) { | ||
if ($origin->isActive()) { | ||
$owner = $origin->getOwner(); | ||
$email = $origin->getOwner()->getEmail(); | ||
$this->helper->preciseFullEmailAddress($email, ClassUtils::getClass($owner), $owner->getId()); | ||
$origins[$origin->getId() . '|' . $origin->getOwner()->getEmail()] = $email; | ||
} | ||
} | ||
} | ||
return $origins; | ||
} | ||
|
||
/** | ||
* @param User $user | ||
* @param array $origins | ||
* @return array | ||
*/ | ||
protected function fillUserEmails(User $user, $origins) | ||
{ | ||
$email = $user->getEmail(); | ||
$origins = $this->processFillUserEmail($email, $origins, $user); | ||
|
||
$userEmails = $user->getEmails(); | ||
foreach ($userEmails as $email) { | ||
$email = $email->getEmail(); | ||
$origins = $this->processFillUserEmail($email, $origins); | ||
} | ||
|
||
return $origins; | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @param array $origins | ||
* @param $owner | ||
* | ||
* @return mixed | ||
*/ | ||
protected function processFillUserEmail($email, $origins, $owner = null) | ||
{ | ||
$key = '0|' . $email; | ||
if (!array_key_exists($key, $origins)) { | ||
if ($owner) { | ||
$this->helper->preciseFullEmailAddress($email, ClassUtils::getClass($owner), $owner->getId()); | ||
} else { | ||
$this->helper->preciseFullEmailAddress($email); | ||
} | ||
$origins[$key] = $email; | ||
} | ||
|
||
return $origins; | ||
} | ||
|
||
/** | ||
* @param User $user | ||
* @param array $origins | ||
* | ||
* @return mixed | ||
*/ | ||
protected function fillMailboxOrigins(User $user, $origins) | ||
{ | ||
$mailboxes = $this->mailboxManager->findAvailableMailboxes($user, $this->securityFacade->getOrganization()); | ||
foreach ($mailboxes as $mailbox) { | ||
$origin = $mailbox->getOrigin(); | ||
if ($origin->isActive()) { | ||
$email = $mailbox->getEmail(); | ||
$this->helper->preciseFullEmailAddress($email); | ||
$email .= ' (Mailbox)'; | ||
$origins[$origin->getId() . '|' . $mailbox->getEmail()] = $email; | ||
} | ||
} | ||
|
||
return $origins; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters