public function previewAction() { $this->updateLayoutWithIdentity(); if ($this->disallowRankLessThan(User::RANK_CORPORAL)) { return false; } $session = new SessionContainer('NightsWatch\\Event\\Create'); if (empty($session->name)) { $this->redirect()->toRoute('home', ['contoller' => 'event', 'action' => 'create']); return false; } $userRepo = $this->getEntityManager()->getRepository('NightsWatch\\Entity\\User'); $eventRepo = $this->getEntityManager()->getRepository('NightsWatch\\Entity\\Event'); $leader = $session->leader; if (!empty($leader)) { $leader = $userRepo->findOneBy(['username' => $leader]); } if (empty($leader)) { $leader = $this->getIdentityEntity(); } $newEvent = false; if (isset($session->id) && $session->id != false) { $event = $eventRepo->find($session->id); if (is_null($event)) { throw new Exception('No such event found'); } } else { $newEvent = true; $event = new Event(); } $event->name = $session->name; $event->description = $session->description; if ($newEvent) { $event->user = $this->getIdentityEntity(); } $event->start = new \DateTime($session->date . ' ' . $session->time); $event->region = $session->region; $event->leader = $leader; $event->type = $session->type; $offset = $session->offset + date('Z'); $add = $offset > 0 ? true : false; $offset = abs($offset); $interval = new \DateInterval('PT' . $offset . 'S'); if ($add) { $event->start->add($interval); } else { $event->start->sub($interval); } $event->lowestViewableRank = $session->rank; if ($this->getRequest()->isPost()) { $this->getEntityManager()->persist($event); $this->getEntityManager()->flush(); $event->user = $this->getIdentityEntity(); $session->name = ''; // Send out the Emails $sendSpecified = isset($session->sendemail) && (bool) $session->sendemail; $sendEmails = $newEvent || $sendSpecified; if ($sendEmails) { $criteria = Criteria::create()->where(Criteria::expr()->gte('rank', $event->lowestViewableRank)); /** @var User[] $users */ $users = $userRepo->matching($criteria); $mail = new \Zend\Mail\Message(); $mail->setSubject('[NightsWatch] Event: ' . $event->name); $mail->setFrom(new Address('*****@*****.**', $event->user->username)); $mail->setTo(new Address('*****@*****.**', 'Members')); $headers = $mail->getHeaders(); if ($newEvent) { $headers->addHeaderLine('Message-Id', '<event-' . $event->id . '@threads.minez-nightswatch.com>'); } else { $headers->addHeaderLine('References', '<event-' . $event->id . '@threads.minez-nightswatch.com>'); $headers->addHeaderLine('In-Reply-To', '<event-' . $event->id . '@threads.minez-nightswatch.com>'); } $headers->addHeaderLine('Threading-Id', '<event-' . $event->id . '@threads.minez-nightswatch.com>'); $mail->setEncoding('UTF-8'); $url = $this->url()->fromRoute('id', ['controller' => 'event', 'action' => 'view', 'id' => $event->id], ['force_canonical' => true]); $niceDate = $event->start->format('M j, Y'); $niceTime = $event->start->format('H:i T'); $region = $event->getRegionName(); // Create a signature $title = trim($event->user->getTitleOrRank()); if ($newEvent) { $lead = 'A new event has been posted to the calendar.'; } else { $lead = 'An event on the calendar has been updated with new, important information.'; } $event->description = "{$lead} All information concerning this event " . 'is classified and only available to members of rank ' . User::getRankName($event->lowestViewableRank) . " and up.\n\n" . 'You can read the details of this event at https://minez-nightswatch.com/event/' . $event->id . "\n\nEvent Details: \nDate: {$niceDate} \nTime: {$niceTime} \nRSVP: [{$url}]({$url}) " . ($event->region > 0 ? "\nRegion: {$region}" : '') . "\n\n" . "{$event->user->username} \n*{$title}*"; // Event Stuff // Not Yet Working. Not sure why. $start = clone $event->start; $start->setTimezone(new \DateTimeZone('UTC')); $dtstart = $start->format('Ymd\\THis\\Z'); $eventRaw = <<<CALENDAR BEGIN:VCALENDAR PRODID:-//NightsWatch//Nights Watch Event Creator//EN VERSION:2.0 CALSCALE:GREGORIAN METHOD:REQUEST BEGIN:VEVENT UID:event-{$event->id}@minez-nightswatch.com DTSTART:{$dtstart} ORGANIZER;CN=Night's Watch:noreply@minez-nightswatch.com SUMMARY:{$event->name} END:VEVENT END:VCALENDAR CALENDAR; $body = new MimeBody(); $bodyHtml = new MimePart($event->getParsedDescription()); $bodyHtml->type = Mime::TYPE_HTML; $bodyText = new MimePart($event->description); $bodyText->type = Mime::TYPE_TEXT; $bodyEvent = new MimePart($eventRaw); $bodyEvent->type = 'text/calendar'; $bodyEvent->disposition = Mime::DISPOSITION_INLINE; $bodyEvent->encoding = Mime::ENCODING_8BIT; $bodyEvent->filename = 'calendar.ics'; $body->setParts([$bodyHtml, $bodyText, $bodyEvent]); $mail->setBody($body); foreach ($users as $user) { if ($user->emailNotifications & User::EMAIL_ANNOUNCEMENT > 0) { $mail->addBcc(new Address($user->email, $user->username)); } } $transport = new Sendmail(); $transport->send($mail); } $this->redirect()->toRoute('id', ['controller' => 'event', 'id' => $event->id]); return false; } return new ViewModel(['event' => $event, 'user' => $this->getIdentityEntity()]); }