/**
  * @param LoggerInterface $logger
  * @param bool            $dryRun
  */
 protected function runActivityLists(LoggerInterface $logger, $dryRun = false)
 {
     // @todo: this workaround should be removed in BAP-9156
     $this->configManager->clear();
     $targetEntities = $this->provider->getTargetEntityClasses(false);
     $toSchema = clone $this->schema;
     $hasSchemaChanges = false;
     foreach ($targetEntities as $targetEntity) {
         $associationName = ExtendHelper::buildAssociationName($targetEntity, ActivityListEntityConfigDumperExtension::ASSOCIATION_KIND);
         $relationTableName = $this->nameGenerator->generateManyToManyJoinTableName(ActivityListEntityConfigDumperExtension::ENTITY_CLASS, $associationName, $targetEntity);
         if (!$toSchema->hasTable($relationTableName)) {
             $hasSchemaChanges = true;
             $this->activityListExtension->addActivityListAssociation($toSchema, $this->metadataHelper->getTableNameByEntityClass($targetEntity));
         }
     }
     if ($hasSchemaChanges) {
         $comparator = new Comparator();
         $platform = $this->connection->getDatabasePlatform();
         $schemaDiff = $comparator->compare($this->schema, $toSchema);
         $queries = $schemaDiff->toSql($platform);
         foreach ($queries as $query) {
             $this->logQuery($logger, $query);
             if (!$dryRun) {
                 $this->connection->executeQuery($query);
             }
         }
     }
 }
Esempio n. 2
0
 /**
  * @param EntityMetadata $entityMetadata
  *
  * @return bool
  */
 protected function isApplicableNoteActivity(EntityMetadata $entityMetadata)
 {
     if ($this->activityListChainProvider->isApplicableTarget($entityMetadata->getClassName(), Note::ENTITY_NAME)) {
         return true;
     }
     return false;
 }
Esempio n. 3
0
 /**
  * @param EmailBodyAdded $event
  */
 public function updateActivityDescription(EmailBodyAdded $event)
 {
     $email = $event->getEmail();
     $activityList = $this->chainProvider->getUpdatedActivityList($email, $this->entityManager);
     if ($activityList) {
         $this->entityManager->persist($activityList);
     }
 }
Esempio n. 4
0
 /**
  * @param object $entity
  * @return bool
  */
 public function supportOwnerProvider($entity)
 {
     foreach ($this->emailOwnerStorage->getProviders() as $provider) {
         if ($this->activityListChainProvider->isSupportedTargetEntity($entity) && $provider->getEmailOwnerClass() === ClassUtils::getClass($entity)) {
             return true;
         }
     }
     return false;
 }
Esempio n. 5
0
 /**
  * Checks if the entity can have activities
  *
  * @param object|null $entity
  * @return bool
  */
 public function isApplicable($entity = null)
 {
     if (null === $entity || !is_object($entity)) {
         return false;
     }
     $entityClass = $this->doctrineHelper->getEntityClass($entity);
     $id = $this->doctrineHelper->getSingleEntityIdentifier($entity);
     $activityListRepo = $this->doctrine->getRepository('OroActivityListBundle:ActivityList');
     return in_array($entityClass, $this->activityListProvider->getTargetEntityClasses()) || (bool) $activityListRepo->getRecordsCountForTargetClassAndId($entityClass, $id);
 }
Esempio n. 6
0
 /**
  * Checks whether the activity list has at least one accessible activity type
  *
  * @param string $entityClass
  *
  * @return bool
  */
 protected function hasApplicableActivityAssociations($entityClass)
 {
     $supportedActivities = $this->activityListProvider->getSupportedActivities();
     foreach ($supportedActivities as $supportedActivity) {
         if ($this->activityListProvider->isApplicableTarget($entityClass, $supportedActivity)) {
             return true;
         }
     }
     return false;
 }
 public function testUpdateActivityDescription()
 {
     $activityList = $this->getMockBuilder('Oro\\Bundle\\ActivityListBundle\\Entity\\ActivityList')->getMock();
     $event = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Event\\EmailBodyAdded')->disableOriginalConstructor()->getMock();
     $email = $this->getMockBuilder('Oro\\Bundle\\EmailBundle\\Entity\\Email')->disableOriginalConstructor()->getMock();
     $event->expects($this->exactly(1))->method('getEmail')->will($this->returnValue($email));
     $this->chainProvider->expects($this->exactly(1))->method('getUpdatedActivityList')->with($this->identicalTo($email), $this->identicalTo($this->entityManager))->will($this->returnValue($activityList));
     $this->entityManager->expects($this->exactly(1))->method('persist')->with($activityList);
     $this->listener->updateActivityDescription($event);
 }
