copy init, list and add code from quickstart to abook_class

This commit is contained in:
Alexey Shpakovsky 2021-05-28 22:49:31 +02:00
parent d1001c7cf2
commit 51610b9601
No known key found for this signature in database
GPG key ID: 5797A726A2A4230A

View file

@ -11,20 +11,50 @@
* This program is licensed under GPL. See COPYING for details * This program is licensed under GPL. See COPYING for details
*/ */
require 'quickconfig.php';
require 'vendor/autoload.php';
use MStilkerich\CardDavClient\{Account, AddressbookCollection, Config};
use MStilkerich\CardDavClient\Services\{Discovery, Sync, SyncHandler};
use Psr\Log\{AbstractLogger, NullLogger, LogLevel};
use Sabre\VObject\Component\VCard;
class NullSyncHandler implements SyncHandler
{
public function addressObjectChanged(string $uri, string $etag, ?VCard $card): void
{
}
public function addressObjectDeleted(string $uri): void
{
}
public function getExistingVCardETags(): array
{
return [];
}
public function finalizeSync(): void
{
}
}
Config::init();
/** /**
* address book carddav backend class * address book carddav backend class
*/ */
class abook_carddav extends addressbook_backend { class abook_carddav extends addressbook_backend {
var $btype = 'local'; var $btype = 'local';
var $bname = 'carddav'; var $bname = 'carddav';
var $writeable = false;
var $writeable = true;
/* ========================== Private ======================= */ /* ========================== Private ======================= */
/* Constructor */ /* Constructor */
function abook_carddav($param) { function abook_carddav($param) {
$this->sname = _("New address book"); $this->sname = _("carddav address book");
if (is_array($param)) { if (is_array($param)) {
if (!empty($param['name'])) { if (!empty($param['name'])) {
@ -50,7 +80,29 @@ class abook_carddav extends addressbook_backend {
* *
*/ */
function open() { function open() {
// ADDME: backend open function // backend open function
$this->$account = new Account(DISCOVERY_URI, USERNAME, PASSWORD);
// Discover the addressbooks for that account
try {
$discover = new Discovery();
$abooks = $discover->discoverAddressbooks($account);
} catch (\Exception $e) {
// $log->error("!!! Error during addressbook discovery: " . $e->getMessage());
exit(1);
}
if (count($abooks) <= 0) {
// $log->warning("Cannot proceed because no addressbooks were found - exiting");
exit(0);
}
//////////////////////////////////////////////////////////
// THE FOLLOWING SHOWS HOW TO PERFORM A SYNCHRONIZATION //
//////////////////////////////////////////////////////////
$this->abook = $abooks[0];
$this->synchandler = new NullSyncHandler();
$this->syncmgr = new Sync();
// initial sync - we don't have a sync-token yet
$this->lastSyncToken = $this->syncmgr->synchronize($this->abook, $this->synchandler, ["FN", "N", "EMAIL", "ORG"], "");
return true; return true;
} }
@ -82,10 +134,11 @@ class abook_carddav extends addressbook_backend {
* Lookup alias * Lookup alias
* @param alias string * @param alias string
*/ */
function lookup($alias) { function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
if (empty($alias)) { // if (empty($alias)) {
return array(); return array();
} // }
/*
$alias = strtolower($alias); $alias = strtolower($alias);
@ -101,6 +154,7 @@ class abook_carddav extends addressbook_backend {
'source' => $this->sname); 'source' => $this->sname);
return $ret; return $ret;
*/
} }
/** /**
@ -110,16 +164,27 @@ class abook_carddav extends addressbook_backend {
function list_addr() { function list_addr() {
$ret = array(); $ret = array();
// ADDME: list all addresses function // list all addresses having an email
$all=this->$abook->query(['EMAIL' => "//"],["FN", "N", "EMAIL", "ORG"]);
array_push($ret,array('nickname' => "nickname", /*
'name' => "firstname lastname", Returns an array of matched VCards:
'firstname' => "firstname", The keys of the array are the URIs of the vcards
'lastname' => "lastname", The values are associative arrays with keys etag (type: string) and vcard (type: VCard)
'email' => "email@address", */
'label' => "info", foreach($all as $uri => $one) {
$vcard = $one['vcard'];
$names = $vcard->N->getParts();
// last,first,additional,prefix,suffix
array_push($ret,array(
'nickname' => $uri,
'name' => (string)$vcard->FN,
'firstname' => (string)$names[1],
'lastname' => (string)$names[0],
'email' => (string)$vcard->EMAIL,
'label' => (string)$vcard->ORG,
'backend' => $this->bnum, 'backend' => $this->bnum,
'source' => $this->sname)); 'source' => $this->sname));
}
return $ret; return $ret;
} }
@ -135,19 +200,32 @@ class abook_carddav extends addressbook_backend {
} }
/* See if user exist already */ /* See if user exist already */
/*
$ret = $this->lookup($userdata['nickname']); $ret = $this->lookup($userdata['nickname']);
if (!empty($ret)) { if (!empty($ret)) {
return $this->set_error(sprintf(_("User '%s' already exist"), return $this->set_error(sprintf(_("User '%s' already exist"),
$ret['nickname'])); $ret['nickname']));
} }
*/
try {
$vcard = new VCard([
'FN' => $userdata['name'],
'N' => [$userdata['lastname'], $userdata['firstname'], '', '', ''],
'EMAIL' => $userdata['email'],
'ORG' => $userdata['label'],
]);
// ADDME: insert address function // insert address function
$this->abook->createCard($vcard);
// now a sync should return that card as well - lets see!
$this->lastSyncToken = $this->syncmgr->synchronize($this->abook, $this->synchandler, ["FN", "N", "EMAIL", "ORG"], $this->lastSyncToken);
// FIXME: // return true if operation is succesful.
// return true if operation is succesful. return true;
return true; } catch (\Exception $e) {
// Return error message if operation fails // Return error message if operation fails
return $this->set_error(_("Address add operation failed")); return $this->set_error(_("Address add operation failed: ") . $e->getMessage());
}
} }
/** /**