2011-08-10 14:28:14 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Addressbook
|
|
|
|
*
|
|
|
|
* @author Jakob Sack
|
|
|
|
* @copyright 2011 Jakob Sack mail@jakobsack.de
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-06-26 05:26:50 +02:00
|
|
|
/**
|
|
|
|
* The following signals are being emitted:
|
2012-09-07 14:32:45 +02:00
|
|
|
*
|
2012-10-25 03:34:12 +02:00
|
|
|
* OCA\Contacts\VCard::post_moveToAddressbook(array('aid' => $aid, 'id' => $id))
|
|
|
|
* OCA\Contacts\VCard::pre_deleteVCard(array('aid' => $aid, 'id' => $id, 'uri' = $uri)); (NOTE: the values can be null depending on which method emits them)
|
|
|
|
* OCA\Contacts\VCard::post_updateVCard($id)
|
|
|
|
* OCA\Contacts\VCard::post_createVCard($newid)
|
2012-06-26 05:26:50 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
namespace OCA\Contacts;
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
use \Sabre\VObject;
|
|
|
|
|
2011-08-10 14:28:14 +02:00
|
|
|
/**
|
|
|
|
* This class contains all hooks.
|
|
|
|
*/
|
2012-10-25 03:34:12 +02:00
|
|
|
class Hooks{
|
2012-07-17 10:45:15 +02:00
|
|
|
/**
|
|
|
|
* @brief Add default Addressbook for a certain user
|
|
|
|
* @param paramters parameters from postCreateUser-Hook
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-03-13 08:52:00 +01:00
|
|
|
public static function userCreated($parameters) {
|
2013-03-16 15:56:21 +01:00
|
|
|
//Addressbook::addDefault($parameters['uid']);
|
2012-07-17 10:45:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-10 14:28:14 +02:00
|
|
|
/**
|
|
|
|
* @brief Deletes all Addressbooks of a certain user
|
|
|
|
* @param paramters parameters from postDeleteUser-Hook
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-03-13 08:52:00 +01:00
|
|
|
public static function userDeleted($parameters) {
|
|
|
|
$backend = new Backend\Database();
|
|
|
|
$addressbook = $backend->getAddressBooksForUser($parameters['uid']);
|
2011-09-17 00:26:57 +02:00
|
|
|
|
2011-08-10 14:28:14 +02:00
|
|
|
foreach($addressbooks as $addressbook) {
|
2013-03-29 05:00:03 +01:00
|
|
|
// Purging of contact categories and and properties is done by backend.
|
2013-03-13 08:52:00 +01:00
|
|
|
$backend->deleteAddressBook($addressbook['id']);
|
2011-08-10 14:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 20:51:59 +01:00
|
|
|
|
2013-03-13 08:52:00 +01:00
|
|
|
/**
|
|
|
|
* Delete any registred address books (Future)
|
|
|
|
*/
|
|
|
|
public static function addressBookDeletion($parameters) {
|
2013-05-27 01:46:02 +02:00
|
|
|
// Clean up sharing
|
|
|
|
\OCP\Share::unshareAll('addressbook', $parameters['addressbookid']);
|
|
|
|
|
|
|
|
if(count($parameters['contactids'])) {
|
|
|
|
// Remove contacts from groups
|
|
|
|
$catctrl = new \OC_VCategories('contact');
|
|
|
|
$catctrl->purgeObjects($parameters['contactids']);
|
|
|
|
|
|
|
|
// Purge property indexes
|
|
|
|
Utils\Properties::purgeIndexes($parameters['contactids']);
|
|
|
|
}
|
2013-03-13 08:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-05-25 01:35:08 +02:00
|
|
|
/**
|
|
|
|
* A contact has been deleted and cleanup for property indexes and
|
|
|
|
* group/contact relations must be performed.
|
|
|
|
*
|
2013-05-27 06:55:21 +02:00
|
|
|
* NOTE: When deleting an entire address book the cleanup is done in the
|
|
|
|
* addressBookDeletion() hook. Any cleanup procedures most be implemented
|
|
|
|
* in both.
|
2013-05-25 01:35:08 +02:00
|
|
|
*
|
|
|
|
* @param array $parameters Currently only the id of the contact.
|
|
|
|
*/
|
2013-03-13 08:52:00 +01:00
|
|
|
public static function contactDeletion($parameters) {
|
2013-05-25 01:35:08 +02:00
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
|
2013-05-27 06:55:21 +02:00
|
|
|
$ids = is_array($parameters['id']) ? $parameters['id'] : array($parameters['id']);
|
2013-03-13 08:52:00 +01:00
|
|
|
$catctrl = new \OC_VCategories('contact');
|
2013-05-27 06:55:21 +02:00
|
|
|
$catctrl->purgeObjects($ids);
|
|
|
|
Utils\Properties::purgeIndexes($ids);
|
2013-03-13 08:52:00 +01:00
|
|
|
|
|
|
|
// Contact sharing not implemented, but keep for future.
|
|
|
|
//\OCP\Share::unshareAll('contact', $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function contactUpdated($parameters) {
|
2013-03-29 05:00:03 +01:00
|
|
|
//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
|
2013-05-25 01:35:08 +02:00
|
|
|
$contact = $parameters['contact'];
|
|
|
|
if(isset($contact->CATEGORIES)) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.' groups: '.print_r($contact->CATEGORIES->getParts(), true), \OCP\Util::DEBUG);
|
|
|
|
$catctrl = new \OC_VCategories('contact');
|
|
|
|
foreach($contact->CATEGORIES->getParts() as $group) {
|
|
|
|
\OCP\Util::writeLog('contacts', __METHOD__.' group: '.$group, \OCP\Util::DEBUG);
|
|
|
|
$catctrl->addToCategory($parameters['id'], $group);
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 08:52:00 +01:00
|
|
|
Utils\Properties::updateIndex($parameters['id'], $parameters['contact']);
|
|
|
|
}
|
|
|
|
|
2013-03-30 04:06:07 +01:00
|
|
|
/**
|
|
|
|
* Scan vCards for categories.
|
|
|
|
*/
|
|
|
|
public static function scanCategories() {
|
|
|
|
$offset = 0;
|
|
|
|
$limit = 10;
|
|
|
|
|
|
|
|
$categories = new \OC_VCategories('contact');
|
|
|
|
|
|
|
|
$app = new App();
|
2013-04-07 22:33:40 +02:00
|
|
|
$backend = $app->getBackend('local');
|
2013-03-30 04:06:07 +01:00
|
|
|
$addressBookInfos = $backend->getAddressBooksForUser();
|
|
|
|
|
|
|
|
foreach($addressBookInfos as $addressBookInfo) {
|
2013-04-01 03:46:45 +02:00
|
|
|
$addressBook = new AddressBook($backend, $addressBookInfo);
|
2013-03-30 04:06:07 +01:00
|
|
|
while($contacts = $addressBook->getChildren($limit, $offset, false)) {
|
|
|
|
foreach($contacts as $contact) {
|
|
|
|
$cards[] = array($contact['id'], $contact['carddata']);
|
|
|
|
}
|
|
|
|
\OCP\Util::writeLog('contacts',
|
|
|
|
__CLASS__.'::'.__METHOD__
|
|
|
|
.', scanning: ' . $limit . ' starting from ' . $offset,
|
|
|
|
\OCP\Util::DEBUG);
|
|
|
|
// only reset on first batch.
|
|
|
|
$categories->rescan($cards, true, ($offset === 0 ? true : false));
|
|
|
|
$offset += $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 03:46:45 +02:00
|
|
|
/**
|
|
|
|
* Scan vCards for categories.
|
|
|
|
*/
|
|
|
|
public static function indexProperties() {
|
|
|
|
$offset = 0;
|
|
|
|
$limit = 10;
|
|
|
|
|
|
|
|
$app = new App();
|
2013-04-07 22:33:40 +02:00
|
|
|
$backend = $app->getBackend('local');
|
2013-04-01 03:46:45 +02:00
|
|
|
$addressBookInfos = $backend->getAddressBooksForUser();
|
|
|
|
|
|
|
|
foreach($addressBookInfos as $addressBookInfo) {
|
|
|
|
$addressBook = new AddressBook($backend, $addressBookInfo);
|
|
|
|
while($contacts = $addressBook->getChildren($limit, $offset, false)) {
|
|
|
|
foreach($contacts as $contact) {
|
|
|
|
$contact->retrieve();
|
|
|
|
}
|
|
|
|
\OCP\Util::writeLog('contacts',
|
|
|
|
__CLASS__.'::'.__METHOD__
|
|
|
|
.', indexing: ' . $limit . ' starting from ' . $offset,
|
|
|
|
\OCP\Util::DEBUG);
|
|
|
|
Utils\Properties::updateIndex($contact->getId(), $contact);
|
|
|
|
$offset += $limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 08:52:00 +01:00
|
|
|
public static function getCalenderSources($parameters) {
|
2013-03-20 11:26:39 +01:00
|
|
|
/*
|
2012-10-25 03:34:12 +02:00
|
|
|
$base_url = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id=';
|
|
|
|
foreach(Addressbook::all(\OCP\USER::getUser()) as $addressbook) {
|
2012-09-07 14:32:45 +02:00
|
|
|
$parameters['sources'][]
|
2012-07-21 00:42:21 +02:00
|
|
|
= array(
|
2012-03-01 20:51:59 +01:00
|
|
|
'url' => $base_url.'birthday_'. $addressbook['id'],
|
|
|
|
'backgroundColor' => '#cccccc',
|
|
|
|
'borderColor' => '#888',
|
|
|
|
'textColor' => 'black',
|
|
|
|
'cache' => true,
|
|
|
|
'editable' => false,
|
|
|
|
);
|
|
|
|
}
|
2013-03-20 11:26:39 +01:00
|
|
|
*/
|
2012-03-01 20:51:59 +01:00
|
|
|
}
|
|
|
|
|
2013-03-13 08:52:00 +01:00
|
|
|
public static function getBirthdayEvents($parameters) {
|
2012-03-01 20:51:59 +01:00
|
|
|
$name = $parameters['calendar_id'];
|
2012-06-14 22:40:18 +02:00
|
|
|
if (strpos($name, 'birthday_') != 0) {
|
2012-03-01 20:51:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$info = explode('_', $name);
|
|
|
|
$aid = $info[1];
|
2012-10-25 03:34:12 +02:00
|
|
|
Addressbook::find($aid);
|
2012-11-22 00:14:03 +01:00
|
|
|
foreach(VCard::all($aid) as $contact) {
|
|
|
|
try {
|
|
|
|
$vcard = VObject\Reader::read($contact['carddata']);
|
|
|
|
} catch (Exception $e) {
|
2012-05-02 21:34:34 +02:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-01 20:51:59 +01:00
|
|
|
$birthday = $vcard->BDAY;
|
2012-12-14 04:33:14 +01:00
|
|
|
if ((string)$birthday) {
|
2013-03-06 14:47:26 +01:00
|
|
|
$title = str_replace('{name}',
|
2013-04-19 12:44:23 +02:00
|
|
|
strtr((string)$vcard->FN, array('\,' => ',', '\;' => ';')),
|
2013-03-06 14:47:26 +01:00
|
|
|
App::$l10n->t('{name}\'s Birthday'));
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
$date = new \DateTime($birthday);
|
2012-11-22 00:14:03 +01:00
|
|
|
$vevent = VObject\Component::create('VEVENT');
|
2012-06-29 15:40:46 +02:00
|
|
|
//$vevent->setDateTime('LAST-MODIFIED', new DateTime($vcard->REV));
|
2012-11-22 00:14:03 +01:00
|
|
|
$vevent->add('DTSTART');
|
|
|
|
$vevent->DTSTART->setDateTime($date,
|
|
|
|
VObject\Property\DateTime::DATE);
|
|
|
|
$vevent->add('DURATION', 'P1D');
|
|
|
|
$vevent->{'UID'} = substr(md5(rand().time()), 0, 10);
|
2012-03-01 20:51:59 +01:00
|
|
|
// DESCRIPTION?
|
2012-11-22 00:14:03 +01:00
|
|
|
$vevent->{'RRULE'} = 'FREQ=YEARLY';
|
2013-03-06 14:47:26 +01:00
|
|
|
$vevent->{'SUMMARY'} = $title;
|
2012-03-01 20:51:59 +01:00
|
|
|
$parameters['events'][] = array(
|
|
|
|
'id' => 0,//$card['id'],
|
|
|
|
'vevent' => $vevent,
|
|
|
|
'repeating' => true,
|
|
|
|
'summary' => $title,
|
2012-07-21 00:42:21 +02:00
|
|
|
'calendardata' => "BEGIN:VCALENDAR\nVERSION:2.0\n"
|
2012-09-07 14:32:45 +02:00
|
|
|
. "PRODID:ownCloud Contacts "
|
2012-10-25 03:34:12 +02:00
|
|
|
. \OCP\App::getAppVersion('contacts') . "\n"
|
2012-07-21 00:42:21 +02:00
|
|
|
. $vevent->serialize() . "END:VCALENDAR"
|
2012-03-01 20:51:59 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-10 14:28:14 +02:00
|
|
|
}
|