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.5KB

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