1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\CalendarBundle\Form\DataTransformer; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
6
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
7
|
|
|
|
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
10
|
|
|
|
11
|
|
|
use Oro\Bundle\SecurityBundle\SecurityFacade; |
12
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
13
|
|
|
use Oro\Bundle\CalendarBundle\Entity\CalendarEvent; |
14
|
|
|
use Oro\Bundle\CalendarBundle\Entity\Repository\CalendarRepository; |
15
|
|
|
|
16
|
|
|
class EventsToUsersTransformer implements DataTransformerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ManagerRegistry |
20
|
|
|
*/ |
21
|
|
|
protected $registry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SecurityFacade |
25
|
|
|
*/ |
26
|
|
|
protected $securityFacade; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ManagerRegistry $registry |
30
|
|
|
* @param SecurityFacade $securityFacade |
31
|
|
|
*/ |
32
|
|
|
public function __construct(ManagerRegistry $registry, SecurityFacade $securityFacade) |
33
|
|
|
{ |
34
|
|
|
$this->registry = $registry; |
35
|
|
|
$this->securityFacade = $securityFacade; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function transform($value) |
42
|
|
|
{ |
43
|
|
|
if (null === $value) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$users = new ArrayCollection(); |
48
|
|
|
|
49
|
|
|
/** @var CalendarEvent $event */ |
50
|
|
|
foreach ($value as $event) { |
51
|
|
|
$users->add($event->getCalendar()->getOwner()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $users; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function reverseTransform($value) |
61
|
|
|
{ |
62
|
|
|
if (!$value) { |
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var CalendarRepository $calendarRepository */ |
67
|
|
|
$calendarRepository = $this->registry->getRepository('OroCalendarBundle:Calendar'); |
68
|
|
|
$organizationId = $this->securityFacade->getOrganizationId(); |
69
|
|
|
|
70
|
|
|
if (!$organizationId) { |
|
|
|
|
71
|
|
|
throw new TransformationFailedException('Can\'t get current organization'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$events = new ArrayCollection(); |
75
|
|
|
|
76
|
|
|
/** @var User $user */ |
77
|
|
|
$userIds = []; |
78
|
|
|
foreach ($value as $user) { |
79
|
|
|
$userIds[] = $user->getId(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$calendars = $calendarRepository->findDefaultCalendars($userIds, $organizationId); |
83
|
|
|
foreach ($calendars as $calendar) { |
84
|
|
|
$event = new CalendarEvent(); |
85
|
|
|
$event->setCalendar($calendar); |
86
|
|
|
$events->add($event); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $events; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: