/**
  * @param Request $request
  * @param Notification $notification
  * @return array
  *
  * @Route("/broadcast/copy/{id}", name="cto_notification_broadcastCopy")
  * @Method({"GET", "POST"})
  * @Template("@CTOApp/DashboardControllers/CTO/Notifications/broadcast.html.twig")
  */
 public function broadcastCopyAction(Request $request, Notification $notification)
 {
     /** @var CtoUser $admin */
     $admin = $this->getUser();
     $newNotification = new Notification();
     $newNotification->setStatus(Notification::STATUS_SEND_IN_PROGRESS)->setAdminCopy($notification->isAdminCopy())->setAutoSending($notification->isAutoSending())->setWhenSend($notification->getWhenSend())->setBroadcastTo($notification->getBroadcastTo())->setType(Notification::TYPE_BROADCAST)->setUserCto($admin)->setDescription($notification->getDescription());
     $form = $this->createForm(new BroadcastType(), $newNotification);
     if ($request->getMethod() == Request::METHOD_POST) {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $now = Carbon::now();
             if ($notification->getWhenSend() < $now) {
                 if ($notification->isAutoSending() and $notification->isSendNow()) {
                     $notification->setAutoSending(true);
                 } else {
                     $notification->setAutoSending(false);
                 }
             }
             /** @var EntityManager $em */
             $em = $this->getDoctrine()->getManager();
             $em->persist($newNotification);
             $em->flush();
             if ($notification->isAutoSending()) {
                 $senderSrv = $this->get('cto.sms.sender');
                 if ($notification->isSendNow()) {
                     $senderSrv->getResqueManager()->put('cto.sms.sender', ['notificationId' => $newNotification->getId(), 'broadcast' => true]);
                 } else {
                     $jobDescription = $senderSrv->getResqueManager()->put('cto.sms.sender', ['notificationId' => $newNotification->getId(), 'broadcast' => true], $this->getParameter('queue_name'), $newNotification->getWhenSend());
                     $newNotification->setResqueJobDescription($jobDescription);
                 }
             }
             $em->flush();
             $this->addFlash('success', 'Нагадування успішно створено.');
             return $this->redirectToRoute('cto_notification_home');
         }
     }
     return ['form' => $form->createView(), 'method' => 'Копіювати'];
 }
Esempio n. 2
0
 public function sendNow(Notification $notification, CtoUser $admin, $broadcast = false)
 {
     if ($broadcast) {
         //   Broadcast
         $iDs = explode(',', $notification->getBroadcastTo());
         if (in_array('-1', $iDs)) {
             $admin = $notification->getUserCto();
             $users = $this->em->getRepository('CTOAppBundle:CtoClient')->clientFilter([], $admin);
             $notification->setBroadcastTo(-1);
         } else {
             $users = $this->em->getRepository('CTOAppBundle:CtoClient')->findBy(['id' => $iDs]);
         }
         /** @var CtoClient $user */
         foreach ($users as $user) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $user->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($user);
                 $report->setPhone($user->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
                 $notification->setStatus(Notification::STATUS_SEND_OK);
             } catch (\Exception $e) {
                 $report = new NotificationReport($user);
                 $report->setPhone($user->getPhone())->setStatus(NotificationReport::REPORT_STATUS_FAILED);
                 $this->em->persist($report);
                 $notification->addReport($report);
                 $notification->setStatus(Notification::STATUS_SEND_FAIL);
             }
         }
         if ($notification->isAdminCopy()) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $admin->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
             } catch (\Exception $e) {
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
             }
         }
         $this->em->flush();
     } else {
         //  Normal
         try {
             $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $notification->getClientCto()->getPhone(), $notification->getDescription());
             $report = new NotificationReport($notification->getClientCto());
             $report->setPhone($notification->getClientCto()->getPhone())->setStatus(NotificationReport::REPORT_STATUS_SENDED);
             $this->em->persist($report);
             $notification->addReport($report);
             $notification->setStatus(Notification::STATUS_SEND_OK);
         } catch (\Exception $e) {
             $report = new NotificationReport($notification->getClientCto());
             $report->setPhone($notification->getClientCto()->getPhone())->setStatus(NotificationReport::REPORT_STATUS_FAILED);
             $this->em->persist($report);
             $notification->addReport($report);
             $notification->setStatus(Notification::STATUS_SEND_FAIL);
         }
         if ($notification->isAdminCopy()) {
             try {
                 $this->clientSMS->sendSMS($this->alfa_sms_name, '+38' . $admin->getPhone(), $notification->getDescription());
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_SENDED);
                 $this->em->persist($report);
                 $notification->addReport($report);
             } catch (\Exception $e) {
                 $report = new NotificationReport($notification->getClientCto());
                 $report->setPhone($admin->getPhone())->setSendToAdmin(true)->setStatus(NotificationReport::REPORT_STATUS_FAILED);
                 $this->em->persist($report);
             }
         }
         $this->em->flush();
     }
 }