diff --git a/appinfo/database.xml b/appinfo/database.xml index dad79f27..703e4edf 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -411,4 +411,64 @@ + + + + *dbprefix*contacts_ocu_cards_properties + + + + + id + integer + 0 + true + 1 + true + 4 + + + + addressbookid + text + + true + 255 + + + + contactid + text + + true + 255 + + + + name + text + + false + 64 + + + + value + text + + false + 255 + + + + preferred + integer + 1 + true + 4 + + + +
+ diff --git a/appinfo/version b/appinfo/version index 8d13a336..7af4042f 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1 +1 @@ -0.3.0.8 \ No newline at end of file +0.3.0.14 \ No newline at end of file diff --git a/lib/backend/database.php b/lib/backend/database.php index 5deb3182..2a0e9a04 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -1018,4 +1018,9 @@ class Database extends AbstractBackend { return self::$preparedQueries[$identifier]; } + + public function getSearchProvider($addressbook){ + return new \OCA\Contacts\AddressbookProvider($addressbook); + } + } diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index c6a698fa..d2c679b0 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -50,7 +50,13 @@ class LocalUsers extends AbstractBackend { * @var string */ private $cardsTableName = '*PREFIX*contacts_ocu_cards'; + + private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; + private $indexProperties = array( + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO'); + public function __construct($userid){ $this->userid = $userid ? $userid : \OCP\User::getUser(); @@ -209,6 +215,9 @@ class LocalUsers extends AbstractBackend { . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } else { + // All done + // now update the index table with all the properties + $this->updateIndex($user, $vcard); return true; } } catch(\Exception $e) { @@ -268,7 +277,6 @@ class LocalUsers extends AbstractBackend { $contact->REV = $now->format(\DateTime::W3C); } - $data = $contact->serialize(); try{ $sql = 'UPDATE ' . $this->cardsTableName . ' SET ' @@ -279,12 +287,15 @@ class LocalUsers extends AbstractBackend { . '`id` = ? ' . 'AND `addressbookid` = ? '; $query = \OCP\DB::prepare($sql); - $result = $query->execute(array($contact->FN, $data, time(), $id, $this->userid)); + $result = $query->execute(array($contact->FN, $contact->serialize(), time(), $id, $this->userid)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } else { + // All done + // now update the indexes in the DB + $this->updateIndex($id, $contact); return true; } } catch(\Exception $e) { @@ -297,4 +308,55 @@ class LocalUsers extends AbstractBackend { public function getSearchProvider(){ return new LocalUsersAddressbookProvider(); } + + private function updateIndex($contactId, $vcard){ + // Utils\Properties::updateIndex($parameters['id'], $contact); + $this->purgeIndex($contactId); + $updatestmt = \OCP\DB::prepare('INSERT INTO `' . $this->indexTableName . '` ' + . '(`addressbookid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)'); + // Insert all properties in the table + foreach($vcard->children as $property) { + if(!in_array($property->name, $this->indexProperties)) { + continue; + } + $preferred = 0; + foreach($property->parameters as $parameter) { + if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { + $preferred = 1; + break; + } + } + try { + $result = $updatestmt->execute( + array( + \OCP\User::getUser(), + $contactId, + $property->name, + substr($property->value, 0, 254), + $preferred, + ) + ); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return false; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + return false; + } + } + } + + private function purgeIndex($id){ + // Remove all indexes from the table + try { + $query = \OCP\DB::prepare('DELETE FROM `' . $this->indexTableName . '`' + . ' WHERE `contactid` = ?'); + $query->execute(array($id)); + + } catch(\Exception $e) { + return false; + } + } } diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 63e09288..3b813109 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -15,18 +15,20 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { * @return array|false */ public function search($pattern, $searchProperties, $options) { - 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 . "%")); + 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);