Blame view
sources/apps/contacts/lib/app.php
4.46 KB
|
923852aa1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
<?php
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Contacts;
use Sabre\VObject,
OCP\AppFramework,
OCA\Contacts\Controller\AddressBookController,
OCA\Contacts\Controller\GroupController,
OCA\Contacts\Controller\ContactController,
OCA\Contacts\Controller\ContactPhotoController,
OCA\Contacts\Controller\SettingsController,
OCA\Contacts\Controller\ImportController;
/**
* This class manages our app actions
*
* TODO: Merge in Dispatcher
*/
App::$l10n = \OC_L10N::get('contacts');
class App {
/**
* @brief Categories of the user
* @var OC_VCategories
*/
public static $categories = null;
/**
* @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
* to their respective classes, if constructed with the 'getBackend' method.
*
* @var array
*/
public static $backendClasses = array(
//'ldap' => 'OCA\Contacts\Backend\Ldap',
'local' => 'OCA\Contacts\Backend\Database',
'shared' => 'OCA\Contacts\Backend\Shared',
);
public function __construct(
$user = null,
$addressBooksTableName = '*PREFIX*addressbook',
$backendsTableName = '*PREFIX*addressbooks_backend',
$dbBackend = null
) {
$this->user = $user ? $user : \OCP\User::getUser();
$this->addressBooksTableName = $addressBooksTableName;
$this->backendsTableName = $backendsTableName;
$this->dbBackend = $dbBackend
? $dbBackend
: new Backend\Database($user);
}
/**
* Gets backend by name.
*
* @param string $name
* @return \Backend\AbstractBackend
*/
public function getBackend($name) {
$name = $name ? $name : 'local';
if (isset(self::$backendClasses[$name])) {
return new self::$backendClasses[$name]($this->user);
} else {
throw new \Exception('No backend for: ' . $name, '404');
}
}
/**
* 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() {
if(!self::$addressBooks) {
foreach(array_keys(self::$backendClasses) as $backendName) {
$backend = self::getBackend($backendName, $this->user);
$addressBooks = $backend->getAddressBooksForUser();
if($backendName === 'local' && count($addressBooks) === 0) {
$id = $backend->createAddressBook(array('displayname' => self::$l10n->t('Contacts')));
if($id !== false) {
$addressBook = $backend->getAddressBook($id);
$addressBooks = array($addressBook);
} else {
\OCP\Util::writeLog(
'contacts',
__METHOD__ . ', Error creating default address book',
\OCP\Util::ERROR
);
}
}
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
* @return AddressBook|null
*/
public function getAddressBook($backendName, $addressbookid) {
//\OCP\Util::writeLog('contacts', __METHOD__ . ': '. $backendName . ', ' . $addressbookid, \OCP\Util::DEBUG);
foreach(self::$addressBooks as $addressBook) {
if($addressBook->getBackend()->name === $backendName
&& $addressBook->getId() === $addressbookid
) {
return $addressBook;
}
}
$backend = self::getBackend($backendName, $this->user);
$info = $backend->getAddressBook($addressbookid);
if(!$info) {
throw new \Exception(self::$l10n->t('Address book not found'), 404);
}
$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
* @return Contact|null
*
*/
public function getContact($backendName, $addressbookid, $id) {
$addressBook = $this->getAddressBook($backendName, $addressbookid);
return $addressBook->getChild($id);
}
}
|