tagprofile.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. class TagprofileAction extends FormAction
  21. {
  22. var $error = null;
  23. protected $target = null;
  24. protected $form = 'TagProfile';
  25. protected function doPreparation()
  26. {
  27. $id = $this->trimmed('id');
  28. $uri = $this->trimmed('uri');
  29. if (!empty($id)) {
  30. $this->target = Profile::getKV('id', $id);
  31. if (!$this->target instanceof Profile) {
  32. // TRANS: Client error displayed when referring to non-existing profile ID.
  33. $this->clientError(_('No profile with that ID.'));
  34. }
  35. } elseif (!empty($uri)) {
  36. $this->target = Profile::fromUri($uri);
  37. } else {
  38. // TRANS: Client error displayed when trying to tag a user but no ID or profile is provided.
  39. $this->clientError(_('No profile identifier provided.'));
  40. }
  41. if (!$this->scoped->canTag($this->target)) {
  42. // TRANS: Client error displayed when trying to tag a user that cannot be tagged.
  43. $this->clientError(_('You cannot tag this user.'));
  44. }
  45. $this->formOpts = $this->target;
  46. return true;
  47. }
  48. function title()
  49. {
  50. if (!$this->target instanceof Profile) {
  51. // TRANS: Title for list form when not on a profile page.
  52. return _('List a profile');
  53. }
  54. // TRANS: Title for list form when on a profile page.
  55. // TRANS: %s is a profile nickname.
  56. return sprintf(_m('ADDTOLIST','List %s'), $this->target->getNickname());
  57. }
  58. function showPage()
  59. {
  60. // Only serve page content if we aren't POSTing via ajax
  61. // otherwise, we serve XML content from doPost()
  62. if (!$this->isPost() || !$this->boolean('ajax')) {
  63. parent::showPage();
  64. }
  65. }
  66. function showContent()
  67. {
  68. $this->elementStart('div', 'entity_profile h-card');
  69. // TRANS: Header in list form.
  70. $this->element('h2', null, _('User profile'));
  71. $avatarUrl = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
  72. $this->element('img', array('src' => $avatarUrl,
  73. 'class' => 'u-photo avatar entity_depiction',
  74. 'width' => AVATAR_PROFILE_SIZE,
  75. 'height' => AVATAR_PROFILE_SIZE,
  76. 'alt' => $this->target->getBestName()));
  77. $this->element('a', array('href' => $this->target->getUrl(),
  78. 'class' => 'entity_nickname p-nickname'),
  79. $this->target->getNickname());
  80. if ($this->target->fullname) {
  81. $this->element('div', 'p-name entity_fn', $this->target->fullname);
  82. }
  83. if ($this->target->location) {
  84. $this->element('div', 'p-locality label entity_location', $this->target->location);
  85. }
  86. if ($this->target->homepage) {
  87. $this->element('a', array('href' => $this->target->homepage,
  88. 'rel' => 'me',
  89. 'class' => 'u-url entity_url'),
  90. $this->target->homepage);
  91. }
  92. if ($this->target->bio) {
  93. $this->element('div', 'p-note entity_note', $this->target->bio);
  94. }
  95. $this->elementEnd('div');
  96. if (Event::handle('StartShowTagProfileForm', array($this, $this->target))) {
  97. parent::showContent();
  98. Event::handle('EndShowTagProfileForm', array($this, $this->target));
  99. }
  100. }
  101. protected function doPost()
  102. {
  103. $tagstring = $this->trimmed('tags');
  104. $token = $this->trimmed('token');
  105. if (Event::handle('StartSavePeopletags', array($this, $tagstring))) {
  106. $tags = array();
  107. $tag_priv = array();
  108. if (is_string($tagstring) && strlen($tagstring) > 0) {
  109. $tags = preg_split('/[\s,]+/', $tagstring);
  110. foreach ($tags as &$tag) {
  111. $private = @$tag[0] === '.';
  112. $tag = common_canonical_tag($tag);
  113. if (!common_valid_profile_tag($tag)) {
  114. // TRANS: Form validation error displayed if a given tag is invalid.
  115. // TRANS: %s is the invalid tag.
  116. throw new ClientException(sprintf(_('Invalid tag: "%s".'), $tag));
  117. }
  118. $tag_priv[$tag] = $private;
  119. }
  120. }
  121. $result = Profile_tag::setTags($this->scoped->getID(), $this->target->getID(), $tags, $tag_priv);
  122. if (!$result) {
  123. throw new ServerException('The tags could not be saved.');
  124. }
  125. if ($this->boolean('ajax')) {
  126. $this->startHTML('text/xml;charset=utf-8');
  127. $this->elementStart('head');
  128. $this->element('title', null, _m('TITLE','Tags'));
  129. $this->elementEnd('head');
  130. $this->elementStart('body');
  131. if ($this->scoped->id == $this->target->id) {
  132. $widget = new SelftagsWidget($this, $this->scoped, $this->target);
  133. $widget->show();
  134. } else {
  135. $widget = new PeopletagsWidget($this, $this->scoped, $this->target);
  136. $widget->show();
  137. }
  138. $this->elementEnd('body');
  139. $this->endHTML();
  140. } else {
  141. // TRANS: Success message if lists are saved.
  142. $this->msg = _('Lists saved.');
  143. $this->showForm();
  144. }
  145. Event::handle('EndSavePeopletags', array($this, $tagstring));
  146. }
  147. }
  148. }