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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Thomas Tanghus <thomas@tanghus.net>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Tags;
  23. class Controller {
  24. protected static function getTagger($type) {
  25. \OC_JSON::checkLoggedIn();
  26. \OC_JSON::callCheck();
  27. try {
  28. $tagger = \OC::$server->getTagManager()->load($type);
  29. return $tagger;
  30. } catch(\Exception $e) {
  31. \OCP\Util::writeLog('core', __METHOD__ . ' Exception: ' . $e->getMessage(), \OCP\Util::ERROR);
  32. $l = new \OC_L10n('core');
  33. \OC_JSON::error(array('message'=> $l->t('Error loading tags')));
  34. exit;
  35. }
  36. }
  37. public static function getTags($args) {
  38. $tagger = self::getTagger($args['type']);
  39. \OC_JSON::success(array('tags'=> $tagger->getTags()));
  40. }
  41. public static function getFavorites($args) {
  42. $tagger = self::getTagger($args['type']);
  43. \OC_JSON::success(array('ids'=> $tagger->getFavorites()));
  44. }
  45. public static function getIdsForTag($args) {
  46. $tagger = self::getTagger($args['type']);
  47. \OC_JSON::success(array('ids'=> $tagger->getIdsForTag($_GET['tag'])));
  48. }
  49. public static function addTag($args) {
  50. $tagger = self::getTagger($args['type']);
  51. $id = $tagger->add(strip_tags($_POST['tag']));
  52. if($id === false) {
  53. $l = new \OC_L10n('core');
  54. \OC_JSON::error(array('message'=> $l->t('Tag already exists')));
  55. } else {
  56. \OC_JSON::success(array('id'=> $id));
  57. }
  58. }
  59. public static function deleteTags($args) {
  60. $tags = $_POST['tags'];
  61. if(!is_array($tags)) {
  62. $tags = array($tags);
  63. }
  64. $tagger = self::getTagger($args['type']);
  65. if(!$tagger->delete($tags)) {
  66. $l = new \OC_L10n('core');
  67. \OC_JSON::error(array('message'=> $l->t('Error deleting tag(s)')));
  68. } else {
  69. \OC_JSON::success();
  70. }
  71. }
  72. public static function tagAs($args) {
  73. $tagger = self::getTagger($args['type']);
  74. if(!$tagger->tagAs($args['id'], $_POST['tag'])) {
  75. $l = new \OC_L10n('core');
  76. \OC_JSON::error(array('message'=> $l->t('Error tagging')));
  77. } else {
  78. \OC_JSON::success();
  79. }
  80. }
  81. public static function unTag($args) {
  82. $tagger = self::getTagger($args['type']);
  83. if(!$tagger->unTag($args['id'], $_POST['tag'])) {
  84. $l = new \OC_L10n('core');
  85. \OC_JSON::error(array('message'=> $l->t('Error untagging')));
  86. } else {
  87. \OC_JSON::success();
  88. }
  89. }
  90. public static function favorite($args) {
  91. $tagger = self::getTagger($args['type']);
  92. if(!$tagger->addToFavorites($args['id'])) {
  93. $l = new \OC_L10n('core');
  94. \OC_JSON::error(array('message'=> $l->t('Error favoriting')));
  95. } else {
  96. \OC_JSON::success();
  97. }
  98. }
  99. public static function unFavorite($args) {
  100. $tagger = self::getTagger($args['type']);
  101. if(!$tagger->removeFromFavorites($args['id'])) {
  102. $l = new \OC_L10n('core');
  103. \OC_JSON::error(array('message'=> $l->t('Error unfavoriting')));
  104. } else {
  105. \OC_JSON::success();
  106. }
  107. }
  108. }