/**
  * Special action which produces an HTML version of the review calendar.
  *
  * @return ViewModel
  */
 public function selectAttendeesAction()
 {
     /** @var CalendarService $calendarService */
     $calendarService = $this->getCalendarService()->setCalendarId($this->params('id'));
     if ($calendarService->isEmpty()) {
         return $this->notFoundAction();
     }
     $data = array_merge_recursive($this->getRequest()->getPost()->toArray());
     $form = new SelectAttendee($calendarService, $this->getContactService());
     $formValues = [];
     $formValues['contact'] = [];
     foreach ($calendarService->getCalendar()->getCalendarContact() as $calendarContact) {
         $formValues['contact'][] = $calendarContact->getContact()->getId();
     }
     $form->setData($formValues);
     if ($this->getRequest()->isPost() && $form->setData($data) && $form->isValid()) {
         $formValues = $form->getData();
         if (isset($formValues['cancel'])) {
             return $this->redirect()->toRoute('community/calendar/calendar', ['id' => $calendarService->getCalendar()->getId()], ['fragment' => 'attendees']);
         }
         $calendar = $calendarService->getCalendar();
         $calendarContacts = $calendar->getCalendarContact();
         if (isset($formValues['contact'])) {
             foreach ($formValues['contact'] as $contactId) {
                 //Try to find the object.
                 $calendarContact = $this->getCalendarService()->findCalendarContactByContactAndCalendar($this->getContactService()->setContactId($contactId)->getContact(), $this->getCalendarService()->getCalendar());
                 $calendarContacts->removeElement($calendarContact);
                 /*
                  * Save a new one.
                  */
                 if (is_null($calendarContact)) {
                     $calendarContact = new Contact();
                     $calendarContact->setContact($this->getContactService()->setContactId($contactId)->getContact());
                     $calendarContact->setRole($this->getCalendarService()->findEntityById('ContactRole', ContactRole::ROLE_ATTENDEE));
                     $calendarContact->setStatus($this->getCalendarService()->findEntityById('ContactStatus', ContactStatus::STATUS_TENTATIVE));
                     $calendarContact->setCalendar($this->getCalendarService()->getCalendar());
                     $this->getCalendarService()->newEntity($calendarContact);
                 }
             }
         }
         //Remove the difference in the leftovers in the $calendarContacts, but only when they are attendee
         foreach ($calendarContacts as $calendarContact) {
             if ($calendarContact->getRole()->getId() === ContactRole::ROLE_ATTENDEE) {
                 $this->getCalendarService()->removeEntity($calendarContact);
             }
         }
         $this->getCalendarService()->updateEntity($calendar);
         $this->flashMessenger()->addInfoMessage(sprintf($this->translate("txt-calendar-attendees-for-%s-have-been-updated"), $calendarService->getCalendar()->getCalendar()));
         return $this->redirect()->toRoute('community/calendar/calendar', ['id' => $calendarService->getCalendar()->getId()], ['fragment' => 'attendees']);
     }
     return new ViewModel(['form' => $form, 'calendarService' => $calendarService]);
 }
 /**
  * @param CalendarContact $calendarContact
  * @param                 $status
  */
 public function updateContactStatus(CalendarContact $calendarContact, $status)
 {
     $calendarContact->setStatus($this->getEntityManager()->getReference($this->getFullEntityName('ContactStatus'), $status));
     $this->updateEntity($calendarContact);
 }