|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\DemoDataBundle\Migrations\Data\Demo\ORM; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
6
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
|
7
|
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
|
8
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
9
|
|
|
use Doctrine\ORM\EntityRepository; |
|
10
|
|
|
use Doctrine\ORM\EntityManager; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\SecurityContext; |
|
15
|
|
|
|
|
16
|
|
|
use Oro\Bundle\CalendarBundle\Entity\Calendar; |
|
17
|
|
|
use Oro\Bundle\CalendarBundle\Entity\CalendarEvent; |
|
18
|
|
|
use Oro\Bundle\CalendarBundle\Entity\CalendarProperty; |
|
19
|
|
|
use Oro\Bundle\CalendarBundle\Entity\Repository\CalendarRepository; |
|
20
|
|
|
use Oro\Bundle\OrganizationBundle\Entity\Organization; |
|
21
|
|
|
use Oro\Bundle\SecurityBundle\Authentication\Token\UsernamePasswordOrganizationToken; |
|
22
|
|
|
use Oro\Bundle\UserBundle\Entity\User; |
|
23
|
|
|
|
|
24
|
|
|
class LoadUsersCalendarData extends AbstractFixture implements ContainerAwareInterface, DependentFixtureInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ContainerInterface */ |
|
27
|
|
|
private $container; |
|
28
|
|
|
|
|
29
|
|
|
/** @var User[] */ |
|
30
|
|
|
private $users; |
|
31
|
|
|
|
|
32
|
|
|
/** @var EntityRepository */ |
|
33
|
|
|
protected $user; |
|
34
|
|
|
|
|
35
|
|
|
/** @var CalendarRepository */ |
|
36
|
|
|
protected $calendar; |
|
37
|
|
|
|
|
38
|
|
|
/** @var Organization */ |
|
39
|
|
|
protected $organization; |
|
40
|
|
|
|
|
41
|
|
|
/** @var EntityManager */ |
|
42
|
|
|
protected $em; |
|
43
|
|
|
|
|
44
|
|
|
/** @var SecurityContext */ |
|
45
|
|
|
protected $securityContext; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getDependencies() |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
'OroCRM\Bundle\DemoDataBundle\Migrations\Data\Demo\ORM\LoadUsersData', |
|
54
|
|
|
'OroCRM\Bundle\DemoDataBundle\Migrations\Data\Demo\ORM\LoadUserData' |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setContainer(ContainerInterface $container = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->container = $container; |
|
64
|
|
|
|
|
65
|
|
|
$this->em = $container->get('doctrine')->getManager(); |
|
|
|
|
|
|
66
|
|
|
$this->securityContext = $container->get('security.context'); |
|
67
|
|
|
|
|
68
|
|
|
$this->user = $this->em->getRepository('OroUserBundle:User'); |
|
69
|
|
|
$this->calendar = $this->em->getRepository('OroCalendarBundle:Calendar'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function load(ObjectManager $manager) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->users = $this->user->findAll(); |
|
78
|
|
|
$this->organization = $this->getReference('default_organization'); |
|
79
|
|
|
|
|
80
|
|
|
$this->loadCalendars(); |
|
81
|
|
|
$this->connectCalendars(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function loadCalendars() |
|
85
|
|
|
{ |
|
86
|
|
|
$days = $this->getDatePeriod(); |
|
87
|
|
|
$events = []; |
|
88
|
|
|
foreach ($days as $day) { |
|
89
|
|
|
/** @var \DateTime $day */ |
|
90
|
|
|
if (!$this->isWeekend($day)) { |
|
91
|
|
|
//work day |
|
92
|
|
|
$event = new CalendarEvent(); |
|
93
|
|
|
$event->setTitle('Work Reminder'); |
|
94
|
|
|
$day->setTime(8, 0, 0); |
|
95
|
|
|
$event->setStart(clone $day); |
|
96
|
|
|
$day->setTime(18, 0, 0); |
|
97
|
|
|
$event->setEnd(clone $day); |
|
98
|
|
|
$event->setAllDay(true); |
|
99
|
|
|
|
|
100
|
|
|
$events['workday'][] = $event; |
|
101
|
|
|
|
|
102
|
|
|
//call |
|
103
|
|
|
$event = new CalendarEvent(); |
|
104
|
|
|
$event->setTitle('Client Call'); |
|
105
|
|
|
$day->setTime(11, 0, 0); |
|
106
|
|
|
$event->setStart(clone $day); |
|
107
|
|
|
$day->setTime(12, 0, 0); |
|
108
|
|
|
$event->setEnd(clone $day); |
|
109
|
|
|
$event->setAllDay(false); |
|
110
|
|
|
|
|
111
|
|
|
$events['call'][] = $event; |
|
112
|
|
|
|
|
113
|
|
|
//meeting |
|
114
|
|
|
$event = new CalendarEvent(); |
|
115
|
|
|
$event->setTitle('Meeting'); |
|
116
|
|
|
$day->setTime(16, 0, 0); |
|
117
|
|
|
$event->setStart(clone $day); |
|
118
|
|
|
$day->setTime(18, 0, 0); |
|
119
|
|
|
$event->setEnd(clone $day); |
|
120
|
|
|
$event->setAllDay(false); |
|
121
|
|
|
|
|
122
|
|
|
$events['meeting'][] = $event; |
|
123
|
|
|
|
|
124
|
|
|
//lunch |
|
125
|
|
|
$event = new CalendarEvent(); |
|
126
|
|
|
$event->setTitle('Lunch'); |
|
127
|
|
|
$day->setTime(12, 0, 0); |
|
128
|
|
|
$event->setStart(clone $day); |
|
129
|
|
|
$day->setTime(12, 30, 0); |
|
130
|
|
|
$event->setEnd(clone $day); |
|
131
|
|
|
$event->setAllDay(false); |
|
132
|
|
|
|
|
133
|
|
|
$events['lunch'][] = $event; |
|
134
|
|
|
|
|
135
|
|
|
//business trip |
|
136
|
|
|
$event = new CalendarEvent(); |
|
137
|
|
|
$event->setTitle('Business trip'); |
|
138
|
|
|
$day->setTime(0, 0, 0); |
|
139
|
|
|
$event->setStart(clone $day); |
|
140
|
|
|
$day->setTime(0, 0, 0); |
|
141
|
|
|
$day->add(\DateInterval::createFromDateString('+3 days')); |
|
142
|
|
|
$event->setEnd(clone $day); |
|
143
|
|
|
$event->setAllDay(true); |
|
144
|
|
|
|
|
145
|
|
|
$events['b_trip'][] = $event; |
|
146
|
|
|
} else { |
|
147
|
|
|
$event = new CalendarEvent(); |
|
148
|
|
|
$event->setTitle('Weekend'); |
|
149
|
|
|
$day->setTime(8, 0, 0); |
|
150
|
|
|
$event->setStart(clone $day); |
|
151
|
|
|
$day->setTime(18, 0, 0); |
|
152
|
|
|
$event->setEnd(clone $day); |
|
153
|
|
|
$event->setAllDay(true); |
|
154
|
|
|
|
|
155
|
|
|
$events['weekend'][] = $event; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
foreach ($this->users as $user) { |
|
160
|
|
|
//get default calendar, each user has default calendar after creation |
|
161
|
|
|
$calendar = $this->calendar->findDefaultCalendar($user->getId(), $this->organization->getId()); |
|
162
|
|
|
$this->setSecurityContext($calendar->getOwner()); |
|
163
|
|
|
foreach ($events as $typeEvents) { |
|
164
|
|
|
if (mt_rand(0, 1)) { |
|
165
|
|
|
foreach ($typeEvents as $typeEvent) { |
|
166
|
|
|
$calendar->addEvent(clone $typeEvent); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->em->persist($calendar); |
|
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$this->em->flush(); |
|
175
|
|
|
$this->em->clear('Oro\Bundle\CalendarBundle\Entity\CalendarEvent'); |
|
176
|
|
|
$this->em->clear('Oro\Bundle\CalendarBundle\Entity\Calendar'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
protected function connectCalendars() |
|
180
|
|
|
{ |
|
181
|
|
|
// first user is admin, often |
|
182
|
|
|
/** @var \Oro\Bundle\UserBundle\Entity\User $admin */ |
|
183
|
|
|
$admin = $this->user->find(1); |
|
184
|
|
|
/** @var Calendar $calendarAdmin */ |
|
185
|
|
|
$calendarAdmin = $this->calendar->findDefaultCalendar($admin->getId(), $admin->getOrganization()->getId()); |
|
186
|
|
|
|
|
187
|
|
|
/** @var \Oro\Bundle\UserBundle\Entity\User $sale */ |
|
188
|
|
|
$sale = $this->user->findOneBy(['username' => 'sale']); |
|
189
|
|
|
/** @var Calendar $calendarSale */ |
|
190
|
|
|
$calendarSale = $this->calendar->findDefaultCalendar($sale->getId(), $sale->getOrganization()->getId()); |
|
191
|
|
|
|
|
192
|
|
|
/** @var \Oro\Bundle\UserBundle\Entity\User $market */ |
|
193
|
|
|
$market = $this->user->findOneBy(['username' => 'marketing']); |
|
194
|
|
|
/** @var Calendar $calendarMarket */ |
|
195
|
|
|
$calendarMarket = $this->calendar->findDefaultCalendar($market->getId(), $market->getOrganization()->getId()); |
|
196
|
|
|
|
|
197
|
|
|
/** @var User[] $users */ |
|
198
|
|
|
$users = $this->getRandomUsers(); |
|
199
|
|
|
|
|
200
|
|
|
foreach ($users as $user) { |
|
201
|
|
|
if (in_array($user->getId(), [$admin->getId(), $sale->getId(), $market->getId()])) { |
|
202
|
|
|
//to prevent self assignment |
|
203
|
|
|
continue; |
|
204
|
|
|
} |
|
205
|
|
|
/** @var Calendar $calendar */ |
|
206
|
|
|
$calendar = $this->calendar->findDefaultCalendar($user->getId(), $user->getOrganization()->getId()); |
|
207
|
|
|
|
|
208
|
|
View Code Duplication |
if (mt_rand(0, 1)) { |
|
|
|
|
|
|
209
|
|
|
$calendarProperty = new CalendarProperty(); |
|
210
|
|
|
$calendarProperty |
|
211
|
|
|
->setTargetCalendar($calendarAdmin) |
|
212
|
|
|
->setCalendarAlias('user') |
|
213
|
|
|
->setCalendar($calendar->getId()); |
|
214
|
|
|
|
|
215
|
|
|
$this->em->persist($calendarProperty); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
View Code Duplication |
if (mt_rand(0, 1)) { |
|
|
|
|
|
|
219
|
|
|
$calendarProperty = new CalendarProperty(); |
|
220
|
|
|
$calendarProperty |
|
221
|
|
|
->setTargetCalendar($calendarSale) |
|
222
|
|
|
->setCalendarAlias('user') |
|
223
|
|
|
->setCalendar($calendar->getId()); |
|
224
|
|
|
|
|
225
|
|
|
$this->em->persist($calendarProperty); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
View Code Duplication |
if (mt_rand(0, 1)) { |
|
|
|
|
|
|
229
|
|
|
$calendarProperty = new CalendarProperty(); |
|
230
|
|
|
$calendarProperty |
|
231
|
|
|
->setTargetCalendar($calendarMarket) |
|
232
|
|
|
->setCalendarAlias('user') |
|
233
|
|
|
->setCalendar($calendar->getId()); |
|
234
|
|
|
|
|
235
|
|
|
$this->em->persist($calendarProperty); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
$this->em->persist($calendar); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$this->em->flush(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @param int $limit |
|
246
|
|
|
* |
|
247
|
|
|
* @return User[] |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getRandomUsers($limit = 5) |
|
250
|
|
|
{ |
|
251
|
|
|
$userIds = $this->user->createQueryBuilder('u') |
|
252
|
|
|
->select('u.id') |
|
253
|
|
|
->getQuery() |
|
254
|
|
|
->getScalarResult(); |
|
255
|
|
|
|
|
256
|
|
|
if (count($userIds) > $limit) { |
|
257
|
|
|
$rawList = []; |
|
258
|
|
|
foreach ($userIds as $key => $value) { |
|
259
|
|
|
// due array_rand() will pick only keywords |
|
260
|
|
|
$rawList[$value['id']] = null; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$keyList = array_rand($rawList, $limit); |
|
264
|
|
|
|
|
265
|
|
|
$criteria = new Criteria(); |
|
266
|
|
|
$criteria->where(Criteria::expr()->in('id', $keyList)); |
|
267
|
|
|
|
|
268
|
|
|
$result = $this->user->createQueryBuilder('u') |
|
269
|
|
|
->addCriteria($criteria) |
|
270
|
|
|
->getQuery() |
|
271
|
|
|
->getResult(); |
|
272
|
|
|
} else { |
|
273
|
|
|
$result = $this->users; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return $result; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @return \DatePeriod |
|
281
|
|
|
*/ |
|
282
|
|
|
protected function getDatePeriod() |
|
283
|
|
|
{ |
|
284
|
|
|
$month = new \DatePeriod( |
|
285
|
|
|
new \DateTime('now'), |
|
286
|
|
|
\DateInterval::createFromDateString('+1 day'), |
|
287
|
|
|
new \DateTime('now +1 month'), |
|
288
|
|
|
\DatePeriod::EXCLUDE_START_DATE |
|
289
|
|
|
); |
|
290
|
|
|
|
|
291
|
|
|
return $month; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @param \DateTime $date |
|
296
|
|
|
* @return bool |
|
297
|
|
|
*/ |
|
298
|
|
|
protected function isWeekend($date) |
|
299
|
|
|
{ |
|
300
|
|
|
$day = date('w', $date->getTimestamp()); |
|
301
|
|
|
if ($day == 0 || $day == 6) { |
|
302
|
|
|
return true; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
return false; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @param User $user |
|
310
|
|
|
*/ |
|
311
|
|
|
protected function setSecurityContext(User $user) |
|
312
|
|
|
{ |
|
313
|
|
|
$token = new UsernamePasswordOrganizationToken($user, $user->getUsername(), 'main', $this->organization); |
|
314
|
|
|
$this->securityContext->setToken($token); |
|
|
|
|
|
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: