@@ -38,87 +38,87 @@ |
||
38 | 38 | */ |
39 | 39 | class Validator implements IValidator { |
40 | 40 | |
41 | - /** @var Definitions */ |
|
42 | - protected $definitions; |
|
43 | - |
|
44 | - /** @var array[] */ |
|
45 | - protected $requiredParameters = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * Constructor |
|
49 | - * |
|
50 | - * @param Definitions $definitions |
|
51 | - */ |
|
52 | - public function __construct(Definitions $definitions) { |
|
53 | - $this->definitions = $definitions; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $subject |
|
58 | - * @param array[] $parameters |
|
59 | - * @throws InvalidObjectExeption |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function validate($subject, array $parameters) { |
|
63 | - $matches = []; |
|
64 | - $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches); |
|
65 | - |
|
66 | - if ($result === false) { |
|
67 | - throw new InvalidObjectExeption(); |
|
68 | - } |
|
69 | - |
|
70 | - if (!empty($matches[1])) { |
|
71 | - foreach ($matches[1] as $parameter) { |
|
72 | - if (!isset($parameters[$parameter])) { |
|
73 | - throw new InvalidObjectExeption('Parameter is undefined'); |
|
74 | - } |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - foreach ($parameters as $parameter) { |
|
79 | - if (!\is_array($parameter)) { |
|
80 | - throw new InvalidObjectExeption('Parameter is malformed'); |
|
81 | - } |
|
82 | - |
|
83 | - $this->validateParameter($parameter); |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param array $parameter |
|
89 | - * @throws InvalidObjectExeption |
|
90 | - */ |
|
91 | - protected function validateParameter(array $parameter) { |
|
92 | - if (!isset($parameter['type'])) { |
|
93 | - throw new InvalidObjectExeption('Object type is undefined'); |
|
94 | - } |
|
95 | - |
|
96 | - $definition = $this->definitions->getDefinition($parameter['type']); |
|
97 | - $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition); |
|
98 | - |
|
99 | - $missingKeys = array_diff($requiredParameters, array_keys($parameter)); |
|
100 | - if (!empty($missingKeys)) { |
|
101 | - throw new InvalidObjectExeption('Object is invalid'); |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param string $type |
|
107 | - * @param array $definition |
|
108 | - * @return string[] |
|
109 | - */ |
|
110 | - protected function getRequiredParameters($type, array $definition) { |
|
111 | - if (isset($this->requiredParameters[$type])) { |
|
112 | - return $this->requiredParameters[$type]; |
|
113 | - } |
|
114 | - |
|
115 | - $this->requiredParameters[$type] = []; |
|
116 | - foreach ($definition['parameters'] as $parameter => $data) { |
|
117 | - if ($data['required']) { |
|
118 | - $this->requiredParameters[$type][] = $parameter; |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - return $this->requiredParameters[$type]; |
|
123 | - } |
|
41 | + /** @var Definitions */ |
|
42 | + protected $definitions; |
|
43 | + |
|
44 | + /** @var array[] */ |
|
45 | + protected $requiredParameters = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * Constructor |
|
49 | + * |
|
50 | + * @param Definitions $definitions |
|
51 | + */ |
|
52 | + public function __construct(Definitions $definitions) { |
|
53 | + $this->definitions = $definitions; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $subject |
|
58 | + * @param array[] $parameters |
|
59 | + * @throws InvalidObjectExeption |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function validate($subject, array $parameters) { |
|
63 | + $matches = []; |
|
64 | + $result = preg_match_all('/\{([a-z0-9]+)\}/i', $subject, $matches); |
|
65 | + |
|
66 | + if ($result === false) { |
|
67 | + throw new InvalidObjectExeption(); |
|
68 | + } |
|
69 | + |
|
70 | + if (!empty($matches[1])) { |
|
71 | + foreach ($matches[1] as $parameter) { |
|
72 | + if (!isset($parameters[$parameter])) { |
|
73 | + throw new InvalidObjectExeption('Parameter is undefined'); |
|
74 | + } |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + foreach ($parameters as $parameter) { |
|
79 | + if (!\is_array($parameter)) { |
|
80 | + throw new InvalidObjectExeption('Parameter is malformed'); |
|
81 | + } |
|
82 | + |
|
83 | + $this->validateParameter($parameter); |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param array $parameter |
|
89 | + * @throws InvalidObjectExeption |
|
90 | + */ |
|
91 | + protected function validateParameter(array $parameter) { |
|
92 | + if (!isset($parameter['type'])) { |
|
93 | + throw new InvalidObjectExeption('Object type is undefined'); |
|
94 | + } |
|
95 | + |
|
96 | + $definition = $this->definitions->getDefinition($parameter['type']); |
|
97 | + $requiredParameters = $this->getRequiredParameters($parameter['type'], $definition); |
|
98 | + |
|
99 | + $missingKeys = array_diff($requiredParameters, array_keys($parameter)); |
|
100 | + if (!empty($missingKeys)) { |
|
101 | + throw new InvalidObjectExeption('Object is invalid'); |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param string $type |
|
107 | + * @param array $definition |
|
108 | + * @return string[] |
|
109 | + */ |
|
110 | + protected function getRequiredParameters($type, array $definition) { |
|
111 | + if (isset($this->requiredParameters[$type])) { |
|
112 | + return $this->requiredParameters[$type]; |
|
113 | + } |
|
114 | + |
|
115 | + $this->requiredParameters[$type] = []; |
|
116 | + foreach ($definition['parameters'] as $parameter => $data) { |
|
117 | + if ($data['required']) { |
|
118 | + $this->requiredParameters[$type][] = $parameter; |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + return $this->requiredParameters[$type]; |
|
123 | + } |
|
124 | 124 | } |
@@ -33,138 +33,138 @@ |
||
33 | 33 | |
34 | 34 | class Groups extends Base { |
35 | 35 | |
36 | - const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self'; |
|
37 | - const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by'; |
|
38 | - const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self'; |
|
39 | - const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by'; |
|
40 | - |
|
41 | - /** @var IGroupManager */ |
|
42 | - protected $groupManager; |
|
43 | - |
|
44 | - /** @var string[] */ |
|
45 | - protected $groupDisplayNames = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * @param IFactory $languageFactory |
|
49 | - * @param IURLGenerator $url |
|
50 | - * @param IManager $activityManager |
|
51 | - * @param IUserManager $userManager |
|
52 | - * @param IGroupManager $groupManager |
|
53 | - */ |
|
54 | - public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager) { |
|
55 | - parent::__construct($languageFactory, $url, $activityManager, $userManager); |
|
56 | - $this->groupManager = $groupManager; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param IEvent $event |
|
61 | - * @return IEvent |
|
62 | - * @throws \InvalidArgumentException |
|
63 | - * @since 11.0.0 |
|
64 | - */ |
|
65 | - public function parseShortVersion(IEvent $event) { |
|
66 | - $parsedParameters = $this->getParsedParameters($event); |
|
67 | - |
|
68 | - if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) { |
|
69 | - $subject = $this->l->t('Shared with group {group}'); |
|
70 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) { |
|
71 | - $subject = $this->l->t('Removed share for group {group}'); |
|
72 | - } else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) { |
|
73 | - $subject = $this->l->t('{actor} shared with group {group}'); |
|
74 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) { |
|
75 | - $subject = $this->l->t('{actor} removed share for group {group}'); |
|
76 | - } else { |
|
77 | - throw new \InvalidArgumentException(); |
|
78 | - } |
|
79 | - |
|
80 | - if ($this->activityManager->getRequirePNG()) { |
|
81 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
82 | - } else { |
|
83 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
84 | - } |
|
85 | - $this->setSubjects($event, $subject, $parsedParameters); |
|
86 | - |
|
87 | - return $event; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param IEvent $event |
|
92 | - * @return IEvent |
|
93 | - * @throws \InvalidArgumentException |
|
94 | - * @since 11.0.0 |
|
95 | - */ |
|
96 | - public function parseLongVersion(IEvent $event) { |
|
97 | - $parsedParameters = $this->getParsedParameters($event); |
|
98 | - |
|
99 | - if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) { |
|
100 | - $subject = $this->l->t('You shared {file} with group {group}'); |
|
101 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) { |
|
102 | - $subject = $this->l->t('You removed group {group} from {file}'); |
|
103 | - } else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) { |
|
104 | - $subject = $this->l->t('{actor} shared {file} with group {group}'); |
|
105 | - } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) { |
|
106 | - $subject = $this->l->t('{actor} removed group {group} from {file}'); |
|
107 | - } else { |
|
108 | - throw new \InvalidArgumentException(); |
|
109 | - } |
|
110 | - |
|
111 | - if ($this->activityManager->getRequirePNG()) { |
|
112 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
113 | - } else { |
|
114 | - $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
115 | - } |
|
116 | - $this->setSubjects($event, $subject, $parsedParameters); |
|
117 | - |
|
118 | - return $event; |
|
119 | - } |
|
120 | - |
|
121 | - protected function getParsedParameters(IEvent $event) { |
|
122 | - $subject = $event->getSubject(); |
|
123 | - $parameters = $event->getSubjectParameters(); |
|
124 | - |
|
125 | - switch ($subject) { |
|
126 | - case self::SUBJECT_RESHARED_GROUP_BY: |
|
127 | - case self::SUBJECT_UNSHARED_GROUP_BY: |
|
128 | - return [ |
|
129 | - 'file' => $this->getFile($parameters[0], $event), |
|
130 | - 'group' => $this->generateGroupParameter($parameters[2]), |
|
131 | - 'actor' => $this->getUser($parameters[1]), |
|
132 | - ]; |
|
133 | - case self::SUBJECT_SHARED_GROUP_SELF: |
|
134 | - case self::SUBJECT_UNSHARED_GROUP_SELF: |
|
135 | - return [ |
|
136 | - 'file' => $this->getFile($parameters[0], $event), |
|
137 | - 'group' => $this->generateGroupParameter($parameters[1]), |
|
138 | - ]; |
|
139 | - } |
|
140 | - return []; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @param string $gid |
|
145 | - * @return array |
|
146 | - */ |
|
147 | - protected function generateGroupParameter($gid) { |
|
148 | - if (!isset($this->groupDisplayNames[$gid])) { |
|
149 | - $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); |
|
150 | - } |
|
151 | - |
|
152 | - return [ |
|
153 | - 'type' => 'user-group', |
|
154 | - 'id' => $gid, |
|
155 | - 'name' => $this->groupDisplayNames[$gid], |
|
156 | - ]; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @param string $gid |
|
161 | - * @return string |
|
162 | - */ |
|
163 | - protected function getGroupDisplayName($gid) { |
|
164 | - $group = $this->groupManager->get($gid); |
|
165 | - if ($group instanceof IGroup) { |
|
166 | - return $group->getDisplayName(); |
|
167 | - } |
|
168 | - return $gid; |
|
169 | - } |
|
36 | + const SUBJECT_SHARED_GROUP_SELF = 'shared_group_self'; |
|
37 | + const SUBJECT_RESHARED_GROUP_BY = 'reshared_group_by'; |
|
38 | + const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self'; |
|
39 | + const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by'; |
|
40 | + |
|
41 | + /** @var IGroupManager */ |
|
42 | + protected $groupManager; |
|
43 | + |
|
44 | + /** @var string[] */ |
|
45 | + protected $groupDisplayNames = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * @param IFactory $languageFactory |
|
49 | + * @param IURLGenerator $url |
|
50 | + * @param IManager $activityManager |
|
51 | + * @param IUserManager $userManager |
|
52 | + * @param IGroupManager $groupManager |
|
53 | + */ |
|
54 | + public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager) { |
|
55 | + parent::__construct($languageFactory, $url, $activityManager, $userManager); |
|
56 | + $this->groupManager = $groupManager; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param IEvent $event |
|
61 | + * @return IEvent |
|
62 | + * @throws \InvalidArgumentException |
|
63 | + * @since 11.0.0 |
|
64 | + */ |
|
65 | + public function parseShortVersion(IEvent $event) { |
|
66 | + $parsedParameters = $this->getParsedParameters($event); |
|
67 | + |
|
68 | + if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) { |
|
69 | + $subject = $this->l->t('Shared with group {group}'); |
|
70 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) { |
|
71 | + $subject = $this->l->t('Removed share for group {group}'); |
|
72 | + } else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) { |
|
73 | + $subject = $this->l->t('{actor} shared with group {group}'); |
|
74 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) { |
|
75 | + $subject = $this->l->t('{actor} removed share for group {group}'); |
|
76 | + } else { |
|
77 | + throw new \InvalidArgumentException(); |
|
78 | + } |
|
79 | + |
|
80 | + if ($this->activityManager->getRequirePNG()) { |
|
81 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
82 | + } else { |
|
83 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
84 | + } |
|
85 | + $this->setSubjects($event, $subject, $parsedParameters); |
|
86 | + |
|
87 | + return $event; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param IEvent $event |
|
92 | + * @return IEvent |
|
93 | + * @throws \InvalidArgumentException |
|
94 | + * @since 11.0.0 |
|
95 | + */ |
|
96 | + public function parseLongVersion(IEvent $event) { |
|
97 | + $parsedParameters = $this->getParsedParameters($event); |
|
98 | + |
|
99 | + if ($event->getSubject() === self::SUBJECT_SHARED_GROUP_SELF) { |
|
100 | + $subject = $this->l->t('You shared {file} with group {group}'); |
|
101 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_SELF) { |
|
102 | + $subject = $this->l->t('You removed group {group} from {file}'); |
|
103 | + } else if ($event->getSubject() === self::SUBJECT_RESHARED_GROUP_BY) { |
|
104 | + $subject = $this->l->t('{actor} shared {file} with group {group}'); |
|
105 | + } else if ($event->getSubject() === self::SUBJECT_UNSHARED_GROUP_BY) { |
|
106 | + $subject = $this->l->t('{actor} removed group {group} from {file}'); |
|
107 | + } else { |
|
108 | + throw new \InvalidArgumentException(); |
|
109 | + } |
|
110 | + |
|
111 | + if ($this->activityManager->getRequirePNG()) { |
|
112 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
113 | + } else { |
|
114 | + $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
115 | + } |
|
116 | + $this->setSubjects($event, $subject, $parsedParameters); |
|
117 | + |
|
118 | + return $event; |
|
119 | + } |
|
120 | + |
|
121 | + protected function getParsedParameters(IEvent $event) { |
|
122 | + $subject = $event->getSubject(); |
|
123 | + $parameters = $event->getSubjectParameters(); |
|
124 | + |
|
125 | + switch ($subject) { |
|
126 | + case self::SUBJECT_RESHARED_GROUP_BY: |
|
127 | + case self::SUBJECT_UNSHARED_GROUP_BY: |
|
128 | + return [ |
|
129 | + 'file' => $this->getFile($parameters[0], $event), |
|
130 | + 'group' => $this->generateGroupParameter($parameters[2]), |
|
131 | + 'actor' => $this->getUser($parameters[1]), |
|
132 | + ]; |
|
133 | + case self::SUBJECT_SHARED_GROUP_SELF: |
|
134 | + case self::SUBJECT_UNSHARED_GROUP_SELF: |
|
135 | + return [ |
|
136 | + 'file' => $this->getFile($parameters[0], $event), |
|
137 | + 'group' => $this->generateGroupParameter($parameters[1]), |
|
138 | + ]; |
|
139 | + } |
|
140 | + return []; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @param string $gid |
|
145 | + * @return array |
|
146 | + */ |
|
147 | + protected function generateGroupParameter($gid) { |
|
148 | + if (!isset($this->groupDisplayNames[$gid])) { |
|
149 | + $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); |
|
150 | + } |
|
151 | + |
|
152 | + return [ |
|
153 | + 'type' => 'user-group', |
|
154 | + 'id' => $gid, |
|
155 | + 'name' => $this->groupDisplayNames[$gid], |
|
156 | + ]; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @param string $gid |
|
161 | + * @return string |
|
162 | + */ |
|
163 | + protected function getGroupDisplayName($gid) { |
|
164 | + $group = $this->groupManager->get($gid); |
|
165 | + if ($group instanceof IGroup) { |
|
166 | + return $group->getDisplayName(); |
|
167 | + } |
|
168 | + return $gid; |
|
169 | + } |
|
170 | 170 | } |
@@ -34,147 +34,147 @@ |
||
34 | 34 | |
35 | 35 | abstract class Base implements IProvider { |
36 | 36 | |
37 | - /** @var IUserManager */ |
|
38 | - protected $userManager; |
|
39 | - |
|
40 | - /** @var string[] */ |
|
41 | - protected $userDisplayNames = []; |
|
42 | - |
|
43 | - /** @var IGroupManager */ |
|
44 | - protected $groupManager; |
|
45 | - |
|
46 | - /** @var string[] */ |
|
47 | - protected $groupDisplayNames = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param IUserManager $userManager |
|
51 | - * @param IGroupManager $groupManager |
|
52 | - */ |
|
53 | - public function __construct(IUserManager $userManager, IGroupManager $groupManager) { |
|
54 | - $this->userManager = $userManager; |
|
55 | - $this->groupManager = $groupManager; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IEvent $event |
|
60 | - * @param string $subject |
|
61 | - * @param array $parameters |
|
62 | - */ |
|
63 | - protected function setSubjects(IEvent $event, $subject, array $parameters) { |
|
64 | - $placeholders = $replacements = []; |
|
65 | - foreach ($parameters as $placeholder => $parameter) { |
|
66 | - $placeholders[] = '{' . $placeholder . '}'; |
|
67 | - $replacements[] = $parameter['name']; |
|
68 | - } |
|
69 | - |
|
70 | - $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
71 | - ->setRichSubject($subject, $parameters); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @param array $eventData |
|
76 | - * @return array |
|
77 | - */ |
|
78 | - protected function generateObjectParameter($eventData) { |
|
79 | - if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) { |
|
80 | - throw new \InvalidArgumentException(); |
|
81 | - } |
|
82 | - |
|
83 | - return [ |
|
84 | - 'type' => 'calendar-event', |
|
85 | - 'id' => $eventData['id'], |
|
86 | - 'name' => $eventData['name'], |
|
87 | - ]; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param array $data |
|
92 | - * @param IL10N $l |
|
93 | - * @return array |
|
94 | - */ |
|
95 | - protected function generateCalendarParameter($data, IL10N $l) { |
|
96 | - if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
97 | - $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
98 | - return [ |
|
99 | - 'type' => 'calendar', |
|
100 | - 'id' => $data['id'], |
|
101 | - 'name' => $l->t('Personal'), |
|
102 | - ]; |
|
103 | - } |
|
104 | - |
|
105 | - return [ |
|
106 | - 'type' => 'calendar', |
|
107 | - 'id' => $data['id'], |
|
108 | - 'name' => $data['name'], |
|
109 | - ]; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param int $id |
|
114 | - * @param string $name |
|
115 | - * @return array |
|
116 | - */ |
|
117 | - protected function generateLegacyCalendarParameter($id, $name) { |
|
118 | - return [ |
|
119 | - 'type' => 'calendar', |
|
120 | - 'id' => $id, |
|
121 | - 'name' => $name, |
|
122 | - ]; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $uid |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - protected function generateUserParameter($uid) { |
|
130 | - if (!isset($this->userDisplayNames[$uid])) { |
|
131 | - $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid); |
|
132 | - } |
|
133 | - |
|
134 | - return [ |
|
135 | - 'type' => 'user', |
|
136 | - 'id' => $uid, |
|
137 | - 'name' => $this->userDisplayNames[$uid], |
|
138 | - ]; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param string $uid |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - protected function getUserDisplayName($uid) { |
|
146 | - $user = $this->userManager->get($uid); |
|
147 | - if ($user instanceof IUser) { |
|
148 | - return $user->getDisplayName(); |
|
149 | - } |
|
150 | - return $uid; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param string $gid |
|
155 | - * @return array |
|
156 | - */ |
|
157 | - protected function generateGroupParameter($gid) { |
|
158 | - if (!isset($this->groupDisplayNames[$gid])) { |
|
159 | - $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); |
|
160 | - } |
|
161 | - |
|
162 | - return [ |
|
163 | - 'type' => 'user-group', |
|
164 | - 'id' => $gid, |
|
165 | - 'name' => $this->groupDisplayNames[$gid], |
|
166 | - ]; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param string $gid |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - protected function getGroupDisplayName($gid) { |
|
174 | - $group = $this->groupManager->get($gid); |
|
175 | - if ($group instanceof IGroup) { |
|
176 | - return $group->getDisplayName(); |
|
177 | - } |
|
178 | - return $gid; |
|
179 | - } |
|
37 | + /** @var IUserManager */ |
|
38 | + protected $userManager; |
|
39 | + |
|
40 | + /** @var string[] */ |
|
41 | + protected $userDisplayNames = []; |
|
42 | + |
|
43 | + /** @var IGroupManager */ |
|
44 | + protected $groupManager; |
|
45 | + |
|
46 | + /** @var string[] */ |
|
47 | + protected $groupDisplayNames = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param IUserManager $userManager |
|
51 | + * @param IGroupManager $groupManager |
|
52 | + */ |
|
53 | + public function __construct(IUserManager $userManager, IGroupManager $groupManager) { |
|
54 | + $this->userManager = $userManager; |
|
55 | + $this->groupManager = $groupManager; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IEvent $event |
|
60 | + * @param string $subject |
|
61 | + * @param array $parameters |
|
62 | + */ |
|
63 | + protected function setSubjects(IEvent $event, $subject, array $parameters) { |
|
64 | + $placeholders = $replacements = []; |
|
65 | + foreach ($parameters as $placeholder => $parameter) { |
|
66 | + $placeholders[] = '{' . $placeholder . '}'; |
|
67 | + $replacements[] = $parameter['name']; |
|
68 | + } |
|
69 | + |
|
70 | + $event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
71 | + ->setRichSubject($subject, $parameters); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @param array $eventData |
|
76 | + * @return array |
|
77 | + */ |
|
78 | + protected function generateObjectParameter($eventData) { |
|
79 | + if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) { |
|
80 | + throw new \InvalidArgumentException(); |
|
81 | + } |
|
82 | + |
|
83 | + return [ |
|
84 | + 'type' => 'calendar-event', |
|
85 | + 'id' => $eventData['id'], |
|
86 | + 'name' => $eventData['name'], |
|
87 | + ]; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param array $data |
|
92 | + * @param IL10N $l |
|
93 | + * @return array |
|
94 | + */ |
|
95 | + protected function generateCalendarParameter($data, IL10N $l) { |
|
96 | + if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI && |
|
97 | + $data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) { |
|
98 | + return [ |
|
99 | + 'type' => 'calendar', |
|
100 | + 'id' => $data['id'], |
|
101 | + 'name' => $l->t('Personal'), |
|
102 | + ]; |
|
103 | + } |
|
104 | + |
|
105 | + return [ |
|
106 | + 'type' => 'calendar', |
|
107 | + 'id' => $data['id'], |
|
108 | + 'name' => $data['name'], |
|
109 | + ]; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param int $id |
|
114 | + * @param string $name |
|
115 | + * @return array |
|
116 | + */ |
|
117 | + protected function generateLegacyCalendarParameter($id, $name) { |
|
118 | + return [ |
|
119 | + 'type' => 'calendar', |
|
120 | + 'id' => $id, |
|
121 | + 'name' => $name, |
|
122 | + ]; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $uid |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + protected function generateUserParameter($uid) { |
|
130 | + if (!isset($this->userDisplayNames[$uid])) { |
|
131 | + $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid); |
|
132 | + } |
|
133 | + |
|
134 | + return [ |
|
135 | + 'type' => 'user', |
|
136 | + 'id' => $uid, |
|
137 | + 'name' => $this->userDisplayNames[$uid], |
|
138 | + ]; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param string $uid |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + protected function getUserDisplayName($uid) { |
|
146 | + $user = $this->userManager->get($uid); |
|
147 | + if ($user instanceof IUser) { |
|
148 | + return $user->getDisplayName(); |
|
149 | + } |
|
150 | + return $uid; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param string $gid |
|
155 | + * @return array |
|
156 | + */ |
|
157 | + protected function generateGroupParameter($gid) { |
|
158 | + if (!isset($this->groupDisplayNames[$gid])) { |
|
159 | + $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); |
|
160 | + } |
|
161 | + |
|
162 | + return [ |
|
163 | + 'type' => 'user-group', |
|
164 | + 'id' => $gid, |
|
165 | + 'name' => $this->groupDisplayNames[$gid], |
|
166 | + ]; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param string $gid |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + protected function getGroupDisplayName($gid) { |
|
174 | + $group = $this->groupManager->get($gid); |
|
175 | + if ($group instanceof IGroup) { |
|
176 | + return $group->getDisplayName(); |
|
177 | + } |
|
178 | + return $gid; |
|
179 | + } |
|
180 | 180 | } |