public static function makeFiltered(Filter $filter, $template, $subject, array $params) { $mq = new MassMailQueue(); $mq->setType(self::TYPE_FILTER); $mq->setFilterSpec(Filter::toArray($filter)); $mq->setTemplate($template); $mq->setSubject($subject); $mq->setParameters($params); return $mq; }
/** * 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()]; }
private function applyQueryFilter(QueryBuilder $query, Filter $filter) { $query->where('u.enabled = 1'); if ($filter->isEmpty()) { return; } if ($nt = $filter->getNotificationType()) { // JOIN NotificationSubscriptions ns ON (u.id = ns.idUser AND ns.idNotificationType = x) $query->innerJoin('u', 'NotificationSubscriptions', 'ns', 'u.id = ns.idUser AND ns.idNotificationType = :ntype'); $query->setParameter('ntype', $nt->getId()); } if ($status = $filter->getUserStatus()) { $query->andWhere('u.status = :status'); $query->setParameter('status', $status); } $sslist = $filter->getScholarships(); if (!empty($sslist)) { $ssids = []; foreach ($sslist as $scholarship) { $ssids[] = $scholarship->getId(); } $ssids = implode(',', $ssids); $query->innerJoin('u', 'Entries', 'e', 'u.id = e.idUser AND e.idScholarship IN(' . $ssids . ')'); } if (class_exists('GotChosen\\SiteBundle\\Entity\\EGGame') && $filter->hasSubmittedGame()) { $query->innerJoin('u', 'Games', 'g', 'u.id = g.user_id'); } if ($lang = $filter->getLanguage()) { $query->innerJoin('u', 'UserProfile', 'up', 'u.id = up.user_id AND up.property_id = 19 AND up.propertyValue = :lang'); $query->setParameter('lang', $lang); } }