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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Tagging;
  25. use \OCP\AppFramework\Db\Mapper,
  26. \OCP\AppFramework\Db\DoesNotExistException,
  27. \OCP\IDBConnection;
  28. /**
  29. * Mapper for Tag entity
  30. */
  31. class TagMapper extends Mapper {
  32. /**
  33. * Constructor.
  34. *
  35. * @param IDBConnection $db Instance of the Db abstraction layer.
  36. */
  37. public function __construct(IDBConnection $db) {
  38. parent::__construct($db, 'vcategory', 'OC\Tagging\Tag');
  39. }
  40. /**
  41. * Load tags from the database.
  42. *
  43. * @param array|string $owners The user(s) whose tags we are going to load.
  44. * @param string $type The type of item for which we are loading tags.
  45. * @return array An array of Tag objects.
  46. */
  47. public function loadTags($owners, $type) {
  48. if(!is_array($owners)) {
  49. $owners = array($owners);
  50. }
  51. $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` '
  52. . 'WHERE `uid` IN (' . str_repeat('?,', count($owners)-1) . '?) AND `type` = ? ORDER BY `category`';
  53. return $this->findEntities($sql, array_merge($owners, array($type)));
  54. }
  55. /**
  56. * Check if a given Tag object already exists in the database.
  57. *
  58. * @param Tag $tag The tag to look for in the database.
  59. * @return bool
  60. */
  61. public function tagExists($tag) {
  62. $sql = 'SELECT `id`, `uid`, `type`, `category` FROM `' . $this->getTableName() . '` '
  63. . 'WHERE `uid` = ? AND `type` = ? AND `category` = ?';
  64. try {
  65. $this->findEntity($sql, array($tag->getOwner(), $tag->getType(), $tag->getName()));
  66. } catch (DoesNotExistException $e) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. }