* Addressbook/REST API: allow to pass filters or a search pattern to addressbook REST API

This commit is contained in:
ralf 2023-10-19 21:34:38 +03:00
parent 40d236b95f
commit b0490bc7d8
3 changed files with 21 additions and 8 deletions

View File

@ -55,7 +55,7 @@ class addressbook_groupdav extends Api\CalDAV\Handler
* G = primary group
* D = distribution lists as groups
* O = sync all in one (/<username>/addressbook/)
* or nummerical account_id, but not user itself
* or numerical account_id, but not user itself
*
* @var array
*/
@ -269,7 +269,9 @@ class addressbook_groupdav extends Api\CalDAV\Handler
if (!in_array(self::$path_attr,$cols)) $cols[] = self::$path_attr;
// we need tid for sync-collection report
if (array_key_exists('tid', $filter) && !isset($filter['tid']) && !in_array('tid', $cols)) $cols[] = 'tid';
for($chunk=0; ($contacts =& $this->bo->search([], $cols, $order, '', '', False, 'AND',
$search = $filter['search'] ?? [];
unset($filter['search']);
for($chunk=0; ($contacts =& $this->bo->search($search, $cols, $order, '', '', False, 'AND',
[$chunk*self::CHUNK_SIZE, self::CHUNK_SIZE], $filter)); ++$chunk)
{
// filter[tid] === null also returns no longer shared contacts, to remove them from devices, we need to mark them here as deleted
@ -449,14 +451,19 @@ class addressbook_groupdav extends Api\CalDAV\Handler
* Process the filters from the CalDAV REPORT request
*
* @param array $options
* @param array &$cal_filters
* @param array &$filters
* @param string $id
* @param int &$nresult on return limit for number or results or unchanged/null
* @return boolean true if filter could be processed
*/
function _report_filters($options,&$filters,$id, &$nresults)
function _report_filters($options, &$filters, $id, &$nresults)
{
if ($options['filters'])
// in case of JSON/REST API pass filters to report
if (Api\CalDAV::isJSON() && !empty($options['filters']) && is_array($options['filters']))
{
$filters += $options['filters']; // using += to no allow overwriting existing filters
}
elseif (!empty($options['filters']))
{
/* Example of a complex filter used by Mac Addressbook
<B:filter test="anyof">

View File

@ -1174,10 +1174,10 @@ class CalDAV extends HTTP_WebDAV_Server
}
}
// ToDo: client want data filtered
// client want data filtered
if (isset($_GET['filters']))
{
$propfind_options['filters'] = $_GET['filters'];
}
// properties to NOT get the default address-data for addressbook-collections and "all" for the rest

View File

@ -72,13 +72,19 @@ curl https://example.org/egroupware/groupdav.php/<username>/addressbook/ -H "Acc
```
</details>
following GET parameters are supported to customize the returned properties:
Following GET parameters are supported to customize the returned properties:
- props[]=<DAV-prop-name> eg. props[]=getetag to return only the ETAG (multiple DAV properties can be specified)
Default for addressbook collections is to only return address-data (JsContact), other collections return all props.
- sync-token=<token> to only request change since last sync-token, like rfc6578 sync-collection REPORT
- nresults=N limit number of responses (only for sync-collection / given sync-token parameter!)
this will return a "more-results"=true attribute and a new "sync-token" attribute to query for the next chunk
The GET parameter `filters` allows to filter or search for a pattern in an addressbook:
- `filters[search]=<pattern>` searches for `<pattern>` in the whole contact like the search in the GUI
- `filters[search][%23<custom-field-name>]=<custom-field-value>` filters by a custom-field value
- `filters[<database-column>]=<value>` filters by a DB-column name and value
> Please note: filters use the database column-names, not JSContact property-names!
<details>
<summary>Example: Getting just ETAGs and displayname of all contacts in a given AB</summary>