2011-12-05 21:51:25 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2013-03-16 15:59:23 +01:00
|
|
|
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
|
2011-12-05 21:51:25 +01:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
namespace OCA\Contacts;
|
|
|
|
|
2012-11-22 00:14:03 +01:00
|
|
|
use Sabre\VObject;
|
|
|
|
|
2011-12-05 21:51:25 +01:00
|
|
|
/**
|
|
|
|
* This class manages our app actions
|
|
|
|
*/
|
2012-10-25 03:34:12 +02:00
|
|
|
App::$l10n = \OC_L10N::get('contacts');
|
|
|
|
|
|
|
|
class App {
|
2013-03-22 14:58:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Categories of the user
|
|
|
|
* @var OC_VCategories
|
|
|
|
*/
|
|
|
|
public static $categories = null;
|
2012-10-24 21:35:51 +02:00
|
|
|
|
2013-03-15 17:01:06 +01:00
|
|
|
/**
|
|
|
|
* @brief language object for calendar app
|
|
|
|
*
|
|
|
|
* @var OC_L10N
|
|
|
|
*/
|
|
|
|
public static $l10n;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array holding the current users address books.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $addressBooks = array();
|
|
|
|
/**
|
|
|
|
* If backends are added to this map, they will be automatically mapped
|
2013-03-25 17:10:21 +01:00
|
|
|
* to their respective classes, if constructed with the 'getBackend' method.
|
2013-03-15 17:01:06 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $backendClasses = array(
|
|
|
|
'database' => 'OCA\Contacts\Backend\Database',
|
|
|
|
'shared' => 'OCA\Contacts\Backend\Shared',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function __construct(
|
2013-03-22 14:58:32 +01:00
|
|
|
$user = null,
|
2013-03-15 17:01:06 +01:00
|
|
|
$addressBooksTableName = '*PREFIX*addressbook',
|
|
|
|
$backendsTableName = '*PREFIX*addressbooks_backend',
|
|
|
|
$dbBackend = null
|
|
|
|
) {
|
2013-03-22 14:58:32 +01:00
|
|
|
$this->user = $user ? $user : \OCP\User::getUser();
|
2013-03-15 17:01:06 +01:00
|
|
|
$this->addressBooksTableName = $addressBooksTableName;
|
|
|
|
$this->backendsTableName = $backendsTableName;
|
2013-03-22 14:58:32 +01:00
|
|
|
$this->dbBackend = $dbBackend
|
|
|
|
? $dbBackend
|
|
|
|
: new Backend\Database($user);
|
2013-03-15 17:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-25 17:10:21 +01:00
|
|
|
* Gets backend by name.
|
2013-03-15 17:01:06 +01:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return \Backend\AbstractBackend
|
|
|
|
*/
|
2013-03-25 17:10:21 +01:00
|
|
|
static public function getBackend($name, $user = null) {
|
2013-03-15 17:01:06 +01:00
|
|
|
$name = $name ? $name : 'database';
|
|
|
|
if (isset(self::$backendClasses[$name])) {
|
2013-03-25 17:10:21 +01:00
|
|
|
return new self::$backendClasses[$name]($user);
|
2013-03-15 17:01:06 +01:00
|
|
|
} else {
|
|
|
|
throw new \Exception('No backend for: ' . $name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all registered address books for current user.
|
|
|
|
* For now this is hard-coded to using the Database and
|
|
|
|
* Shared backends, but eventually admins will be able to
|
|
|
|
* register additional backends, and users will be able to
|
|
|
|
* subscribe to address books using those backends.
|
|
|
|
*
|
|
|
|
* @return AddressBook[]
|
|
|
|
*/
|
2013-03-22 14:58:32 +01:00
|
|
|
public function getAddressBooksForUser() {
|
2013-03-15 17:01:06 +01:00
|
|
|
if(!self::$addressBooks) {
|
|
|
|
foreach(array_keys(self::$backendClasses) as $backendName) {
|
2013-03-25 17:10:21 +01:00
|
|
|
$backend = self::getBackend($backendName, $this->user);
|
2013-03-15 17:01:06 +01:00
|
|
|
$addressBooks = $backend->getAddressBooksForUser();
|
2013-03-28 03:36:23 +01:00
|
|
|
if($backendName === 'database' && count($addressBooks) === 0) {
|
|
|
|
$id = $backend->createAddressBook(array('displayname' => 'Contacts'));
|
|
|
|
if($id !== false) {
|
|
|
|
$addressBook = $backend->getAddressBook($id);
|
|
|
|
$addressBooks = array($addressBook);
|
|
|
|
} else {
|
|
|
|
// TODO: Write log
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 17:01:06 +01:00
|
|
|
foreach($addressBooks as $addressBook) {
|
|
|
|
$addressBook['backend'] = $backendName;
|
|
|
|
self::$addressBooks[] = new AddressBook($backend, $addressBook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self::$addressBooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an address book from a specific backend.
|
|
|
|
*
|
|
|
|
* @param string $backendName
|
|
|
|
* @param string $addressbookid
|
2013-03-16 15:59:23 +01:00
|
|
|
* @return AddressBook|null
|
2013-03-15 17:01:06 +01:00
|
|
|
*/
|
|
|
|
public function getAddressBook($backendName, $addressbookid) {
|
|
|
|
foreach(self::$addressBooks as $addressBook) {
|
|
|
|
if($addressBook->backend->name === $backendName
|
|
|
|
&& $addressBook->getId() === $addressbookid
|
|
|
|
) {
|
|
|
|
return $addressBook;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Check for return values
|
2013-03-25 17:10:21 +01:00
|
|
|
$backend = self::getBackend($backendName, $this->user);
|
2013-03-15 17:01:06 +01:00
|
|
|
$info = $backend->getAddressBook($addressbookid);
|
|
|
|
// FIXME: Backend name should be set by the backend.
|
|
|
|
$info['backend'] = $backendName;
|
|
|
|
$addressBook = new AddressBook($backend, $info);
|
|
|
|
self::$addressBooks[] = $addressBook;
|
|
|
|
return $addressBook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Contact from an address book from a specific backend.
|
|
|
|
*
|
|
|
|
* @param string $backendName
|
|
|
|
* @param string $addressbookid
|
|
|
|
* @param string $id - Contact id
|
2013-03-16 15:59:23 +01:00
|
|
|
* @return Contact|null
|
|
|
|
*
|
2013-03-15 17:01:06 +01:00
|
|
|
*/
|
|
|
|
public function getContact($backendName, $addressbookid, $id) {
|
|
|
|
$addressBook = $this->getAddressBook($backendName, $addressbookid);
|
|
|
|
// TODO: Check for return value
|
|
|
|
return $addressBook->getChild($id);
|
|
|
|
}
|
|
|
|
|
2013-03-22 14:58:32 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2012-06-27 02:12:14 +02:00
|
|
|
/**
|
2012-05-13 23:25:51 +02:00
|
|
|
* @brief returns the categories for the user
|
|
|
|
* @return (Array) $categories
|
|
|
|
*/
|
2012-09-17 16:15:31 +02:00
|
|
|
public static function getCategories($format = null) {
|
|
|
|
$categories = self::getVCategories()->categories($format);
|
2012-06-27 02:12:14 +02:00
|
|
|
return ($categories ? $categories : self::getDefaultCategories());
|
2012-04-12 22:31:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scan vcards for categories.
|
|
|
|
* @param $vccontacts VCards to scan. null to check all vcards for the current user.
|
|
|
|
*/
|
|
|
|
public static function scanCategories($vccontacts = null) {
|
|
|
|
if (is_null($vccontacts)) {
|
2012-11-01 15:31:36 +01:00
|
|
|
$vcaddressbooks = Addressbook::all(\OCP\USER::getUser());
|
2012-04-12 22:31:28 +02:00
|
|
|
if(count($vcaddressbooks) > 0) {
|
|
|
|
$vcaddressbookids = array();
|
|
|
|
foreach($vcaddressbooks as $vcaddressbook) {
|
2012-11-01 15:31:36 +01:00
|
|
|
if($vcaddressbook['userid'] === \OCP\User::getUser()) {
|
2012-10-03 22:03:29 +02:00
|
|
|
$vcaddressbookids[] = $vcaddressbook['id'];
|
|
|
|
}
|
2012-04-12 22:31:28 +02:00
|
|
|
}
|
2012-07-09 00:16:14 +02:00
|
|
|
$start = 0;
|
|
|
|
$batchsize = 10;
|
2012-10-25 03:34:12 +02:00
|
|
|
$categories = new \OC_VCategories('contact');
|
2012-07-30 05:31:06 +02:00
|
|
|
while($vccontacts =
|
2012-10-30 07:08:41 +01:00
|
|
|
VCard::all($vcaddressbookids, $start, $batchsize)) {
|
2012-07-09 00:16:14 +02:00
|
|
|
$cards = array();
|
|
|
|
foreach($vccontacts as $vccontact) {
|
2012-09-11 14:19:28 +02:00
|
|
|
$cards[] = array($vccontact['id'], $vccontact['carddata']);
|
2012-07-09 00:16:14 +02:00
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::writeLog('contacts',
|
2012-07-30 05:31:06 +02:00
|
|
|
__CLASS__.'::'.__METHOD__
|
|
|
|
.', scanning: '.$batchsize.' starting from '.$start,
|
2012-10-25 03:34:12 +02:00
|
|
|
\OCP\Util::DEBUG);
|
2012-07-09 00:16:14 +02:00
|
|
|
// only reset on first batch.
|
2012-09-11 14:19:28 +02:00
|
|
|
$categories->rescan($cards,
|
2012-07-30 05:31:06 +02:00
|
|
|
true,
|
|
|
|
($start == 0 ? true : false));
|
2012-07-09 00:16:14 +02:00
|
|
|
$start += $batchsize;
|
|
|
|
}
|
2012-04-12 22:31:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-05 21:51:25 +01:00
|
|
|
}
|