public function installDefaultNotifications(User $user) { $em = $this->getEntityManager(); /** @var NotificationTypeRepository $ntrepo */ $ntrepo = $em->getRepository('GotChosenSiteBundle:NotificationType'); /** @var NotificationType $ntype */ foreach ($ntrepo->getDefaults() as $ntype) { $sub = NotificationSub::make($user, $ntype); $em->persist($sub); } $em->flush(); }
protected function handleNotificationForm(User $user, Request $request, UserRepository $userRepo) { $notificationTypes = $this->repo('NotificationType')->findAll(); $currentSubIds = $user->getNotificationSubs()->map(function (NotificationSub $sub) { return $sub->getNotificationType()->getId(); })->toArray(); $formBuilder = $this->createFormBuilder(); $formData = []; $notificationTypesById = []; /** @var NotificationType $notificationType */ foreach ($notificationTypes as $notificationType) { $nid = $notificationType->getId(); $notificationTypesById[$nid] = $notificationType; // kill me $isEG = stripos($notificationType->getCommType()->getTypeName(), 'evolution games') !== false; $formBuilder->add('notification_' . $nid, 'checkbox', ['label' => ($isEG ? '[EG]' : '') . $notificationType->getName(), 'required' => false, 'widget_checkbox_label' => 'widget']); $formData['notification_' . $nid] = in_array($nid, $currentSubIds); } $formBuilder->setData($formData); $form = $formBuilder->getForm(); $form->handleRequest($request); if ($form->isValid()) { foreach ($formData as $key => $x) { $id = (int) str_replace('notification_', '', $key); $enabled = $form->get($key)->getData(); $sub = NotificationSub::make($user, $notificationTypesById[$id]); if ($enabled) { if (!$user->hasNotificationSub($sub)) { $user->addNotificationSub($sub); $this->em()->persist($sub); } } else { if ($oldsub = $user->hasNotificationSub($sub)) { $user->removeNotificationSub($oldsub); $this->em()->remove($oldsub); } } } $this->em()->flush(); $this->flash('success', 'Your profile has been updated.'); return $this->redirectRoute('user_profile_edit', ['tab' => 'notifications']); } return $form; }
private function maybeAddSubscription(User $user, NotificationType $type) { $sub = NotificationSub::make($user, $type); if (!$user->hasNotificationSub($sub)) { $this->em()->persist($sub); } }
public function hasNotificationSub(NotificationSub $n) { /** @var NotificationSub $sub */ foreach ($this->getNotificationSubs() as $sub) { if ($n->equals($sub)) { return $sub; } } return false; }
public function equals(NotificationSub $other) { return $other->getNotificationType()->getId() == $this->getNotificationType()->getId() && $other->getUser()->getId() == $this->getUser()->getId(); }