-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Augment the category menu by system tags and already used categories.
This commit add all available "collaborative tags" and all already used categories into option groups of the tags-menu of the side-bar editor. Determining the set of already used categories is a little bit ugly: it used the oc_calendarobject_props table which might be considered "internal". However, this is the Nextcloud calendar app which only talks to the Nextcloud calendar server. So using this "internal ingredient" might be acceptable. This commit addresses and is a related to a couple of open issues: #3735 Calendar Categories: Propose Categories already used - this should be fixed by this commit #1644 Add own categories, delete default ones - this is partly fixed in the sense that collaboritive tags are now also proposed as calendar categories. - still default categories cannot be deleted - however, using option groups one at least has some sort of overview about the origin of the proposed category nextcloud/server#29950 Save VEVENT CATEGORIES as vcategory - this issue is totally "ignored" by this commit as the proposed solution there is not needed (the categories are already there in the oc_calendarobject_props table) - that would have to be discussed there: but my impression that the tables and classed mentioned there are obsolete and no longer used. Signed-off-by: Claus-Justus Heine <[email protected]>
- Loading branch information
Showing
6 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* Calendar App | ||
* | ||
* @copyright 2023 Claus-Justus Heine <[email protected]> | ||
* | ||
* @author Claus-Justus Heine <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or any later version. | ||
* | ||
* This library 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 library. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Calendar\Service; | ||
|
||
use OCP\Calendar\ICalendar; | ||
use OCP\Calendar\IManager as ICalendarManager; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
use OCP\IL10N; | ||
use OCP\SystemTag\ISystemTag; | ||
use OCP\SystemTag\ISystemTagManager; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class CategoriesService { | ||
/** @var null|string */ | ||
private $userId; | ||
|
||
/** @var ICalendarManager */ | ||
private $calendarManager; | ||
|
||
/** @var ISystemTagManager */ | ||
private $systemTagManager; | ||
|
||
/** @var IDBConnection */ | ||
private $db; | ||
|
||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
/** @var IL10N */ | ||
private $l; | ||
|
||
private const CALENDAR_OBJECT_PROPERTIES_TABLE = 'calendarobjects_props'; | ||
|
||
public function __construct(?string $userId, | ||
ICalendarManager $calendarManager, | ||
ISystemTagManager $systemTagManager, | ||
IDBConnection $db, | ||
LoggerInterface $logger, | ||
IL10N $l10n) { | ||
$this->userId = $userId; | ||
$this->calendarManager = $calendarManager; | ||
$this->systemTagManager = $systemTagManager; | ||
$this->db = $db; | ||
$this->logger = $logger; | ||
$this->l = $l10n; | ||
} | ||
|
||
private function getUsedCategories(): array { | ||
if (empty($this->userId)) { | ||
return []; | ||
} | ||
$calendars = $this->calendarManager->getCalendarsForPrincipal('principals/users/' . $this->userId); | ||
$count = count($calendars); | ||
if ($count === 0) { | ||
return []; | ||
} | ||
$calendarIds = array_map(fn (ICalendar $calendar) => $calendar->getKey(), $calendars); | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->selectDistinct('value') | ||
->from(self::CALENDAR_OBJECT_PROPERTIES_TABLE) | ||
->where($qb->expr()->in('calendarid', $qb->createNamedParameter($calendarIds, IQueryBuilder::PARAM_INT_ARRAY))) | ||
->andWhere($qb->expr()->eq('name', $qb->createNamedParameter('CATEGORIES'))); | ||
$result = $qb->executeQuery(); | ||
$rawCategories = $result->fetchAll(); | ||
$result->closeCursor(); | ||
|
||
$categories = array_values(array_filter(array_unique(array_merge(...array_map(fn ($result) => explode(',', $result['value'] ?? ''), $rawCategories))))); | ||
|
||
return $categories; | ||
} | ||
|
||
public function getCategories(): array { | ||
$systemTags = $this->systemTagManager->getAllTags(visibilityFilter: true); | ||
|
||
$systemTagCategoryLabels = []; | ||
/** @var ISystemTag $systemTag */ | ||
foreach ($systemTags as $systemTag) { | ||
if (!$systemTag->isUserAssignable() || !$systemTag->isUserVisible()) { | ||
continue; | ||
} | ||
$systemTagCategoryLabels[] = $systemTag->getName(); | ||
} | ||
sort($systemTagCategoryLabels); | ||
$systemTagCategoryLabels = array_values(array_filter(array_unique($systemTagCategoryLabels))); | ||
|
||
$rfcCategoryLabels = [ | ||
$this->l->t('Anniversary'), | ||
$this->l->t('Appointment'), | ||
$this->l->t('Business'), | ||
$this->l->t('Education'), | ||
$this->l->t('Holiday'), | ||
$this->l->t('Meeting'), | ||
$this->l->t('Miscellaneous'), | ||
$this->l->t('Non-working hours'), | ||
$this->l->t('Not in office'), | ||
$this->l->t('Personal'), | ||
$this->l->t('Phone call'), | ||
$this->l->t('Sick day'), | ||
$this->l->t('Special occasion'), | ||
$this->l->t('Travel'), | ||
$this->l->t('Vacation'), | ||
]; | ||
sort($rfcCategoryLabels); | ||
$rfcCategoryLabels = array_values(array_filter(array_unique($rfcCategoryLabels))); | ||
|
||
$standardCategories = array_merge($systemTagCategoryLabels, $rfcCategoryLabels); | ||
$customCategoryLabels = array_values(array_filter($this->getUsedCategories(), fn ($label) => !in_array($label, $standardCategories))); | ||
|
||
$categories = [ | ||
[ | ||
'group' => $this->l->t('Custom Categories'), | ||
'options' => array_map(fn (string $label) => [ 'label' => $label, 'value' => $label ], $customCategoryLabels), | ||
], | ||
[ | ||
'group' => $this->l->t('Collaborative Tags'), | ||
'options' => array_map(fn (string $label) => [ 'label' => $label, 'value' => $label ], $systemTagCategoryLabels), | ||
], | ||
[ | ||
'group' => $this->l->t('Standard Categories'), | ||
'options' => array_map(fn (string $label) => [ 'label' => $label, 'value' => $label ], $rfcCategoryLabels), | ||
], | ||
]; | ||
|
||
|
||
return $categories; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters