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

165 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
2014-01-26 00:40:22 +01:00
* @author Thomas Tanghus
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
*
* 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;
2013-09-17 18:46:59 +02:00
use Sabre\VObject,
OCP\AppFramework,
OCA\Contacts\Controller\AddressBookController,
2014-05-09 20:30:14 +02:00
OCA\Contacts\Controller\BackendController,
2013-09-17 18:46:59 +02:00
OCA\Contacts\Controller\GroupController,
OCA\Contacts\Controller\ContactController,
OCA\Contacts\Controller\ContactPhotoController,
OCA\Contacts\Controller\SettingsController,
OCA\Contacts\Controller\ImportController;
2012-11-22 00:14:03 +01:00
/**
* This class manages our app actions
2013-10-18 15:29:16 +02:00
*
* TODO: Merge in Dispatcher
*/
2012-10-25 03:34:12 +02:00
App::$l10n = \OC_L10N::get('contacts');
class App {
/**
* @brief Categories of the user
* @var OC_VCategories
*/
public static $categories = null;
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(
'local' => 'OCA\Contacts\Backend\Database',
2013-03-15 17:01:06 +01:00
'shared' => 'OCA\Contacts\Backend\Shared',
2014-06-19 15:16:12 +02:00
// 'localusers' => 'OC\Contacts\Backend\LocalUsers',
2013-03-15 17:01:06 +01:00
);
public function __construct(
$user = null,
2013-03-15 17:01:06 +01:00
$addressBooksTableName = '*PREFIX*addressbook',
$backendsTableName = '*PREFIX*addressbooks_backend',
$dbBackend = null
) {
$this->user = $user ? $user : \OCP\User::getUser();
2013-03-15 17:01:06 +01:00
$this->addressBooksTableName = $addressBooksTableName;
$this->backendsTableName = $backendsTableName;
$this->dbBackend = $dbBackend
? $dbBackend
: new Backend\Database($user);
2014-05-09 20:30:14 +02:00
if (\OCP\Config::getAppValue('contacts', 'backend_ldap', "false") === "true") {
self::$backendClasses['ldap'] = 'OCA\Contacts\Backend\Ldap';
}
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-10-17 02:10:34 +02:00
public function getBackend($name) {
$name = $name ? $name : 'local';
2013-03-15 17:01:06 +01:00
if (isset(self::$backendClasses[$name])) {
2013-10-17 02:10:34 +02:00
return new self::$backendClasses[$name]($this->user);
2013-03-15 17:01:06 +01:00
} else {
2013-05-26 22:15:52 +02:00
throw new \Exception('No backend for: ' . $name, '404');
2013-03-15 17:01:06 +01:00
}
}
/**
* 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[]
*/
public function getAddressBooksForUser() {
2014-03-08 19:09:17 +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();
2014-03-08 19:09:17 +01:00
foreach ($addressBooks as $addressBook) {
2013-03-15 17:01:06 +01:00
$addressBook['backend'] = $backendName;
self::$addressBooks[] = new AddressBook($backend, $addressBook);
}
2014-03-08 19:09:17 +01:00
2013-03-15 17:01:06 +01:00
}
2014-03-08 19:09:17 +01:00
2013-03-15 17:01:06 +01:00
}
2014-03-08 19:09:17 +01:00
2013-03-15 17:01:06 +01:00
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) {
2013-10-17 02:10:34 +02:00
//\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG);
2014-03-08 19:09:17 +01:00
foreach (self::$addressBooks as $addressBook) {
if ($addressBook->getBackend()->name === $backendName
2013-03-15 17:01:06 +01:00
&& $addressBook->getId() === $addressbookid
) {
return $addressBook;
}
}
2013-10-17 02:10:34 +02:00
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);
2014-03-08 19:09:17 +01:00
if (!$info) {
2013-05-26 22:15:52 +02:00
throw new \Exception(self::$l10n->t('Address book not found'), 404);
}
2014-03-08 19:09:17 +01:00
2013-03-15 17:01:06 +01:00
$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);
return $addressBook->getChild($id);
}
}