1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Update DB on getContacts, getContact and search

This commit is contained in:
LEDfan 2014-04-06 09:58:49 +02:00
parent 25d79d534c
commit a65549f089
2 changed files with 49 additions and 33 deletions

View File

@ -113,49 +113,24 @@ class LocalUsers extends AbstractBackend {
* There are as many contacts in this addressbook as in this ownCloud installation * There are as many contacts in this addressbook as in this ownCloud installation
*/ */
public function getContacts($addressbookid, array $options = array()){ public function getContacts($addressbookid, array $options = array()){
$this->updateDatabase();
$contacts = array(); $contacts = array();
try{ try{
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?';
$query = \OCP\DB::prepare($sql); $query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid)); $result = $query->execute(array($this->userid));
if (\OCP\DB::isError($result)) { if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return array(); return true;
} else { } else {
while($row = $result->fetchRow()){ while($row = $result->fetchRow()){
$row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; $row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE;
$contacts[] = $row; $contacts[] = $row;
}
$contactsId = array();
foreach($contacts as $contact){
$contactsId[] = $contact['id'];
}
$users = \OCP\User::getUsers();
$recall = false;
$add = array_diff($users, $contactsId);
$remove = array_diff($contactsId, $users);
if(count($add) > 0){
$this->addContacts($add, $addressbookid);
$recall = true;
}
if(count($remove) > 0){
$this->removeContacts($remove, $addressbookid);
$recall = true;
}
if($recall === true){
return $this->getContacts($addressbookid);
} else {
return $contacts;
} }
} }
return $contacts;
} catch(\Exception $e) { } catch(\Exception $e) {
\OCP\Util::writeLog('contacts', __METHOD__.' exception: ' \OCP\Util::writeLog('contacts', __METHOD__.' exception: '
. $e->getMessage(), \OCP\Util::ERROR); . $e->getMessage(), \OCP\Util::ERROR);
@ -333,7 +308,7 @@ class LocalUsers extends AbstractBackend {
* @return \OCA\Contacts\LocalUsersAddressbookProvider * @return \OCA\Contacts\LocalUsersAddressbookProvider
*/ */
public function getSearchProvider(){ public function getSearchProvider(){
return new LocalUsersAddressbookProvider(); return new LocalUsersAddressbookProvider($this);
} }
/** /**
@ -400,4 +375,36 @@ class LocalUsers extends AbstractBackend {
return false; return false;
} }
} }
public function updateDatabase(){
$sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($this->userid));
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('contacts', __METHOD__. 'DB error: '
. \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
return true;
} else {
while($row = $result->fetchRow()){
$contactsId[] = $row['id'];
}
$users = \OCP\User::getUsers();
$add = array_diff($users, $contactsId);
$remove = array_diff($contactsId, $users);
if(count($add) > 0){
$this->addContacts($add, $addressbookid);
$recall = true;
}
if(count($remove) > 0){
$this->removeContacts($remove, $addressbookid);
$recall = true;
}
return true;
}
}
} }

View File

@ -3,6 +3,7 @@
namespace OCA\Contacts; namespace OCA\Contacts;
use OCA\Contacts\Utils\Properties; use OCA\Contacts\Utils\Properties;
use OCA\Contacts\Backend\LocalUsers;
class LocalUsersAddressbookProvider implements \OCP\IAddressBook { class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
@ -27,8 +28,13 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
*/ */
private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; private $indexTableName = '*PREFIX*contacts_ocu_cards_properties';
public function __construct(){ /**
* @var LocalUsers
*/
private $backend;
public function __construct(LocalUsers $backend){
$this->backend = $backend;
} }
/** /**
@ -38,6 +44,9 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook {
* @return array|false * @return array|false
*/ */
public function search($pattern, $searchProperties, $options) { public function search($pattern, $searchProperties, $options) {
// First make sure the database is updated
$this->backend->updateDatabase();
$ids = array(); $ids = array();
$results = array(); $results = array();
$query = 'SELECT DISTINCT `contactid` FROM `' . $this->indexTableName . '` WHERE ('; $query = 'SELECT DISTINCT `contactid` FROM `' . $this->indexTableName . '` WHERE (';