roundcubemail-plugins-kolab/plugins/libkolab/lib/kolab_storage_dav_cache_event.php

155 lines
5.4 KiB
PHP
Raw Normal View History

2022-10-06 15:59:53 +02:00
<?php
/**
* Kolab storage cache class for calendar event objects
*
2022-10-16 14:22:59 +02:00
* @author Aleksander Machniak <machniak@apheleia-it.ch>
2022-10-06 15:59:53 +02:00
*
2022-10-16 14:22:59 +02:00
* Copyright (C) 2013-2022 Apheleia IT AG <contact@apheleia-it.ch>
2022-10-06 15:59:53 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class kolab_storage_dav_cache_event extends kolab_storage_dav_cache
{
2022-10-11 15:27:59 +02:00
protected $extra_cols = ['dtstart','dtend'];
protected $data_props = ['categories', 'status', 'attendees'];
protected $fulltext_cols = ['title', 'description', 'location', 'attendees:name', 'attendees:email', 'categories'];
2022-10-06 15:59:53 +02:00
/**
* Helper method to convert the given Kolab object into a dataset to be written to cache
*
* @override
*/
protected function _serialize($object)
{
$sql_data = parent::_serialize($object);
$sql_data['dtstart'] = $this->_convert_datetime($object['start']);
$sql_data['dtend'] = $this->_convert_datetime($object['end']);
// extend date range for recurring events
2022-10-16 12:33:04 +02:00
if (!empty($object['recurrence'])) {
$recurrence = libcalendaring::get_recurrence($object);
2022-10-06 15:59:53 +02:00
$dtend = $recurrence->end() ?: new DateTime('now +100 years');
$sql_data['dtend'] = $this->_convert_datetime($dtend);
}
// extend start/end dates to spawn all exceptions
// FIXME: This should be done via libcalendaring_recurrence use above?
if (!empty($object['exceptions']) && is_array($object['exceptions'])) {
2022-10-06 15:59:53 +02:00
foreach ($object['exceptions'] as $exception) {
if ($exception['start'] instanceof DateTimeInterface) {
2022-10-06 15:59:53 +02:00
$exstart = $this->_convert_datetime($exception['start']);
if ($exstart < $sql_data['dtstart']) {
$sql_data['dtstart'] = $exstart;
}
}
if ($exception['end'] instanceof DateTimeInterface) {
2022-10-06 15:59:53 +02:00
$exend = $this->_convert_datetime($exception['end']);
if ($exend > $sql_data['dtend']) {
$sql_data['dtend'] = $exend;
}
}
}
}
2024-01-24 11:24:41 +01:00
$sql_data['tags'] = ' ' . implode(' ', $this->get_tags($object)) . ' '; // pad with spaces for strict/prefix search
$sql_data['words'] = ' ' . implode(' ', $this->get_words($object)) . ' ';
2022-10-11 15:27:59 +02:00
2022-10-06 15:59:53 +02:00
return $sql_data;
}
2022-10-11 15:27:59 +02:00
/**
* Callback to get words to index for fulltext search
*
* @return array List of words to save in cache
*/
public function get_words($object = [])
{
$data = '';
foreach ($this->fulltext_cols as $colname) {
2024-01-24 11:24:41 +01:00
[$col, $field] = strpos($colname, ':') ? explode(':', $colname) : [$colname, null];
if (empty($object[$col])) {
continue;
}
2022-10-11 15:27:59 +02:00
if ($field) {
$a = [];
foreach ((array) $object[$col] as $attr) {
if (!empty($attr[$field])) {
$a[] = $attr[$field];
}
2022-10-11 15:27:59 +02:00
}
2024-01-24 11:24:41 +01:00
$val = implode(' ', $a);
} else {
$val = is_array($object[$col]) ? implode(' ', $object[$col]) : $object[$col];
2022-10-11 15:27:59 +02:00
}
if (is_string($val) && strlen($val)) {
2022-10-11 15:27:59 +02:00
$data .= $val . ' ';
}
2022-10-11 15:27:59 +02:00
}
$words = rcube_utils::normalize_string($data, true);
// collect words from recurrence exceptions
if (!empty($object['exceptions']) && is_array($object['exceptions'])) {
2022-10-11 15:27:59 +02:00
foreach ($object['exceptions'] as $exception) {
$words = array_merge($words, $this->get_words($exception));
}
}
return array_unique($words);
}
/**
* Callback to get object specific tags to cache
*
* @return array List of tags to save in cache
*/
public function get_tags($object)
{
$tags = [];
if (!empty($object['valarms'])) {
$tags[] = 'x-has-alarms';
}
// create tags reflecting participant status
if (is_array($object['attendees'])) {
foreach ($object['attendees'] as $attendee) {
2024-01-24 11:24:41 +01:00
if (!empty($attendee['email']) && !empty($attendee['status'])) {
2022-10-11 15:27:59 +02:00
$tags[] = 'x-partstat:' . $attendee['email'] . ':' . strtolower($attendee['status']);
2024-01-24 11:24:41 +01:00
}
2022-10-11 15:27:59 +02:00
}
}
// collect tags from recurrence exceptions
if (!empty($object['exceptions']) && is_array($object['exceptions'])) {
2022-10-11 15:27:59 +02:00
foreach ($object['exceptions'] as $exception) {
$tags = array_merge($tags, $this->get_tags($exception));
}
}
if (!empty($object['status'])) {
$tags[] = 'x-status:' . strtolower($object['status']);
2022-10-11 15:27:59 +02:00
}
return array_unique($tags);
}
2022-10-06 15:59:53 +02:00
}