/** * Create and submit a mass mail entry. * @param Request $request * @return array * * @Route("/admin/mail/create", name="admin_mail_create") * @Template */ public function createAction(Request $request) { /** @var User $currentUser */ $currentUser = $this->getUser(); $templates = $this->getTemplates(); $ntypes = $this->getNotificationTypes(); /** @var UserManager $userManager */ $userManager = $this->get('fos_user.user_manager'); $formData = ['isPreview' => true, 'previewEmail' => $currentUser->getEmail()]; $fb = $this->createFormBuilder($formData); $fb->add('template', 'choice', ['choices' => $templates])->add('subject', 'text')->add('body', 'textarea')->add('isPreview', 'checkbox', ['required' => false, 'label' => 'Is Preview?', 'render_optional_text' => false, 'widget_checkbox_label' => 'label'])->add('previewEmail', 'email', ['required' => false, 'label' => 'Preview E-mail', 'render_optional_text' => false])->add('language', 'choice', ['choices' => ['ANY' => '--- ANY ---', 'en' => 'English', 'es' => 'Spanish', 'pt' => 'Portuguese']])->add('notificationType', 'choice', ['choices' => $ntypes, 'label' => 'Notification Type'])->add('userStatus', 'choice', ['choices' => User::$status_types, 'label' => 'User Status'])->add('scholarships', 'entity', ['class' => 'GotChosenSiteBundle:Scholarship', 'property' => 'scholarshipName', 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('s')->where('s.startDate < :now')->andWhere('s.endDate > :now')->setParameter('now', new \DateTime()); }, 'multiple' => true, 'required' => false, 'render_optional_text' => false, 'expanded' => true])->add('hasSubmittedGame', 'checkbox', ['required' => false, 'label' => 'Has Submitted Game?', 'render_optional_text' => false, 'widget_checkbox_label' => 'label']); $form = $fb->getForm(); $form->handleRequest($request); if ($form->isValid()) { if ($form->get('isPreview')->getData()) { // ------------------- // Send preview // /** @var User $user */ $user = $userManager->findUserByEmail($form->get('previewEmail')->getData()); if ($user) { $entry = MassMailQueue::makePreview($user, $form->get('template')->getData(), $form->get('subject')->getData(), ['body' => $form->get('body')->getData()]); $this->em()->persist($entry); $this->em()->flush(); $this->flash('success', 'Preview message queued for delivery'); } else { $this->flash('error', 'User for given e-mail address not found'); } } else { // ------------------- // Send for real // $filter = new Filter(); $lang = $form->get('language')->getData(); if ($lang != 'ANY') { $filter->setLanguage($lang); } $ntId = $form->get('notificationType')->getData(); if ($ntId != 0) { /** @var NotificationType $nt */ $nt = $this->repo('NotificationType')->find($form->get('notificationType')->getData()); $filter->setNotificationType($nt); } $status = $form->get('userStatus')->getData(); $filter->setUserStatus($status); $sslist = $form->get('scholarships')->getData(); if (!empty($sslist)) { $filter->setScholarships($sslist); } if ($form->get('hasSubmittedGame')->getData()) { $filter->setHasSubmittedGame(true); } $entry = MassMailQueue::makeFiltered($filter, $form->get('template')->getData(), $form->get('subject')->getData(), ['body' => $form->get('body')->getData()]); $this->em()->persist($entry); $this->em()->flush(); $this->flash('success', 'Message queued for delivery'); return $this->redirectRoute('admin_mail'); } } return ['form' => $form->createView()]; }