protected function prepareTableData($matchHash, Duplicate $item, array &$sortParams, $enablePrimaryKey = true)
 {
     $data = array('ROOT_ENTITY_ID' => 0, 'ROOT_ENTITY_NAME' => '', 'ROOT_ENTITY_TITLE' => '', 'ROOT_ENTITY_PHONE' => '', 'ROOT_ENTITY_EMAIL' => '', 'QUANTITY' => 0);
     if ($enablePrimaryKey) {
         $data['USER_ID'] = $this->getUserID();
         $data['ENTITY_TYPE_ID'] = $this->getEntityTypeID();
         $data['TYPE_ID'] = $this->typeID;
         $data['MATCH_HASH'] = $matchHash;
         $criterion = $item->getCriterion();
         $data['MATCHES'] = serialize($criterion->getMatches());
     }
     $entityID = $item->getRootEntityID();
     if ($entityID > 0) {
         $data['ROOT_ENTITY_ID'] = $entityID;
         $pers = isset($sortParams['PERS']) ? $sortParams['PERS'] : null;
         if (is_array($pers) && isset($pers[$entityID]) && isset($pers[$entityID]['FULL_NAME'])) {
             $data['ROOT_ENTITY_NAME'] = $pers[$entityID]['FULL_NAME'];
         }
         $org = isset($sortParams['ORG']) ? $sortParams['ORG'] : null;
         if (is_array($org) && isset($org[$entityID]) && isset($org[$entityID]['TITLE'])) {
             $data['ROOT_ENTITY_TITLE'] = $org[$entityID]['TITLE'];
         }
         $comm = isset($sortParams['COMM']) ? $sortParams['COMM'] : null;
         if (is_array($comm) && isset($comm[$entityID])) {
             if (isset($comm[$entityID]['PHONE'])) {
                 $data['ROOT_ENTITY_PHONE'] = $comm[$entityID]['PHONE'];
             }
             if (isset($comm[$entityID]['EMAIL'])) {
                 $data['ROOT_ENTITY_EMAIL'] = $comm[$entityID]['EMAIL'];
             }
         }
     }
     return $data;
 }
 protected function onAfterDuplicateCreated(Duplicate $dup, $entityTypeID, $userID, $enablePermissionCheck, $enableRanking, array &$rankings)
 {
     $name = $this->name;
     $lastName = $this->lastName;
     $secondName = $this->secondName;
     $rootEntityID = $dup->getRootEntityID();
     if ($secondName === '' && $name === '') {
         return;
     }
     $permissionSql = '';
     if ($enablePermissionCheck) {
         $permissions = isset($params['PERMISSIONS']) ? $params['PERMISSIONS'] : null;
         if ($permissions === null) {
             $permissions = \CCrmPerms::GetUserPermissions($userID);
         }
         $permissionSql = \CCrmPerms::BuildSql(\CCrmOwnerType::ResolveName($entityTypeID), '', 'READ', array('RAW_QUERY' => true, 'PERMS' => $permissions));
         if ($permissionSql === false) {
             //Access denied;
             return;
         }
     }
     if ($secondName !== '') {
         $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity());
         $query->addSelect('ENTITY_ID');
         $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
         $query->addFilter('=LAST_NAME', $lastName);
         $query->addFilter('=NAME', $name);
         $query->addFilter('=SECOND_NAME', '');
         if ($rootEntityID) {
             $query->addFilter('!ENTITY_ID', $rootEntityID);
             $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)), $entityTypeID, $rootEntityID, $userID));
         }
         if ($enablePermissionCheck && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
         $dbResult = $query->exec();
         while ($fields = $dbResult->fetch()) {
             $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0;
             if ($entityID <= 0) {
                 continue;
             }
             $entity = new DuplicateEntity($entityTypeID, $entityID);
             $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName, 'NAME' => $name)));
             if ($enableRanking) {
                 $rankings[] = $entity->getRanking();
             }
             $dup->addEntity($entity);
         }
     }
     if ($name !== '') {
         $query = new Main\Entity\Query(DuplicatePersonMatchCodeTable::getEntity());
         $query->addSelect('ENTITY_ID');
         $query->addFilter('=ENTITY_TYPE_ID', $entityTypeID);
         $query->addFilter('=LAST_NAME', $lastName);
         $query->addFilter('=NAME', '');
         $query->addFilter('=SECOND_NAME', '');
         if ($rootEntityID) {
             $query->addFilter('!ENTITY_ID', $rootEntityID);
             $query->addFilter('!@ENTITY_ID', DuplicateIndexMismatch::prepareQueryField(self::createFromMatches(array('LAST_NAME' => $lastName)), $entityTypeID, $rootEntityID, $userID));
         }
         if ($enablePermissionCheck && $permissionSql !== '') {
             $query->addFilter('@ENTITY_ID', new Main\DB\SqlExpression($permissionSql));
         }
         $dbResult = $query->exec();
         while ($fields = $dbResult->fetch()) {
             $entityID = isset($fields['ENTITY_ID']) ? intval($fields['ENTITY_ID']) : 0;
             if ($entityID <= 0) {
                 continue;
             }
             $entity = new DuplicateEntity($entityTypeID, $entityID);
             $entity->setCriterion(self::createFromMatches(array('LAST_NAME' => $lastName)));
             if ($enableRanking) {
                 $rankings[] = $entity->getRanking();
             }
             $dup->addEntity($entity);
         }
     }
 }