mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
work on create calendar
This commit is contained in:
@@ -284,12 +284,8 @@ class CalendarController extends AbstractController
|
||||
$entity = new Calendar();
|
||||
if ($request->query->has('mainUser')) {
|
||||
$entity->setMainUser($this->userRepository->find($request->query->getInt('mainUser')));
|
||||
} else {
|
||||
$entity->setMainUser($this->getUser());
|
||||
}
|
||||
|
||||
dump($entity);
|
||||
|
||||
// if ($user instanceof User) {
|
||||
// $entity->setPerson($user);
|
||||
// }
|
||||
|
@@ -172,6 +172,29 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addUser(User $user): self
|
||||
{
|
||||
if (!$this->getUsers()->contains($user)) {
|
||||
$this->addInvite((new Invite())->setUser($user));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeUser(User $user): self
|
||||
{
|
||||
if (!$this->getUsers()->contains($user)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$invite = $this->invites
|
||||
->filter(function (Invite $invite) use ($user) { return $invite->getUser() === $user; })
|
||||
->first();
|
||||
$this->removeInvite($invite);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addPerson(Person $person): self
|
||||
{
|
||||
$this->persons[] = $person;
|
||||
@@ -309,14 +332,12 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
|
||||
return $this->getProfessionals();
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
/**
|
||||
* @return Collection|User[]
|
||||
*/
|
||||
public function getUsers(): Collection
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getusers(): Collection
|
||||
{
|
||||
return $this->getInvites(); //TODO get users of the invite
|
||||
return $this->getInvites()->map(function (Invite $i) { return $i->getUser(); });
|
||||
}
|
||||
|
||||
public static function loadValidatorMetadata(ClassMetadata $metadata): void
|
||||
@@ -335,7 +356,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface
|
||||
|
||||
public function removeInvite(Invite $invite): self
|
||||
{
|
||||
$this->invites->removeElement($invite);
|
||||
if ($this->invites->removeElement($invite)) {
|
||||
$invite->setCalendar(null);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@@ -40,14 +40,14 @@ class Invite implements TrackUpdateInterface, TrackCreationInterface
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=Calendar::class, inversedBy="invites")
|
||||
*/
|
||||
private ?Calendar $calendar;
|
||||
private ?Calendar $calendar = null;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
*/
|
||||
private ?int $id;
|
||||
private ?int $id = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="text", nullable=false, options={"default": "pending"})
|
||||
@@ -58,7 +58,7 @@ class Invite implements TrackUpdateInterface, TrackCreationInterface
|
||||
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
*/
|
||||
private ?User $user;
|
||||
private ?User $user = null;
|
||||
|
||||
public function getCalendar(): ?Calendar
|
||||
{
|
||||
@@ -82,8 +82,6 @@ class Invite implements TrackUpdateInterface, TrackCreationInterface
|
||||
|
||||
/**
|
||||
* @internal use Calendar::addInvite instead
|
||||
* @param Calendar|null $calendar
|
||||
* @return void
|
||||
*/
|
||||
public function setCalendar(?Calendar $calendar): void
|
||||
{
|
||||
@@ -99,6 +97,10 @@ class Invite implements TrackUpdateInterface, TrackCreationInterface
|
||||
|
||||
public function setUser(?User $user): self
|
||||
{
|
||||
if ($user instanceof User && $this->user instanceof User && $user !== $this->user) {
|
||||
throw new \LogicException("Not allowed to associate an invite to a different user");
|
||||
}
|
||||
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
|
@@ -70,6 +70,7 @@ class CalendarType extends AbstractType
|
||||
|
||||
if ($options['data'] instanceof Calendar && $options['data']->getId() === null) {
|
||||
$builder->add('mainUser', PickUserDynamicType::class, [
|
||||
'multiple' => false,
|
||||
'required' => true,
|
||||
'help' => 'chill_calendar.form.The main user is mandatory. He will organize the appointment.',
|
||||
]);
|
||||
@@ -192,26 +193,6 @@ class CalendarType extends AbstractType
|
||||
return $this->om->getRepository(Location::class)->findOneBy(['id' => (int) $id]);
|
||||
}
|
||||
));
|
||||
|
||||
$builder->add('invites', HiddenType::class);
|
||||
$builder->get('invites')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
static function (iterable $usersAsIterable): string {
|
||||
$userIds = [];
|
||||
|
||||
foreach ($usersAsIterable as $value) {
|
||||
$userIds[] = $value->getId();
|
||||
}
|
||||
|
||||
return implode(',', $userIds);
|
||||
},
|
||||
function (?string $usersAsString): array {
|
||||
return array_map(
|
||||
fn (string $id): ?Invite => $this->om->getRepository(Invite::class)->findOneBy(['id' => (int) $id]),
|
||||
explode(',', $usersAsString)
|
||||
);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
|
@@ -180,8 +180,8 @@ const store = createStore({
|
||||
startDateInput.value = payload.startStr;
|
||||
let endDateInput = document.getElementById("chill_activitybundle_activity_endDate");
|
||||
endDateInput.value = payload.endStr;
|
||||
let mainUserInput = document.getElementById("chill_activitybundle_activity_mainUser");
|
||||
mainUserInput.value = payload.users.logged.id;
|
||||
//let mainUserInput = document.getElementById("chill_activitybundle_activity_mainUser");
|
||||
//mainUserInput.value = payload.users.logged.id;
|
||||
commit('setEvents', payload);
|
||||
},
|
||||
updateEvent({ commit }, payload) {
|
||||
@@ -192,8 +192,8 @@ const store = createStore({
|
||||
endDateInput.value = payload.event.end.toISOString();
|
||||
let calendarRangeInput = document.getElementById("chill_activitybundle_activity_calendarRange");
|
||||
calendarRangeInput.value = Number(payload.event.extendedProps.calendarRangeId);
|
||||
let mainUserInput = document.getElementById("chill_activitybundle_activity_mainUser");
|
||||
mainUserInput.value = Number(payload.event.source.id);
|
||||
//let mainUserInput = document.getElementById("chill_activitybundle_activity_mainUser");
|
||||
//mainUserInput.value = Number(payload.event.source.id);
|
||||
commit('setEvents', payload);
|
||||
},
|
||||
|
||||
|
44
src/Bundle/ChillCalendarBundle/Tests/Entity/CalendarTest.php
Normal file
44
src/Bundle/ChillCalendarBundle/Tests/Entity/CalendarTest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\CalendarBundle\Tests\Entity;
|
||||
|
||||
use Chill\CalendarBundle\Entity\Calendar;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CalendarTest extends TestCase
|
||||
{
|
||||
public function testAddUser()
|
||||
{
|
||||
$calendar = new Calendar();
|
||||
|
||||
$this->assertCount(0, $calendar->getInvites());
|
||||
$this->assertCount(0, $calendar->getUsers());
|
||||
|
||||
$calendar->addUser($user0 = new User());
|
||||
|
||||
$this->assertCount(1, $calendar->getInvites());
|
||||
$this->assertCount(1, $calendar->getUsers());
|
||||
$this->assertSame($user0, $calendar->getUsers()->first());
|
||||
|
||||
$calendar->addUser($user1 = new User());
|
||||
|
||||
$this->assertCount(2, $calendar->getInvites());
|
||||
$this->assertCount(2, $calendar->getUsers());
|
||||
$this->assertContains($user0, $calendar->getUsers());
|
||||
$this->assertContains($user1, $calendar->getUsers());
|
||||
|
||||
$calendar->removeUser($user0);
|
||||
|
||||
$this->assertCount(1, $calendar->getInvites());
|
||||
$this->assertCount(1, $calendar->getUsers());
|
||||
$this->assertNotSame($user0, $calendar->getUsers()->first());
|
||||
$this->assertSame($user1, $calendar->getUsers()->first());
|
||||
|
||||
$calendar->removeUser($user1);
|
||||
|
||||
$this->assertCount(0, $calendar->getInvites());
|
||||
$this->assertCount(0, $calendar->getUsers());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user