/** * @param $pattern * @param $searchProperties * @param $options * @return array|false */ 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 \t\t\tSELECT \t\t\t\tDISTINCT \t\t\t\t`{$propTable}`.`contactid`, \t\t\t\t`{$contTable}`.`addressbookid`, \t\t\t\t`{$addrTable}`.`userid` \t\t\tFROM \t\t\t\t`{$propTable}` \t\t\tINNER JOIN \t\t\t\t`{$contTable}` \t\t\tON `{$contTable}`.`id` = `{$propTable}`.`contactid` \t\t\t\tINNER JOIN `{$addrTable}` \t\t\tON `{$addrTable}`.id = `{$contTable}`.addressbookid \t\t\tWHERE \t\t\t\t`{$contTable}`.addressbookid = ? AND \t\t\t\t( SQL; $params = array(); $meta = $this->addressBook->getMetaData(); $params[] = $meta['id']; foreach ($searchProperties as $property) { $params[] = $property; $params[] = '%' . $pattern . '%'; $query .= '(`name` = ? AND `value` LIKE ?) OR '; } $query = substr($query, 0, strlen($query) - 4); $query .= ')'; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute($params); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } 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 { // 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' => $addressbookKey, 'contactId' => $contact->getId())); $url = \OC_Helper::makeURLAbsolute($url); $j['data']['PHOTO'] = "VALUE=uri:{$url}"; } $results[] = $this->convertToSearchResult($j); } } return $results; }
/** * @return string defining the technical unique key */ public function getKey() { $metaData = $this->addressBook->getMetaData(); return $metaData['backend'] . ':' . $metaData['id']; }