コード例 #1
0
ファイル: vcard.php プロジェクト: netcon-source/apps
 /**
  * @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::find($aid);
     $addressbook = Addressbook::find($aid);
     if ($addressbook['userid'] != \OCP\User::getUser()) {
         $sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $aid);
         if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE)) {
             return false;
         }
     }
     if (is_array($id)) {
         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('contact', $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 `*PREFIX*contacts_cards` 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 (\OC_DB::isError($result)) {
                 \OC_Log::write('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
                 return false;
             }
         } catch (\Exception $e) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
             \OCP\Util::writeLog('contacts', __METHOD__ . ', ids: ' . join(',', $vals), \OCP\Util::DEBUG);
             \OCP\Util::writeLog('contacts', __METHOD__ . ', SQL:' . $prep, \OCP\Util::DEBUG);
             return false;
         }
     } else {
         $stmt = null;
         if ($isAddressbook) {
             $stmt = \OCP\DB::prepare('UPDATE `*PREFIX*contacts_cards` SET `addressbookid` = ? WHERE `addressbookid` = ?');
         } else {
             $card = self::find($id);
             if (!$card) {
                 return false;
             }
             $oldAddressbook = Addressbook::find($card['addressbookid']);
             if ($oldAddressbook['userid'] != \OCP\User::getUser()) {
                 $sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $id, \OCP\Share::FORMAT_NONE, null, true);
                 if (!$sharedContact || !($sharedContact['permissions'] & \OCP\PERMISSION_DELETE)) {
                     return false;
                 }
             }
             $stmt = \OCP\DB::prepare('UPDATE `*PREFIX*contacts_cards` SET `addressbookid` = ? WHERE `id` = ?');
         }
         try {
             $result = $stmt->execute(array($aid, $id));
             if (\OC_DB::isError($result)) {
                 \OC_Log::write('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
                 return false;
             }
         } catch (\Exception $e) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::DEBUG);
             \OCP\Util::writeLog('contacts', __METHOD__ . ' id: ' . $id, \OCP\Util::DEBUG);
             return false;
         }
     }
     \OC_Hook::emit('\\OCA\\Contacts\\VCard', 'post_moveToAddressbook', array('aid' => $aid, 'id' => $id));
     Addressbook::touch($aid);
     return true;
 }