Forums › Forums › OroCRM › OroCRM – Programming Questions › CalendarEvents Api post
This topic contains 3 replies, has 2 voices, and was last updated by zhex900 6 years, 6 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- September 1, 2017 at 4:18 pm #25474
Hi,
Is it possible to add attendees to Calendar events via Api? Do I have to add my own extension? - CreatorTopic
- AuthorReplies
- September 5, 2017 at 6:00 am #25475
Hi,
For now the calendar related entities are not implemented in API.
We have internal task to do this, but I do not know in which version it will be implemented.
Fell free to implement it by yourself. It will be great if you could contribute it to the CalendarBundle.September 5, 2017 at 6:08 am #25476Thank you!
September 9, 2017 at 10:07 am #25477My first API controller. As a note, I have extended Attendee to be either a Contact or an User.
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112use FOS\RestBundle\Controller\Annotations\NamePrefix;use FOS\RestBundle\Controller\Annotations\RouteResource;use Oro\Bundle\CalendarBundle\Entity\CalendarEvent;use Oro\Bundle\CalendarBundle\Entity\Attendee;use Oro\Bundle\ContactBundle\Entity\Contact;use Oro\Bundle\SecurityBundle\Exception\ForbiddenException;use Oro\Bundle\SoapBundle\Form\Handler\ApiFormHandler;use Oro\Bundle\CalendarBundle\Controller\Api\Rest\CalendarEventController as BaseController;use Symfony\Component\HttpFoundation\Response;use FOS\RestBundle\Controller\Annotations\Put;use FOS\RestBundle\Util\Codes;use Nelmio\ApiDocBundle\Annotation\ApiDoc;use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;/*** @RouteResource("calendarevent")* @NamePrefix("oro_api_")*/class CalendarEventController extends BaseController{/*** @return ApiFormHandler*/public function getFormHandler(){return $this->get('campus_calendar.calendar_event.form.handler.api');}/*** Add an exisitng contact to a calendar event as an attendee** @param int $eventId Calendar event id* @param int $contactId Contact id** @Put("/calendarevents/{eventId}/attendee/{contactId}",* requirements={"eventId"="\d+", "contactId"="\d+"})** @ApiDoc(* description="Add an existing contact as an attendee to a Calendar Event",* resource=true* )* @AclAncestor("oro_calendar_event_update")** @return Response*/public function putAddAttendeeAction($eventId, $contactId){$em = $this->getDoctrine()->getManager();/** @var CalendarEvent $event */$event = $em->getRepository('OroCalendarBundle:CalendarEvent')->find($eventId);/** @var Contact $contact */$contact = $em->getRepository('OroContactBundle:Contact')->find($contactId);$attendee = $this->getAttendeeByContact($event, $contact);/** Both event and contact have to exist and* Contact is not already an attendee*/try {if (isset($attendee)) {$reason = 'Contact is already attending this event';throw new ForbiddenException($reason);}$attendee = $this->createAttendee($contact);$event->addAttendee($attendee);$em->persist($attendee);$em->persist($event);$em->flush();$view = $this->view('attendee id: ' . $attendee->getId(), Codes::HTTP_OK);} catch (ForbiddenException $forbiddenEx) {$view = $this->view(['reason' => $forbiddenEx->getReason()], Codes::HTTP_FORBIDDEN);}return $this->buildResponse($view, self::ACTION_UPDATE, ['id' => $eventId, 'entity' => $event]);}/*** Get attendee of Calendar Event by related User of Attendee.** @param CalendarEvent $event* @param Contact $contact** @return Attendee|null*/protected function getAttendeeByContact(CalendarEvent $event, Contact $contact){$attendees = $event->getAttendees();foreach ($attendees as $attendee) {if ($attendee->getContact() == $contact) {return $attendee;}}return null;}/*** @param Contact $contact** @return Attendee*/protected function createAttendee(Contact $contact){$attendee = new Attendee();$attendee->setDisplayName($contact->getFirstName() . ' ' . $contact->getLastName());$attendee->setContact($contact);$attendee->setEmail($contact->getEmail());return $attendee;}} - AuthorReplies
The forum ‘OroCRM – Programming Questions’ is closed to new topics and replies.