TagManager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. namespace OC;
  28. use OC\Tagging\TagMapper;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\IDBConnection;
  31. use OCP\ITagManager;
  32. use OCP\ITags;
  33. use OCP\IUserSession;
  34. class TagManager implements ITagManager {
  35. /** @var TagMapper */
  36. private $mapper;
  37. /** @var IUserSession */
  38. private $userSession;
  39. /** @var IDBConnection */
  40. private $connection;
  41. public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection) {
  42. $this->mapper = $mapper;
  43. $this->userSession = $userSession;
  44. $this->connection = $connection;
  45. }
  46. /**
  47. * Create a new \OCP\ITags instance and load tags from db.
  48. *
  49. * @see \OCP\ITags
  50. * @param string $type The type identifier e.g. 'contact' or 'event'.
  51. * @param array $defaultTags An array of default tags to be used if none are stored.
  52. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  53. * @param string $userId user for which to retrieve the tags, defaults to the currently
  54. * logged in user
  55. * @return \OCP\ITags
  56. *
  57. * since 20.0.0 $includeShared isn't used anymore
  58. */
  59. public function load($type, $defaultTags = [], $includeShared = false, $userId = null) {
  60. if (is_null($userId)) {
  61. $user = $this->userSession->getUser();
  62. if ($user === null) {
  63. // nothing we can do without a user
  64. return null;
  65. }
  66. $userId = $this->userSession->getUser()->getUId();
  67. }
  68. return new Tags($this->mapper, $userId, $type, $defaultTags);
  69. }
  70. /**
  71. * Get all users who favorited an object
  72. *
  73. * @param string $objectType
  74. * @param int $objectId
  75. * @return array
  76. */
  77. public function getUsersFavoritingObject(string $objectType, int $objectId): array {
  78. $query = $this->connection->getQueryBuilder();
  79. $query->select('uid')
  80. ->from('vcategory_to_object', 'o')
  81. ->innerJoin('o', 'vcategory', 'c', $query->expr()->eq('o.categoryid', 'c.id'))
  82. ->where($query->expr()->eq('objid', $query->createNamedParameter($objectId, IQueryBuilder::PARAM_INT)))
  83. ->andWhere($query->expr()->eq('c.type', $query->createNamedParameter($objectType)))
  84. ->andWhere($query->expr()->eq('c.category', $query->createNamedParameter(ITags::TAG_FAVORITE)));
  85. $result = $query->execute();
  86. $users = $result->fetchAll(\PDO::FETCH_COLUMN);
  87. $result->closeCursor();
  88. return $users;
  89. }
  90. }