PredefinedStatusService.php 4.4 KB

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