1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-10 21:24:33 +01:00
OwncloudContactsOfficial/lib/hooks.php

260 lines
8.3 KiB
PHP
Raw Normal View History

2011-08-10 14:28:14 +02:00
<?php
/**
* ownCloud - Addressbook
*
* @author Jakob Sack
2014-01-26 00:40:22 +01:00
* @author Thomas Tanghus
2011-08-10 14:28:14 +02:00
* @copyright 2011 Jakob Sack mail@jakobsack.de
2014-01-26 00:40:22 +01:00
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
2011-08-10 14:28:14 +02:00
*
* 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{
/**
* @brief Add default Addressbook for a certain user
* @param paramters parameters from postCreateUser-Hook
* @return array
*/
public static function userCreated($parameters) {
//Addressbook::addDefault($parameters['uid']);
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
*/
public static function userDeleted($parameters) {
2013-09-14 15:53:53 +02:00
$backend = new Backend\Database($parameters['uid']);
$addressBooks = $backend->getAddressBooksForUser();
2013-09-14 15:53:53 +02:00
foreach($addressBooks as $addressBook) {
// Purging of contact categories and and properties is done by backend.
2013-09-14 15:53:53 +02:00
$backend->deleteAddressBook($addressBook['id']);
2011-08-10 14:28:14 +02:00
}
}
/**
* Delete any registred address books (Future)
*/
public static function addressBookDeletion($parameters) {
// Clean up sharing
\OCP\Share::unshareAll('addressbook', $parameters['addressbookid']);
if(count($parameters['contactids'])) {
// Remove contacts from groups
2013-09-26 20:44:47 +02:00
$tagMgr = \OC::$server->getTagManager()->load('contact');
2013-09-24 13:39:45 +02:00
$tagMgr->purgeObjects($parameters['contactids']);
// Purge property indexes
Utils\Properties::purgeIndexes($parameters['contactids']);
}
}
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.
*
* 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.
*/
public static function contactDeletion($parameters) {
2014-03-18 23:51:32 +01:00
\OCP\Util::writeLog('contacts', __METHOD__.' id: '.print_r($parameters['id'], true), \OCP\Util::DEBUG);
$ids = is_array($parameters['id']) ? $parameters['id'] : array($parameters['id']);
2013-09-26 20:44:47 +02:00
$tagMgr = \OC::$server->getTagManager()->load('contact');
2013-09-24 13:39:45 +02:00
$tagMgr->purgeObjects($ids);
Utils\Properties::purgeIndexes($ids);
// Contact sharing not implemented, but keep for future.
//\OCP\Share::unshareAll('contact', $id);
}
2013-06-27 15:52:18 +02:00
public static function contactAdded($parameters) {
2013-09-24 13:39:45 +02:00
\OCP\Util::writeLog('contacts', __METHOD__.' id: '.$parameters['id'], \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);
2013-09-26 20:44:47 +02:00
$tagMgr = \OC::$server->getTagManager()->load('contact');
2013-05-25 01:35:08 +02:00
foreach($contact->CATEGORIES->getParts() as $group) {
\OCP\Util::writeLog('contacts', __METHOD__.' group: '.$group, \OCP\Util::DEBUG);
2013-09-24 13:39:45 +02:00
$tagMgr->tagAs($parameters['id'], $group);
2013-05-25 01:35:08 +02:00
}
}
2013-06-27 15:52:18 +02:00
Utils\Properties::updateIndex($parameters['id'], $contact);
}
public static function contactUpdated($parameters) {
//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
$contact = $parameters['contact'];
Utils\Properties::updateIndex($parameters['contactId'], $contact);
// If updated via CardDAV we don't know if PHOTO has changed
if(isset($parameters['carddav']) && $parameters['carddav']) {
if(isset($contact->PHOTO) || isset($contact->LOGO)) {
Utils\Properties::cacheThumbnail(
$parameters['backend'],
$parameters['addressBookId'],
$parameters['contactId'],
null,
$contact,
array('update' => true)
);
}
$tagMgr = \OC::$server->getTagManager()->load('contact');
$tagMgr->purgeObjects(array($parameters['contactId']));
if(isset($contact->CATEGORIES)) {
$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['contactId']);
}
}
}
/**
* Scan vCards for categories.
*/
public static function scanCategories() {
$offset = 0;
$limit = 10;
2013-09-26 20:44:47 +02:00
$tagMgr = \OC::$server->getTagManager()->load('contact');
2013-09-24 13:39:45 +02:00
$tags = array();
2014-03-09 15:03:21 +01:00
foreach ($tagMgr->getTags() as $tag) {
2013-09-24 13:39:45 +02:00
$tags[] = $tag['name'];
}
// reset tags
$tagMgr->delete($tags);
2014-03-08 04:32:18 +01:00
$app = new App();
$backend = $app->getBackend('local');
$addressBookInfos = $backend->getAddressBooksForUser();
2014-03-09 15:03:21 +01:00
foreach ($addressBookInfos as $addressBookInfo) {
$addressBook = new AddressBook($backend, $addressBookInfo);
2014-03-09 15:03:21 +01:00
while ($contacts = $addressBook->getChildren($limit, $offset, false)) {
foreach ($contacts as $contact) {
if (isset($contact->CATEGORIES)) {
2013-09-24 13:39:45 +02:00
$tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $contact->getId());
}
}
\OCP\Util::writeLog('contacts',
2013-09-24 13:39:45 +02:00
__METHOD__ .', scanning: ' . $limit . ' starting from ' . $offset,
\OCP\Util::DEBUG);
$offset += $limit;
}
}
}
/**
2013-09-24 13:39:45 +02:00
* Scan vCards for properties.
*/
public static function indexProperties() {
$offset = 0;
$limit = 10;
$app = new App();
$backend = $app->getBackend('local');
$addressBookInfos = $backend->getAddressBooksForUser();
2014-03-09 15:03:21 +01:00
foreach ($addressBookInfos as $addressBookInfo) {
$addressBook = new AddressBook($backend, $addressBookInfo);
2014-09-07 10:34:25 +02:00
$contacts = $addressBook->getChildren($limit, $offset, false);
\OCP\Util::writeLog('contacts',
__METHOD__ . ', indexing: ' . $limit . ' starting from ' . $offset,
\OCP\Util::DEBUG);
foreach ($contacts as $contact) {
if(!$contact->retrieve()) {
\OCP\Util::writeLog('contacts',
__METHOD__ . ', Error loading contact ' .print_r($contact, true),
\OCP\Util::DEBUG);
2014-03-18 15:14:11 +01:00
}
2014-09-07 10:34:25 +02:00
Utils\Properties::updateIndex($contact->getId(), $contact);
}
2014-09-07 10:34:25 +02:00
$offset += $limit;
}
$stmt = \OCP\DB::prepare('DELETE FROM `*PREFIX*contacts_cards_properties`
WHERE NOT EXISTS(SELECT NULL
FROM `*PREFIX*contacts_cards`
WHERE `*PREFIX*contacts_cards`.id = `*PREFIX*contacts_cards_properties`.contactid)');
$result = $stmt->execute(array());
}
public static function getCalenderSources($parameters) {
//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
$app = new App();
$addressBooks = $app->getAddressBooksForUser();
2014-03-18 00:58:01 +01:00
$baseUrl = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id=';
2014-03-09 15:03:21 +01:00
foreach ($addressBooks as $addressBook) {
$info = $addressBook->getMetaData();
2012-09-07 14:32:45 +02:00
$parameters['sources'][]
2012-07-21 00:42:21 +02:00
= array(
2014-03-18 00:58:01 +01:00
'url' => $baseUrl . 'birthday_'. $info['backend'].'_' . $info['id'],
'backgroundColor' => '#cccccc',
'borderColor' => '#888',
'textColor' => 'black',
'cache' => true,
'editable' => false,
);
}
}
public static function getBirthdayEvents($parameters) {
//\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
$name = $parameters['calendar_id'];
2014-03-09 15:03:21 +01:00
if (strpos($name, 'birthday_') != 0) {
return;
}
2014-03-09 15:03:21 +01:00
$info = explode('_', $name);
$backend = $info[1];
$aid = $info[2];
$app = new App();
$addressBook = $app->getAddressBook($backend, $aid);
2014-03-09 15:03:21 +01:00
foreach ($addressBook->getBirthdayEvents() as $vevent) {
$parameters['events'][] = array(
'id' => 0,
'vevent' => $vevent,
'repeating' => true,
'summary' => $vevent->SUMMARY,
'calendardata' => $vevent->serialize()
);
}
}
2011-08-10 14:28:14 +02:00
}