diff --git a/lib/addressbook.php b/lib/addressbook.php index f9535997..942a185e 100644 --- a/lib/addressbook.php +++ b/lib/addressbook.php @@ -22,6 +22,8 @@ namespace OCA\Contacts; +use OC_L10N; +use OCA\Contacts\Backend\AbstractBackend; use OCP\AppFramework\Http; /** @@ -148,7 +150,7 @@ class Addressbook extends AbstractPIMCollection { /** * @brief Activate an address book - * @param bool active + * @param bool $active * @return void */ public function setActive($active) { @@ -411,7 +413,7 @@ class Addressbook extends AbstractPIMCollection { if (!$this->hasPermission(\OCP\PERMISSION_UPDATE)) { throw new \Exception( self::$l10n->t('Access denied'), - STATUS_FORBIDDEN + Http::STATUS_FORBIDDEN ); } @@ -447,7 +449,7 @@ class Addressbook extends AbstractPIMCollection { */ public function delete() { if (!$this->hasPermission(\OCP\PERMISSION_DELETE)) { - throw new Exception( + throw new \Exception( self::$l10n->t('You don\'t have permissions to delete the address book.'), Http::STATUS_FORBIDDEN ); diff --git a/lib/addressbookprovider.php b/lib/addressbookprovider.php index 5f34c436..21f661cf 100644 --- a/lib/addressbookprovider.php +++ b/lib/addressbookprovider.php @@ -21,7 +21,10 @@ */ namespace OCA\Contacts; +use OCA\Contacts\Utils\JSONSerializer; use OCA\Contacts\Utils\Properties; +use OCA\Contacts\Utils\TemporaryPhoto; +use OCA\Contacts\VObject\VCard; /** * This class manages our addressbooks. @@ -79,47 +82,6 @@ class AddressbookProvider implements \OCP\IAddressBook { return $this->addressBook->getPermissions(); } - 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; - } - } - } - /** * @param $pattern * @param $searchProperties @@ -151,21 +113,23 @@ class AddressbookProvider implements \OCP\IAddressBook { } if(count($ids) > 0) { - $query = 'SELECT `' . self::CONTACT_TABLE . '`.`addressbookid`, `' . self::PROPERTY_TABLE . '`.`contactid`, `' - . self::PROPERTY_TABLE . '`.`name`, `' . self::PROPERTY_TABLE . '`.`value` FROM `' - . self::PROPERTY_TABLE . '`,`' . self::CONTACT_TABLE . '` WHERE `' - . self::CONTACT_TABLE . '`.`addressbookid` = \'' . $this->addressBook->getId() . '\' AND `' - . self::PROPERTY_TABLE . '`.`contactid` = `' . self::CONTACT_TABLE . '`.`id` AND `' - . self::PROPERTY_TABLE . '`.`contactid` IN (' . join(',', array_fill(0, count($ids), '?')) . ')'; + foreach($ids as $id){ + $contact = $this->addressBook->getChild($id); + $j = JSONSerializer::serializeContact($contact); + if (isset($contact->PHOTO)) { + $url =\OCP\Util::linkToRoute('contacts_contact_photo', + array( + 'backend' => $contact->getBackend()->name, + 'addressBookId' => $this->addressBook->getId(), + 'contactId' => $contact->getId() + )); + $url = \OC_Helper::makeURLAbsolute($url); + $j['data']['PHOTO'] = "VALUE=uri:$url"; + } + $results[]= $j['data']; + } + } - //\OCP\Util::writeLog('contacts', __METHOD__ . 'DB query: ' . $query, \OCP\Util::DEBUG); - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute($ids); - } - while( $row = $result->fetchRow()) { - $this->getProperty($results, $row); - } - return $results; }