mirror of
https://github.com/owncloudarchive/contacts.git
synced 2025-01-18 07:52:21 +01:00
Add searc() func tion
This commit is contained in:
parent
90a090af98
commit
2835402b57
@ -2,8 +2,15 @@
|
||||
|
||||
namespace OCA\Contacts;
|
||||
|
||||
use OCA\Contacts\Utils\Properties;
|
||||
|
||||
|
||||
class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
|
||||
|
||||
private $indexTableName = '*PREFIX*contacts_ocu_cards_properties';
|
||||
private $contactTableName = '*PREFIX*contacts_ocu_cards';
|
||||
|
||||
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
@ -15,39 +22,45 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
|
||||
* @return array|false
|
||||
*/
|
||||
public function search($pattern, $searchProperties, $options) {
|
||||
if($pattern !== ''){
|
||||
if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){
|
||||
$query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) ';
|
||||
$stmt = \OCP\DB::prepare($query);
|
||||
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%"));
|
||||
} elseif(in_array("FN", $searchProperties)){
|
||||
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? ';
|
||||
$stmt = \OCP\DB::prepare($query);
|
||||
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%"));
|
||||
} elseif(in_array("id", $searchProperties)){
|
||||
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? ';
|
||||
$stmt = \OCP\DB::prepare($query);
|
||||
$result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%"));
|
||||
}
|
||||
} else {
|
||||
$query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?';
|
||||
$stmt = \OCP\DB::prepare($query);
|
||||
$result = $stmt->execute(array(\OCP\User::getUser()));
|
||||
$ids = array();
|
||||
$results = array();
|
||||
$query = 'SELECT DISTINCT `contactid` FROM `' . $this->indexTableName . '` WHERE (';
|
||||
$params = array();
|
||||
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;
|
||||
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
|
||||
\OCP\Util::ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
$contacts = array();
|
||||
|
||||
while( $row = $result->fetchRow()) {
|
||||
$contacts[] = $row;
|
||||
$ids[] = $row['contactid'];
|
||||
}
|
||||
|
||||
return $contacts;
|
||||
if(count($ids) > 0) {
|
||||
$query = 'SELECT `' . $this->contactTableName . '`.`addressbookid`, `' . $this->indexTableName . '`.`contactid`, `'
|
||||
. $this->indexTableName . '`.`name`, `' . $this->indexTableName . '`.`value` FROM `'
|
||||
. $this->indexTableName . '`,`' . $this->contactTableName . '` WHERE `'
|
||||
. $this->contactTableName . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `'
|
||||
. $this->indexTableName . '`.`contactid` = `' . $this->contactTableName . '`.`id` AND `'
|
||||
. $this->indexTableName . '`.`contactid` IN (' . join(',', array_fill(0, count($ids), '?')) . ')';
|
||||
|
||||
$stmt = \OCP\DB::prepare($query);
|
||||
$result = $stmt->execute($ids);
|
||||
}
|
||||
|
||||
while( $row = $result->fetchRow()) {
|
||||
$this->getProperty($results, $row);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function getKey(){
|
||||
@ -80,5 +93,46 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
|
||||
public function delete($id){
|
||||
|
||||
}
|
||||
|
||||
private function getProperty(&$results, $row) {
|
||||
if(!$row['name'] || !$row['value']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$value = null;
|
||||
|
||||
switch($row['name']) {
|
||||
case 'PHOTO':
|
||||
$value = 'VALUE=uri:' . \OCP\Util::linkToAbsolute('contacts', 'photo.php') . '?id=' . $row['contactid'];
|
||||
break;
|
||||
case 'N':
|
||||
case 'ORG':
|
||||
case 'ADR':
|
||||
case 'GEO':
|
||||
case 'CATEGORIES':
|
||||
$property = \Sabre\VObject\Property::create($row['name'], $row['value']);
|
||||
$value = $property->getParts();
|
||||
break;
|
||||
default:
|
||||
$value = $value = strtr($row['value'], array('\,' => ',', '\;' => ';'));
|
||||
break;
|
||||
}
|
||||
|
||||
if(in_array($row['name'], Properties::$multiProperties)) {
|
||||
if(!isset($results[$row['contactid']])) {
|
||||
$results[$row['contactid']] = array('id' => $row['contactid'], $row['name'] => array($value));
|
||||
} elseif(!isset($results[$row['contactid']][$row['name']])) {
|
||||
$results[$row['contactid']][$row['name']] = array($value);
|
||||
} else {
|
||||
$results[$row['contactid']][$row['name']][] = $value;
|
||||
}
|
||||
} else {
|
||||
if(!isset($results[$row['contactid']])) {
|
||||
$results[$row['contactid']] = array('id' => $row['contactid'], $row['name'] => $value);
|
||||
} elseif(!isset($results[$row['contactid']][$row['name']])) {
|
||||
$results[$row['contactid']][$row['name']] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user