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