public function calculateEntityCount(DuplicateCriterion $criterion, array $options = null)
 {
     $entityTypeID = $this->getEntityTypeID();
     $enablePermissionCheck = $this->isPermissionCheckEnabled();
     $userID = $this->getUserID();
     $query = new Main\Entity\Query(DuplicateCommunicationMatchCodeTable::getEntity());
     $query->addSelect('QTY');
     $query->registerRuntimeField('', new Main\Entity\ExpressionField('QTY', 'COUNT(*)'));
     $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
     if ($enablePermissionCheck) {
         $permissionSql = $this->preparePermissionSql();
         if ($permissionSql === false) {
             //Access denied;
             return 0;
         }
         if (is_string($permissionSql) && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
     }
     $matches = $criterion->getMatches();
     $type = isset($matches['TYPE']) ? $matches['TYPE'] : '';
     if ($type === '') {
         throw new Main\ArgumentException("Parameter 'TYPE' is required.", 'matches');
     }
     $value = isset($matches['VALUE']) ? $matches['VALUE'] : '';
     if ($type === '') {
         throw new Main\ArgumentException("Parameter 'VALUE' is required.", 'matches');
     }
     $query->addFilter('=TYPE', $type);
     $query->addFilter('=VALUE', $value);
     $rootEntityID = 0;
     if (is_array($options) && isset($options['ROOT_ENTITY_ID'])) {
         $rootEntityID = (int) $options['ROOT_ENTITY_ID'];
     }
     if ($rootEntityID > 0) {
         $query->addFilter('!ENTITY_ID', $rootEntityID);
         $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField($criterion, $entityTypeID, $rootEntityID, $userID));
     }
     $limit = 0;
     if (is_array($options) && isset($options['LIMIT'])) {
         $limit = (int) $options['LIMIT'];
     }
     if ($limit > 0) {
         $query->setLimit($limit);
     }
     $dbResult = $query->exec();
     $fields = $dbResult->fetch();
     return is_array($fields) && isset($fields['QTY']) ? intval($fields['QTY']) : 0;
 }
Beispiel #2
0
 public function merge($seedID, $targID, Integrity\DuplicateCriterion $targCriterion)
 {
     if (!is_int($seedID)) {
         $seedID = (int) $seedID;
     }
     if (!is_int($targID)) {
         $targID = (int) $targID;
     }
     $entityTypeID = $this->entityTypeID;
     if ($this->enablePermissionCheck && !$this->userIsAdmin) {
         $userPermissions = $this->getUserPermissions();
         if (!$this->checkEntityReadPermission($seedID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::READ_DENIED);
         }
         if (!$this->checkEntityDeletePermission($seedID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $seedID, self::ROLE_SEED, EntityMergerException::DELETE_DENIED);
         }
         if (!$this->checkEntityReadPermission($targID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::READ_DENIED);
         }
         if (!$this->checkEntityUpdatePermission($targID, $userPermissions)) {
             throw new EntityMergerException($entityTypeID, $targID, self::ROLE_TARG, EntityMergerException::UPDATE_DENIED);
         }
     }
     $collisions = self::getMergeCollisions($seedID, $targID);
     $seed = $this->getEntityFields($seedID, self::ROLE_SEED);
     $targ = $this->getEntityFields($targID, self::ROLE_TARG);
     $entityFieldInfos = $this->getEntityFieldsInfo();
     $userFieldInfos = $this->getEntityUserFieldsInfo();
     EntityMerger::mergeEntityFields($seed, $targ, $entityFieldInfos);
     EntityMerger::mergeUserFields($seed, $targ, $userFieldInfos);
     $seedMultiFields = $this->getEntityMultiFields($seedID, self::ROLE_SEED);
     $targMultiFields = $this->getEntityMultiFields($targID, self::ROLE_TARG);
     EntityMerger::mergeMultiFields($seedMultiFields, $targMultiFields);
     if (!empty($targMultiFields)) {
         $targ['FM'] = $targMultiFields;
     }
     //$recoveryData = self::prepareRecoveryData($seed, $entityFieldInfos, $userFieldInfos);
     //$recoveryData->setEntityTypeID($entityTypeID);
     //$recoveryData->setEntityID($seedID);
     //$this->setupRecoveryData($recoveryData, $seed);
     //if(!empty($seedMultiFields))
     //{
     //	$recoveryData->setDataItem('MULTI_FIELDS', $seedMultiFields);
     //}
     //$activityIDs = \CCrmActivity::GetBoundIDs($entityTypeID, $seedID);
     //if(!empty($activityIDs))
     //{
     //	$recoveryData->setDataItem('ACTIVITY_IDS', $activityIDs);
     //}
     //$eventIDs = array();
     //$result = \CCrmEvent::GetListEx(
     //	array('EVENT_REL_ID' => 'ASC'),
     //	array(
     //		'ENTITY_TYPE' => $entityTypeName,
     //		'ENTITY_ID' => $seedID,
     //		'EVENT_TYPE' => 0,
     //		'CHECK_PERMISSIONS' => 'N'
     //	),
     //	false,
     //	false,
     //	array('EVENT_REL_ID')
     //);
     //if(is_object($result))
     //{
     //	while($eventFields = $result->Fetch())
     //	{
     //		$eventIDs[] = (int)$eventFields['EVENT_REL_ID'];
     //	}
     //}
     //if(!empty($eventIDs))
     //{
     //	$recoveryData->setDataItem('EVENT_IDS', $eventIDs);
     //}
     //$recoveryData->setUserID($this->userID);
     //$recoveryData->save();
     $this->updateEntity($targID, $targ, self::ROLE_TARG);
     $this->rebind($seedID, $targID);
     $matches = $this->getRegisteredEntityMatches($entityTypeID, $seedID);
     $targIndexTypeID = $targCriterion->getIndexTypeID();
     if (!isset($matches[$targIndexTypeID])) {
         $matches[$targIndexTypeID] = array();
     }
     $targetMatchHash = $targCriterion->getMatchHash();
     if (!isset($matches[$targIndexTypeID][$targetMatchHash])) {
         $matches[$targIndexTypeID][$targetMatchHash] = $targCriterion->getMatches();
     }
     $this->deleteEntity($seedID, self::ROLE_SEED, array('ENABLE_DUP_INDEX_INVALIDATION' => false));
     if (!empty($matches)) {
         $this->processEntityDeletion($entityTypeID, $seedID, $matches);
     }
     Integrity\DuplicateIndexBuilder::markAsJunk($entityTypeID, $seedID);
     if (!empty($collisions)) {
         $messageFields = $this->prepareCollisionMessageFields($collisions, $seed, $targ);
         if (is_array($messageFields) && !empty($messageFields) && Main\Loader::includeModule('im')) {
             $messageFields['FROM_USER_ID'] = $this->userID;
             $messageFields['MESSAGE_TYPE'] = IM_MESSAGE_SYSTEM;
             $messageFields['NOTIFY_TYPE'] = IM_NOTIFY_FROM;
             $messageFields['NOTIFY_MODULE'] = 'crm';
             $messageFields['NOTIFY_EVENT'] = 'merge';
             $messageFields['NOTIFY_TAG'] = 'CRM|MERGE|COLLISION';
             \CIMNotify::Add($messageFields);
         }
     }
 }