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

139 lines
3.7 KiB
PHP
Raw Normal View History

2014-04-03 18:36:11 +02:00
<?php
namespace OCA\Contacts;
2014-04-05 20:35:29 +02:00
use OCA\Contacts\Utils\Properties;
2014-04-03 18:36:11 +02:00
class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
2014-04-05 20:35:29 +02:00
private $indexTableName = '*PREFIX*contacts_ocu_cards_properties';
private $contactTableName = '*PREFIX*contacts_ocu_cards';
2014-04-03 18:36:11 +02:00
public function __construct(){
}
/**
* @param $pattern
* @param $searchProperties
* @param $options
* @return array|false
*/
public function search($pattern, $searchProperties, $options) {
2014-04-05 20:35:29 +02:00
$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 ';
2014-04-03 18:36:11 +02:00
}
2014-04-05 20:35:29 +02:00
$query = substr($query, 0, strlen($query) - 4);
$query .= ')';
$stmt = \OCP\DB::prepare($query);
$result = $stmt->execute($params);
2014-04-03 18:36:11 +02:00
if (\OCP\DB::isError($result)) {
2014-04-05 20:35:29 +02:00
\OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result),
\OCP\Util::ERROR);
return false;
2014-04-03 18:36:11 +02:00
}
while( $row = $result->fetchRow()) {
2014-04-05 20:35:29 +02:00
$ids[] = $row['contactid'];
2014-04-03 18:36:11 +02:00
}
2014-04-05 20:35:29 +02:00
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;
2014-04-03 18:36:11 +02:00
}
public function getKey(){
}
/**
* In comparison to getKey() this function returns a human readable (maybe translated) name
* @return mixed
*/
public function getDisplayName(){
}
public function createOrUpdate($properties){
}
/**
* @return mixed
*/
public function getPermissions(){
}
/**
* @param object $id the unique identifier to a contact
* @return bool successful or not
*/
public function delete($id){
2014-04-03 18:59:34 +02:00
2014-04-03 18:36:11 +02:00
}
2014-04-05 20:35:29 +02:00
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;
}
}
}
2014-04-03 18:36:11 +02:00
}