예제 #1
0
 /**
  * @brief Move card(s) to an address book
  * @param integer $aid Address book id
  * @param $id Array or integer of cards to be moved.
  * @return boolean
  *
  */
 public static function moveToAddressBook($aid, $id, $isAddressbook = false)
 {
     $addressbook = Addressbook::find($aid);
     if ($addressbook['userid'] != \OCP\User::getUser()) {
         $sharedAddressbook = \OCP\Share::getItemSharedWithBySource(App::SHAREADDRESSBOOK, App::SHAREADDRESSBOOKPREFIX . $aid);
         if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE)) {
             throw new \Exception(App::$l10n->t('You don\'t have permissions to move contacts into this address book'));
         }
     }
     if (is_array($id)) {
         // NOTE: This block is currently not used and need rewrite if used!
         foreach ($id as $index => $cardId) {
             $card = self::find($cardId);
             if (!$card) {
                 unset($id[$index]);
             }
             $oldAddressbook = Addressbook::find($card['addressbookid']);
             if ($oldAddressbook['userid'] != \OCP\User::getUser()) {
                 $sharedContact = \OCP\Share::getItemSharedWithBySource(App::SHARECONTACT, App::SHARECONTACTPREFIX . $cardId, \OCP\Share::FORMAT_NONE, null, true);
                 if (!$sharedContact || !($sharedContact['permissions'] & \OCP\PERMISSION_DELETE)) {
                     unset($id[$index]);
                 }
             }
         }
         $id_sql = join(',', array_fill(0, count($id), '?'));
         $prep = 'UPDATE `' . App::ContactsTable . '` SET `addressbookid` = ? WHERE `id` IN (' . $id_sql . ')';
         try {
             $stmt = \OCP\DB::prepare($prep);
             //$aid = array($aid);
             $vals = array_merge((array) $aid, $id);
             $result = $stmt->execute($vals);
             if (\OCP\DB::isError($result)) {
                 \OCP\Util::writeLog(App::$appname, __METHOD__ . 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
                 throw new \Exception(App::$l10n->t('Database error during move.'));
             }
         } catch (\Exception $e) {
             \OCP\Util::writeLog(App::$appname, __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
             \OCP\Util::writeLog(App::$appname, __METHOD__ . ', ids: ' . join(',', $vals), \OCP\Util::DEBUG);
             \OCP\Util::writeLog(App::$appname, __METHOD__ . ', SQL:' . $prep, \OCP\Util::DEBUG);
             throw new \Exception(App::$l10n->t('Database error during move.'));
         }
     } else {
         $stmt = null;
         if ($isAddressbook) {
             $stmt = \OCP\DB::prepare('UPDATE `' . App::ContactsTable . '` SET `addressbookid` = ? WHERE `addressbookid` = ?');
         } else {
             $card = self::find($id);
             if (!$card) {
                 throw new \Exception(App::$l10n->t('Error finding card to move.'));
             }
             $oldAddressbook = Addressbook::find($card['addressbookid']);
             if ($oldAddressbook['userid'] != \OCP\User::getUser()) {
                 $sharedAddressbook = \OCP\Share::getItemSharedWithBySource(App::SHAREADDRESSBOOK, App::SHAREADDRESSBOOKPREFIX . $oldAddressbook['id']);
                 if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_DELETE)) {
                     throw new \Exception(App::$l10n->t('You don\'t have permissions to move contacts from this address book'));
                 }
             }
             Addressbook::touch($oldAddressbook['id']);
             $stmt = \OCP\DB::prepare('UPDATE `' . App::ContactsTable . '` SET `addressbookid` = ? WHERE `id` = ?');
         }
         try {
             $result = $stmt->execute(array($aid, $id));
             if (\OCP\DB::isError($result)) {
                 \OCP\Util::writeLog(App::$appname, __METHOD__ . 'DB error: ' . \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
                 throw new \Exception(App::$l10n->t('Database error during move.'));
             }
         } catch (\Exception $e) {
             \OCP\Util::writeLog(App::$appname, __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::DEBUG);
             \OCP\Util::writeLog(App::$appname, __METHOD__ . ' id: ' . $id, \OCP\Util::DEBUG);
             throw new \Exception(App::$l10n->t('Database error during move.'));
         }
     }
     //\OC_Hook::emit('\OCA\Contacts\VCard', 'post_moveToAddressbook', array('aid' => $aid, 'id' => $id));
     Addressbook::touch($aid);
     return true;
 }