/**
  * 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]);
 }
Esempio n. 2
0
 /**
  * @param array $data
  * @param Calendar $calendar
  *
  * array (size=5)
  * 'type' => string '2' (length=1)
  * 'added' => string '20388' (length=5)
  * 'removed' => string '' (length=0)
  * 'sql' => string '' (length=0)
  *
  *
  */
 public function updateCalendarContacts(Calendar $calendar, array $data)
 {
     //Update the contacts
     if (!empty($data['added'])) {
         foreach (explode(',', $data['added']) as $contactId) {
             $contact = $this->getContactService()->findEntityById('contact', $contactId);
             $contactService = clone $this->getContactService();
             $contactService->setContact($contact);
             if (!$contactService->isEmpty() && !$this->calendarHasContact($calendar, $contact)) {
                 $calendarContact = new CalendarContact();
                 $calendarContact->setContact($contact);
                 $calendarContact->setCalendar($calendar);
                 /**
                  * Add every new user as attendee
                  *
                  * @var $role Entity\ContactRole
                  */
                 $role = $this->findEntityById('contactRole', Entity\ContactRole::ROLE_ATTENDEE);
                 $calendarContact->setRole($role);
                 /**
                  * Add every new user as attendee
                  *
                  * @var $status Entity\ContactStatus
                  */
                 $status = $this->findEntityById('contactStatus', Entity\ContactStatus::STATUS_TENTATIVE);
                 $calendarContact->setStatus($status);
                 $this->newEntity($calendarContact);
             }
         }
     }
     //Update the contacts
     if (!empty($data['removed'])) {
         foreach (explode(',', $data['removed']) as $contactId) {
             foreach ($calendar->getCalendarContact() as $calendarContact) {
                 if ($calendarContact->getContact()->getId() === (int) $contactId) {
                     $this->removeEntity($calendarContact);
                 }
             }
         }
     }
 }