tagmapper.php 2.3 KB

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