1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00

Contacts: Moved misc. functionality to hooks and utility classes.

This commit is contained in:
Thomas Tanghus 2013-03-13 08:52:00 +01:00
parent 4c093890f1
commit 3d278c0e11
7 changed files with 117 additions and 97 deletions

View File

@ -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');

View File

@ -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;
}
}
}
}

View File

@ -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])) {

View File

@ -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;
}
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}
}
}
}