Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

119 lines
2.7 KiB

3 years ago
  1. <?php
  2. /**
  3. * @file mod/tagrm.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Text\BBCode;
  7. use Friendica\Core\L10n;
  8. use Friendica\Core\System;
  9. use Friendica\Database\DBA;
  10. use Friendica\Model\Item;
  11. use Friendica\Model\Term;
  12. function tagrm_post(App $a)
  13. {
  14. if (!local_user()) {
  15. $a->internalRedirect($_SESSION['photo_return']);
  16. }
  17. if (x($_POST,'submit') && ($_POST['submit'] === L10n::t('Cancel'))) {
  18. $a->internalRedirect($_SESSION['photo_return']);
  19. }
  20. $tags = [];
  21. foreach (defaults($_POST, 'tag', []) as $tag) {
  22. array_push($tags, hex2bin(notags(trim($tag))));
  23. }
  24. $item_id = defaults($_POST,'item', 0);
  25. update_tags($item_id, $tags);
  26. $a->internalRedirect($_SESSION['photo_return']);
  27. // NOTREACHED
  28. }
  29. /**
  30. * Updates tags from an item
  31. * @param $item_id
  32. * @param $tags array
  33. */
  34. function update_tags($item_id, $tags){
  35. if (empty($item_id) || empty($tags)){
  36. return;
  37. }
  38. $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
  39. if (!DBA::isResult($item)) {
  40. return;
  41. }
  42. $old_tags = explode(',', $item['tag']);
  43. foreach ($tags as $new_tag) {
  44. foreach ($old_tags as $count => $old_tag) {
  45. if (strcmp($old_tag, $new_tag) == 0) {
  46. unset($old_tags[$count]);
  47. break;
  48. }
  49. }
  50. }
  51. $tag_str = implode(',',$old_tags);
  52. Term::insertFromTagFieldByItemId($item_id, $tag_str);
  53. info(L10n::t('Tag(s) removed') . EOL );
  54. }
  55. function tagrm_content(App $a)
  56. {
  57. $o = '';
  58. if (!local_user()) {
  59. $a->internalRedirect($_SESSION['photo_return']);
  60. // NOTREACHED
  61. }
  62. if ($a->argc == 3){
  63. update_tags($a->argv[1], [hex2bin(notags(trim($a->argv[2])))]);
  64. $a->internalRedirect($_SESSION['photo_return']);
  65. }
  66. $item_id = (($a->argc > 1) ? intval($a->argv[1]) : 0);
  67. if (!$item_id) {
  68. $a->internalRedirect($_SESSION['photo_return']);
  69. // NOTREACHED
  70. }
  71. $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]);
  72. if (!DBA::isResult($item)) {
  73. $a->internalRedirect($_SESSION['photo_return']);
  74. }
  75. $arr = explode(',', $item['tag']);
  76. if (empty($item['tag'])) {
  77. $a->internalRedirect($_SESSION['photo_return']);
  78. }
  79. $o .= '<h3>' . L10n::t('Remove Item Tag') . '</h3>';
  80. $o .= '<p id="tag-remove-desc">' . L10n::t('Select a tag to remove: ') . '</p>';
  81. $o .= '<form id="tagrm" action="tagrm" method="post" >';
  82. $o .= '<input type="hidden" name="item" value="' . $item_id . '" />';
  83. $o .= '<ul>';
  84. foreach ($arr as $x) {
  85. $o .= '<li><input type="checkbox" name="tag[]" value="' . bin2hex($x) . '" >' . BBCode::convert($x) . '</input></li>';
  86. }
  87. $o .= '</ul>';
  88. $o .= '<input id="tagrm-submit" type="submit" name="submit" value="' . L10n::t('Remove') .'" />';
  89. $o .= '<input id="tagrm-cancel" type="submit" name="submit" value="' . L10n::t('Cancel') .'" />';
  90. $o .= '</form>';
  91. return $o;
  92. }