TagManager.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Reiter <ockham@raz.or.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Tanghus <thomas@tanghus.net>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. /**
  26. * Factory class creating instances of \OCP\ITags
  27. *
  28. * A tag can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
  29. * anything else that is either parsed from a vobject or that the user chooses
  30. * to add.
  31. * Tag names are not case-sensitive, but will be saved with the case they
  32. * are entered in. If a user already has a tag 'family' for a type, and
  33. * tries to add a tag named 'Family' it will be silently ignored.
  34. */
  35. namespace OC;
  36. use OC\Tagging\TagMapper;
  37. class TagManager implements \OCP\ITagManager {
  38. /**
  39. * User session
  40. *
  41. * @var \OCP\IUserSession
  42. */
  43. private $userSession;
  44. /**
  45. * TagMapper
  46. *
  47. * @var TagMapper
  48. */
  49. private $mapper;
  50. /**
  51. * Constructor.
  52. *
  53. * @param TagMapper $mapper Instance of the TagMapper abstraction layer.
  54. * @param \OCP\IUserSession $userSession the user session
  55. */
  56. public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) {
  57. $this->mapper = $mapper;
  58. $this->userSession = $userSession;
  59. }
  60. /**
  61. * Create a new \OCP\ITags instance and load tags from db.
  62. *
  63. * @see \OCP\ITags
  64. * @param string $type The type identifier e.g. 'contact' or 'event'.
  65. * @param array $defaultTags An array of default tags to be used if none are stored.
  66. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  67. * @param string $userId user for which to retrieve the tags, defaults to the currently
  68. * logged in user
  69. * @return \OCP\ITags
  70. */
  71. public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) {
  72. if (is_null($userId)) {
  73. $user = $this->userSession->getUser();
  74. if ($user === null) {
  75. // nothing we can do without a user
  76. return null;
  77. }
  78. $userId = $this->userSession->getUser()->getUId();
  79. }
  80. return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared);
  81. }
  82. }