/**
  * Symfony controller action for creating a new CampaignChain Activity.
  *
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
  * @throws \Exception
  */
 public function newAction(Request $request)
 {
     $operation = null;
     /*
      * Set Activity's context from user's choice.
      */
     $wizard = $this->get('campaignchain.core.activity.wizard');
     if (!$wizard->getCampaign()) {
         return $this->redirectToRoute('campaignchain_core_activities_new');
     }
     $campaignService = $this->get('campaignchain.core.campaign');
     $campaign = $campaignService->getCampaign($wizard->getCampaign());
     $locationService = $this->get('campaignchain.core.location');
     if ($wizard->getLocation()) {
         $location = $locationService->getLocation($wizard->getLocation());
     } else {
         $location = null;
     }
     $this->setActivityContext($campaign, $location);
     /** @var Activity $activity */
     $activity = $wizard->getActivity();
     $activity->setActivityModule($wizard->getActivityModule());
     $activity->setEqualsOperation($this->parameters['equals_operation']);
     $form = $this->createForm($this->getActivityFormType('new'), $activity);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $activity = $wizard->end();
         try {
             $activity = $this->createActivity($activity, $form);
             $this->addFlash('success', 'Your new activity <a href="' . $this->generateUrl('campaignchain_core_activity_edit', array('id' => $activity->getId())) . '">' . $activity->getName() . '</a> was created successfully.');
             return $this->redirect($this->generateUrl('campaignchain_core_activities'));
         } catch (\Exception $e) {
             $this->addFlash('warning', $e->getMessage());
             $this->getLogger()->error($e->getMessage(), array('file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace()));
         }
     }
     if ($location) {
         $channelModule = $wizard->getChannelModule();
         $channelModuleBundle = $wizard->getChannelModuleBundle();
     } else {
         $channelModule = null;
         $channelModuleBundle = null;
     }
     /*
      * Define default rendering options and then apply those defined by the
      * module's handler if applicable.
      */
     $defaultRenderOptions = array('template' => 'CampaignChainCoreBundle:Operation:new.html.twig', 'vars' => array('page_title' => 'New Activity', 'activity' => $activity, 'campaign' => $this->campaign, 'campaign_module' => $this->campaign->getCampaignModule(), 'channel_module' => $channelModule, 'channel_module_bundle' => $channelModuleBundle, 'location' => $this->location, 'form' => $form->createView(), 'form_submit_label' => 'Save', 'form_cancel_route' => 'campaignchain_core_activities_new'));
     $handlerRenderOptions = $this->handler->getNewRenderOptions();
     return $this->renderWithHandlerOptions($defaultRenderOptions, $handlerRenderOptions);
 }
示例#2
0
 public function getCampaignWithChildren(Campaign $campaign)
 {
     $data['type'] = 'campaign';
     $data['campaignchain_id'] = (string) $campaign->getId();
     $data['text'] = $campaign->getName();
     $data['route_edit_api'] = $campaign->getCampaignModule()->getRoutes()['edit_api'];
     $data['route_plan_detail'] = $campaign->getCampaignModule()->getRoutes()['plan_detail'];
     $campaignService = $this->container->get('campaignchain.core.campaign');
     $data['tpl_teaser'] = $campaignService->tplTeaser($campaign->getCampaignModule(), array('only_icon' => true, 'size' => 24));
     // Define the trigger hook's identifier.
     if (!$campaign->getInterval() && $campaign->getTriggerHook()) {
         $data['id'] = (string) $campaign->getId() . '_campaign';
         $hookService = $this->container->get($campaign->getTriggerHook()->getServices()['entity']);
         $hook = $hookService->getHook($campaign);
         $data['start_date'] = $hook->getStartDate()->format(self::FORMAT_TIMELINE_DATE);
         if ($hook->getEndDate()) {
             $data['end_date'] = $hook->getEndDate()->format(self::FORMAT_TIMELINE_DATE);
         } else {
             $data['end_date'] = $data['start_date'];
         }
         $ganttCampaignData[] = $data;
     } elseif ($campaign->getInterval()) {
         // Handle repeating campaigns.
         if (!$campaign->getIntervalEndOccurrence()) {
             $occurrences = 1;
         } else {
             $occurrences = $campaign->getIntervalEndOccurrence();
         }
         /** @var \DateTime $startDate */
         $startDate = $campaign->getIntervalStartDate();
         /** @var \DateInterval $duration */
         $duration = $campaign->getStartDate()->diff($campaign->getEndDate());
         $now = new \DateTime('now');
         $maxTimelineDate = clone $now;
         $maxTimelineDate->modify($this->container->getParameter('campaignchain.max_date_interval'));
         $firstInstanceId = null;
         $repeatingCount = 0;
         // If an actual instance exists as a child campaign, then add it.
         /** @var Campaign $childCampaign */
         $childCampaign = $this->em->getRepository('CampaignChain\\CoreBundle\\Entity\\Campaign')->findOneByParent($campaign);
         if ($childCampaign) {
             $firstInstanceId = $data['id'] = (string) $campaign->getId() . '_campaign';
             $data['start_date'] = $childCampaign->getStartDate()->format(self::FORMAT_TIMELINE_DATE);
             $data['end_date'] = $childCampaign->getEndDate()->format(self::FORMAT_TIMELINE_DATE);
             $ganttCampaignData[] = $data;
         }
         /*
          * Let's iterate through all the future instances of a repeating
          * campaign.
          */
         for ($i = 0; $i < $occurrences; $i++) {
             /*
              * Take the interval's start/end date for the first instance,
              * otherwise calculate the start/end date by adding the
              * campaign's interval to the start/end date of the
              * previous instance.
              */
             $startDate->modify($campaign->getInterval());
             $endDate = clone $startDate;
             $endDate->add($duration);
             $hasEnded = $campaign->getIntervalEndDate() != NULL && $endDate > $campaign->getIntervalEndDate();
             /*
              * If the instance is in the past, skip it,
              * because we only want ongoing or upcoming ones.
              */
             if (!$hasEnded && $startDate >= $campaign->getIntervalNextRun() && ($startDate > $now || $endDate > $now)) {
                 /*
                  * Is this the first repeating campaign instance?
                  */
                 if (!$firstInstanceId) {
                     /*
                      * This is the first instance, so we define it as
                      * the parent of the other repeating campaign
                      * instances.
                      */
                     $firstInstanceId = $data['id'] = (string) $campaign->getId() . '_campaign';
                 } else {
                     /*
                      * This is not the first instance of a repeating
                      * campaign in the timeline, so let's add the first instance
                      * as the parent campaign.
                      */
                     $data['id'] = (string) $campaign->getId() . '_' . $repeatingCount . '_campaign';
                     $data['parent'] = $firstInstanceId;
                     $repeatingCount++;
                 }
                 $data['start_date'] = $startDate->format(self::FORMAT_TIMELINE_DATE);
                 $data['interval'] = $campaign->getIntervalHumanReadable();
                 $data['end_date'] = $endDate->format(self::FORMAT_TIMELINE_DATE);
                 $ganttCampaignData[] = $data;
             }
             if ($campaign->getIntervalEndOccurrence() == NULL && (!$hasEnded && $startDate < $maxTimelineDate)) {
                 $occurrences++;
             }
         }
     } else {
         throw new \Exception('Unknown campaign type.');
     }
     return $ganttCampaignData;
 }
示例#3
0
 public function getCampaignURI(Campaign $campaign)
 {
     $bundleName = $campaign->getCampaignModule()->getBundle()->getName();
     $moduleIdentifier = $campaign->getCampaignModule()->getIdentifier();
     return $bundleName . '/' . $moduleIdentifier;
 }