-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Augment the category menu by system tags and already used categories #5161
Merged
ChristophWurst
merged 1 commit into
nextcloud:main
from
rotdrop:feature/main/modifyable-default-categories
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,144 @@ | ||
<?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\ICalendarQuery; | ||
use OCP\Calendar\IManager as ICalendarManager; | ||
use OCP\IL10N; | ||
use OCP\SystemTag\ISystemTag; | ||
use OCP\SystemTag\ISystemTagManager; | ||
|
||
/** | ||
* @psalm-type Category = array{label: string, value: string} | ||
* @psalm-type CategoryGroup = array{group: string, options: array<int, Category>} | ||
*/ | ||
class CategoriesService { | ||
/** @var ICalendarManager */ | ||
private $calendarManager; | ||
|
||
/** @var ISystemTagManager */ | ||
private $systemTagManager; | ||
|
||
/** @var IL10N */ | ||
private $l; | ||
|
||
public function __construct(ICalendarManager $calendarManager, | ||
ISystemTagManager $systemTagManager, | ||
IL10N $l10n) { | ||
$this->calendarManager = $calendarManager; | ||
$this->systemTagManager = $systemTagManager; | ||
$this->l = $l10n; | ||
} | ||
|
||
/** | ||
* This is a simplistic brute-force extraction of all already used | ||
* categories from all events accessible to the given user. | ||
* | ||
* @return array | ||
*/ | ||
private function getUsedCategories(string $userId): array { | ||
$categories = []; | ||
$principalUri = 'principals/users/' . $userId; | ||
$query = $this->calendarManager->newQuery($principalUri); | ||
$query->addSearchProperty(ICalendarQuery::SEARCH_PROPERTY_CATEGORIES); | ||
$calendarObjects = $this->calendarManager->searchForPrincipal($query); | ||
foreach ($calendarObjects as $objectInfo) { | ||
foreach ($objectInfo['objects'] as $calendarObject) { | ||
if (isset($calendarObject['CATEGORIES'])) { | ||
$categories[] = explode(',', $calendarObject['CATEGORIES'][0][0]); | ||
} | ||
} | ||
} | ||
|
||
// Avoid injecting "broken" categories into the UI (avoid empty | ||
// categories and categories surrounded by spaces) | ||
$categories = array_filter(array_map(fn ($label) => trim($label), array_unique(array_merge(...$categories)))); | ||
|
||
return $categories; | ||
} | ||
|
||
/** | ||
* Return a grouped array with all previously used categories, all system | ||
* tags and all categories found in the iCalendar RFC. | ||
* | ||
* @return CategoryGroup[] | ||
*/ | ||
public function getCategories(string $userId): array { | ||
$systemTags = $this->systemTagManager->getAllTags(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($userId), 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI I have moved the userId from an injectable to a parameter because that makes the service usable without a user context, e.g. if we make use of it in a background job or CLI task one day