PageRenderTime 30ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/systemtags/activity/extension.php

https://gitlab.com/wuhang2003/core
PHP | 335 lines | 162 code | 35 blank | 138 comment | 17 complexity | de731302be39f24733b37e0ae049ec7c MD5 | raw file
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\SystemTags\Activity;
  22. use OCP\Activity\IExtension;
  23. use OCP\Activity\IManager;
  24. use OCP\IL10N;
  25. use OCP\L10N\IFactory;
  26. /**
  27. * Class Extension
  28. *
  29. * @package OCA\SystemTags\Activity
  30. */
  31. class Extension implements IExtension {
  32. const APP_NAME = 'systemtags';
  33. const CREATE_TAG = 'create_tag';
  34. const UPDATE_TAG = 'update_tag';
  35. const DELETE_TAG = 'delete_tag';
  36. const ASSIGN_TAG = 'assign_tag';
  37. const UNASSIGN_TAG = 'unassign_tag';
  38. /** @var IFactory */
  39. protected $languageFactory;
  40. /** @var IManager */
  41. protected $activityManager;
  42. /**
  43. * @param IFactory $languageFactory
  44. * @param IManager $activityManager
  45. */
  46. public function __construct(IFactory $languageFactory, IManager $activityManager) {
  47. $this->languageFactory = $languageFactory;
  48. $this->activityManager = $activityManager;
  49. }
  50. protected function getL10N($languageCode = null) {
  51. return $this->languageFactory->get(self::APP_NAME, $languageCode);
  52. }
  53. /**
  54. * The extension can return an array of additional notification types.
  55. * If no additional types are to be added false is to be returned
  56. *
  57. * @param string $languageCode
  58. * @return array|false
  59. */
  60. public function getNotificationTypes($languageCode) {
  61. $l = $this->getL10N($languageCode);
  62. return array(
  63. self::APP_NAME => (string) $l->t('<strong>System tags</strong> for a file have been modified'),
  64. );
  65. }
  66. /**
  67. * For a given method additional types to be displayed in the settings can be returned.
  68. * In case no additional types are to be added false is to be returned.
  69. *
  70. * @param string $method
  71. * @return array|false
  72. */
  73. public function getDefaultTypes($method) {
  74. return $method === self::METHOD_STREAM ? [self::APP_NAME] : false;
  75. }
  76. /**
  77. * A string naming the css class for the icon to be used can be returned.
  78. * If no icon is known for the given type false is to be returned.
  79. *
  80. * @param string $type
  81. * @return string|false
  82. */
  83. public function getTypeIcon($type) {
  84. switch ($type) {
  85. case self::APP_NAME:
  86. return false;
  87. }
  88. return false;
  89. }
  90. /**
  91. * The extension can translate a given message to the requested languages.
  92. * If no translation is available false is to be returned.
  93. *
  94. * @param string $app
  95. * @param string $text
  96. * @param array $params
  97. * @param boolean $stripPath
  98. * @param boolean $highlightParams
  99. * @param string $languageCode
  100. * @return string|false
  101. */
  102. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  103. if ($app !== self::APP_NAME) {
  104. return false;
  105. }
  106. $l = $this->getL10N($languageCode);
  107. if ($this->activityManager->isFormattingFilteredObject()) {
  108. $translation = $this->translateShort($text, $l, $params);
  109. if ($translation !== false) {
  110. return $translation;
  111. }
  112. }
  113. return $this->translateLong($text, $l, $params);
  114. }
  115. /**
  116. * @param string $text
  117. * @param IL10N $l
  118. * @param array $params
  119. * @return bool|string
  120. */
  121. protected function translateShort($text, IL10N $l, array $params) {
  122. switch ($text) {
  123. case self::ASSIGN_TAG:
  124. $params[2] = $this->convertParameterToTag($params[2], $l);
  125. if ($this->actorIsCurrentUser($params[0])) {
  126. return (string) $l->t('You assigned system tag %3$s', $params);
  127. }
  128. return (string) $l->t('%1$s assigned system tag %3$s', $params);
  129. case self::UNASSIGN_TAG:
  130. $params[2] = $this->convertParameterToTag($params[2], $l);
  131. if ($this->actorIsCurrentUser($params[0])) {
  132. return (string) $l->t('You unassigned system tag %3$s', $params);
  133. }
  134. return (string) $l->t('%1$s unassigned system tag %3$s', $params);
  135. }
  136. return false;
  137. }
  138. /**
  139. * @param string $text
  140. * @param IL10N $l
  141. * @param array $params
  142. * @return bool|string
  143. */
  144. protected function translateLong($text, IL10N $l, array $params) {
  145. switch ($text) {
  146. case self::CREATE_TAG:
  147. $params[1] = $this->convertParameterToTag($params[1], $l);
  148. if ($this->actorIsCurrentUser($params[0])) {
  149. return (string) $l->t('You created system tag %2$s', $params);
  150. }
  151. return (string) $l->t('%1$s created system tag %2$s', $params);
  152. case self::DELETE_TAG:
  153. $params[1] = $this->convertParameterToTag($params[1], $l);
  154. if ($this->actorIsCurrentUser($params[0])) {
  155. return (string) $l->t('You deleted system tag %2$s', $params);
  156. }
  157. return (string) $l->t('%1$s deleted system tag %2$s', $params);
  158. case self::UPDATE_TAG:
  159. $params[1] = $this->convertParameterToTag($params[1], $l);
  160. $params[2] = $this->convertParameterToTag($params[2], $l);
  161. if ($this->actorIsCurrentUser($params[0])) {
  162. return (string) $l->t('You updated system tag %3$s to %2$s', $params);
  163. }
  164. return (string) $l->t('%1$s updated system tag %3$s to %2$s', $params);
  165. case self::ASSIGN_TAG:
  166. $params[2] = $this->convertParameterToTag($params[2], $l);
  167. if ($this->actorIsCurrentUser($params[0])) {
  168. return (string) $l->t('You assigned system tag %3$s to %2$s', $params);
  169. }
  170. return (string) $l->t('%1$s assigned system tag %3$s to %2$s', $params);
  171. case self::UNASSIGN_TAG:
  172. $params[2] = $this->convertParameterToTag($params[2], $l);
  173. if ($this->actorIsCurrentUser($params[0])) {
  174. return (string) $l->t('You unassigned system tag %3$s from %2$s', $params);
  175. }
  176. return (string) $l->t('%1$s unassigned system tag %3$s from %2$s', $params);
  177. }
  178. return false;
  179. }
  180. /**
  181. * Check if the author is the current user
  182. *
  183. * @param string $user Parameter e.g. `<user display-name="admin">admin</user>`
  184. * @return bool
  185. */
  186. protected function actorIsCurrentUser($user) {
  187. try {
  188. return strip_tags($user) === $this->activityManager->getCurrentUserId();
  189. } catch (\UnexpectedValueException $e) {
  190. return false;
  191. }
  192. }
  193. /**
  194. * The extension can define the type of parameters for translation
  195. *
  196. * Currently known types are:
  197. * * file => will strip away the path of the file and add a tooltip with it
  198. * * username => will add the avatar of the user
  199. *
  200. * @param string $app
  201. * @param string $text
  202. * @return array|false
  203. */
  204. public function getSpecialParameterList($app, $text) {
  205. if ($app === self::APP_NAME) {
  206. switch ($text) {
  207. case self::CREATE_TAG:
  208. case self::DELETE_TAG:
  209. return array(
  210. 0 => 'username',
  211. //1 => 'systemtag description',
  212. );
  213. case self::UPDATE_TAG:
  214. return array(
  215. 0 => 'username',
  216. //1 => 'systemtag description',
  217. //2 => 'systemtag description',
  218. );
  219. case self::ASSIGN_TAG:
  220. case self::UNASSIGN_TAG:
  221. return array(
  222. 0 => 'username',
  223. 1 => 'file',
  224. //2 => 'systemtag description',
  225. );
  226. }
  227. }
  228. return false;
  229. }
  230. /**
  231. * The extension can define the parameter grouping by returning the index as integer.
  232. * In case no grouping is required false is to be returned.
  233. *
  234. * @param array $activity
  235. * @return integer|false
  236. */
  237. public function getGroupParameter($activity) {
  238. return false;
  239. }
  240. /**
  241. * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
  242. * and 'apps' which hold arrays with the relevant entries.
  243. * If no further entries are to be added false is no be returned.
  244. *
  245. * @return array|false
  246. */
  247. public function getNavigation() {
  248. return false;
  249. }
  250. /**
  251. * The extension can check if a custom filter (given by a query string like filter=abc) is valid or not.
  252. *
  253. * @param string $filterValue
  254. * @return boolean
  255. */
  256. public function isFilterValid($filterValue) {
  257. return false;
  258. }
  259. /**
  260. * The extension can filter the types based on the filter if required.
  261. * In case no filter is to be applied false is to be returned unchanged.
  262. *
  263. * @param array $types
  264. * @param string $filter
  265. * @return array|false
  266. */
  267. public function filterNotificationTypes($types, $filter) {
  268. return false;
  269. }
  270. /**
  271. * For a given filter the extension can specify the sql query conditions including parameters for that query.
  272. * In case the extension does not know the filter false is to be returned.
  273. * The query condition and the parameters are to be returned as array with two elements.
  274. * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  275. *
  276. * @param string $filter
  277. * @return array|false
  278. */
  279. public function getQueryForFilter($filter) {
  280. return false;
  281. }
  282. /**
  283. * @param string $parameter
  284. * @param IL10N $l
  285. * @return string
  286. */
  287. protected function convertParameterToTag($parameter, IL10N $l) {
  288. if (preg_match('/^\<parameter\>\{\{\{(.*)\|\|\|(.*)\}\}\}\<\/parameter\>$/', $parameter, $matches)) {
  289. switch ($matches[2]) {
  290. case 'assignable':
  291. return '<parameter>' . $matches[1] . '</parameter>';
  292. case 'not-assignable':
  293. return '<parameter>' . $l->t('%s (not-assignable)', $matches[1]) . '</parameter>';
  294. case 'invisible':
  295. return '<parameter>' . $l->t('%s (invisible)', $matches[1]) . '</parameter>';
  296. }
  297. }
  298. return $parameter;
  299. }
  300. }