1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-10 21:24:33 +01:00

Update search classes to with the new search system

Add a Contact search result type to distinguish these results within a search view.

reformat code
This commit is contained in:
Andrew Brown 2013-12-14 19:04:02 -05:00 committed by Jörn Friedrich Dreyer
parent 25571ee31a
commit 30152ecbf6
3 changed files with 159 additions and 1 deletions

View File

@ -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');

116
lib/search/contact.php Normal file
View File

@ -0,0 +1,116 @@
<?php
/**
* ownCloud
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
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;
}
}

42
lib/search/provider.php Normal file
View File

@ -0,0 +1,42 @@
<?php
/**
* ownCloud
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
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;
}
}