/**
  * Get facts data per dimension.
  *
  * @param Campaign $campaign
  * @param Activity $activity
  * @param boolean  $percent
  *
  * @return array
  */
 public function getFacts(Campaign $campaign, Activity $activity, $percent = false)
 {
     $qb = $this->em->createQueryBuilder();
     $qb->select('r.time, r.value, IDENTITY(r.metric) as metric')->from('CampaignChain\\CoreBundle\\Entity\\ReportAnalyticsActivityFact', 'r')->where('r.activity = :activityId')->andWhere('r.campaign = :campaignId')->orderBy('r.time', 'ASC')->setParameter('activityId', $activity->getId())->setParameter('campaignId', $campaign->getId());
     $query = $qb->getQuery();
     $facts = $query->getArrayResult();
     $factsData = [];
     $tmp = [];
     foreach ($facts as $fact) {
         $tmp[$fact['metric']][] = [$fact['time']->getTimestamp() * 1000, $fact['value']];
     }
     foreach (array_keys($tmp) as $k) {
         $factsData[$k]['data'] = $this->serializer->serialize($tmp[$k], 'json');
         $factsData[$k]['id'] = $k;
         if ($percent) {
             $factsData[$k]['percent'] = $this->getDimensionPercent($campaign, $activity, $k);
         }
     }
     return $factsData;
 }
 public function editActivity(Activity $activity, Form $form, $content)
 {
     /** @var ActivityService $activityService */
     $activityService = $this->get('campaignchain.core.activity');
     /** @var Operation $operation */
     $operation = $activityService->getOperation($activity->getId());
     $em = $this->getDoctrine()->getManager();
     // Make sure that data stays intact by using transactions.
     try {
         $em->getConnection()->beginTransaction();
         if ($this->handler->hasContent('edit')) {
             // Get the content data from request.
             $content = $this->handler->processContent($operation, $form->get($this->contentModuleFormName)->getData());
             if ($this->parameters['equals_operation']) {
                 // The activity equals the operation. Thus, we update the operation with the same data.
                 $operation->setName($activity->getName());
                 $em->persist($operation);
             } else {
                 throw new \Exception('Multiple Operations for one Activity not implemented yet.');
             }
             $em->persist($content);
         }
         $hookService = $this->get('campaignchain.core.hook');
         /** @var Activity $activity */
         $activity = $hookService->processHooks($this->activityBundleName, $this->activityModuleIdentifier, $activity, $form);
         // Check if the content can be executed.
         if (isset($this->validators['operations'])) {
             $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->persist($activity);
         $em->flush();
         // The module tries to execute the job immediately.
         $this->handler->postPersistEditEvent($this->operations[0], $form, $content);
         $em->getConnection()->commit();
         return $activity;
     } catch (\Exception $e) {
         $em->getConnection()->rollback();
         throw $e;
     }
 }
 /**
  * Compares the status message of an already scheduled Activity with the
  * content of a new/edited Activity.
  *
  * @param Activity $existingActivity
  * @param StatusBase $content
  * @return array
  */
 protected function isUniqueContent(Activity $existingActivity, StatusBase $content)
 {
     /** @var StatusBase $existingStatus */
     $existingStatus = $this->em->getRepository('CampaignChainOperationFacebookBundle:StatusBase')->findOneByOperation($existingActivity->getOperations()[0]);
     if ($existingStatus->getMessage() == $content->getMessage()) {
         return array('status' => false, 'message' => 'Same status message has already been scheduled: ' . '<a href="' . $this->router->generate('campaignchain_activity_facebook_publish_status_edit', array('id' => $existingActivity->getId())) . '">' . $existingActivity->getName() . '</a>. ' . 'Either change the message or leave at least ' . $this->maxDuplicateInterval . ' between yours and the other post.');
     } else {
         return array('status' => true);
     }
 }