Esempio n. 8
0
 /**
  * Checks if the entity can have activities
  *
  * @param object|null $entity
  * @param int|null    $pageType
  * @return bool
  */
 public function isApplicable($entity = null, $pageType = null)
 {
     if ($pageType === null || !is_object($entity) || !$this->doctrineHelper->isManageableEntity($entity) || $this->doctrineHelper->isNewEntity($entity)) {
         return false;
     }
     $pageType = (int) $pageType;
     $id = $this->doctrineHelper->getSingleEntityIdentifier($entity);
     $entityClass = $this->doctrineHelper->getEntityClass($entity);
     $activityListRepo = $this->doctrine->getRepository('OroActivityListBundle:ActivityList');
     return $this->isAllowedOnPage($entity, $pageType) && (in_array($entityClass, $this->activityListProvider->getTargetEntityClasses()) || (bool) $activityListRepo->getRecordsCountForTargetClassAndId($entityClass, $id));
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 protected function executeAction($context)
 {
     $email = $this->contextAccessor->getValue($context, $this->activityEntity);
     $targetEntity = $this->contextAccessor->getValue($context, $this->targetEntity);
     $activityList = $this->chainProvider->getActivityListEntitiesByActivityEntity($email);
     if ($activityList) {
         $this->entityManager->persist($activityList);
     }
     $result = $this->activityManager->addAssociation($email, $targetEntity);
     if ($this->attribute !== null) {
         $this->contextAccessor->setValue($context, $this->attribute, $result);
     }
 }
Esempio n. 10
0
 /**
  * @param array         $insertedEntities
  * @param EntityManager $entityManager
  * @return bool
  */
 public function processInsertEntities($insertedEntities, EntityManager $entityManager)
 {
     if (!empty($insertedEntities)) {
         foreach ($insertedEntities as $entity) {
             $activityList = $this->chainProvider->getActivityListEntitiesByActivityEntity($entity);
             if ($activityList) {
                 $entityManager->persist($activityList);
             }
         }
         return true;
     }
     return false;
 }
 /**
  * Gets the list of configs for entities which can be the target of the association
  *
  * @return ConfigInterface[]
  */
 protected function getTargetEntityConfigs()
 {
     if (null === $this->targetEntityConfigs) {
         $targetEntityClasses = $this->listProvider->getTargetEntityClasses(false);
         $this->targetEntityConfigs = [];
         $configs = $this->configManager->getProvider('extend')->getConfigs();
         foreach ($configs as $config) {
             if ($config->is('upgradeable') && in_array($config->getId()->getClassName(), $targetEntityClasses)) {
                 $this->targetEntityConfigs[] = $config;
             }
         }
     }
     return $this->targetEntityConfigs;
 }
Esempio n. 12
0
 public function testOnBuildMetadata()
 {
     $entity = Note::ENTITY_NAME;
     $alias = 'oro_bundle_notebundle_entity_note';
     $calls = 1;
     $config = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface');
     $this->configProvider->expects($this->any())->method('getConfig')->willReturn($config);
     $config->expects($this->exactly($calls))->method('get')->willReturn('label');
     $fieldMetadataOptions = ['display' => true, 'activity' => true, 'type' => $entity, 'field_name' => $alias, 'is_collection' => true, 'template' => 'OroActivityListBundle:Merge:value.html.twig', 'label' => 'Items', 'merge_modes' => [MergeModes::NOTES_UNITE, MergeModes::NOTES_REPLACE]];
     $fieldMetadata = new FieldMetadata($fieldMetadataOptions);
     $this->entityMetadata->expects($this->at(1))->method('addFieldMetadata')->with($this->equalTo($fieldMetadata));
     $this->activityListChainProvider->expects($this->exactly($calls))->method('isApplicableTarget')->willReturn(true);
     $event = new EntityMetadataEvent($this->entityMetadata);
     $this->listener->onBuildMetadata($event);
 }
 /**
  * @param EntityManager $em
  * @param array $data
  * @param string $entityIdField
  *
  * @return QueryBuilder
  */
 protected function createActivityQueryBuilder(EntityManager $em, array $data, $entityIdField)
 {
     $joinField = sprintf('%s.%s', $this->activityListAlias, ExtendHelper::buildAssociationName($data['entityClassName'], ActivityListEntityConfigDumperExtension::ASSOCIATION_KIND));
     $activityQb = $em->getRepository('OroActivityListBundle:ActivityList')->createQueryBuilder($this->activityListAlias)->select('1')->setMaxResults(1);
     $availableActivityAssociations = $this->activityManager->getActivityAssociations($data['entityClassName']);
     $availableClasses = array_map(function ($assoc) {
         return $assoc['className'];
     }, $availableActivityAssociations);
     $chosenActivities = $data['activityType']['value'];
     if (count($chosenActivities) === 1 && empty($chosenActivities[0])) {
         $chosenActivities = $this->activityListChainProvider->getSupportedActivities();
     }
     $chosenClasses = array_map(function ($className) {
         return $this->entityRoutingHelper->decodeClassName($className);
     }, $chosenActivities);
     $unavailableChoices = array_diff($chosenClasses, $availableClasses);
     if ($unavailableChoices) {
         $activityQb->andWhere('1 = 0');
         return $activityQb;
     }
     $activityQb->join($joinField, $this->activityAlias)->andWhere(sprintf('%s.id = %s.%s', $this->activityAlias, $this->getEntityAlias(), $entityIdField));
     $entityField = $this->getField();
     $dateRangeField = strpos($entityField, '$') === 0 ? substr($entityField, 1) : null;
     if ($dateRangeField) {
         $data['dateRange'] = $data['filter']['data'];
         unset($data['filter']);
     }
     $this->activityListFilterHelper->addFiltersToQuery($activityQb, $data, $dateRangeField, $this->activityListAlias);
     if (isset($data['filter'])) {
         $activityDs = new OrmFilterDatasourceAdapter($activityQb);
         $expr = $activityDs->expr()->exists($this->createRelatedActivityDql($activityDs, $data));
         $this->applyFilterToClause($activityDs, $expr);
     }
     return $activityQb;
 }
Esempio n. 14
0
 /**
  * @param EmailBodyAdded $event
  *
  * @throws \Exception
  */
 public function updateActivityDescription(EmailBodyAdded $event)
 {
     $this->entityManager->beginTransaction();
     try {
         $email = $event->getEmail();
         $activityList = $this->chainProvider->getUpdatedActivityList($email, $this->entityManager);
         if ($activityList) {
             $this->entityManager->persist($activityList);
             $this->entityManager->flush();
         }
         $this->entityManager->commit();
     } catch (\Exception $e) {
         $this->entityManager->rollback();
         throw $e;
     }
 }
Esempio n. 15
0
 /**
  * @param string $relatedEntityClass
  * @param mixed $relatedEntityId
  *
  * @return QueryBuilder
  */
 protected function createActivityListQb($relatedEntityClass, $relatedEntityId)
 {
     $activityListQb = $this->getActivityListRepository()->getBaseActivityListQueryBuilder($relatedEntityClass, $relatedEntityId);
     $activityListQb->andWhere('activity.relatedActivityId = e.id')->andWhere('activity.relatedActivityClass = :related_activity_class');
     $this->activityListAclHelper->applyAclCriteria($activityListQb, $this->activityListProvider->getProviders());
     return $activityListQb;
 }
Esempio n. 16
0
 public function setUp()
 {
     $this->activityListProvider = $this->getMockBuilder('Oro\\Bundle\\ActivityListBundle\\Provider\\ActivityListChainProvider')->disableOriginalConstructor()->getMock();
     $this->doctrine = $this->getMockBuilder('Doctrine\\Common\\Persistence\\ManagerRegistry')->disableOriginalConstructor()->getMock();
     $this->activityListProvider->expects($this->any())->method('getTargetEntityClasses')->will($this->returnValue(['Oro\\Bundle\\ActivityListBundle\\Tests\\Unit\\Placeholder\\Fixture\\TestTarget']));
     $this->doctrineHelper = $this->getMockBuilder('\\Oro\\Bundle\\EntityBundle\\ORM\\DoctrineHelper')->setMethods(['isNewEntity', 'isManageableEntity'])->setConstructorArgs([$this->doctrine])->getMock();
     $this->doctrineHelper->expects($this->any())->method('isNewEntity')->will($this->returnCallback(function ($entity) {
         if (method_exists($entity, 'getId')) {
             return !(bool) $entity->getId();
         }
         throw new \RuntimeException('Something wrong');
     }));
     $this->doctrineHelper->expects($this->any())->method('isManageableEntity')->willReturnCallback(function ($entity) {
         return !$entity instanceof TestNonManagedTarget;
     });
     $this->configureConfigProvider();
     $this->filter = new PlaceholderFilter($this->activityListProvider, $this->doctrine, $this->doctrineHelper, $this->configProvider);
 }
 /**
  * @param ActivityList $entity
  *
  * @return bool
  */
 protected function isDateUpdatable($entity)
 {
     $provider = $this->activityListChainProvider->getProviderByClass($entity->getRelatedActivityClass());
     if ($provider instanceof ActivityListDateProviderInterface) {
         return $provider->isDateUpdatable();
     } else {
         return true;
     }
 }
Esempio n. 18
0
 /**
  * @param ObjectManager             $manager
  * @param ActivityListChainProvider $provider
  * @param array                     $entities
  * @param string                    $ownerField
  * @param string                    $organizationField
  */
 protected function saveActivityLists(ObjectManager $manager, ActivityListChainProvider $provider, $entities, $ownerField = '', $organizationField = '')
 {
     $accessor = PropertyAccess::createPropertyAccessor();
     foreach ($entities as $entity) {
         if ($ownerField && $organizationField) {
             $owner = $accessor->getValue($entity, $ownerField);
             if ($owner instanceof User) {
                 $this->setSecurityContext($owner, $accessor->getValue($entity, $organizationField));
             }
         }
         $activityListEntity = $provider->getActivityListEntitiesByActivityEntity($entity);
         if ($activityListEntity) {
             $manager->persist($activityListEntity);
         }
     }
     $manager->flush();
     $manager->clear();
 }
Esempio n. 19
0
 /**
  * @param string $entityClass
  * @param int $entityId
  * @param array $filter
  *
  * @return QueryBuilder
  */
 protected function prepareQB($entityClass, $entityId, $filter)
 {
     $qb = $this->getBaseQB($entityClass, $entityId);
     $this->activityInheritanceTargetsHelper->applyInheritanceActivity($qb, $entityClass, $entityId);
     if ($this->config->get('oro_activity_list.grouping')) {
         $qb->andWhere($qb->expr()->andX('activity.head = true'));
     }
     $this->activityListFilterHelper->addFiltersToQuery($qb, $filter);
     $this->activityListAclHelper->applyAclCriteria($qb, $this->chainProvider->getProviders());
     return $qb;
 }
Esempio n. 20
0
 /**
  * Get Grouped Entities by Activity Entity
  *
  * @param object $entity
  * @param string $widgetId
  * @param array  $filterMetadata
  *
  * @return array
  */
 public function getGroupedEntities($entity, $targetActivityClass, $targetActivityId, $widgetId, $filterMetadata)
 {
     $results = [];
     $entityProvider = $this->chainProvider->getProviderForEntity(ClassUtils::getRealClass($entity));
     if ($this->isGroupingApplicable($entityProvider)) {
         $groupedActivities = $entityProvider->getGroupedEntities($entity);
         $activityResults = $this->getEntityViewModels($groupedActivities, ['class' => $targetActivityClass, 'id' => $targetActivityId]);
         $results = ['entityId' => $entity->getId(), 'ignoreHead' => true, 'widgetId' => $widgetId, 'activityListData' => json_encode(['count' => count($activityResults), 'data' => $activityResults]), 'commentOptions' => ['listTemplate' => '#template-activity-item-comment', 'canCreate' => true], 'activityListOptions' => ['configuration' => $this->chainProvider->getActivityListOption($this->config), 'template' => '#template-activity-list', 'itemTemplate' => '#template-activity-item', 'urls' => [], 'loadingContainerSelector' => '.activity-list.sub-list', 'dateRangeFilterMetadata' => $filterMetadata, 'routes' => [], 'pager' => false]];
     }
     return $results;
 }
Esempio n. 21
0
 public function testExecuteActionWithoutAttribute()
 {
     $options = ['email' => '$.email', 'target_entity' => '$.target_entity'];
     $fakeContext = ['fake', 'things', 'are', 'here'];
     $this->contextAccessor->expects($this->at(0))->method('getValue')->with($this->equalTo($fakeContext), $this->equalTo('$.email'))->will($this->returnValue($email = new Email()));
     $this->contextAccessor->expects($this->at(1))->method('getValue')->with($this->equalTo($fakeContext), $this->equalTo('$.target_entity'))->will($this->returnValue($target = new User()));
     $this->contextAccessor->expects($this->never())->method('setValue');
     $this->activityListChainProvider->expects($this->any())->method('getActivityListEntitiesByActivityEntity')->with($this->equalTo($email))->will($this->returnValue($list = new ActivityList()));
     $this->entityManager->expects($this->once())->method('persist')->with($this->equalTo($list));
     $this->emailActivityManager->expects($this->once())->method('addAssociation')->with($this->equalTo($email), $this->equalTo($target))->will($this->returnValue(true));
     $this->action->initialize($options);
     $this->action->execute($fakeContext);
 }
Esempio n. 22
0
 /**
  * Fill Activity list owners from activity entity
  *
  * @param array $entities
  * @param EntityManager $entityManager
  *
  * @return bool
  */
 public function processFillOwners($entities, EntityManager $entityManager)
 {
     if ($entities) {
         foreach ($entities as $entity) {
             $activityProvider = $this->chainProvider->getProviderForOwnerEntity($entity);
             $activityList = $this->chainProvider->getActivityListByEntity($entity, $entityManager);
             if ($activityList) {
                 $this->fillOwners($activityProvider, $entity, $activityList);
             }
         }
         return true;
     }
     return false;
 }
 /**
  * @param QueryBuilder $qb
  * @param array        $filterData
  * @param string       $rangeField
  * @param string       $activityListAlias
  */
 public function addFiltersToQuery(QueryBuilder $qb, $filterData, $rangeField = 'updatedAt', $activityListAlias = 'activity')
 {
     $dataSourceAdapter = new OrmFilterDatasourceAdapter($qb);
     if (isset($filterData['dateRange'])) {
         $this->dateTimeRangeFilter->init($rangeField, ['data_name' => sprintf('%s.%s', $activityListAlias, $rangeField)]);
         $datetimeForm = $this->dateTimeRangeFilter->getForm();
         if (!$datetimeForm->isSubmitted()) {
             $datetimeForm->submit($filterData['dateRange']);
         }
         $this->dateTimeRangeFilter->apply($dataSourceAdapter, $datetimeForm->getData());
     }
     if (isset($filterData['activityType'])) {
         $routingHelper = $this->routingHelper;
         $filterData['activityType']['value'] = array_map(function ($activityClass) use($routingHelper) {
             return $routingHelper->resolveEntityClass($activityClass);
         }, $filterData['activityType']['value']);
         $this->choiceFilter->init('relatedActivityClass', ['data_name' => sprintf('%s.relatedActivityClass', $activityListAlias), 'options' => ['field_options' => ['multiple' => true, 'choices' => array_flip($this->chainProvider->getSupportedActivities())]]]);
         $typeForm = $this->choiceFilter->getForm();
         if (!$typeForm->isSubmitted()) {
             $typeForm->submit($filterData['activityType']);
         }
         $this->choiceFilter->apply($dataSourceAdapter, $typeForm->getData());
     }
 }
Esempio n. 24
0
 public function testIsApplicable()
 {
     $repo = $this->getMockBuilder('Oro\\Bundle\\ActivityListBundle\\Entity\\Repository\\ActivityListRepository')->disableOriginalConstructor()->getMock();
     $this->doctrine->expects($this->any())->method('getRepository')->will($this->returnValue($repo));
     $repo->expects($this->any())->method('getRecordsCountForTargetClassAndId')->with('Oro\\Bundle\\ActivityListBundle\\Tests\\Unit\\Placeholder\\Fixture\\TestNonActiveTarget', 123)->willReturn(10);
     $entity = new TestNonActiveTarget(123);
     $entityClass = get_class($entity);
     $activityClass = 'Test\\Activity';
     $config = new Config(new EntityConfigId('activity', $entityClass));
     $config->set(ActivityScope::SHOW_ON_PAGE, '\\Oro\\Bundle\\ActivityBundle\\EntityConfig\\ActivityScope::VIEW_PAGE');
     $config->set('activities', [$activityClass]);
     $this->configManager->expects($this->once())->method('hasConfig')->with($entityClass)->willReturn(true);
     $this->configManager->expects($this->once())->method('getEntityConfig')->with('activity', $entityClass)->willReturn($config);
     $this->activityListProvider->expects($this->once())->method('getSupportedActivities')->willReturn([$activityClass]);
     $this->activityListProvider->expects($this->exactly(1))->method('isApplicableTarget')->with($entityClass, $activityClass)->willReturn(true);
     $this->assertTrue($this->filter->isApplicable($entity, ActivityScope::VIEW_PAGE));
 }
 public function testGetUpdatedActivityList()
 {
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $repo = $this->getMockBuilder('Oro\\Bundle\\ActivityListBundle\\Entity\\Repository\\ActivityListRepository')->disableOriginalConstructor()->getMock();
     $activityEntity = new ActivityList();
     $repo->expects($this->once())->method('findOneBy')->willReturn($activityEntity);
     $em->expects($this->once())->method('getRepository')->willReturn($repo);
     $testEntity = new \stdClass();
     $testEntity->subject = 'testSubject';
     $testEntity->description = 'testDescription';
     $this->testActivityProvider->setTargets([new \stdClass()]);
     $this->doctrineHelper->expects($this->any())->method('getEntityClass')->willReturnCallback(function ($entity) use($testEntity) {
         if ($entity === $testEntity) {
             return 'Test\\Entity';
         }
         return get_class($entity);
     });
     $result = $this->provider->getUpdatedActivityList($testEntity, $em);
     $this->assertEquals('update', $result->getVerb());
     $this->assertEquals('testSubject', $result->getSubject());
 }
 public function testGetProviderByOwnerClass()
 {
     $this->assertEquals($this->testActivityProvider, $this->provider->getProviderByOwnerClass(TestActivityProvider::ACTIVITY_CLASS_NAME));
 }
Esempio n. 27
0
 /**
  * Get ActivityOwner entity by ActivityList entity
  *
  * @param ActivityList              $entity
  * @param ActivityListChainProvider $provider
  *
  * @return ActivityOwner[]
  */
 protected function prepareActivityOwnerData($entity, $provider)
 {
     /** @var  DoctrineHelper $helper */
     $helper = $this->container->get('oro_entity.doctrine_helper');
     $relatedActivityEntity = $helper->getEntity($entity->getRelatedActivityClass(), $entity->getRelatedActivityId());
     $entityProvider = $provider->getProviderForEntity($entity->getRelatedActivityClass());
     $activityOwners = $entityProvider->getActivityOwners($relatedActivityEntity, $entity);
     return $activityOwners;
 }