1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

also search for shared contacts

This commit is contained in:
LEDfan 2014-11-21 13:03:15 +01:00
parent b6cdd06db2
commit 579819bee8

View File

@ -34,6 +34,7 @@ class AddressbookProvider implements \OCP\IAddressBook {
const CONTACT_TABLE = '*PREFIX*contacts_cards';
const PROPERTY_TABLE = '*PREFIX*contacts_cards_properties';
const ADDRESSBOOK_TABLE = '*PREFIX*contacts_addressbooks';
/**
* Addressbook id
@ -92,23 +93,32 @@ class AddressbookProvider implements \OCP\IAddressBook {
public function search($pattern, $searchProperties, $options) {
$propTable = self::PROPERTY_TABLE;
$contTable = self::CONTACT_TABLE;
$addrTable = self::ADDRESSBOOK_TABLE;
$results = array();
/**
* This query will fetch all contacts which match the $searchProperties
* It will look up the addressbookid of the contact and the user id of the owner of the contact app
*/
$query = <<<SQL
SELECT
DISTINCT
`$propTable`.`contactid`,
`$contTable`.`addressbookid`
`$contTable`.`addressbookid`,
`$addrTable`.`userid`
FROM
`$propTable`
INNER JOIN
`$contTable`
ON `$contTable`.`id` = `$propTable`.`contactid`
INNER JOIN `$addrTable`
ON `$addrTable`.id = `$contTable`.addressbookid
WHERE
`$propTable`.`userid` = ?
AND (
(
SQL;
$params = array(\OCP\User::getUser());
$params = array();
foreach ($searchProperties as $property) {
$params[] = $property;
$params[] = '%' . $pattern . '%';
@ -127,22 +137,31 @@ SQL;
while ($row = $result->fetchRow()) {
$id = $row['contactid'];
$addressbookKey = $row['addressbookid'];
// Check if we are the owner of the contact
if ($row['userid'] !== \OCP\User::getUser()) {
// we aren't the owner of the contact
try {
// gues that it is a local addressbook
$contact = $this->app->getContact('local', $addressbookKey, $id);
} catch (\Exception $e) {
if ($e->getCode() === 404) {
// not a local thus it is a shared
// it is possible that the contact is shared with us
// if so, $contact will be an object
// if not getContact will throw an Exception
$contact = $this->app->getContact('shared', $addressbookKey, $id);
} catch (\Exception $e){
// the contact isn't shared with us
$contact = null;
}
} else {
// We are the owner of the contact
// thus we can easily fetch it
$contact = $this->app->getContact('local', $addressbookKey, $id);
}
if ($contact !== null) {
$j = JSONSerializer::serializeContact($contact);
$j['data']['id'] = $id;
if (isset($contact->PHOTO)) {
$url = \OCP\Util::linkToRoute('contacts_contact_photo',
array(
'backend' => $contact->getBackend()->name,
'addressBookId' => $this->addressBook->getId(),
'addressBookId' => $addressbookKey,
'contactId' => $contact->getId()
));
$url = \OC_Helper::makeURLAbsolute($url);
@ -150,6 +169,7 @@ SQL;
}
$results[] = $this->convertToSearchResult($j);
}
}
return $results;
}