diff --git a/lib/backend/abstractbackend.php b/lib/backend/abstractbackend.php index b0e37645..9461387c 100644 --- a/lib/backend/abstractbackend.php +++ b/lib/backend/abstractbackend.php @@ -25,28 +25,30 @@ namespace OCA\Contacts\Backend; use OCA\Contacts\VObject\VCard; /** - * Subclass this class for address book backends - * - * The following methods MUST be implemented: - * @method array getAddressBooksForUser(array $options = array()) - * @method array|null getAddressBook(string $addressbookid, array $options = array()) - * @method array getContacts(string $addressbookid, array $options = array()) - * @method array|null getContact(string $addressbookid, mixed $id, array $options = array()) - * The following methods MAY be implemented: - * @method bool hasAddressBook(string $addressbookid) - * @method bool updateAddressBook(string $addressbookid, array $updates, array $options = array()) - * @method string createAddressBook(array $properties, array $options = array()) - * @method bool deleteAddressBook(string $addressbookid, array $options = array()) - * @method int lastModifiedAddressBook(string $addressbookid) - * @method array numContacts(string $addressbookid) - * @method bool updateContact(string $addressbookid, string $id, VCard $contact, array $options = array()) - * @method string createContact(string $addressbookid, VCard $contact, array $properties) - * @method bool deleteContact(string $addressbookid, string $id, array $options = array()) - * @method int lastModifiedContact(string $addressbookid) + * Subclass this class for address book backends. */ - abstract class AbstractBackend { + /** + * The following methods MUST be implemented: + * + * @method array getAddressBooksForUser(array $options = array()) + * @method array|null getAddressBook(string $addressbookid, array $options = array()) + * @method array getContacts(string $addressbookid, array $options = array()) + * @method array|null getContact(string $addressbookid, mixed $id, array $options = array()) + * The following methods MAY be implemented: + * @method bool hasAddressBook(string $addressbookid) + * @method bool updateAddressBook(string $addressbookid, array $updates, array $options = array()) + * @method string createAddressBook(array $properties, array $options = array()) + * @method bool deleteAddressBook(string $addressbookid, array $options = array()) + * @method int lastModifiedAddressBook(string $addressbookid) + * @method array numContacts(string $addressbookid) + * @method bool updateContact(string $addressbookid, string $id, VCard $contact, array $options = array()) + * @method string createContact(string $addressbookid, VCard $contact, array $properties) + * @method bool deleteContact(string $addressbookid, string $id, array $options = array()) + * @method int lastModifiedContact(string $addressbookid) + */ + /** * The name of the backend. * @var string @@ -376,6 +378,7 @@ abstract class AbstractBackend { * * @param string $addressBookId. * @param string $contactId. + * @throws \BadMethodCallException * @return string */ protected function combinedKey($addressBookId = null, $contactId = null) { @@ -446,8 +449,8 @@ abstract class AbstractBackend { * @param array the preferences, format array('param1' => 'value', 'param2' => 'value') * @return boolean */ - public function setPreferences($addressbookid, array $params) { - + public function setPreferences($addressBookId, array $params) { + $key = $this->combinedKey($addressBookId); $key = 'prefs_' . $key; diff --git a/lib/backend/database.php b/lib/backend/database.php index cbcfeaa4..0e0105d0 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -28,7 +28,7 @@ use OCA\Contacts\Contact, Sabre\VObject\Reader; /** - * Subclass this class for Contacts backends + * Subclass this class for Contacts backends. */ class Database extends AbstractBackend { @@ -36,6 +36,30 @@ class Database extends AbstractBackend { public $name = 'local'; static private $preparedQueries = array(); + /** + * The cached address books. + * @var array[] + */ + public $addressbooks; + + /** + * The table that holds the address books. + * @var string + */ + public $addressBooksTableName; + + /** + * The table that holds the contact vCards. + * @var string + */ + public $cardsTableName; + + /** + * The table that holds the indexed vCard properties. + * @var string + */ + public $indexTableName; + /** * Sets up the backend * @@ -123,6 +147,7 @@ class Database extends AbstractBackend { if (!$row) { throw new \Exception('Address Book not found', 404); } + $row['permissions'] = \OCP\PERMISSION_ALL; $row['backend'] = $this->name; $this->addressbooks[$addressbookid] = $row; @@ -478,31 +503,30 @@ class Database extends AbstractBackend { $noCollection = isset($options['noCollection']) ? $options['noCollection'] : false; - $where_query = '`id` = ?'; + $whereQuery = '`id` = ?'; if (is_array($id)) { - $where_query = ''; + $whereQuery = ''; if (isset($id['id'])) { $id = $id['id']; } elseif (isset($id['uri'])) { - $where_query = '`uri` = ?'; + $whereQuery = '`uri` = ?'; $id = $id['uri']; } else { throw new \Exception( __METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.' ); - return null; } } $ids = array($id); if (!$noCollection) { - $where_query .= ' AND `addressbookid` = ?'; + $whereQuery .= ' AND `addressbookid` = ?'; $ids[] = $addressbookid; } try { - $query = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, `addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' - . $this->cardsTableName . '` WHERE ' . $where_query; + $query = 'SELECT `id`, `uri`, `carddata`, `lastmodified`, `addressbookid` AS `parent`, `fullname` AS `displayname` FROM `' + . $this->cardsTableName . '` WHERE ' . $whereQuery; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute($ids);