Skip to content

Commit

Permalink
fix(addressbook): Fix exception when using multiple address book sour…
Browse files Browse the repository at this point in the history
…ce and searching. Fixes #5906.
  • Loading branch information
WoodySlum committed Dec 12, 2023
1 parent 3cd8608 commit f85c531
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
74 changes: 38 additions & 36 deletions UI/Contacts/UIxContactsListActions.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ - (void) saveSortValue

- (NSArray *) contactInfos
{
id <SOGoContactFolder> folder;
id <SOGoContactFolder> folder, sourceFolder;
NSString *ascending, *valueText;
NSArray *searchFields, *fields, *folders, *tmpGlobalAddressBookResults;
NSMutableArray *filteredContacts, *headers, *results, *globalAddressBookResults;
Expand Down Expand Up @@ -170,51 +170,53 @@ - (NSArray *) contactInfos

[contactInfos release];

folders = [[[self clientObject] container] subFolders];

sourceFolder = [self clientObject];
globalAddressBookResults = nil;
for (folder in folders) {
// Global AB
if ([folder isKindOfClass: [SOGoContactSourceFolder class]] && (![folder isEqual: [self clientObject]])) {
tmpGlobalAddressBookResults = [folder lookupContactsWithFilter: valueText
onCriteria: nil
sortBy: [self sortKey]
ordering: ordering
inDomain: [[context activeUser] domain]];
if (globalAddressBookResults) {
globalAddressBookResults = [globalAddressBookResults arrayByAddingObjectsFromArray: tmpGlobalAddressBookResults];
} else {
globalAddressBookResults = [NSMutableArray arrayWithArray: tmpGlobalAddressBookResults];
if (![sourceFolder isKindOfClass: [SOGoContactSourceFolder class]]) {
folders = [[[self clientObject] container] subFolders];

for (folder in folders) {
// Global AB
if ([folder isKindOfClass: [SOGoContactSourceFolder class]] && (![folder isEqual: [self clientObject]])) {
tmpGlobalAddressBookResults = [folder lookupContactsWithFilter: valueText
onCriteria: nil
sortBy: [self sortKey]
ordering: ordering
inDomain: [[context activeUser] domain]];
if (globalAddressBookResults) {
globalAddressBookResults = [NSMutableArray arrayWithArray: [globalAddressBookResults arrayByAddingObjectsFromArray: tmpGlobalAddressBookResults]];
} else {
globalAddressBookResults = [NSMutableArray arrayWithArray: tmpGlobalAddressBookResults];
}
}
}
}

// Process for global address book instead of array
if (globalAddressBookResults) {
for (i = 0 ; i < [globalAddressBookResults count] ; i++) {
tmpDict = [NSMutableDictionary dictionaryWithDictionary: [globalAddressBookResults objectAtIndex: i]];
// Flatten emails
if ([tmpDict objectForKey: @"c_mail"] && [[tmpDict objectForKey: @"c_mail"] isKindOfClass:[NSArray class]] && [[tmpDict objectForKey: @"c_mail"] count] > 0) {
[tmpDict setObject:[[tmpDict objectForKey: @"c_mail"] componentsJoinedByString: @","] forKey:@"c_mail"];
}
// Process for global address book instead of array
if (globalAddressBookResults) {
for (i = 0 ; i < [globalAddressBookResults count] ; i++) {
tmpDict = [NSMutableDictionary dictionaryWithDictionary: [globalAddressBookResults objectAtIndex: i]];
// Flatten emails
if ([tmpDict objectForKey: @"c_mail"] && [[tmpDict objectForKey: @"c_mail"] isKindOfClass:[NSArray class]] && [[tmpDict objectForKey: @"c_mail"] count] > 0) {
[tmpDict setObject:[[tmpDict objectForKey: @"c_mail"] componentsJoinedByString: @","] forKey:@"c_mail"];
}

if ((![tmpDict objectForKey:@"c_cn"] || [tmpDict objectForKey:@"c_cn"] == [NSNull null]) && [tmpDict objectForKey:@"c_name"]) {
// Replace c_cn if not filled
[tmpDict setObject:[tmpDict objectForKey:@"c_name"] forKey:@"c_cn"];
}
if ((![tmpDict objectForKey:@"c_cn"] || [tmpDict objectForKey:@"c_cn"] == [NSNull null]) && [tmpDict objectForKey:@"c_name"]) {
// Replace c_cn if not filled
[tmpDict setObject:[tmpDict objectForKey:@"c_name"] forKey:@"c_cn"];
}

if ((![tmpDict objectForKey:@"c_uid"] || [tmpDict objectForKey:@"c_uid"] == [NSNull null]) && [tmpDict objectForKey:@"c_id"]) {
// Replace c_uid if not filled
[tmpDict setObject:[tmpDict objectForKey:@"c_uid"] forKey:@"c_id"];
}
if ((![tmpDict objectForKey:@"c_uid"] || [tmpDict objectForKey:@"c_uid"] == [NSNull null]) && [tmpDict objectForKey:@"c_id"]) {
// Replace c_uid if not filled
[tmpDict setObject:[tmpDict objectForKey:@"c_uid"] forKey:@"c_id"];
}

[globalAddressBookResults replaceObjectAtIndex:i withObject: tmpDict];
[globalAddressBookResults replaceObjectAtIndex:i withObject: tmpDict];
}
}
}

// Current AB
folder = [self clientObject];
results = [NSMutableArray arrayWithArray: [folder lookupContactsWithFilter: valueText
results = [NSMutableArray arrayWithArray: [sourceFolder lookupContactsWithFilter: valueText
onCriteria: searchFields
sortBy: [self sortKey]
ordering: ordering
Expand All @@ -224,7 +226,7 @@ - (NSArray *) contactInfos
for (i = 0 ; i < [results count] ; i++) {
tmpDict = [NSMutableDictionary dictionaryWithDictionary: [results objectAtIndex: i]];
// Add sourceid
[tmpDict setObject:[folder nameInContainer] forKey:@"sourceid"];
[tmpDict setObject:[sourceFolder nameInContainer] forKey:@"sourceid"];
[results replaceObjectAtIndex:i withObject: tmpDict];
}

Expand Down
2 changes: 1 addition & 1 deletion UI/WebServerResources/js/Contacts/AddressBook.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
// Respect the order of the results
_.forEach(results, function(cardId, index) {
var oldIndex, removedCards;
if (cards[index].id != cardId) {
if (!_.isUndefined(cards[index]) && cards[index].id != cardId) {
oldIndex = _.findIndex(cards, _.bind(compareIds, cardId));
removedCards = cards.splice(oldIndex, 1);
cards.splice(index, 0, removedCards[0]);
Expand Down

0 comments on commit f85c531

Please sign in to comment.