tag.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Add photo tag action
  4. *
  5. * @author Cash Costello
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
  7. */
  8. $coordinates_str = get_input('coordinates');
  9. $username = get_input('username');
  10. $image_guid = get_input('guid');
  11. if ($image_guid == 0) {
  12. register_error(elgg_echo("tidypics:phototagging:error"));
  13. forward(REFERER);
  14. }
  15. $image = get_entity($image_guid);
  16. if (!$image) {
  17. register_error(elgg_echo("tidypics:phototagging:error"));
  18. forward(REFERER);
  19. }
  20. if (empty($username)) {
  21. register_error(elgg_echo("tidypics:phototagging:error"));
  22. forward(REFERER);
  23. }
  24. $user = get_user_by_username($username);
  25. if (!$user) {
  26. // plain tag
  27. $relationships_type = 'word';
  28. $value = $username;
  29. } else {
  30. $relationships_type = 'user';
  31. $value = $user->guid;
  32. }
  33. $tag = new stdClass();
  34. $tag->coords = $coordinates_str;
  35. $tag->type = $relationships_type;
  36. $access_id = $image->getAccessID();
  37. $existing_tags = false;
  38. if ($tag->type === 'word') {
  39. $new_tags = string_to_tag_array($value);
  40. // check to see if the photo has this tag and add if not
  41. if(!isset($image->tags)) {
  42. $image->tags = $new_tags;
  43. } else if (!is_array($image->tags)) {
  44. if(in_array($image->tags, $new_tags)) {
  45. $existing_tags = true;
  46. $value = '';
  47. $tagarray = string_to_tag_array($image->tags);
  48. foreach($new_tags as $new_tag) {
  49. if (!in_array($newtag, $tagarray)) {
  50. $tagarray[] = $newtag;
  51. $value .= ', ' . $newtag;
  52. }
  53. }
  54. if (strlen($value) > 0) {
  55. $value = substr($value, 2);
  56. }
  57. $image->deleteMetadata('tags');
  58. $image->tags = $tagarray;
  59. } else {
  60. $tagarray = string_to_tag_array($image->tags);
  61. $image->deleteMetadata('tags');
  62. $image->tags = array_merge($tagarray, $new_tags);
  63. }
  64. } else {
  65. $tagarray = $image->tags;
  66. $value = '';
  67. foreach($new_tags as $newtag) {
  68. if (!in_array($newtag, $tagarray)) {
  69. $tagarray[] = $newtag;
  70. $value .= ', ' . $newtag;
  71. } else {
  72. $existing_tags = true;
  73. }
  74. }
  75. if (strlen($value) > 0) {
  76. $value = substr($value, 2);
  77. }
  78. $image->deleteMetadata('tags');
  79. $image->tags = $tagarray;
  80. }
  81. }
  82. if (strlen($value) > 0) {
  83. $tag->value = $value;
  84. $annotation_id = $image->annotate('phototag', serialize($tag), $access_id);
  85. if ($annotation_id) {
  86. // if tag is a user id, add relationship for searching (find all images with user x)
  87. if ($tag->type === 'user') {
  88. if (!check_entity_relationship($tag->value, 'phototag', $image_guid)) {
  89. add_entity_relationship($tag->value, 'phototag', $image_guid);
  90. // also add this to the river - subject is tagger, object is the tagged user
  91. $tagger = elgg_get_logged_in_user_entity();
  92. elgg_create_river_item(array('view' => 'river/object/image/tag',
  93. 'action_type' => 'tag',
  94. 'subject_guid' => $tagger->guid,
  95. 'object_guid' => $user->guid,
  96. 'access_id' => $access_id,
  97. 'annotation_id' => $annotation_id));
  98. // notify user of tagging as long as not self
  99. if ($tagger->guid != $user->guid) {
  100. notify_user($user->guid,
  101. $tagger->guid,
  102. elgg_echo('tidypics:tag:subject'),
  103. elgg_echo('tidypics:tag:body', array($image->getTitle(), $tagger->name, $image->getURL()))
  104. );
  105. }
  106. }
  107. } else if ($tag->type === 'word') {
  108. // also add this to the river - subject is tagger, object is the tagged image
  109. $tagger = elgg_get_logged_in_user_entity();
  110. elgg_create_river_item(array('view' => 'river/object/image/wordtag',
  111. 'action_type' => 'wordtag',
  112. 'subject_guid' => $tagger->guid,
  113. 'object_guid' => $image->guid,
  114. 'access_id' => $access_id,
  115. 'annotation_id' => $annotation_id));
  116. }
  117. if ($existing_tags) {
  118. system_message(elgg_echo("tidypics:phototagging:success_partly"));
  119. }else {
  120. system_message(elgg_echo("tidypics:phototagging:success"));
  121. }
  122. }
  123. } else {
  124. register_error(elgg_echo("tidypics:phototagging:nosuccess"));
  125. }
  126. forward(REFERER);