public function createActivity(Activity $activity, Form $form)
 {
     // Apply context of Activity.
     if (!$activity->getCampaign()) {
         $activity->setCampaign($this->campaign);
     } elseif (!$this->campaign) {
         $this->campaign = $activity->getCampaign();
     }
     if (!$activity->getChannel()) {
         $activity->setChannel($this->channel);
     } elseif (!$this->channel) {
         $this->channel = $activity->getChannel();
     }
     if (!$activity->getLocation()) {
         $activity->setLocation($this->location);
     } elseif (!$this->location) {
         $this->location = $activity->getLocation();
     }
     // The Module's content.
     $content = null;
     // If Activity module is not set, then do it.
     if (!$activity->getActivityModule()) {
         $moduleService = $this->get('campaignchain.core.module');
         $activity->setActivityModule($moduleService->getModule($this->activityBundleName, $this->activityModuleIdentifier));
     }
     // Does the Activity module relate to at least one Channel module?
     $hasChannel = $activity->getActivityModule()->getChannelModules()->count();
     // The Module's content.
     $content = null;
     $operation = new Operation();
     if ($this->parameters['equals_operation']) {
         if ($hasChannel) {
             if (!$activity->getChannel()) {
                 $activity->setChannel($this->channel);
             } elseif (!$this->channel) {
                 $this->channel = $activity->getChannel();
             }
             if (!$activity->getLocation()) {
                 $activity->setLocation($this->location);
             } elseif (!$this->location) {
                 $this->location = $activity->getLocation();
             }
         }
         // Allow the module to change some data based on its custom input.
         if ($form->has($this->contentModuleFormName)) {
             $this->handler->postFormSubmitNewEvent($activity, $form->get($this->contentModuleFormName)->getData());
             // Allow a module's handler to modify the Activity data.
             $activity = $this->handler->processActivity($activity, $form->get($this->contentModuleFormName)->getData());
         }
         // Get the operation module.
         $operationService = $this->get('campaignchain.core.operation');
         $operation = $operationService->newOperationByActivity($activity, $this->contentBundleName, $this->contentModuleIdentifier);
         // Will the Operation create a Location, i.e. the Operation
         // will be accessible through a URL after publishing?
         if ($operation->getOperationModule()->ownsLocation()) {
             // Get the location module.
             $locationService = $this->get('campaignchain.core.location');
             $locationModule = $locationService->getLocationModule($this->locationBundleName, $this->locationModuleIdentifier);
             $contentLocation = new Location();
             $contentLocation->setLocationModule($locationModule);
             $contentLocation->setParent($activity->getLocation());
             $contentLocation->setName($activity->getName());
             $contentLocation->setStatus(Medium::STATUS_UNPUBLISHED);
             $contentLocation->setOperation($operation);
             $operation->addLocation($contentLocation);
             if ($form->has($this->contentModuleFormName)) {
                 // Allow a module's handler to modify the Operation's Location.
                 $contentLocation = $this->handler->processContentLocation($contentLocation, $form->get($this->contentModuleFormName)->getData());
             }
         }
         if ($form->has($this->contentModuleFormName)) {
             // Process the Operation's content.
             $content = $this->handler->processContent($operation, $form->get($this->contentModuleFormName)->getData());
         }
         if ($content) {
             // Link the Operation details with the operation.
             $content->setOperation($operation);
         }
     } elseif (count($this->parameters['operations']) > 1) {
         $content = $this->handler->processMultiOperationMultiContent($activity, $form, $this->parameters['operations']);
     }
     $em = $this->getDoctrine()->getManager();
     // Make sure that data stays intact by using transactions.
     try {
         $em->getConnection()->beginTransaction();
         $em->persist($activity);
         if (!$content) {
             $content = $this->handler->processSingleContentMultiOperation($activity, $form);
         }
         if ($content) {
             $em->persist($content);
         }
         // We need the activity ID for storing the hooks. Hence we must
         // flush here.
         $em->flush();
         $hookService = $this->get('campaignchain.core.hook');
         /** @var Activity $activity */
         $activity = $hookService->processHooks($this->parameters['bundle_name'], $this->parameters['module_identifier'], $activity, $form, true);
         // Check if the content can be executed.
         if (isset($this->validators['operations']) && $content) {
             $isExecutable = $this->validators['operations'][0]->isExecutableByLocation($content, $activity->getStartDate());
             if (!$isExecutable['status']) {
                 throw new \Exception($isExecutable['message']);
             }
             $activity->setMustValidate($this->validators['operations'][0]->mustValidate($content, $activity->getStartDate()));
         }
         $em->flush();
         $em->getConnection()->commit();
     } catch (\Exception $e) {
         $em->getConnection()->rollback();
         throw $e;
     }
     // The module tries to execute the job immediately.
     $this->handler->postPersistNewEvent($operation, $form, $content);
     return $activity;
 }
Example #2
0
 public function getNewActivity()
 {
     // Reset memory of pre-selected campaign.
     $this->container->get('session')->set('campaignchain.campaign', null);
     // Merge context with persistence manager.
     $repository = $this->container->get('doctrine')->getManager();
     $this->session->resume();
     $campaign = $this->session->get('campaignchain_campaign');
     $campaign = $repository->merge($campaign);
     $activityModule = $this->session->get('campaignchain_activityModule');
     $activityModule = $repository->merge($activityModule);
     // Create new Activity.
     $activity = new Activity();
     $activity->setActivityModule($activityModule);
     $activity->setCampaign($campaign);
     if ($this->session->has('campaignchain_location')) {
         $location = $this->session->get('campaignchain_location');
         $location = $repository->getRepository('CampaignChainCoreBundle:Location')->find($location);
         //$location = $repository->merge($location);
         $activity->setLocation($location);
         $activity->setChannel($location->getChannel());
     }
     return $activity;
 }