From 3d278c0e1137ccfb8b8d8eed29c9b360d122c349 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Wed, 13 Mar 2013 08:52:00 +0100 Subject: [PATCH] Contacts: Moved misc. functionality to hooks and utility classes. --- appinfo/app.php | 8 +++- lib/app.php | 80 --------------------------------------- lib/backend/database.php | 12 +++--- lib/backend/shared.php | 6 +-- lib/hooks.php | 38 ++++++++++++++++--- lib/share/addressbook.php | 5 +++ lib/utils/properties.php | 65 +++++++++++++++++++++++++++++++ 7 files changed, 117 insertions(+), 97 deletions(-) diff --git a/appinfo/app.php b/appinfo/app.php index d2c8a883..91e01cb2 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -39,8 +39,12 @@ Sabre\VObject\Property::$classMap['TEL'] = 'OCA\Contacts\VObject\StringProperty' Sabre\VObject\Property::$classMap['IMPP'] = 'OCA\Contacts\VObject\StringProperty'; Sabre\VObject\Property::$classMap['URL'] = 'OCA\Contacts\VObject\StringProperty'; -OCP\Util::connectHook('OC_User', 'post_createUser', 'OCA\Contacts\Hooks', 'createUser'); -OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Contacts\Hooks', 'deleteUser'); +OCP\Util::connectHook('OC_User', 'post_createUser', 'OCA\Contacts\Hooks', 'userCreated'); +OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Contacts\Hooks', 'userDeleted'); +OCP\Util::connectHook('OCA\Contacts', 'pre_deleteAddressBook', 'OCA\Contacts\Hooks', 'addresBookDeletion'); +OCP\Util::connectHook('OCA\Contacts', 'pre_deleteContact', 'OCA\Contacts\Hooks', 'contactDeletion'); +OCP\Util::connectHook('OCA\Contacts', 'post_createContact', 'OCA\Contacts\Hooks', 'contactUpdated'); +OCP\Util::connectHook('OCA\Contacts', 'post_updateContact', 'OCA\Contacts\Hooks', 'contactUpdated'); OCP\Util::connectHook('OC_Calendar', 'getEvents', 'OCA\Contacts\Hooks', 'getBirthdayEvents'); OCP\Util::connectHook('OC_Calendar', 'getSources', 'OCA\Contacts\Hooks', 'getCalenderSources'); diff --git a/lib/app.php b/lib/app.php index 85653d04..606b7751 100644 --- a/lib/app.php +++ b/lib/app.php @@ -16,35 +16,9 @@ use Sabre\VObject; App::$l10n = \OC_L10N::get('contacts'); class App { - /* - * @brief language object for calendar app - */ - - public static $l10n; - /* - * @brief categories of the user - */ - public static $categories = null; - const THUMBNAIL_PREFIX = 'contact-thumbnail-'; const THUMBNAIL_SIZE = 28; - /** - * @brief returns the vcategories object of the user - * @return (object) $vcategories - */ - public static function getVCategories() { - if (is_null(self::$categories)) { - if(\OC_VCategories::isEmpty('contact')) { - self::scanCategories(); - } - self::$categories = new \OC_VCategories('contact', - null, - self::getDefaultCategories()); - } - return self::$categories; - } - /** * @brief returns the categories for the user * @return (Array) $categories @@ -142,58 +116,4 @@ class App { return \OC_Cache::get(self::THUMBNAIL_PREFIX . $id); } - public static function updateDBProperties($contactid, $vcard = null) { - $stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_cards_properties` WHERE `contactid` = ?'); - try { - $stmt->execute(array($contactid)); - } catch(\Exception $e) { - \OCP\Util::writeLog('contacts', __METHOD__. - ', exception: ' . $e->getMessage(), \OCP\Util::ERROR); - \OCP\Util::writeLog('contacts', __METHOD__.', id: ' - . $id, \OCP\Util::DEBUG); - throw new \Exception( - App::$l10n->t( - 'There was an error deleting properties for this contact.' - ) - ); - } - - if(is_null($vcard)) { - return; - } - - $stmt = \OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_cards_properties` ' - . '(`userid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)' ); - foreach($vcard->children as $property) { - if(!in_array($property->name, self::$index_properties)) { - continue; - } - $preferred = 0; - foreach($property->parameters as $parameter) { - if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { - $preferred = 1; - break; - } - } - try { - $result = $stmt->execute( - array( - \OCP\User::getUser(), - $contactid, - $property->name, - $property->value, - $preferred, - ) - ); - if (\OC_DB::isError($result)) { - \OCP\Util::writeLog('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); - return false; - } - } - } } diff --git a/lib/backend/database.php b/lib/backend/database.php index acade1b2..a9dcdb28 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -227,7 +227,7 @@ class Database extends AbstractBackend { * @return bool */ public function deleteAddressBook($addressbookid) { - \OC_Hook::emit('\OCA\Contacts\VCard', 'pre_deleteAddressBook', + \OC_Hook::emit('\OCA\Contacts', 'pre_deleteAddressBook', array('id' => $addressbookid) ); @@ -420,8 +420,8 @@ class Database extends AbstractBackend { } $newid = \OCP\DB::insertid($this->cardsTableName); - $this->touch(addressbookid); - \OC_Hook::emit('\OCA\Contacts\VCard', 'post_createVCard', + $this->touchAddressBook(addressbookid); + \OC_Hook::emit('\OCA\Contacts', 'post_createContact', array('id' => $id, 'contact' => $contact) ); return $newid; @@ -485,8 +485,8 @@ class Database extends AbstractBackend { return false; } - $this->touch(addressbookid); - \OC_Hook::emit('\OCA\Contacts\VCard', 'post_updateVCard', + $this->touchAddressBook(addressbookid); + \OC_Hook::emit('\OCA\Contacts', 'post_updateContact', array('id' => $id, 'contact' => $contact) ); return true; @@ -519,7 +519,7 @@ class Database extends AbstractBackend { } else { $qname = 'deletecontactsbyid'; } - \OC_Hook::emit('\OCA\Contacts\VCard', 'pre_deleteContact', + \OC_Hook::emit('\OCA\Contacts', 'pre_deleteContact', array('id' => $id) ); if(!isset(self::$preparedQueries[$qname])) { diff --git a/lib/backend/shared.php b/lib/backend/shared.php index 6d3ef46c..3b04a2f5 100644 --- a/lib/backend/shared.php +++ b/lib/backend/shared.php @@ -82,7 +82,7 @@ class Shared extends Database { public function getContacts($addressbookid, $limit = null, $offset = null, $omitdata = false) { //\OCP\Util::writeLog('contacts', __METHOD__.' addressbookid: ' // . $addressbookid, \OCP\Util::DEBUG); - /*$addressbook = \OCP\Share::getItemSharedWithBySource( + $addressbook = \OCP\Share::getItemSharedWithBySource( 'addressbook', $addressbookid, Contacts\Share_Backend_Addressbook::FORMAT_ADDRESSBOOKS, @@ -90,7 +90,7 @@ class Shared extends Database { true // includeCollection ); \OCP\Util::writeLog('contacts', __METHOD__.' shared: ' - . print_r($addressbook, true), \OCP\Util::DEBUG);*/ + . print_r($addressbook, true), \OCP\Util::DEBUG); $addressbook = $this->getAddressBook($addressbookid); $permissions = $addressbook['permissions']; @@ -123,4 +123,4 @@ class Shared extends Database { return $cards; } -} \ No newline at end of file +} diff --git a/lib/hooks.php b/lib/hooks.php index 184474c7..60c6be37 100644 --- a/lib/hooks.php +++ b/lib/hooks.php @@ -42,7 +42,7 @@ class Hooks{ * @param paramters parameters from postCreateUser-Hook * @return array */ - static public function createUser($parameters) { + public static function userCreated($parameters) { Addressbook::addDefault($parameters['uid']); return true; } @@ -52,17 +52,43 @@ class Hooks{ * @param paramters parameters from postDeleteUser-Hook * @return array */ - static public function deleteUser($parameters) { - $addressbooks = Addressbook::all($parameters['uid']); + public static function userDeleted($parameters) { + $backend = new Backend\Database(); + $addressbook = $backend->getAddressBooksForUser($parameters['uid']); foreach($addressbooks as $addressbook) { - Addressbook::delete($addressbook['id']); + $contacts = $backend->getContacts($addressbook['id'], null, null, true); + foreach($contacts as $contact) { + $backend->deleteContact($addressbook['id'], $contact['id']); + } + \OCP\Share::unshareAll('addressbook', $addressbook['id']); + $backend->deleteAddressBook($addressbook['id']); } return true; } - static public function getCalenderSources($parameters) { + /** + * Delete any registred address books (Future) + */ + public static function addressBookDeletion($parameters) { + } + + public static function contactDeletion($parameters) { + // TODO: Purge contact index + $catctrl = new \OC_VCategories('contact'); + $catctrl->purgeObject($parameters['id']); + Utils\Properties::updateIndex($parameters['id']); + + // Contact sharing not implemented, but keep for future. + //\OCP\Share::unshareAll('contact', $id); + } + + public static function contactUpdated($parameters) { + Utils\Properties::updateIndex($parameters['id'], $parameters['contact']); + } + + public static function getCalenderSources($parameters) { $base_url = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id='; foreach(Addressbook::all(\OCP\USER::getUser()) as $addressbook) { $parameters['sources'][] @@ -77,7 +103,7 @@ class Hooks{ } } - static public function getBirthdayEvents($parameters) { + public static function getBirthdayEvents($parameters) { $name = $parameters['calendar_id']; if (strpos($name, 'birthday_') != 0) { return; diff --git a/lib/share/addressbook.php b/lib/share/addressbook.php index 65f86cce..ba0848a6 100644 --- a/lib/share/addressbook.php +++ b/lib/share/addressbook.php @@ -79,6 +79,8 @@ class Share_Backend_Addressbook implements \OCP\Share_Backend_Collection { * It is only called through calls to the public getItem(s)Shared(With) functions. */ public function formatItems($items, $format, $parameters = null, $include = false) { + \OCP\Util::writeLog('contacts', __METHOD__ + . ' ' . $include . ' ' . print_r($items, true), \OCP\Util::DEBUG); $addressbooks = array(); if ($format == self::FORMAT_ADDRESSBOOKS) { foreach ($items as $item) { @@ -91,6 +93,9 @@ class Share_Backend_Addressbook implements \OCP\Share_Backend_Collection { $addressbooks[] = $addressbook; } } + } else { + foreach ($items as $item) { + } } return $addressbooks; } diff --git a/lib/utils/properties.php b/lib/utils/properties.php index f43c04d0..e927c686 100644 --- a/lib/utils/properties.php +++ b/lib/utils/properties.php @@ -25,6 +25,10 @@ namespace OCA\Contacts\Utils; Properties::$l10n = \OC_L10N::get('contacts'); Class Properties { + + private static $deleteindexstmt; + private static $updateindexstmt; + /** * @brief language object for calendar app * @@ -179,4 +183,65 @@ Class Properties { (string)$l10n->t('Other'), ); } + + public static function updateIndex($contactid, $vcard = null) { + if(!isset(self::$deleteindexstmt)) { + self::$deleteindexstmt + = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_cards_properties`'; + . ' WHERE `contactid` = ?'); + } + try { + self::$deleteindexstmt->execute(array($contactid)); + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__. + ', exception: ' . $e->getMessage(), \OCP\Util::ERROR); + \OCP\Util::writeLog('contacts', __METHOD__.', id: ' + . $id, \OCP\Util::DEBUG); + throw new \Exception( + App::$l10n->t( + 'There was an error deleting properties for this contact.' + ) + ); + } + + if(is_null($vcard)) { + return; + } + + if(!isset(self::$updateindexstmt)) { + self::$updateindexstmt = \OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_cards_properties` ' + . '(`userid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)' ); + } + foreach($vcard->children as $property) { + if(!in_array($property->name, self::$index_properties)) { + continue; + } + $preferred = 0; + foreach($property->parameters as $parameter) { + if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { + $preferred = 1; + break; + } + } + try { + $result = self::$updateindexstmt->execute( + array( + \OCP\User::getUser(), + $contactid, + $property->name, + $property->value, + $preferred, + ) + ); + if (\OC_DB::isError($result)) { + \OCP\Util::writeLog('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); + return false; + } + } + } }