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.

TagManager.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Reiter <ockham@raz.or.at>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. /**
  28. * Factory class creating instances of \OCP\ITags
  29. *
  30. * A tag can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
  31. * anything else that is either parsed from a vobject or that the user chooses
  32. * to add.
  33. * Tag names are not case-sensitive, but will be saved with the case they
  34. * are entered in. If a user already has a tag 'family' for a type, and
  35. * tries to add a tag named 'Family' it will be silently ignored.
  36. */
  37. namespace OC;
  38. use OC\Tagging\TagMapper;
  39. use OCP\DB\QueryBuilder\IQueryBuilder;
  40. use OCP\IDBConnection;
  41. use OCP\ITagManager;
  42. use OCP\ITags;
  43. use OCP\IUserSession;
  44. class TagManager implements ITagManager {
  45. /** @var TagMapper */
  46. private $mapper;
  47. /** @var IUserSession */
  48. private $userSession;
  49. /** @var IDBConnection */
  50. private $connection;
  51. public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection) {
  52. $this->mapper = $mapper;
  53. $this->userSession = $userSession;
  54. $this->connection = $connection;
  55. }
  56. /**
  57. * Create a new \OCP\ITags instance and load tags from db.
  58. *
  59. * @see \OCP\ITags
  60. * @param string $type The type identifier e.g. 'contact' or 'event'.
  61. * @param array $defaultTags An array of default tags to be used if none are stored.
  62. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  63. * @param string $userId user for which to retrieve the tags, defaults to the currently
  64. * logged in user
  65. * @return \OCP\ITags
  66. *
  67. * since 20.0.0 $includeShared isn't used anymore
  68. */
  69. public function load($type, $defaultTags = [], $includeShared = false, $userId = null) {
  70. if (is_null($userId)) {
  71. $user = $this->userSession->getUser();
  72. if ($user === null) {
  73. // nothing we can do without a user
  74. return null;
  75. }
  76. $userId = $this->userSession->getUser()->getUId();
  77. }
  78. return new Tags($this->mapper, $userId, $type, $defaultTags);
  79. }
  80. /**
  81. * Get all users who favorited an object
  82. *
  83. * @param string $objectType
  84. * @param int $objectId
  85. * @return array
  86. */
  87. public function getUsersFavoritingObject(string $objectType, int $objectId): array {
  88. $query = $this->connection->getQueryBuilder();
  89. $query->select('uid')
  90. ->from('vcategory_to_object', 'o')
  91. ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id'))
  92. ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT)))
  93. ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType)))
  94. ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE)));
  95. $result = $query->execute();
  96. $users = $result->fetchAll(\PDO::FETCH_COLUMN);
  97. $result->closeCursor();
  98. return $users;
  99. }
  100. }