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.

TagMapper.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Bernhard Reiter <ockham@raz.or.at>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. namespace OC\Tagging;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Db\QBMapper;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\IDBConnection;
  31. /**
  32. * Mapper for Tag entity
  33. *
  34. * @template-extends QBMapper<Tag>
  35. */
  36. class TagMapper extends QBMapper {
  37. /**
  38. * Constructor.
  39. *
  40. * @param IDBConnection $db Instance of the Db abstraction layer.
  41. */
  42. public function __construct(IDBConnection $db) {
  43. parent::__construct($db, 'vcategory', Tag::class);
  44. }
  45. /**
  46. * Load tags from the database.
  47. *
  48. * @param array $owners The user(s) whose tags we are going to load.
  49. * @param string $type The type of item for which we are loading tags.
  50. * @return array An array of Tag objects.
  51. */
  52. public function loadTags(array $owners, string $type): array {
  53. $qb = $this->db->getQueryBuilder();
  54. $qb->select(['id', 'uid', 'type', 'category'])
  55. ->from($this->getTableName())
  56. ->where($qb->expr()->in('uid', $qb->createNamedParameter($owners, IQueryBuilder::PARAM_STR_ARRAY)))
  57. ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_STR)))
  58. ->orderBy('category');
  59. return $this->findEntities($qb);
  60. }
  61. /**
  62. * Check if a given Tag object already exists in the database.
  63. *
  64. * @param Tag $tag The tag to look for in the database.
  65. */
  66. public function tagExists(Tag $tag): bool {
  67. $qb = $this->db->getQueryBuilder();
  68. $qb->select(['id', 'uid', 'type', 'category'])
  69. ->from($this->getTableName())
  70. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($tag->getOwner(), IQueryBuilder::PARAM_STR)))
  71. ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter($tag->getType(), IQueryBuilder::PARAM_STR)))
  72. ->andWhere($qb->expr()->eq('category', $qb->createNamedParameter($tag->getName(), IQueryBuilder::PARAM_STR)));
  73. try {
  74. $this->findEntity($qb);
  75. } catch (DoesNotExistException $e) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. }