You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PredefinedStatusService.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Service;
  24. use OCP\IL10N;
  25. /**
  26. * Class DefaultStatusService
  27. *
  28. * We are offering a set of default statuses, so we can
  29. * translate them into different languages.
  30. *
  31. * @package OCA\UserStatus\Service
  32. */
  33. class PredefinedStatusService {
  34. private const MEETING = 'meeting';
  35. private const COMMUTING = 'commuting';
  36. private const SICK_LEAVE = 'sick-leave';
  37. private const VACATIONING = 'vacationing';
  38. private const REMOTE_WORK = 'remote-work';
  39. /** @var IL10N */
  40. private $l10n;
  41. /**
  42. * DefaultStatusService constructor.
  43. *
  44. * @param IL10N $l10n
  45. */
  46. public function __construct(IL10N $l10n) {
  47. $this->l10n = $l10n;
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function getDefaultStatuses(): array {
  53. return [
  54. [
  55. 'id' => self::MEETING,
  56. 'icon' => '📅',
  57. 'message' => $this->getTranslatedStatusForId(self::MEETING),
  58. 'clearAt' => [
  59. 'type' => 'period',
  60. 'time' => 3600,
  61. ],
  62. ],
  63. [
  64. 'id' => self::COMMUTING,
  65. 'icon' => '🚌',
  66. 'message' => $this->getTranslatedStatusForId(self::COMMUTING),
  67. 'clearAt' => [
  68. 'type' => 'period',
  69. 'time' => 1800,
  70. ],
  71. ],
  72. [
  73. 'id' => self::REMOTE_WORK,
  74. 'icon' => '🏡',
  75. 'message' => $this->getTranslatedStatusForId(self::REMOTE_WORK),
  76. 'clearAt' => [
  77. 'type' => 'end-of',
  78. 'time' => 'day',
  79. ],
  80. ],
  81. [
  82. 'id' => self::SICK_LEAVE,
  83. 'icon' => '🤒',
  84. 'message' => $this->getTranslatedStatusForId(self::SICK_LEAVE),
  85. 'clearAt' => [
  86. 'type' => 'end-of',
  87. 'time' => 'day',
  88. ],
  89. ],
  90. [
  91. 'id' => self::VACATIONING,
  92. 'icon' => '🌴',
  93. 'message' => $this->getTranslatedStatusForId(self::VACATIONING),
  94. 'clearAt' => null,
  95. ],
  96. ];
  97. }
  98. /**
  99. * @param string $id
  100. * @return array|null
  101. */
  102. public function getDefaultStatusById(string $id): ?array {
  103. foreach ($this->getDefaultStatuses() as $status) {
  104. if ($status['id'] === $id) {
  105. return $status;
  106. }
  107. }
  108. return null;
  109. }
  110. /**
  111. * @param string $id
  112. * @return string|null
  113. */
  114. public function getIconForId(string $id): ?string {
  115. switch ($id) {
  116. case self::MEETING:
  117. return '📅';
  118. case self::COMMUTING:
  119. return '🚌';
  120. case self::SICK_LEAVE:
  121. return '🤒';
  122. case self::VACATIONING:
  123. return '🌴';
  124. case self::REMOTE_WORK:
  125. return '🏡';
  126. default:
  127. return null;
  128. }
  129. }
  130. /**
  131. * @param string $lang
  132. * @param string $id
  133. * @return string|null
  134. */
  135. public function getTranslatedStatusForId(string $id): ?string {
  136. switch ($id) {
  137. case self::MEETING:
  138. return $this->l10n->t('In a meeting');
  139. case self::COMMUTING:
  140. return $this->l10n->t('Commuting');
  141. case self::SICK_LEAVE:
  142. return $this->l10n->t('Out sick');
  143. case self::VACATIONING:
  144. return $this->l10n->t('Vacationing');
  145. case self::REMOTE_WORK:
  146. return $this->l10n->t('Working remotely');
  147. default:
  148. return null;
  149. }
  150. }
  151. /**
  152. * @param string $id
  153. * @return bool
  154. */
  155. public function isValidId(string $id): bool {
  156. return \in_array($id, [
  157. self::MEETING,
  158. self::COMMUTING,
  159. self::SICK_LEAVE,
  160. self::VACATIONING,
  161. self::REMOTE_WORK,
  162. ], true);
  163. }
  164. }