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.

Controller.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Thomas Tanghus <thomas@tanghus.net>
  7. *
  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\Core\Tags;
  24. class Controller {
  25. protected static function getTagger($type) {
  26. \OC_JSON::checkLoggedIn();
  27. \OC_JSON::callCheck();
  28. try {
  29. $tagger = \OC::$server->getTagManager()->load($type);
  30. return $tagger;
  31. } catch(\Exception $e) {
  32. \OCP\Util::writeLog('core', __METHOD__ . ' Exception: ' . $e->getMessage(), \OCP\Util::ERROR);
  33. $l = new \OC_L10n('core');
  34. \OC_JSON::error(array('message'=> $l->t('Error loading tags')));
  35. exit;
  36. }
  37. }
  38. public static function getTags($args) {
  39. $tagger = self::getTagger($args['type']);
  40. \OC_JSON::success(array('tags'=> $tagger->getTags()));
  41. }
  42. public static function getFavorites($args) {
  43. $tagger = self::getTagger($args['type']);
  44. \OC_JSON::success(array('ids'=> $tagger->getFavorites()));
  45. }
  46. public static function getIdsForTag($args) {
  47. $tagger = self::getTagger($args['type']);
  48. \OC_JSON::success(array('ids'=> $tagger->getIdsForTag($_GET['tag'])));
  49. }
  50. public static function addTag($args) {
  51. $tagger = self::getTagger($args['type']);
  52. $id = $tagger->add(strip_tags($_POST['tag']));
  53. if($id === false) {
  54. $l = new \OC_L10n('core');
  55. \OC_JSON::error(array('message'=> $l->t('Tag already exists')));
  56. } else {
  57. \OC_JSON::success(array('id'=> $id));
  58. }
  59. }
  60. public static function deleteTags($args) {
  61. $tags = $_POST['tags'];
  62. if(!is_array($tags)) {
  63. $tags = array($tags);
  64. }
  65. $tagger = self::getTagger($args['type']);
  66. if(!$tagger->delete($tags)) {
  67. $l = new \OC_L10n('core');
  68. \OC_JSON::error(array('message'=> $l->t('Error deleting tag(s)')));
  69. } else {
  70. \OC_JSON::success();
  71. }
  72. }
  73. public static function tagAs($args) {
  74. $tagger = self::getTagger($args['type']);
  75. if(!$tagger->tagAs($args['id'], $_POST['tag'])) {
  76. $l = new \OC_L10n('core');
  77. \OC_JSON::error(array('message'=> $l->t('Error tagging')));
  78. } else {
  79. \OC_JSON::success();
  80. }
  81. }
  82. public static function unTag($args) {
  83. $tagger = self::getTagger($args['type']);
  84. if(!$tagger->unTag($args['id'], $_POST['tag'])) {
  85. $l = new \OC_L10n('core');
  86. \OC_JSON::error(array('message'=> $l->t('Error untagging')));
  87. } else {
  88. \OC_JSON::success();
  89. }
  90. }
  91. public static function favorite($args) {
  92. $tagger = self::getTagger($args['type']);
  93. if(!$tagger->addToFavorites($args['id'])) {
  94. $l = new \OC_L10n('core');
  95. \OC_JSON::error(array('message'=> $l->t('Error favoriting')));
  96. } else {
  97. \OC_JSON::success();
  98. }
  99. }
  100. public static function unFavorite($args) {
  101. $tagger = self::getTagger($args['type']);
  102. if(!$tagger->removeFromFavorites($args['id'])) {
  103. $l = new \OC_L10n('core');
  104. \OC_JSON::error(array('message'=> $l->t('Error unfavoriting')));
  105. } else {
  106. \OC_JSON::success();
  107. }
  108. }
  109. }