/**
  * {@inheritdoc}
  */
 public function afterProcess($entity)
 {
     $owner = $entity->getOwner();
     $owner->setUpdatedAt(new \DateTime('now', new \DateTimeZone('UTC')));
     $changeSet = $this->getChangeSet($owner);
     $this->entityManager->persist($owner);
     $this->entityManager->flush();
     return $changeSet;
 }
 /**
  * @param AbstractLogger $logger
  *
  * @return int
  */
 public function recalculate(AbstractLogger $logger)
 {
     $logger->notice('Recalculating contacting activities...');
     $logger->info(sprintf('<info>Processing started at %s</info>', date('Y-m-d H:i:s')));
     /** @var ConfigProvider $activityConfigProvider */
     $activityConfigProvider = $this->getContainer()->get('oro_entity_config.provider.activity');
     /** @var ActivityContactProvider $activityContactProvider */
     $activityContactProvider = $this->getContainer()->get('orocrm_activity_contact.provider');
     $contactingActivityClasses = $activityContactProvider->getSupportedActivityClasses();
     $entityConfigsWithApplicableActivities = $activityConfigProvider->filter(function (ConfigInterface $entity) use($contactingActivityClasses) {
         return $entity->get('activities') && array_intersect($contactingActivityClasses, $entity->get('activities'));
     });
     if ($entityConfigsWithApplicableActivities) {
         $logger->info(sprintf('<comment>Total found %d entities with enabled contacting activities</comment>', count($entityConfigsWithApplicableActivities)));
         $this->em = $this->getContainer()->get('doctrine')->getManager();
         $this->activityListRepository = $this->em->getRepository(ActivityList::ENTITY_NAME);
         /** @var ActivityListener $activityListener */
         $activityListener = $this->getContainer()->get('orocrm_activity_contact.listener.activity_listener');
         /** @var ActivityListFilterHelper $activityListHelper */
         $activityListHelper = $this->getContainer()->get('oro_activity_list.filter.helper');
         foreach ($entityConfigsWithApplicableActivities as $activityScopeConfig) {
             $entityClassName = $activityScopeConfig->getId()->getClassName();
             $offset = 0;
             $startTimestamp = time();
             $allRecordIds = $this->getTargetIds($entityClassName);
             while ($allRecords = $this->getRecordsToRecalculate($entityClassName, $allRecordIds, $offset)) {
                 $needsFlush = false;
                 foreach ($allRecords as $record) {
                     $this->resetRecordStatistic($record);
                     /** @var QueryBuilder $qb */
                     $qb = $this->activityListRepository->getBaseActivityListQueryBuilder($entityClassName, $record->getId());
                     $activityListHelper->addFiltersToQuery($qb, ['activityType' => ['value' => $contactingActivityClasses]]);
                     /** @var ActivityList[] $activities */
                     $activities = $qb->getQuery()->getResult();
                     if ($activities) {
                         foreach ($activities as $activityListItem) {
                             /** @var object $activity */
                             $activity = $this->em->getRepository($activityListItem->getRelatedActivityClass())->find($activityListItem->getRelatedActivityId());
                             $activityListener->onAddActivity(new ActivityEvent($activity, $record));
                         }
                         $this->em->persist($record);
                         $needsFlush = true;
                     }
                 }
                 if ($needsFlush) {
                     $this->em->flush();
                 }
                 $this->em->clear();
                 $offset += self::BATCH_SIZE;
             }
             $endTimestamp = time();
             $logger->info(sprintf('Entity "%s", %d records processed (<comment>%d sec.</comment>).', $entityClassName, count($allRecordIds), $endTimestamp - $startTimestamp));
         }
     }
     $logger->info(sprintf('<info>Processing finished at %s</info>', date('Y-m-d H:i:s')));
     return self::STATUS_SUCCESS;
 }
 public function preSubmitData(FormEvent $event)
 {
     list($entityId, $model) = $this->prepareEvent($event);
     $saved = [];
     if ($entityId) {
         $saved = $this->relations->findByFieldId($model->getId(), $entityId);
         array_walk($saved, function (&$item) {
             $item = $item->getOption()->getId();
         });
     }
     $data = $event->getData();
     if (empty($data)) {
         $data = [];
     }
     if (!is_array($data)) {
         $data = [$data];
     }
     if ($entityId) {
         /**
          * Save selected options
          */
         $toSave = array_intersect($data, $saved);
         foreach ($data as $option) {
             if (!in_array($option, $saved)) {
                 $optionRelation = new OptionSetRelation();
                 $optionRelation->setData(null, $entityId, $model, $this->options->find($option));
                 $toSave[] = $option;
                 $this->em->persist($optionRelation);
             }
         }
         /**
          * Remove unselected
          */
         if ($entityId && $this->relations->count($model->getId(), $entityId)) {
             $toRemove = $this->relations->findByNotIn($model->getId(), $entityId, $toSave);
             foreach ($toRemove as $option) {
                 $this->em->remove($option);
             }
         }
     }
 }
 /**
  * PRE_SUBMIT event handler
  *
  * @param FormEvent $event
  */
 public function preSubmitData(FormEvent $event)
 {
     $entityId = $this->getEntityId($event);
     if ($entityId) {
         $configFieldModel = $this->configManager->getConfigFieldModel($event->getForm()->getConfig()->getOption('entityClassName'), $event->getForm()->getConfig()->getOption('entityFieldName'));
         $savedOptionIds = $this->getSavedOptionIds($configFieldModel, $entityId);
         $data = $event->getData();
         if (empty($data)) {
             $data = [];
         }
         if (!is_array($data)) {
             $data = [$data];
         }
         /**
          * Save selected options
          */
         $toSave = array_intersect($data, $savedOptionIds);
         foreach ($data as $option) {
             if (!in_array($option, $savedOptionIds)) {
                 $optionRelation = new OptionSetRelation();
                 $optionRelation->setData(null, $entityId, $configFieldModel, $this->options->find($option));
                 $toSave[] = $option;
                 $this->em->persist($optionRelation);
             }
         }
         /**
          * Remove unselected
          */
         if ($entityId && $this->relations->count($configFieldModel->getId(), $entityId)) {
             $toRemove = $this->relations->findByNotIn($configFieldModel->getId(), $entityId, $toSave);
             foreach ($toRemove as $option) {
                 $this->em->remove($option);
             }
         }
     }
 }