/**
  * @NoAdminRequired
  */
 public function saveContact()
 {
     $params = $this->request->urlParams;
     $data = isset($this->request->post['data']) ? $this->request->post['data'] : null;
     $response = new JSONResponse();
     $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $contact = $addressBook->getChild($params['contactId']);
     if (!$data) {
         return $response->bailOut(App::$l10n->t('No contact data in request.'));
     }
     if (!$contact) {
         return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
     }
     if (!$contact->mergeFromArray($data)) {
         return $response->bailOut(App::$l10n->t('Error merging into contact.'));
     }
     if (!$contact->save()) {
         return $response->bailOut(App::$l10n->t('Error saving contact to backend.'));
     }
     return $response->setData(JSONSerializer::serializeContact($contact));
 }
 /**
  * @NoAdminRequired
  */
 public function moveChild()
 {
     $params = $this->request->urlParams;
     $targetInfo = $this->request->post['target'];
     $response = new JSONResponse();
     // TODO: Check if the backend supports move (is 'local' or 'shared') and use that operation instead.
     // If so, set status 204 and don't return the serialized contact.
     $fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
     $contact = $fromAddressBook->getChild($params['contactId']);
     if (!$contact) {
         throw new \Exception(App::$l10n->t('Error retrieving contact'), 500);
     }
     $contactId = $targetAddressBook->addChild($contact);
     // Retrieve the contact again to be sure it's in sync
     $contact = $targetAddressBook->getChild($contactId);
     if (!$contact) {
         throw new \Exception(App::$l10n->t('Error saving contact'), 500);
     }
     if (!$fromAddressBook->deleteChild($params['contactId'])) {
         // Don't bail out because we have to return the contact
         return $response->debug(App::$l10n->t('Error removing contact from other address book.'));
     }
     $serialized = JSONSerializer::serializeContact($contact);
     if (is_null($serialized)) {
         throw new \Exception(App::$l10n->t('Error getting moved contact'));
     }
     return $response->setParams($serialized);
 }
    /**
     * @param $pattern
     * @param $searchProperties
     * @param $options
     * @return array|false
     */
    public function search($pattern, $searchProperties, $options)
    {
        $propTable = self::PROPERTY_TABLE;
        $contTable = self::CONTACT_TABLE;
        $addrTable = self::ADDRESSBOOK_TABLE;
        $results = array();
        /**
         * This query will fetch all contacts which match the $searchProperties
         * It will look up the addressbookid of the contact and the user id of the owner of the contact app
         */
        $query = <<<SQL
\t\t\tSELECT
\t\t\t\tDISTINCT
\t\t\t\t`{$propTable}`.`contactid`,
\t\t\t\t`{$contTable}`.`addressbookid`,
\t\t\t\t`{$addrTable}`.`userid`

\t\t\tFROM
\t\t\t\t`{$propTable}`
\t\t\tINNER JOIN
\t\t\t\t`{$contTable}`
\t\t\tON `{$contTable}`.`id` = `{$propTable}`.`contactid`
  \t\t\t\tINNER JOIN `{$addrTable}`
\t\t\tON `{$addrTable}`.id = `{$contTable}`.addressbookid
\t\t\tWHERE
\t\t\t\t`{$contTable}`.addressbookid = ? AND
\t\t\t\t(
SQL;
        $params = array();
        $meta = $this->addressBook->getMetaData();
        $params[] = $meta['id'];
        foreach ($searchProperties as $property) {
            $params[] = $property;
            $params[] = '%' . $pattern . '%';
            $query .= '(`name` = ? AND `value` LIKE ?) OR ';
        }
        $query = substr($query, 0, strlen($query) - 4);
        $query .= ')';
        $stmt = \OCP\DB::prepare($query);
        $result = $stmt->execute($params);
        if (\OCP\DB::isError($result)) {
            \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
            return false;
        }
        while ($row = $result->fetchRow()) {
            $id = $row['contactid'];
            $addressbookKey = $row['addressbookid'];
            // Check if we are the owner of the contact
            if ($row['userid'] !== \OCP\User::getUser()) {
                // we aren't the owner of the contact
                try {
                    // it is possible that the contact is shared with us
                    // if so, $contact will be an object
                    // if not getContact will throw an Exception
                    $contact = $this->app->getContact('shared', $addressbookKey, $id);
                } catch (\Exception $e) {
                    // the contact isn't shared with us
                    $contact = null;
                }
            } else {
                // We are the owner of the contact
                // thus we can easily fetch it
                $contact = $this->app->getContact('local', $addressbookKey, $id);
            }
            if ($contact !== null) {
                $j = JSONSerializer::serializeContact($contact);
                $j['data']['id'] = $id;
                if (isset($contact->PHOTO)) {
                    $url = \OCP\Util::linkToRoute('contacts_contact_photo', array('backend' => $contact->getBackend()->name, 'addressBookId' => $addressbookKey, 'contactId' => $contact->getId()));
                    $url = \OC_Helper::makeURLAbsolute($url);
                    $j['data']['PHOTO'] = "VALUE=uri:{$url}";
                }
                $results[] = $this->convertToSearchResult($j);
            }
        }
        return $results;
    }
 /**
  * @NoAdminRequired
  */
 public function moveChild()
 {
     $params = $this->request->urlParams;
     $targetInfo = $this->request->post['target'];
     $response = new JSONResponse();
     // TODO: Check if the backend supports move (is 'local' or 'shared') and use that operation instead.
     // If so, set status 204 and don't return the serialized contact.
     $fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
     $contact = $fromAddressBook->getChild($params['contactId']);
     if (!$contact) {
         $response->bailOut(App::$l10n->t('Error retrieving contact.'));
         return $response;
     }
     try {
         $contactId = $targetAddressBook->addChild($contact);
     } catch (Exception $e) {
         return $response->bailOut($e->getMessage());
     }
     $contact = $targetAddressBook->getChild($contactId);
     if (!$contact) {
         return $response->bailOut(App::$l10n->t('Error saving contact.'));
     }
     if (!$fromAddressBook->deleteChild($params['contactId'])) {
         // Don't bail out because we have to return the contact
         return $response->debug(App::$l10n->t('Error removing contact from other address book.'));
     }
     return $response->setParams(JSONSerializer::serializeContact($contact));
 }
Beispiel #5
0
 /**
  * @param $pattern
  * @param $searchProperties
  * @param $options
  * @return array|false
  */
 public function search($pattern, $searchProperties, $options)
 {
     $ids = array();
     $results = array();
     $query = 'SELECT DISTINCT `contactid` FROM `' . self::PROPERTY_TABLE . '` WHERE (';
     $params = array();
     foreach ($searchProperties as $property) {
         $params[] = $property;
         $params[] = '%' . $pattern . '%';
         $query .= '(`name` = ? AND `value` LIKE ?) OR ';
     }
     $query = substr($query, 0, strlen($query) - 4);
     $query .= ')';
     $stmt = \OCP\DB::prepare($query);
     $result = $stmt->execute($params);
     if (\OCP\DB::isError($result)) {
         \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
         return false;
     }
     while ($row = $result->fetchRow()) {
         $ids[] = $row['contactid'];
     }
     if (count($ids) > 0) {
         foreach ($ids as $id) {
             $contact = $this->addressBook->getChild($id);
             $j = JSONSerializer::serializeContact($contact);
             $j['data']['id'] = $id;
             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[] = $this->convertToSearchResult($j);
         }
     }
     return $results;
 }
 public function testUnsetByChecksum()
 {
     $serialized = JSONSerializer::serializeContact($this->contact);
     $checksum = $serialized['data']['EMAIL'][0]['checksum'];
     $this->assertTrue(isset($this->contact->EMAIL));
     $this->contact->unsetPropertyByChecksum($checksum);
     $this->assertTrue(!isset($this->contact->EMAIL));
 }