From 4c638f101e09cbe43a2114c64ebb30774bafce4b Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Thu, 16 Aug 2012 00:24:38 +0200 Subject: [PATCH] Merge addressbooks. --- apps/contacts/ajax/contact/move.php | 30 +++++++++-------------------- apps/contacts/js/contacts.js | 30 ++++++++++++++++++++++++++--- apps/contacts/lib/vcard.php | 9 +++++++-- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/apps/contacts/ajax/contact/move.php b/apps/contacts/ajax/contact/move.php index a3336c3cb6..053343c47e 100644 --- a/apps/contacts/ajax/contact/move.php +++ b/apps/contacts/ajax/contact/move.php @@ -7,35 +7,23 @@ * later. * See the COPYING-README file. */ - + OCP\JSON::checkLoggedIn(); OCP\JSON::checkAppEnabled('contacts'); OCP\JSON::callCheck(); -$ids = $_POST['ids']; +$id = intval($_POST['id']); $aid = intval($_POST['aid']); +$isaddressbook = isset($_POST['isaddressbook']) ? true: false; + +// Ownership checking OC_Contacts_App::getAddressbook($aid); - -if(!is_array($ids)) { - $ids = array($ids,); -} -$goodids = array(); -foreach ($ids as $id){ - try { - $card = OC_Contacts_App::getContactObject( intval($id) ); - if($card) { - $goodids[] = $id; - } - } catch (Exception $e) { - OCP\Util::writeLog('contacts', 'Error moving contact "'.$id.'" to addressbook "'.$aid.'"'.$e->getMessage(), OCP\Util::ERROR); - } -} try { - OC_Contacts_VCard::moveToAddressBook($aid, $ids); + OC_Contacts_VCard::moveToAddressBook($aid, $id, $isaddressbook); } catch (Exception $e) { $msg = $e->getMessage(); - OCP\Util::writeLog('contacts', 'Error moving contacts "'.implode(',', $ids).'" to addressbook "'.$aid.'"'.$msg, OCP\Util::ERROR); + OCP\Util::writeLog('contacts', 'Error moving contacts "'.implode(',', $id).'" to addressbook "'.$aid.'"'.$msg, OCP\Util::ERROR); OC_JSON::error(array('data' => array('message' => $msg,))); } - -OC_JSON::success(array('data' => array('ids' => $goodids,))); \ No newline at end of file + +OC_JSON::success(array('data' => array('ids' => $id,))); \ No newline at end of file diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js index 35637de050..f535dc03f3 100644 --- a/apps/contacts/js/contacts.js +++ b/apps/contacts/js/contacts.js @@ -1437,8 +1437,12 @@ OC.Contacts={ if(dragitem.data('bookid') == droptarget.data('id')) { return false; } - var droplist = (droptarget.is('ul'))?droptarget:droptarget.next(); - $.post(OC.filePath('contacts', 'ajax', 'contact/move.php'), { ids: dragitem.data('id'), aid: droptarget.data('id') }, + var droplist = (droptarget.is('ul')) ? droptarget : droptarget.next(); + $.post(OC.filePath('contacts', 'ajax', 'contact/move.php'), + { + id: dragitem.data('id'), + aid: droptarget.data('id') + }, function(jsondata){ if(jsondata.status == 'success'){ dragitem.attr('data-bookid', droptarget.data('id')) @@ -1454,7 +1458,27 @@ OC.Contacts={ }); }, dropAddressbook:function(event, dragitem, droptarget) { - alert('Dropping address books not implemented yet'); + if(confirm(t('contacts', 'Do you want to merge these address books?'))) { + if(dragitem.data('bookid') == droptarget.data('id')) { + return false; + } + var droplist = (droptarget.is('ul')) ? droptarget : droptarget.next(); + $.post(OC.filePath('contacts', 'ajax', 'contact/move.php'), + { + id: dragitem.data('id'), + aid: droptarget.data('id'), + isaddressbook: 1 + }, + function(jsondata){ + if(jsondata.status == 'success'){ + OC.Contacts.Contacts.update(); // Easier to refresh the whole bunch. + } else { + OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error')); + } + }); + } else { + return false; + } }, /** * @params params An object with the properties 'contactlist':a jquery object of the ul to insert into, diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php index b213bcd762..91ac89f553 100644 --- a/apps/contacts/lib/vcard.php +++ b/apps/contacts/lib/vcard.php @@ -589,7 +589,7 @@ class OC_Contacts_VCard{ * @return boolean * */ - public static function moveToAddressBook($aid, $id) { + public static function moveToAddressBook($aid, $id, $isAddressbook = false) { OC_Contacts_App::getAddressbook($aid); // check for user ownership. if(is_array($id)) { $id_sql = join(',', array_fill(0, count($id), '?')); @@ -606,8 +606,13 @@ class OC_Contacts_VCard{ return false; } } else { - try { + $stmt = null; + if($isAddressbook) { + $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_cards SET addressbookid = ? WHERE addressbookid = ?' ); + } else { $stmt = OCP\DB::prepare( 'UPDATE *PREFIX*contacts_cards SET addressbookid = ? WHERE id = ?' ); + } + try { $result = $stmt->execute(array($aid, $id)); } catch(Exception $e) { OCP\Util::writeLog('contacts', __CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(), OCP\Util::DEBUG);