PredefinedStatusService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. public const OUT_OF_OFFICE = 'out-of-office';
  46. /** @var IL10N */
  47. private $l10n;
  48. /**
  49. * DefaultStatusService constructor.
  50. *
  51. * @param IL10N $l10n
  52. */
  53. public function __construct(IL10N $l10n) {
  54. $this->l10n = $l10n;
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function getDefaultStatuses(): array {
  60. return [
  61. [
  62. 'id' => self::MEETING,
  63. 'icon' => '📅',
  64. 'message' => $this->getTranslatedStatusForId(self::MEETING),
  65. 'clearAt' => [
  66. 'type' => 'period',
  67. 'time' => 3600,
  68. ],
  69. ],
  70. [
  71. 'id' => self::COMMUTING,
  72. 'icon' => '🚌',
  73. 'message' => $this->getTranslatedStatusForId(self::COMMUTING),
  74. 'clearAt' => [
  75. 'type' => 'period',
  76. 'time' => 1800,
  77. ],
  78. ],
  79. [
  80. 'id' => self::REMOTE_WORK,
  81. 'icon' => '🏡',
  82. 'message' => $this->getTranslatedStatusForId(self::REMOTE_WORK),
  83. 'clearAt' => [
  84. 'type' => 'end-of',
  85. 'time' => 'day',
  86. ],
  87. ],
  88. [
  89. 'id' => self::SICK_LEAVE,
  90. 'icon' => '🤒',
  91. 'message' => $this->getTranslatedStatusForId(self::SICK_LEAVE),
  92. 'clearAt' => [
  93. 'type' => 'end-of',
  94. 'time' => 'day',
  95. ],
  96. ],
  97. [
  98. 'id' => self::VACATIONING,
  99. 'icon' => '🌴',
  100. 'message' => $this->getTranslatedStatusForId(self::VACATIONING),
  101. 'clearAt' => null,
  102. ],
  103. [
  104. 'id' => self::CALL,
  105. 'icon' => '💬',
  106. 'message' => $this->getTranslatedStatusForId(self::CALL),
  107. 'clearAt' => null,
  108. 'visible' => false,
  109. ],
  110. [
  111. 'id' => self::OUT_OF_OFFICE,
  112. 'icon' => '🛑',
  113. 'message' => $this->getTranslatedStatusForId(self::OUT_OF_OFFICE),
  114. 'clearAt' => null,
  115. 'visible' => false,
  116. ],
  117. ];
  118. }
  119. /**
  120. * @param string $id
  121. * @return array|null
  122. */
  123. public function getDefaultStatusById(string $id): ?array {
  124. foreach ($this->getDefaultStatuses() as $status) {
  125. if ($status['id'] === $id) {
  126. return $status;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * @param string $id
  133. * @return string|null
  134. */
  135. public function getIconForId(string $id): ?string {
  136. switch ($id) {
  137. case self::MEETING:
  138. return '📅';
  139. case self::COMMUTING:
  140. return '🚌';
  141. case self::SICK_LEAVE:
  142. return '🤒';
  143. case self::VACATIONING:
  144. return '🌴';
  145. case self::OUT_OF_OFFICE:
  146. return '🛑';
  147. case self::REMOTE_WORK:
  148. return '🏡';
  149. case self::CALL:
  150. return '💬';
  151. default:
  152. return null;
  153. }
  154. }
  155. /**
  156. * @param string $lang
  157. * @param string $id
  158. * @return string|null
  159. */
  160. public function getTranslatedStatusForId(string $id): ?string {
  161. switch ($id) {
  162. case self::MEETING:
  163. return $this->l10n->t('In a meeting');
  164. case self::COMMUTING:
  165. return $this->l10n->t('Commuting');
  166. case self::SICK_LEAVE:
  167. return $this->l10n->t('Out sick');
  168. case self::VACATIONING:
  169. return $this->l10n->t('Vacationing');
  170. case self::OUT_OF_OFFICE:
  171. return $this->l10n->t('Out of office');
  172. case self::REMOTE_WORK:
  173. return $this->l10n->t('Working remotely');
  174. case self::CALL:
  175. return $this->l10n->t('In a call');
  176. default:
  177. return null;
  178. }
  179. }
  180. /**
  181. * @param string $id
  182. * @return bool
  183. */
  184. public function isValidId(string $id): bool {
  185. return \in_array($id, [
  186. self::MEETING,
  187. self::COMMUTING,
  188. self::SICK_LEAVE,
  189. self::VACATIONING,
  190. self::OUT_OF_OFFICE,
  191. self::REMOTE_WORK,
  192. IUserStatus::MESSAGE_CALL,
  193. IUserStatus::MESSAGE_AVAILABILITY,
  194. IUserStatus::MESSAGE_VACATION,
  195. IUserStatus::MESSAGE_CALENDAR_BUSY,
  196. IUserStatus::MESSAGE_CALENDAR_BUSY_TENTATIVE,
  197. ], true);
  198. }
  199. }