better TEL listing and other code cleanup

This commit is contained in:
Alexey Shpakovsky 2021-05-30 18:27:40 +02:00
parent f1ea748a32
commit 6255ace52f

View file

@ -55,28 +55,24 @@ class abook_carddav extends addressbook_backend {
} }
/** /**
* Given a $vcard object and its $uri, returns squirrelmail contact (array). * Given a $vcard object, returns squirrelmail contact (array).
* Optional $email arg overwrites the one stored in vcard. * $uri is needed to specify nickname if $this->writeable
* Optional $email and $tel args overwrite those stored in vcard.
* Respects $this->writeable: * Respects $this->writeable:
* for writeable addressbooks, 'nickname' must be unique identifier - * for writeable addressbooks, 'nickname' must be unique identifier -
* in our case, last part of uid id used * in our case, last part of uid is used
* for non-writeable addressbooks, 'nickname' doesn't matter that much - * for non-writeable addressbooks, 'nickname' doesn't matter that much -
* so we put ORG there * so we put ORG there
*/ */
function vcard2sq($uri, $vcard, $email=null, $tel=null) { function vcard2sq($uri, $vcard, $email=null, $tel='def') {
if(!$email) { $email = (string)$vcard->EMAIL; }
if($tel=='def') { $tel = (string)$vcard->TEL; }
if($this->writeable) { if($this->writeable) {
$nickname = substr($uri, $this->abook_uri_len); $nickname = substr($uri, $this->abook_uri_len);
$label = (string)$vcard->ORG; $label = (string)$vcard->ORG;
} else { } else {
$nickname = (string)$vcard->ORG; $nickname = (string)$vcard->ORG;
if($tel) { $label = $tel;
$label = $tel;
} else {
$label = (string)$vcard->TEL;
}
}
if(!$email) {
$email = (string)$vcard->EMAIL;
} }
$names = $vcard->N->getParts(); $names = $vcard->N->getParts();
// last,first,additional,prefix,suffix // last,first,additional,prefix,suffix
@ -108,7 +104,7 @@ class abook_carddav extends addressbook_backend {
function run_query($query, $match_all=false, $limit=0) { function run_query($query, $match_all=false, $limit=0) {
$ret = array(); $ret = array();
$fields = ["FN", "N", "EMAIL", "ORG"]; $fields = ["FN", "N", "EMAIL", "ORG"];
if(!$this->writeable) { $fields[] = "TEL"; if(!$this->writeable) { $fields[] = "TEL"; }
$all=$this->abook->query($query,$fields,$match_all,$limit); $all=$this->abook->query($query,$fields,$match_all,$limit);
/* /*
Returns an array of matched VCards: Returns an array of matched VCards:
@ -120,19 +116,13 @@ class abook_carddav extends addressbook_backend {
$vcard = $one['vcard']; $vcard = $one['vcard'];
if(!isset($vcard->EMAIL)) { continue; } if(!isset($vcard->EMAIL)) { continue; }
if($this->writeable) { if($this->writeable) {
// all one line per each vcard // one line per each vcard
$ret[] = $this->vcard2sq($uri, $vcard); $ret[] = $this->vcard2sq($uri, $vcard);
} else { } else {
// pick a TEL for each EMAIL. // one line per each email
// if # of TELs is greater than EMAILs - extra are ignored foreach($vcard->EMAIL as $i => $email) {
// if # of EMAILs is greater than TELs - empty TELs are shown // also show one TEL for one EMAIL (extra TELs are ignored)
// TODO: could this be rewritten better? $ret[] = $this->vcard2sq($uri, $vcard, $email, (string)$vcard->TEL[$i]);
$tels = array();
foreach($vcard->TEL as $tel) { $tels[] = (string)$tel; }
$i=0;
foreach($vcard->EMAIL as $email) {
// all one line per each email
$ret[] = $this->vcard2sq($uri, $vcard, $email, @$tels[$i++]);
} }
} }
if($limit == 1) { return $ret[0]; } if($limit == 1) { return $ret[0]; }
@ -194,22 +184,14 @@ class abook_carddav extends addressbook_backend {
vcard(VCard): VCard as Sabre/VObject VCard vcard(VCard): VCard as Sabre/VObject VCard
*/ */
$vcard = $one['vcard']; $vcard = $one['vcard'];
// TODO: verify that something was returned
return $this->vcard2sq($uri, $vcard); return $this->vcard2sq($uri, $vcard);
} }
if($field == SM_ABOOK_FIELD_FIRSTNAME) { if($field == SM_ABOOK_FIELD_FIRSTNAME) { } // TODO: this will be harder
// TODO: this will be harder if($field == SM_ABOOK_FIELD_LASTNAME) { $filter=['N' => "/$value;/^", 'EMAIL' => "//"]; }
} if($field == SM_ABOOK_FIELD_EMAIL) { $filter=['EMAIL' => "/$value/="]; }
if($field == SM_ABOOK_FIELD_LASTNAME) { if($field == SM_ABOOK_FIELD_LABEL) { $filter=['ORG' => "/$value/=", 'EMAIL' => "//"]; }
$filter=['N' => "/$value;/^", 'EMAIL' => "//"];
}
if($field == SM_ABOOK_FIELD_EMAIL) {
$filter=['EMAIL' => "/$value/="];
}
if($field == SM_ABOOK_FIELD_LABEL) {
$filter=['ORG' => "/$value/=", 'EMAIL' => "//"];
}
if(!isset($filter)) { return array(); } if(!isset($filter)) { return array(); }
return $this->run_query($filter,true,1); return $this->run_query($filter,true,1);
} }