tagmanager.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @author Bernhard Reiter <ockham@raz.or.at>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Thomas Tanghus <thomas@tanghus.net>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. /**
  25. * Factory class creating instances of \OCP\ITags
  26. *
  27. * A tag can be e.g. 'Family', 'Work', 'Chore', 'Special Occation' or
  28. * anything else that is either parsed from a vobject or that the user chooses
  29. * to add.
  30. * Tag names are not case-sensitive, but will be saved with the case they
  31. * are entered in. If a user already has a tag 'family' for a type, and
  32. * tries to add a tag named 'Family' it will be silently ignored.
  33. */
  34. namespace OC;
  35. use OC\Tagging\TagMapper;
  36. class TagManager implements \OCP\ITagManager {
  37. /**
  38. * User session
  39. *
  40. * @var \OCP\IUserSession
  41. */
  42. private $userSession;
  43. /**
  44. * TagMapper
  45. *
  46. * @var TagMapper
  47. */
  48. private $mapper;
  49. /**
  50. * Constructor.
  51. *
  52. * @param TagMapper $mapper Instance of the TagMapper abstraction layer.
  53. * @param \OCP\IUserSession $userSession the user session
  54. */
  55. public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) {
  56. $this->mapper = $mapper;
  57. $this->userSession = $userSession;
  58. }
  59. /**
  60. * Create a new \OCP\ITags instance and load tags from db.
  61. *
  62. * @see \OCP\ITags
  63. * @param string $type The type identifier e.g. 'contact' or 'event'.
  64. * @param array $defaultTags An array of default tags to be used if none are stored.
  65. * @param boolean $includeShared Whether to include tags for items shared with this user by others.
  66. * @param string $userId user for which to retrieve the tags, defaults to the currently
  67. * logged in user
  68. * @return \OCP\ITags
  69. */
  70. public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) {
  71. if (is_null($userId)) {
  72. $user = $this->userSession->getUser();
  73. if ($user === null) {
  74. // nothing we can do without a user
  75. return null;
  76. }
  77. $userId = $this->userSession->getUser()->getUId();
  78. }
  79. return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared);
  80. }
  81. }