diff --git a/appinfo/app.php b/appinfo/app.php index f3cd2319..9738d5ae 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -55,7 +55,7 @@ $api->connectHook('OC_Calendar', 'getSources', 'OCA\Contacts\Hooks', 'getCalende \OCP\Util::addscript('contacts', 'loader'); \OCP\Util::addscript('contacts', 'admin'); -\OC_Search::registerProvider('OCA\Contacts\SearchProvider'); +\OC_Search::registerProvider('OCA\Contacts\Search\Provider'); //\OCP\Share::registerBackend('contact', 'OCA\Contacts\Share_Backend_Contact'); \OCP\Share::registerBackend('addressbook', 'OCA\Contacts\Share\Addressbook', 'contact'); //\OCP\App::registerPersonal('contacts','personalsettings'); diff --git a/lib/search/contact.php b/lib/search/contact.php new file mode 100644 index 00000000..ae0113ae --- /dev/null +++ b/lib/search/contact.php @@ -0,0 +1,116 @@ +. + * + */ + +namespace OCA\Contacts\Search; + +/** + * A contact search result + */ +class Contact extends \OC\Search\Result { + + /** + * Type name; translated in templates + * + * @var string + */ + public $type = 'contact'; + + /** + * Contact address + * + * @var string + */ + public $address; + + /** + * Contact phone numbers + * + * @var string + */ + public $phone; + + /** + * Contact e-mail + * + * @var string + */ + public $email; + + /** + * Contact nickname + * + * @var string + */ + public $nickname; + + /** + * Contact organization + * + * @var string + */ + public $organization; + + /** + * Constructor + * + * @param array $data + * @return \OCA\Calendar\Search\Contact + */ + public function __construct(array $data = null) { + $this->id = $data['id']; + $this->name = stripcslashes($data['FN']); + $this->link = \OCP\Util::linkToRoute('contacts_index') . '#' . $data['id']; + $this->address = $this->checkAndMerge($data, 'ADR'); + $this->phone = $this->checkAndMerge($data, 'TEL'); + $this->email = $this->checkAndMerge($data, 'EMAIL'); + $this->nickname = $this->checkAndMerge($data, 'NICKNAME'); + $this->organization = $this->checkAndMerge($data, 'ORG'); + } + + /** + * Check a contact property and return its value; handles properties with + * multiple values by merging them into a comma-separated list + * + * @param array $data + * @param string $property + * @return string or null + */ + private function checkAndMerge($data, $property) { + // check property + if (!is_array($data) || !array_key_exists($property, $data)) { + return null; + } + // check value + if (!is_array($data[$property])) { + return stripcslashes($data[$property]); + } + // or merge array + if (count($data[$property]) > 0) { + $list = array(); + foreach ($data[$property] as $value) { + $list[] = stripcslashes($value); + } + return implode(', ', $list); + } + // default + return null; + } + +} \ No newline at end of file diff --git a/lib/search/provider.php b/lib/search/provider.php new file mode 100644 index 00000000..2a5d398a --- /dev/null +++ b/lib/search/provider.php @@ -0,0 +1,42 @@ +. + * + */ + +namespace OCA\Contacts\Search; + +/** + * The updated contacts search provider + */ +class Provider extends \OC\Search\Provider { + + /** + * Search for contacts + * + * @param string $query + * @return array list of \OCA\Calendar\Search\Contact + */ + function search($query) { + $_results = \OCP\Contacts::search($query, array('N', 'FN', 'EMAIL', 'NICKNAME', 'ORG')); + $results = array(); + foreach ($_results as $_result) { + $results[] = new \OCA\Contacts\Search\Contact($_result); + } + return $results; + } +}